| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- /**
- * @file SyncVerifyCard
- * @editor Created by vscode
- * @author WPing丶
- * @date 2024/11/19
- * @time 14:22:25
- *
- * 备注:同步储值卡消费记录
- */
- namespace app\commands;
- use app\models\Order;
- use app\models\OrderRefund;
- use app\models\User;
- use app\models\VerifyCard;
- use app\models\VerifyCardLog;
- use app\models\VerifyCardSale;
- use yii\console\Controller;
- class SyncVerifyCard extends Controller
- {
- /**
- * 模块名:index
- * 代码描述:同步储值卡消费记录
- * 作者:WPing丶
- * 请求方式:GET
- * 创建时间:2024/11/19 14:31:15
- */
- public function actionIndex()
- {
- //清空所有储值卡的消费记录数据(兼容有脏数据的情况,一般情况下注释)
- VerifyCardLog::updateAll([
- 'use_price' => 0,
- 'use_order_ids' => null,
- 'is_complete' => 0,
- ]);
- $usersWithBalancePay = User::find()
- ->alias('u') // 给 User 表起一个别名
- ->leftJoin(['o' => Order::tableName()], 'u.id = o.user_id') // 连接 Order 表
- ->leftJoin(['r' => OrderRefund::tableName()], 'r.order_id = o.id') // 连接 OrderRefund 表
- ->leftJoin(['vcl' => VerifyCardLog::tableName()], 'vcl.user_id = u.id') // 连接 VerifyCardLog 表
- ->where([
- 'o.is_pay' => 1, // 订单已支付
- 'o.is_delete' => 0, // 未删除
- 'o.is_recycle' => 0 // 未被回收
- ])
- ->andWhere([
- 'or',
- ['o.pay_type' => 3], // 使用余额支付
- [
- 'or',
- [
- 'and',
- 'o.is_combine_pay' => 1, // 组合支付
- ['>', 'combine_money', 0] // 且余额抵扣大于0
- ],
- ['>', 'o.balance', 0]
- ]
- ])
- ->andWhere(['<>', 'o.trade_status', 1]) // 排除交易状态为 1 的订单
- ->andWhere(['IS NOT', 'vcl.id', null]) // 排除没有储值卡的用户
- ->andWhere([
- 'or',
- ['r.id' => null], // 没有退款记录
- ['<>', 'r.status', 1] // 或者退款状态不是 1
- ])
- ->select('u.id') // 查询用户的 ID
- ->distinct() // 去重
- ->column(); // 获取所有用户 ID
- debug_log('待同步的User_id:'.implode(',', $usersWithBalancePay) , 'VerifyCard.log');
- foreach($usersWithBalancePay as $user_id) {
- //查询出用户所有的储值卡
- $card_list = VerifyCardLog::find()->alias('vcl')
- ->leftJoin(['vcs' => VerifyCardSale::tableName()], 'vcl.sale_id=vcs.id')
- ->leftJoin(['vc' => VerifyCard::tableName()], 'vcs.verify_card_id=vc.id')
- ->where([
- 'vcl.store_id' => 1,
- 'vc.type' => 3,
- 'vcl.user_id' => $user_id,
- 'vcl.type' => VerifyCardLog::WRITE_TYPE_EXCHANGE,
- 'vcl.is_complete' => 0,
- ])
- ->select('vc.total_price as price, vcl.*')
- ->orderBy(['vcl.use_time' => SORT_ASC])
- ->asArray()
- ->all();
- debug_log('用户User_id:'.$user_id.',的已激活储值卡' , 'VerifyCard.log');
- debug_log(json_encode($card_list,JSON_UNESCAPED_UNICODE), 'VerifyCard.log');
-
- // if(count($card_list) == 0) {
- // //用户没有储值卡的跳出循环
- // continue;
- // }
- $order_balance = 0;
- $last_order_id = 0;
- $last_order_ids = [];
- foreach($card_list as $card) {
- $time = $card['use_time'];
- $order_list = Order::find()->alias('o')
- ->leftJoin(['r' => OrderRefund::tableName()], 'r.order_id = o.id') // 连接 OrderRefund 表
- ->where([
- 'o.is_pay' => 1, // 订单已支付
- 'o.is_delete' => 0, // 未删除
- 'o.is_recycle' => 0, // 未被回收
- 'o.user_id' => $user_id,
- ])
- ->andWhere([
- 'or',
- ['o.pay_type' => 3], // 使用余额支付
- [
- 'or',
- [
- 'and',
- 'o.is_combine_pay' => 1, // 组合支付
- ['>', 'combine_money', 0] // 且余额抵扣大于0
- ],
- ['>', 'o.balance', 0]
- ]
- ])
- ->andWhere(['<>', 'o.trade_status', 1]) // 排除交易状态为 1 的订单
- ->andWhere(['>', 'o.pay_time', $time]) // 订单支付时间要大于储值卡的激活时间
- ->andWhere(['NOT IN', 'o.id', $last_order_ids]) // 订单支付时间要大于储值卡的激活时间
- ->andWhere([
- 'or',
- ['r.id' => null], // 没有退款记录
- ['<>', 'r.status', 1] // 或者退款状态不是 1
- ])
- ->select('o.*')
- ->orderBy(['o.pay_time' => SORT_ASC])
- ->asArray()
- ->all();
- // if(count($order_list) == 0) {
- // //用户有储值卡但未下单的跳出循环
- // continue 2;
- // }
- foreach($order_list as $order) {
- $pay_price = $order['combine_money'] > 0 ? $order['combine_money'] : $order['pay_price'];
- $log = VerifyCardLog::findOne($card['id']);
- $card_balance = bcsub($card['price'],$log->use_price,2);//卡内余额
- if($order['id'] == $last_order_id) {
- $pay_price = $order_balance;
- }
- if($pay_price <= $card_balance) {
- //储值卡内余额足以支付这笔订单
- $log->use_price += $pay_price;
- if($log->use_order_ids) {
- $use_order_ids = explode(',', $log->use_order_ids);
- $use_order_ids[] = $order['id'];
- $log->use_order_ids = implode(',', $use_order_ids);
- } else {
- $log->use_order_ids = $order['id'];
- }
- if($pay_price == $card_balance) {//如果储值卡内余额刚好够支付当前订单
- $log->is_complete = 1;
- }
- if (!$log->save()) {
- debug_log('储值卡消费记录保存失败', 'VerifyCard.log');
- debug_log($log->errors, 'VerifyCard.log');
- }
- $order_balance = 0;
- $last_order_id = 0;
- debug_log('OKOKOKOKOKOKOKOKOKOK,用户User_id:'.$user_id.'的用户于'.date('Y-m-d H:i:s',$order['pay_time']).'使用card_id为'.$card['id'].'的储值卡,支付了order_no为'.$order['order_no'].'的订单后,储值卡还剩下:¥'.bcsub($card_balance,$pay_price,2).'元' , 'VerifyCard.log');
- $last_order_ids[] = $order['id'];
- if($pay_price == $card_balance) {
- continue 2;
- }
- } else {
- //储值卡内余额不足支付这笔订单
- $log->use_price += $card_balance;
- if($log->use_order_ids) {
- $use_order_ids = explode(',', $log->use_order_ids);
- $use_order_ids[] = $order['id'];
- $log->use_order_ids = implode(',', $use_order_ids);
- } else {
- $log->use_order_ids = $order['id'];
- }
- $log->is_complete = 1;
- if (!$log->save()) {
- debug_log('储值卡消费记录保存失败', 'VerifyCard.log');
- debug_log($log->errors, 'VerifyCard.log');
- }
- $order_balance = bcsub($pay_price, $card_balance, 2);
- $last_order_id = $order['id'];
- debug_log('NONONONONONONONONONO,用户User_id:'.$user_id.'的用户于'.date('Y-m-d H:i:s',$order['pay_time']).'使用card_id为'.$card['id'].'的储值卡,支付了order_no为'.$order['order_no'].'的订单后,储值卡还剩下:¥0元' , 'VerifyCard.log');
- continue 2;
- }
- }
- }
- }
- }
- }
|