CustomController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. * 每天凌晨 3:35 执行:阴历生日跨年刷新
  93. * 从 xhUser 分页取出 lunar=1 且 birthdayTime 已过的用户,农历转当年/次年阳历后回写用户并同步 xhCustom
  94. * 命令:./yii custom/update-birthday
  95. */
  96. public function actionUpdateBirthday()
  97. {
  98. try {
  99. date_default_timezone_set('PRC');
  100. ini_set('memory_limit', '512M');
  101. set_time_limit(0);
  102. $calendar = new Calendar();
  103. $now = time();
  104. $todayStart = strtotime(date('Y-m-d'));
  105. $updatedUserCount = 0;
  106. // 生日主数据在 xhUser;按 id 分页避免一次加载全表
  107. $query = User::find()
  108. ->select('id, birthday, birthdayTime, birthdayMonth, birthdayDate, lunar')
  109. ->where(['lunar' => 1])
  110. ->andWhere(['<', 'birthdayTime', $now])
  111. ->orderBy('id ASC');
  112. foreach ($query->batch(200) as $userList) {
  113. foreach ($userList as $user) {
  114. $userId = $user->id;
  115. $birthdayMonth = $user->birthdayMonth;
  116. $birthdayDate = $user->birthdayDate;
  117. $year = (int)date('Y');
  118. try {
  119. // 农历转公历,有多处使用,需要同步修改,关键词 change_my_birthday
  120. $result = $calendar->lunar($year, $birthdayMonth, $birthdayDate);
  121. $month = $result['gregorian_month'] ?? 1;
  122. $day = $result['gregorian_day'] ?? 1;
  123. $birthday = $year . '-' . $month . '-' . $day;
  124. // 今年阳历生日已过,则换算明年农历对应阳历
  125. if (strtotime($birthday) < $todayStart) {
  126. $year++;
  127. $result = $calendar->lunar($year, $birthdayMonth, $birthdayDate);
  128. $month = $result['gregorian_month'] ?? 1;
  129. $day = $result['gregorian_day'] ?? 1;
  130. $birthday = $year . '-' . $month . '-' . $day;
  131. }
  132. } catch (\InvalidArgumentException $e) {
  133. noticeUtil::push(
  134. '阴历转阳历脚本单条失败,userId:' . $userId . ' 月份:' . $birthdayMonth . ' 日期:' . $birthdayDate . ' ' . $e->getMessage(),
  135. '15280215347'
  136. );
  137. continue;
  138. }
  139. $birthdayTime = strtotime($birthday);
  140. if ($birthdayTime <= 0) {
  141. continue;
  142. }
  143. // 先更新用户表(生日源数据),再按 userId 同步门店客户
  144. UserClass::updateById($userId, [
  145. 'birthday' => $birthday,
  146. 'birthdayTime' => $birthdayTime,
  147. ]);
  148. CustomClass::updateByCondition(['userId' => $userId], [
  149. 'birthday' => $birthday,
  150. 'birthdayTime' => $birthdayTime,
  151. ]);
  152. $updatedUserCount++;
  153. }
  154. }
  155. Yii::info('阴历转阳历脚本成功执行,更新用户数:' . $updatedUserCount);
  156. echo "阴历转阳历完成,更新用户数:{$updatedUserCount}\n";
  157. } catch (\Exception $e) {
  158. Yii::error('阴历转阳历脚本执行失败,原因:' . $e->getMessage());
  159. echo '阴历转阳历脚本执行失败,原因:' . $e->getMessage() . "\n";
  160. }
  161. }
  162. // 把所有客户的累计消费和成长值、积分都刷新一下
  163. public function actionRefreshCustomData()
  164. {
  165. try {
  166. ini_set('memory_limit', '2045M');
  167. set_time_limit(0);
  168. // 1. 查询 xhShop 表,获取所有 ptStyle = 1 的门店数据,每次取出500个进行处理
  169. $query = Shop::find()
  170. ->select('id, mainId')
  171. ->where(['ptStyle' => 1,'id'=>6362])
  172. ->orderBy('id DESC')
  173. ->asArray();
  174. // 2. 把每个门店下的所有用户进行遍历,刷新他们的累计消费和成长值、积分
  175. foreach ($query->batch(500) as $allShops) {
  176. if (!empty($allShops)) {
  177. foreach ($allShops as $shop) {
  178. $shopId = $shop['id'];
  179. $mainId = $shop['mainId'];
  180. echo '门店id:' . $shopId;
  181. $customList = CustomClass::getAllByCondition(['shopId' => $shopId], null, 'id, userId, balance, buyAmount, growth, integral, member, memberName', null, true);
  182. $levelData = MemberLevelClass::getAllByCondition(['mainId' => $mainId], 'amount DESC', 'id, name, level, amount');
  183. if (!empty($customList)) {
  184. foreach ($customList as $custom) {
  185. $buyAmount = 0;
  186. // 获取所有零售订单,计算消费金额
  187. $orderList = \bizHd\order\classes\OrderClass::getAllByCondition(['customId' => $custom->id, 'payStatus' => 1], null, 'id, forward, actPrice, payWay');
  188. if (!empty($orderList)) {
  189. foreach ($orderList as $order) {
  190. if ($order['forward'] == 0) { //0 常规订单
  191. $buyAmount = bcadd($buyAmount, $order['actPrice'], 2);
  192. } else { //1 红冲
  193. $buyAmount = bcsub($buyAmount, $order['actPrice'], 2);
  194. }
  195. }
  196. }
  197. $custom->buyAmount = $buyAmount;
  198. $balance = $custom->balance ?? 0;
  199. if ($balance > 0) {
  200. $total = bcadd($balance, $buyAmount, 2);
  201. } else {
  202. $total = $buyAmount;
  203. }
  204. $custom->growth = $total;
  205. $custom->integral = $total;
  206. //echo " customId:" . $custom->id;
  207. // 根据 growth 设置等级
  208. $memberData = CustomClass::getCustomExpenseLevel($custom->growth, $mainId, $levelData); // 设置等级多处需要同步修改的,请搜索:getCustomExpenseLevel
  209. //echo "--+--- ";
  210. $level = $memberData['level'] ?? 0;
  211. $level = trim($level);
  212. $level = floatval($level);
  213. $memberName = $memberData['name'] ?? '';
  214. $memberName = trim($memberName);
  215. //echo $level."+".$memberName;
  216. $custom->member = $level;
  217. $custom->memberName = $memberName;
  218. //$custom->listend = false; //关闭对 xhCustom 的 buyAmount 变动监听
  219. $custom->save();
  220. //echo " 0000 \n";
  221. // 更新客户所对应花店的消费金额
  222. $hdId = $custom->hdId;
  223. if (!empty($hdId)) {
  224. HdClass::updateById($hdId, ['expendAmount' => $buyAmount]);
  225. }
  226. }
  227. }
  228. //echo "\n";
  229. }
  230. }
  231. echo "end\n";
  232. }
  233. } catch (\Exception $e) {
  234. Yii::error('把所有客户的累计消费和成长值、积分都刷新一下脚本执行失败,原因:' . $e->getMessage());
  235. }
  236. }
  237. //批量创建客户
  238. private function createCustomer()
  239. {
  240. //重新写这个方法,用rabbitMQ来实现
  241. }
  242. //批发端分店从首店同步客户
  243. private function copyCustomers()
  244. {
  245. //重新写这个方法,用rabbitMQ来实现
  246. }
  247. //从文件批量创建客户 ssh 20240426
  248. public function actionFileCreateCustom()
  249. {
  250. //重新写这个方法,用rabbitMQ来实现
  251. }
  252. }