field('UserId,sum(RefundAmount) as money') ->where(['OrderState' => ['in', ProductOrderEnum::$Refunds]]) ->group(['UserId']) ->fetchAll(); foreach ($order_refund_list_db as $info){ $order_refund_list[$info['UserId']] = $info['money']; } //支付金额 $order_pay_list_db = $product_order_model->field('UserId,sum(PaymentAmount) as money') ->where(['OrderState' => ['in', ProductOrderEnum::$PayComplete]]) ->group(['UserId']) ->fetchAll(); //根据等级 $i=1; foreach ($order_pay_list_db as $info){ //如果有退款,先减去退款 if(isset( $order_refund_list[$info['UserId']])){ $info['money'] -= $order_refund_list[$info['UserId']]; } $temp = []; $temp = [ 'ConsumeAmount' => $info['money'], ]; if ($info['money']>=200 && $info['money']<500){ $temp['discount_id'] = 1; } elseif($info['money']>=500 && $info['money']<1500) { $temp['discount_id'] = 2; } elseif($info['money']>=1500 && $info['money']<3000) { $temp['discount_id'] = 4; } elseif($info['money']>=3000 && $info['money']<6000) { $temp['discount_id'] = 5; } elseif($info['money']>=6000) { $temp['discount_id'] = 6; } $user_model->updateOne(['Id'=>$info['UserId'],'agent_id'=>0],$temp); echo $i."\r"; $i++; }