CustomController.php 12 KB

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