CustomController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 common\components\noticeUtil;
  11. use common\components\util;
  12. use bizHd\user\classes\UserClass;
  13. use Overtrue\ChineseCalendar\Calendar;
  14. use Yii;
  15. use yii\console\Controller;
  16. class CustomController extends Controller
  17. {
  18. //欠款信息不一致跟踪 ssh 20220307 ./yii custom/adjust-debt
  19. public function actionAdjustDebt()
  20. {
  21. //感觉好像没有用了,可以停了 ssh 20250724
  22. util::stop();
  23. ini_set('memory_limit', '2045M');
  24. set_time_limit(0);
  25. $shopList = ShopClass::getAllByCondition(['ptStyle' => 2], null, '*', null, true);
  26. if (empty($shopList)) {
  27. return false;
  28. }
  29. foreach ($shopList as $key => $shop) {
  30. $shopId = $shop->id ?? 0;
  31. $query = new \yii\db\Query();
  32. $query->from(Custom::tableName());
  33. $query->where(['ownShopId' => $shopId]);
  34. $query->orderBy('addTime ASC');
  35. foreach ($query->batch(50) as $customList) {
  36. if (!empty($customList)) {
  37. foreach ($customList as $custom) {
  38. //$customName = $custom['name'] ?? '';
  39. $customId = $custom['id'] ?? 0;
  40. $where = ['customId' => $customId, 'debt' => 1];
  41. $orderList = OrderClass::getAllByCondition($where, 'addTime DESC', '*');
  42. $ghsOrderDebtAmount = 0;
  43. if (!empty($orderList)) {
  44. foreach ($orderList as $order) {
  45. $actPrice = $order['remainDebtPrice'] ?? 0;
  46. $ghsOrderDebtAmount = bcadd($ghsOrderDebtAmount, $actPrice, 2);
  47. }
  48. }
  49. $ghsId = $custom['ghsId'] ?? 0;
  50. $ghs = \bizHd\ghs\classes\GhsClass::getById($ghsId);
  51. $where = ['ghsId' => $ghsId, 'debt' => PurchaseClass::DEBT_YES];
  52. $cgList = PurchaseClass::getAllByCondition($where, 'addTime DESC', '*');
  53. $hdCgOrderDebtAmount = 0;
  54. if (!empty($cgList)) {
  55. foreach ($cgList as $cg) {
  56. $actPrice = $cg['remainDebtPrice'] ?? 0;
  57. $hdCgOrderDebtAmount = bcadd($hdCgOrderDebtAmount, $actPrice, 2);
  58. }
  59. }
  60. if ($ghsOrderDebtAmount < 0 || $hdCgOrderDebtAmount < 0) {
  61. noticeUtil::push("!!!!!!!供货商 {$ghsId} 客户id {$customId} 出现负数 $ghsOrderDebtAmount $hdCgOrderDebtAmount", '15280215347');
  62. echo "@@@@@@@@@@@@@供货商 {$ghsId} 客户id {$customId} 出现负数 $ghsOrderDebtAmount $hdCgOrderDebtAmount \n";
  63. }
  64. $ghsOrderDebtAmount = floatval($ghsOrderDebtAmount);
  65. $hdCgOrderDebtAmount = floatval($hdCgOrderDebtAmount);
  66. if ($ghsOrderDebtAmount != $hdCgOrderDebtAmount) {
  67. noticeUtil::push("!!!!!!!供货商 {$ghsId} 客户id {$customId} 二边欠款单总金额不一致", '15280215347');
  68. echo "---------供货商 {$ghsId} 客户id {$customId} 二边 欠款单 总金额 不一致 {$ghsOrderDebtAmount} {$hdCgOrderDebtAmount} \n";
  69. continue;
  70. }
  71. $customSaveDebtAmount = $custom['debtAmount'] ? floatval($custom['debtAmount']) : 0;
  72. $ghsSaveDebtAmount = $ghs['debtAmount'] ? floatval($ghs['debtAmount']) : 0;
  73. if ($customSaveDebtAmount != $ghsOrderDebtAmount) {
  74. echo "*********客户 {$customId} 欠款 和 订单总额 不一致 $customSaveDebtAmount $ghsOrderDebtAmount \n";
  75. noticeUtil::push("*********客户 {$customId} 欠款总金额 和 订单总合 不一致 $customSaveDebtAmount $ghsOrderDebtAmount ", '15280215347');
  76. continue;
  77. }
  78. if ($ghsSaveDebtAmount != $hdCgOrderDebtAmount) {
  79. echo "#########欠供货商 {$ghsId} 和 订单总额 不一致 $ghsSaveDebtAmount $hdCgOrderDebtAmount \n";
  80. noticeUtil::push("#########欠供货商 {$ghsId} 金额 和 订单总合 不一致 $ghsSaveDebtAmount $hdCgOrderDebtAmount ", '15280215347');
  81. continue;
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. //每天凌晨3点35分执行,取出所有 lunar==1 的 且 birthdayTime 小于当前时间的客户,然后把他们的阴历转阳历,然后更新他们的 birthdayTime
  89. public function actionUpdateBirthday()
  90. {
  91. try {
  92. $calendar = new Calendar();
  93. $customList = CustomClass::getAllByCondition(['lunar' => 1, 'birthdayTime<' => time()], null, 'id, userId, birthday, birthdayTime, birthdayMonth, birthdayDate', null, true);
  94. foreach ($customList as $custom) {
  95. $birthdayMonth = $custom->birthdayMonth;
  96. $birthdayDate = $custom->birthdayDate;
  97. $year = date("Y");
  98. // 农历转公历
  99. $result = $calendar->lunar($year, $birthdayMonth, $birthdayDate);
  100. $month = $result['gregorian_month'];
  101. $day = $result['gregorian_day'];
  102. $birthday = $year . '-' . $month . '-' . $day;
  103. // 如果今年的生日已经过去,则计算明年的生日
  104. if (strtotime($birthday) < strtotime(date('Y-m-d'))) {
  105. $year++;
  106. $result = $calendar->lunar($year, $birthdayMonth, $birthdayDate);
  107. $month = $result['gregorian_month'];
  108. $day = $result['gregorian_day'];
  109. $birthday = $year . '-' . $month . '-' . $day;
  110. }
  111. $custom->birthday = $birthday;
  112. $custom->birthdayTime = strtotime($birthday);
  113. $custom->save();
  114. UserClass::updateById($custom->userId, [
  115. 'birthday' => $birthday,
  116. 'birthdayTime' => $custom->birthdayTime
  117. ]);
  118. }
  119. Yii::info('阴历转阳历脚本成功执行');
  120. } catch (\Exception $e) {
  121. Yii::error('阴历转阳历脚本执行失败,原因:' . $e->getMessage());
  122. }
  123. }
  124. // 把所有客户的累计消费和成长值、积分都刷新一下
  125. public function actionRefreshCustomData()
  126. {
  127. ini_set('memory_limit', '2045M');
  128. set_time_limit(0);
  129. $redisKey = 'refresh_custom_data_processed_shops';
  130. // 1. 查询 xhShop 表,获取所有 ptStyle = 1 的门店数据,每次取出500个进行处理
  131. $query = \bizHd\shop\models\Shop::find()
  132. ->select('id, mainId')
  133. ->where(['ptStyle' => 1])
  134. ->orderBy('id DESC')
  135. ->asArray();
  136. // 2. 把每个门店下的所有用户进行遍历,刷新他们的累计消费和成长值、积分
  137. foreach ($query->batch(500) as $allShops) {
  138. if (!empty($allShops)) {
  139. foreach ($allShops as $shop) {
  140. $shopId = $shop['id'];
  141. $mainId = $shop['mainId'];
  142. // 判断是否已经执行过,执行过则跳过
  143. $hasProcessed = Yii::$app->redis->executeCommand('SISMEMBER', [$redisKey, $shopId]);
  144. if ($hasProcessed) {
  145. continue;
  146. }
  147. $customs = CustomClass::getAllByCondition(['shopId' => $shopId], null, 'id, userId, buyAmount, growth, member, memberName', null, true);
  148. $levelDatas = MemberLevelClass::getAllByCondition(['mainId' => $mainId], 'amount DESC', 'id, name, level, amount');
  149. if (!empty($customs) && !empty($levelDatas)) {
  150. foreach ($customs as $c) {
  151. $buyAmount = 0;
  152. // 获取所有零售订单,计算消费金额
  153. $orderList = \bizHd\order\classes\OrderClass::getAllByCondition(['customId' => $c->id], null, 'id, forward, actPrice');
  154. if (!empty($orderList)) {
  155. foreach ($orderList as $order) {
  156. if ($order['forward'] == 0) { //0 常规订单
  157. $buyAmount = bcadd($buyAmount, $order['actPrice'], 2);
  158. } elseif ($order['forward'] == 1) { //1 售后付款单(红冲)
  159. $buyAmount = bcsub($buyAmount, $order['actPrice'], 2);
  160. } else {
  161. Yii::info('订单类型不存在,订单ID:' . $order['id']);
  162. continue;
  163. }
  164. }
  165. }
  166. $c->buyAmount = $buyAmount;
  167. $c->growth = $buyAmount;
  168. $c->integral = $buyAmount;
  169. // 根据 growth 设置等级
  170. $memberData = CustomClass::getCustomExpenseLevel($c->growth, $mainId, $levelDatas); // 设置等级多处需要同步修改的,请搜索:getCustomExpenseLevel
  171. $c->member = $memberData['level'];
  172. $c->memberName = $memberData['name'];
  173. $c->listend = false; //关闭对 xhCustom 的 buyAmount 变动监听
  174. $c->save();
  175. // 更新客户所对应花店的消费金额
  176. HdClass::updateByCondition(['shopId' => $shopId, 'userId' => $c->userId], ['expendAmount' => $buyAmount]);
  177. }
  178. }
  179. // 记录该 shopId 已经执行完成
  180. Yii::$app->redis->executeCommand('SADD', [$redisKey, $shopId]);
  181. }
  182. }
  183. }
  184. // 脚本执行完之后,清除缓存
  185. Yii::$app->redis->executeCommand('DEL', [$redisKey]);
  186. }
  187. //批量创建客户
  188. private function createCustomer()
  189. {
  190. //重新写这个方法,用rabbitMQ来实现
  191. }
  192. //批发端分店从首店同步客户
  193. private function copyCustomers()
  194. {
  195. //重新写这个方法,用rabbitMQ来实现
  196. }
  197. //从文件批量创建客户 ssh 20240426
  198. public function actionFileCreateCustom()
  199. {
  200. //重新写这个方法,用rabbitMQ来实现
  201. }
  202. }