CustomController.php 12 KB

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