CustomController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace console\controllers;
  3. use biz\shop\classes\ShopClass;
  4. use bizGhs\custom\models\Custom;
  5. use bizGhs\order\classes\OrderClass;
  6. use bizHd\custom\classes\CustomClass;
  7. use bizHd\custom\classes\HdClass;
  8. use bizHd\member\classes\MemberLevelClass;
  9. use bizHd\purchase\classes\PurchaseClass;
  10. use bizHd\shop\models\Shop;
  11. use common\components\dict;
  12. use common\components\noticeUtil;
  13. use common\components\util;
  14. use bizHd\user\classes\UserClass;
  15. use bizHd\user\models\User;
  16. use Overtrue\ChineseCalendar\Calendar;
  17. use Yii;
  18. use yii\console\Controller;
  19. class CustomController extends Controller
  20. {
  21. //欠款信息不一致跟踪 ssh 20220307 ./yii custom/adjust-debt
  22. public function actionAdjustDebt()
  23. {
  24. //感觉好像没有用了,可以停了 ssh 20250724
  25. util::stop();
  26. ini_set('memory_limit', '2045M');
  27. set_time_limit(0);
  28. $shopList = ShopClass::getAllByCondition(['ptStyle' => 2], null, '*', null, true);
  29. if (empty($shopList)) {
  30. return false;
  31. }
  32. foreach ($shopList as $key => $shop) {
  33. $shopId = $shop->id ?? 0;
  34. $query = new \yii\db\Query();
  35. $query->from(Custom::tableName());
  36. $query->where(['ownShopId' => $shopId]);
  37. $query->orderBy('addTime ASC');
  38. foreach ($query->batch(50) as $customList) {
  39. if (!empty($customList)) {
  40. foreach ($customList as $custom) {
  41. //$customName = $custom['name'] ?? '';
  42. $customId = $custom['id'] ?? 0;
  43. $where = ['customId' => $customId, 'debt' => 1];
  44. $orderList = OrderClass::getAllByCondition($where, 'addTime DESC', '*');
  45. $ghsOrderDebtAmount = 0;
  46. if (!empty($orderList)) {
  47. foreach ($orderList as $order) {
  48. $actPrice = $order['remainDebtPrice'] ?? 0;
  49. $ghsOrderDebtAmount = bcadd($ghsOrderDebtAmount, $actPrice, 2);
  50. }
  51. }
  52. $ghsId = $custom['ghsId'] ?? 0;
  53. $ghs = \bizHd\ghs\classes\GhsClass::getById($ghsId);
  54. $where = ['ghsId' => $ghsId, 'debt' => PurchaseClass::DEBT_YES];
  55. $cgList = PurchaseClass::getAllByCondition($where, 'addTime DESC', '*');
  56. $hdCgOrderDebtAmount = 0;
  57. if (!empty($cgList)) {
  58. foreach ($cgList as $cg) {
  59. $actPrice = $cg['remainDebtPrice'] ?? 0;
  60. $hdCgOrderDebtAmount = bcadd($hdCgOrderDebtAmount, $actPrice, 2);
  61. }
  62. }
  63. if ($ghsOrderDebtAmount < 0 || $hdCgOrderDebtAmount < 0) {
  64. noticeUtil::push("!!!!!!!供货商 {$ghsId} 客户id {$customId} 出现负数 $ghsOrderDebtAmount $hdCgOrderDebtAmount", '15280215347');
  65. echo "@@@@@@@@@@@@@供货商 {$ghsId} 客户id {$customId} 出现负数 $ghsOrderDebtAmount $hdCgOrderDebtAmount \n";
  66. }
  67. $ghsOrderDebtAmount = floatval($ghsOrderDebtAmount);
  68. $hdCgOrderDebtAmount = floatval($hdCgOrderDebtAmount);
  69. if ($ghsOrderDebtAmount != $hdCgOrderDebtAmount) {
  70. noticeUtil::push("!!!!!!!供货商 {$ghsId} 客户id {$customId} 二边欠款单总金额不一致", '15280215347');
  71. echo "---------供货商 {$ghsId} 客户id {$customId} 二边 欠款单 总金额 不一致 {$ghsOrderDebtAmount} {$hdCgOrderDebtAmount} \n";
  72. continue;
  73. }
  74. $customSaveDebtAmount = $custom['debtAmount'] ? floatval($custom['debtAmount']) : 0;
  75. $ghsSaveDebtAmount = $ghs['debtAmount'] ? floatval($ghs['debtAmount']) : 0;
  76. if ($customSaveDebtAmount != $ghsOrderDebtAmount) {
  77. echo "*********客户 {$customId} 欠款 和 订单总额 不一致 $customSaveDebtAmount $ghsOrderDebtAmount \n";
  78. noticeUtil::push("*********客户 {$customId} 欠款总金额 和 订单总合 不一致 $customSaveDebtAmount $ghsOrderDebtAmount ", '15280215347');
  79. continue;
  80. }
  81. if ($ghsSaveDebtAmount != $hdCgOrderDebtAmount) {
  82. echo "#########欠供货商 {$ghsId} 和 订单总额 不一致 $ghsSaveDebtAmount $hdCgOrderDebtAmount \n";
  83. noticeUtil::push("#########欠供货商 {$ghsId} 金额 和 订单总合 不一致 $ghsSaveDebtAmount $hdCgOrderDebtAmount ", '15280215347');
  84. continue;
  85. }
  86. }
  87. }
  88. }
  89. }
  90. }
  91. // 把所有客户的累计消费和成长值、积分都刷新一下
  92. public function actionRefreshCustomData()
  93. {
  94. try {
  95. ini_set('memory_limit', '2045M');
  96. set_time_limit(0);
  97. // 1. 查询 xhShop 表,获取所有 ptStyle = 1 的门店数据,每次取出500个进行处理
  98. $query = Shop::find()
  99. ->select('id, mainId')
  100. ->where(['ptStyle' => 1,'id'=>6362])
  101. ->orderBy('id DESC')
  102. ->asArray();
  103. // 2. 把每个门店下的所有用户进行遍历,刷新他们的累计消费和成长值、积分
  104. foreach ($query->batch(500) as $allShops) {
  105. if (!empty($allShops)) {
  106. foreach ($allShops as $shop) {
  107. $shopId = $shop['id'];
  108. $mainId = $shop['mainId'];
  109. echo '门店id:' . $shopId;
  110. $customList = CustomClass::getAllByCondition(['shopId' => $shopId], null, 'id, userId, balance, buyAmount, growth, integral, member, memberName', null, true);
  111. $levelData = MemberLevelClass::getAllByCondition(['mainId' => $mainId], 'amount DESC', 'id, name, level, amount');
  112. if (!empty($customList)) {
  113. foreach ($customList as $custom) {
  114. $buyAmount = 0;
  115. // 获取所有零售订单,计算消费金额
  116. $orderList = \bizHd\order\classes\OrderClass::getAllByCondition(['customId' => $custom->id, 'payStatus' => 1], null, 'id, forward, actPrice, payWay');
  117. if (!empty($orderList)) {
  118. foreach ($orderList as $order) {
  119. if ($order['forward'] == 0) { //0 常规订单
  120. $buyAmount = bcadd($buyAmount, $order['actPrice'], 2);
  121. } else { //1 红冲
  122. $buyAmount = bcsub($buyAmount, $order['actPrice'], 2);
  123. }
  124. }
  125. }
  126. $custom->buyAmount = $buyAmount;
  127. $balance = $custom->balance ?? 0;
  128. if ($balance > 0) {
  129. $total = bcadd($balance, $buyAmount, 2);
  130. } else {
  131. $total = $buyAmount;
  132. }
  133. $custom->growth = $total;
  134. $custom->integral = $total;
  135. //echo " customId:" . $custom->id;
  136. // 根据 growth 设置等级
  137. $memberData = CustomClass::getCustomExpenseLevel($custom->growth, $mainId, $levelData); // 设置等级多处需要同步修改的,请搜索:getCustomExpenseLevel
  138. //echo "--+--- ";
  139. $level = $memberData['level'] ?? 0;
  140. $level = trim($level);
  141. $level = floatval($level);
  142. $memberName = $memberData['name'] ?? '';
  143. $memberName = trim($memberName);
  144. //echo $level."+".$memberName;
  145. $custom->member = $level;
  146. $custom->memberName = $memberName;
  147. //$custom->listend = false; //关闭对 xhCustom 的 buyAmount 变动监听
  148. $custom->save();
  149. //echo " 0000 \n";
  150. // 更新客户所对应花店的消费金额
  151. $hdId = $custom->hdId;
  152. if (!empty($hdId)) {
  153. HdClass::updateById($hdId, ['expendAmount' => $buyAmount]);
  154. }
  155. }
  156. }
  157. //echo "\n";
  158. }
  159. }
  160. echo "end\n";
  161. }
  162. } catch (\Exception $e) {
  163. Yii::error('把所有客户的累计消费和成长值、积分都刷新一下脚本执行失败,原因:' . $e->getMessage());
  164. }
  165. }
  166. //批量创建客户
  167. private function createCustomer()
  168. {
  169. //重新写这个方法,用rabbitMQ来实现
  170. }
  171. //批发端分店从首店同步客户
  172. private function copyCustomers()
  173. {
  174. //重新写这个方法,用rabbitMQ来实现
  175. }
  176. //从文件批量创建客户 ssh 20240426
  177. public function actionFileCreateCustom()
  178. {
  179. //重新写这个方法,用rabbitMQ来实现
  180. }
  181. }