'余额支付', 20 => '微信支付' ]; /** * 关联用户表 * @return \think\model\relation\BelongsTo */ public function user() { return $this->belongsTo(User::class); } /** * 关联米卡订单备注表 */ public function riceCardOrderRemark() { return $this->hasMany(RiceCardOrderRemark::class, 'order_id'); } /** * 获取订单图片 */ public function getRiceCardImgAttr($v){ $setting = Setting:: getItem('storage',10001); $oss_domain = $setting['engine']['aliyun']['domain']??''; return (stripos($v,"http") === false) ? $oss_domain.'/'.$v : $v; } /** * 订单详情 * @param array|int $where * @param array $with * @return null|static */ public static function detail($where, array $with = []) { return static::get($where, $with); } /** * 未支付订单自动关闭 * @param int $storeId * @param int $closeMinutes 自定关闭订单分钟数 * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function closeEvent(int $storeId, int $closeMinutes) { // 截止时间 $deadlineTime = time() - ((int)$closeMinutes * 60); // 查询截止时间未支付的订单 $model = new RiceCardOrder; $list = RiceCardOrder::where("pay_status",0)->where('order_status',10)->where("create_time",'<',Date("Y-m-d H:i:s",$deadlineTime))->select(); // 订单ID集 $orderIds = helper::getArrayColumn($list, 'id'); if (!empty($orderIds)) { // 批量更新订单状态为已取消 $model->onBatchUpdate($orderIds, ['order_status' => 40]); //把下单减的库存返回去 $this->backStock($list); } // 记录日志 Tools::taskLogs('RiceCardOrderCancel', 'closeEvent', [ 'storeId' => $storeId, 'closeMinutes' => $closeMinutes, 'deadlineTime' => $deadlineTime, 'orderIds' => helper::jsonEncode($orderIds) ]); } //订单返回库存 public function backStock($list){ $riceCard = []; foreach($list as $row){ // 更新条件: 订单已经支付或者订单商品为 "下单减库存" $riceCard[] = [ 'where' => ['id' => $row->rice_card_id], 'data' => ['stock' => ['inc', $row->buy_num]] ]; } if(!empty($riceCard)){ (new RiceCard)->updateRiceCard($riceCard); } } /** * 批量更新订单 * @param $orderIds * @param $data * @return false|int */ public function onBatchUpdate($orderIds, $data) { return static::updateBase($data, [['id', 'in', $orderIds]]); } }