| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
- namespace console\controllers;
- use biz\shop\classes\ShopClass;
- use bizGhs\custom\models\Custom;
- use bizGhs\order\classes\OrderClass;
- use bizHd\custom\classes\CustomClass;
- use bizHd\custom\classes\HdClass;
- use bizHd\member\classes\MemberLevelClass;
- use bizHd\purchase\classes\PurchaseClass;
- use common\components\noticeUtil;
- use common\components\util;
- use bizHd\user\classes\UserClass;
- use Overtrue\ChineseCalendar\Calendar;
- use Yii;
- use yii\console\Controller;
- class CustomController extends Controller
- {
- //欠款信息不一致跟踪 ssh 20220307 ./yii custom/adjust-debt
- public function actionAdjustDebt()
- {
- //感觉好像没有用了,可以停了 ssh 20250724
- util::stop();
- ini_set('memory_limit', '2045M');
- set_time_limit(0);
- $shopList = ShopClass::getAllByCondition(['ptStyle' => 2], null, '*', null, true);
- if (empty($shopList)) {
- return false;
- }
- foreach ($shopList as $key => $shop) {
- $shopId = $shop->id ?? 0;
- $query = new \yii\db\Query();
- $query->from(Custom::tableName());
- $query->where(['ownShopId' => $shopId]);
- $query->orderBy('addTime ASC');
- foreach ($query->batch(50) as $customList) {
- if (!empty($customList)) {
- foreach ($customList as $custom) {
- //$customName = $custom['name'] ?? '';
- $customId = $custom['id'] ?? 0;
- $where = ['customId' => $customId, 'debt' => 1];
- $orderList = OrderClass::getAllByCondition($where, 'addTime DESC', '*');
- $ghsOrderDebtAmount = 0;
- if (!empty($orderList)) {
- foreach ($orderList as $order) {
- $actPrice = $order['remainDebtPrice'] ?? 0;
- $ghsOrderDebtAmount = bcadd($ghsOrderDebtAmount, $actPrice, 2);
- }
- }
- $ghsId = $custom['ghsId'] ?? 0;
- $ghs = \bizHd\ghs\classes\GhsClass::getById($ghsId);
- $where = ['ghsId' => $ghsId, 'debt' => PurchaseClass::DEBT_YES];
- $cgList = PurchaseClass::getAllByCondition($where, 'addTime DESC', '*');
- $hdCgOrderDebtAmount = 0;
- if (!empty($cgList)) {
- foreach ($cgList as $cg) {
- $actPrice = $cg['remainDebtPrice'] ?? 0;
- $hdCgOrderDebtAmount = bcadd($hdCgOrderDebtAmount, $actPrice, 2);
- }
- }
- if ($ghsOrderDebtAmount < 0 || $hdCgOrderDebtAmount < 0) {
- noticeUtil::push("!!!!!!!供货商 {$ghsId} 客户id {$customId} 出现负数 $ghsOrderDebtAmount $hdCgOrderDebtAmount", '15280215347');
- echo "@@@@@@@@@@@@@供货商 {$ghsId} 客户id {$customId} 出现负数 $ghsOrderDebtAmount $hdCgOrderDebtAmount \n";
- }
- $ghsOrderDebtAmount = floatval($ghsOrderDebtAmount);
- $hdCgOrderDebtAmount = floatval($hdCgOrderDebtAmount);
- if ($ghsOrderDebtAmount != $hdCgOrderDebtAmount) {
- noticeUtil::push("!!!!!!!供货商 {$ghsId} 客户id {$customId} 二边欠款单总金额不一致", '15280215347');
- echo "---------供货商 {$ghsId} 客户id {$customId} 二边 欠款单 总金额 不一致 {$ghsOrderDebtAmount} {$hdCgOrderDebtAmount} \n";
- continue;
- }
- $customSaveDebtAmount = $custom['debtAmount'] ? floatval($custom['debtAmount']) : 0;
- $ghsSaveDebtAmount = $ghs['debtAmount'] ? floatval($ghs['debtAmount']) : 0;
- if ($customSaveDebtAmount != $ghsOrderDebtAmount) {
- echo "*********客户 {$customId} 欠款 和 订单总额 不一致 $customSaveDebtAmount $ghsOrderDebtAmount \n";
- noticeUtil::push("*********客户 {$customId} 欠款总金额 和 订单总合 不一致 $customSaveDebtAmount $ghsOrderDebtAmount ", '15280215347');
- continue;
- }
- if ($ghsSaveDebtAmount != $hdCgOrderDebtAmount) {
- echo "#########欠供货商 {$ghsId} 和 订单总额 不一致 $ghsSaveDebtAmount $hdCgOrderDebtAmount \n";
- noticeUtil::push("#########欠供货商 {$ghsId} 金额 和 订单总合 不一致 $ghsSaveDebtAmount $hdCgOrderDebtAmount ", '15280215347');
- continue;
- }
- }
- }
- }
- }
- }
- //每天凌晨3点35分执行,取出所有 lunar==1 的 且 birthdayTime 小于当前时间的客户,然后把他们的阴历转阳历,然后更新他们的 birthdayTime
- public function actionUpdateBirthday()
- {
- try {
- $calendar = new Calendar();
- $customList = CustomClass::getAllByCondition(['lunar' => 1, 'birthdayTime<' => time()], null, 'id, userId, birthday, birthdayTime, birthdayMonth, birthdayDate', null, true);
- foreach ($customList as $custom) {
- $birthdayMonth = $custom->birthdayMonth;
- $birthdayDate = $custom->birthdayDate;
- $year = date("Y");
- // 农历转公历
- $result = $calendar->lunar($year, $birthdayMonth, $birthdayDate);
- $month = $result['gregorian_month'];
- $day = $result['gregorian_day'];
- $birthday = $year . '-' . $month . '-' . $day;
- // 如果今年的生日已经过去,则计算明年的生日
- if (strtotime($birthday) < strtotime(date('Y-m-d'))) {
- $year++;
- $result = $calendar->lunar($year, $birthdayMonth, $birthdayDate);
- $month = $result['gregorian_month'];
- $day = $result['gregorian_day'];
- $birthday = $year . '-' . $month . '-' . $day;
- }
- $custom->birthday = $birthday;
- $custom->birthdayTime = strtotime($birthday);
- $custom->save();
- UserClass::updateById($custom->userId, [
- 'birthday' => $birthday,
- 'birthdayTime' => $custom->birthdayTime
- ]);
- }
- Yii::info('阴历转阳历脚本成功执行');
- } catch (\Exception $e) {
- Yii::error('阴历转阳历脚本执行失败,原因:' . $e->getMessage());
- }
- }
- // 把所有客户的累计消费和成长值、积分都刷新一下
- public function actionRefreshCustomData()
- {
- ini_set('memory_limit', '2045M');
- set_time_limit(0);
- $redisKey = 'refresh_custom_data_processed_shops';
- // 1. 查询 xhShop 表,获取所有 ptStyle = 1 的门店数据,每次取出500个进行处理
- $query = \bizHd\shop\models\Shop::find()
- ->select('id, mainId')
- ->where(['ptStyle' => 1])
- ->orderBy('id DESC')
- ->asArray();
- // 2. 把每个门店下的所有用户进行遍历,刷新他们的累计消费和成长值、积分
- foreach ($query->batch(500) as $allShops) {
- if (!empty($allShops)) {
- foreach ($allShops as $shop) {
- $shopId = $shop['id'];
- $mainId = $shop['mainId'];
- // 判断是否已经执行过,执行过则跳过
- $hasProcessed = Yii::$app->redis->executeCommand('SISMEMBER', [$redisKey, $shopId]);
- if ($hasProcessed) {
- continue;
- }
- $customs = CustomClass::getAllByCondition(['shopId' => $shopId], null, 'id, userId, buyAmount, growth, member, memberName', null, true);
- $levelDatas = MemberLevelClass::getAllByCondition(['mainId' => $mainId], 'amount DESC', 'id, name, level, amount');
- if (!empty($customs) && !empty($levelDatas)) {
- foreach ($customs as $c) {
- $buyAmount = 0;
- // 获取所有零售订单,计算消费金额
- $orderList = \bizHd\order\classes\OrderClass::getAllByCondition(['customId' => $c->id], null, 'id, forward, actPrice');
- if (!empty($orderList)) {
- foreach ($orderList as $order) {
- if ($order['forward'] == 0) { //0 常规订单
- $buyAmount = bcadd($buyAmount, $order['actPrice'], 2);
- } elseif ($order['forward'] == 1) { //1 售后付款单(红冲)
- $buyAmount = bcsub($buyAmount, $order['actPrice'], 2);
- } else {
- Yii::info('订单类型不存在,订单ID:' . $order['id']);
- continue;
- }
- }
- }
- $c->buyAmount = $buyAmount;
- $c->growth = $buyAmount;
- $c->integral = $buyAmount;
- // 根据 growth 设置等级
- $memberData = CustomClass::getCustomExpenseLevel($c->growth, $mainId, $levelDatas); // 设置等级多处需要同步修改的,请搜索:getCustomExpenseLevel
- $c->member = $memberData['level'];
- $c->memberName = $memberData['name'];
- $c->listend = false; //关闭对 xhCustom 的 buyAmount 变动监听
- $c->save();
- // 更新客户所对应花店的消费金额
- HdClass::updateByCondition(['shopId' => $shopId, 'userId' => $c->userId], ['expendAmount' => $buyAmount]);
- }
- }
- // 记录该 shopId 已经执行完成
- Yii::$app->redis->executeCommand('SADD', [$redisKey, $shopId]);
- }
- }
- }
- // 脚本执行完之后,清除缓存
- Yii::$app->redis->executeCommand('DEL', [$redisKey]);
- }
- //批量创建客户
- private function createCustomer()
- {
- //重新写这个方法,用rabbitMQ来实现
- }
- //批发端分店从首店同步客户
- private function copyCustomers()
- {
- //重新写这个方法,用rabbitMQ来实现
- }
- //从文件批量创建客户 ssh 20240426
- public function actionFileCreateCustom()
- {
- //重新写这个方法,用rabbitMQ来实现
- }
- }
|