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 执行:阴历生日跨年刷新 * 从 xhUser 分页取出 lunar=1 且 birthdayTime 已过的用户,农历转当年/次年阳历后回写用户并同步 xhCustom * 命令:./yii custom/update-birthday */ public function actionUpdateBirthday() { try { date_default_timezone_set('PRC'); ini_set('memory_limit', '512M'); set_time_limit(0); $calendar = new Calendar(); $now = time(); $todayStart = strtotime(date('Y-m-d')); $updatedUserCount = 0; // 生日主数据在 xhUser;按 id 分页避免一次加载全表 $query = User::find() ->select('id, birthday, birthdayTime, birthdayMonth, birthdayDate, lunar') ->where(['lunar' => 1]) ->andWhere(['<', 'birthdayTime', $now]) ->orderBy('id ASC'); foreach ($query->batch(200) as $userList) { foreach ($userList as $user) { $userId = $user->id; $birthdayMonth = $user->birthdayMonth; $birthdayDate = $user->birthdayDate; $year = (int)date('Y'); try { // 农历转公历,有多处使用,需要同步修改,关键词 change_my_birthday $result = $calendar->lunar($year, $birthdayMonth, $birthdayDate); $month = $result['gregorian_month'] ?? 1; $day = $result['gregorian_day'] ?? 1; $birthday = $year . '-' . $month . '-' . $day; // 今年阳历生日已过,则换算明年农历对应阳历 if (strtotime($birthday) < $todayStart) { $year++; $result = $calendar->lunar($year, $birthdayMonth, $birthdayDate); $month = $result['gregorian_month'] ?? 1; $day = $result['gregorian_day'] ?? 1; $birthday = $year . '-' . $month . '-' . $day; } } catch (\InvalidArgumentException $e) { noticeUtil::push( '阴历转阳历脚本单条失败,userId:' . $userId . ' 月份:' . $birthdayMonth . ' 日期:' . $birthdayDate . ' ' . $e->getMessage(), '15280215347' ); continue; } $birthdayTime = strtotime($birthday); if ($birthdayTime <= 0) { continue; } // 先更新用户表(生日源数据),再按 userId 同步门店客户 UserClass::updateById($userId, [ 'birthday' => $birthday, 'birthdayTime' => $birthdayTime, ]); CustomClass::updateByCondition(['userId' => $userId], [ 'birthday' => $birthday, 'birthdayTime' => $birthdayTime, ]); $updatedUserCount++; } } Yii::info('阴历转阳历脚本成功执行,更新用户数:' . $updatedUserCount); echo "阴历转阳历完成,更新用户数:{$updatedUserCount}\n"; } catch (\Exception $e) { Yii::error('阴历转阳历脚本执行失败,原因:' . $e->getMessage()); echo '阴历转阳历脚本执行失败,原因:' . $e->getMessage() . "\n"; } } // 把所有客户的累计消费和成长值、积分都刷新一下 public function actionRefreshCustomData() { try { ini_set('memory_limit', '2045M'); set_time_limit(0); // 1. 查询 xhShop 表,获取所有 ptStyle = 1 的门店数据,每次取出500个进行处理 $query = Shop::find() ->select('id, mainId') ->where(['ptStyle' => 1,'id'=>6362]) ->orderBy('id DESC') ->asArray(); // 2. 把每个门店下的所有用户进行遍历,刷新他们的累计消费和成长值、积分 foreach ($query->batch(500) as $allShops) { if (!empty($allShops)) { foreach ($allShops as $shop) { $shopId = $shop['id']; $mainId = $shop['mainId']; echo '门店id:' . $shopId; $customList = CustomClass::getAllByCondition(['shopId' => $shopId], null, 'id, userId, balance, buyAmount, growth, integral, member, memberName', null, true); $levelData = MemberLevelClass::getAllByCondition(['mainId' => $mainId], 'amount DESC', 'id, name, level, amount'); if (!empty($customList)) { foreach ($customList as $custom) { $buyAmount = 0; // 获取所有零售订单,计算消费金额 $orderList = \bizHd\order\classes\OrderClass::getAllByCondition(['customId' => $custom->id, 'payStatus' => 1], null, 'id, forward, actPrice, payWay'); if (!empty($orderList)) { foreach ($orderList as $order) { if ($order['forward'] == 0) { //0 常规订单 $buyAmount = bcadd($buyAmount, $order['actPrice'], 2); } else { //1 红冲 $buyAmount = bcsub($buyAmount, $order['actPrice'], 2); } } } $custom->buyAmount = $buyAmount; $balance = $custom->balance ?? 0; if ($balance > 0) { $total = bcadd($balance, $buyAmount, 2); } else { $total = $buyAmount; } $custom->growth = $total; $custom->integral = $total; //echo " customId:" . $custom->id; // 根据 growth 设置等级 $memberData = CustomClass::getCustomExpenseLevel($custom->growth, $mainId, $levelData); // 设置等级多处需要同步修改的,请搜索:getCustomExpenseLevel //echo "--+--- "; $level = $memberData['level'] ?? 0; $level = trim($level); $level = floatval($level); $memberName = $memberData['name'] ?? ''; $memberName = trim($memberName); //echo $level."+".$memberName; $custom->member = $level; $custom->memberName = $memberName; //$custom->listend = false; //关闭对 xhCustom 的 buyAmount 变动监听 $custom->save(); //echo " 0000 \n"; // 更新客户所对应花店的消费金额 $hdId = $custom->hdId; if (!empty($hdId)) { HdClass::updateById($hdId, ['expendAmount' => $buyAmount]); } } } //echo "\n"; } } echo "end\n"; } } catch (\Exception $e) { Yii::error('把所有客户的累计消费和成长值、积分都刷新一下脚本执行失败,原因:' . $e->getMessage()); } } //批量创建客户 private function createCustomer() { //重新写这个方法,用rabbitMQ来实现 } //批发端分店从首店同步客户 private function copyCustomers() { //重新写这个方法,用rabbitMQ来实现 } //从文件批量创建客户 ssh 20240426 public function actionFileCreateCustom() { //重新写这个方法,用rabbitMQ来实现 } }