255], [['price'], 'number'] ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'mall_id' => 'Mall ID', 'user_id' => 'User ID', 'parent_id' => 'Parent ID', 'store_id' => 'Store ID', 'order_id' => 'Order ID', 'shipping_type' => 'Shipping Type', 'order_no' => 'Order No', 'order_status' => 'Order Status', 'created_at' => 'Created At', 'updated_at' => 'Updated At', ]; } public function getBase_user() { return $this->hasOne(User::class, ['id' => 'user_id'])->select(['id', 'username','nickname', 'avatar_url','mobile']); } public function getDetail() { return $this->hasMany(OrderDetail::class, ['order_id' => 'id']); } public function getOrderDetail() { return $this->hasMany(OrderDetail::class, ['order_id' => 'order_id']); } public function getReserve() { return $this->hasOne(OrderReserve::class, ['order_id' => 'id']); } public function getStore() { return $this->hasOne(HappinessStore::class, ['id' => 'store_id'])->asArray(true); } public static function setData(array $params) { try { if (!empty($params['mall_id']) && !empty($params['id'])) { $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id']]); if (empty($model)) { $model = new self(); $model->loadDefaultValues(); } } else { $model = new self(); $model->loadDefaultValues(); } if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage()); if (!$model->save()) throw new \Exception($model->getErrorMessage()); return $model; } catch (\Exception $e) { self::$static_error = $e->getMessage(); return false; } } /** * @name: * @msg: 关闭订单 * @param {*} $params * @param {*} $force * @return {*} */ public static function close($params, $force = false) { $trans = Yii::$app->db->beginTransaction(); $stock_locks = []; $close_lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_GOODS, $params['id']); try { // 加锁 $close_identification = uniqid(); $stock_locks[$close_lock_key] = $close_identification; // if (!LockHelper::lock($close_lock_key, $close_identification, 30, 60)) throw new \Exception('正在关闭订单'); $cond = ['mall_id' => $params['mall_id'], 'id' => $params['id']]; isset($params['user_id']) && $cond['user_id'] = $params['user_id']; isset($params['store_id']) && $cond['store_id'] = $params['store_id']; $model = self::findOne($cond); if (empty($model)) throw new Exception('订单不存在或已删除1'); // 判断订单是否已经被关闭 if ($model->order_status == OrderStatusEnum::REPEAL) throw new Exception('订单已经关闭'); // 判断是否是未支付的订单 if ($force == false && $model->order_status != OrderStatusEnum::NOT_PAY) throw new Exception('订单已经被处理'); $order = Order::findOne(['order_no'=>$model['order_no']]); // $model->close_time = time(); $model->order_status = OrderStatusEnum::REPEAL; if (!$model->save()) throw new Exception($model->getErrorMessage()); // if(!empty($order)){ // $order->close_time = $model->close_time; // $order->order_status = $model->order_status; // if (!$order->save()) throw new Exception($order->getErrorMessage()); // } // $goods_attr_arr = array_column($model->detail, 'num', 'goods_attr_id'); // //操作规格库存-回退 // foreach ($goods_attr_arr as $goods_attr_id => $num) { // $lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_GOODS_ATTR, $goods_attr_id); // $identification = uniqid(); // if (!LockHelper::lock($lock_key, $identification, 30, 60)) throw new Exception('当前访问人数过多'); // $stock_locks[$lock_key] = $identification; // //操作商品库存-回退 // HappinessStoreGoodsAttr::handleStock($goods_attr_id, $num, $params['store_id']); // } $trans->commit(); // 触发订单取消事件 // 解锁 // LockHelper::batchUlock($stock_locks); return $model; } catch (Exception $e) { $trans->rollback(); // 解锁 LockHelper::batchUlock($stock_locks); self::$static_error = $e->getMessage(); return false; } } public static function searchParams($mall_id, $search) { // 查询参数 $isReserve = $search['is_reserve']; $where[] = ['=', 'o.mall_id', $mall_id]; $where[] = ['>=', 'o.status', StatusEnum::DISABLED]; if ($isReserve) { $where[] = ['=', 'o.order_source', OrderSourceEnum::SOURCE_RESERVE]; } else { $where[] = ['not in', 'o.order_source', [OrderSourceEnum::SOURCE_SELF_MALL, OrderSourceEnum::SOURCE_RESERVE]]; } if (isset($search['order_id'])) $where[] = ['in', 'o.id', explode(',', $search['order_id'])]; ($search['payment_type'] ?? false) && $where[] = ['=', 'o.payment_type', $search['payment_type']]; ($search['shipping_type'] ?? false) && $where[] = ['=', 'o.shipping_type', $search['shipping_type']]; if (isset($search['order_status']) && $search['order_status'] != 999) { $where[] = ['=', 'o.order_status', $search['order_status']]; } ($search['order_no'] ?? false) && $where[] = ['=', 'o.order_no', $search['order_no']]; ($search['start'] ?? false) && $where[] = ['>=', 'o.created_at', strtotime($search['start'])]; ($search['end'] ?? false) && $where[] = ['<=', 'o.created_at', strtotime($search['end'])]; if ($search['type_value'] ?? false) { switch ($search['type']) { case 'mobile': $uids = User::find()->select('id')->where(['like', 'mobile', $search['type_value'] . '%', false])->column(); !empty($uids) && $where[] = ['in', 'yo.user_id', $uids]; break; case 'user_id': $where[] = ['in', 'yo.user_id', $search['type_value']]; break; default: } } if (!empty($search['store_name'])) { $store_ids = HappinessStore::find()->select('id')->where(['like', 'store_name', '%'.$search['store_name'].'%', false])->column(); if (!empty($store_ids)) { $where[] = ['in', 'yo.store_id', $store_ids]; } else { $where[] = ['yo.store_id' => 0]; } } if (!empty($search['store_user_id'])) { $store_ids = HappinessStore::find()->select('id')->where(['user_id' => $search['store_user_id']])->column(); if (!empty($store_ids)) { $where[] = ['in', 'yo.store_id', $store_ids]; } else { $where[] = ['yo.store_id' => 0]; } } return $where; } public static function OrderExpressDelivery($params,$trigger_event = true){ try{ $store_order = HappinessOrder::getOne([['mall_id' => $params['mall_id'], 'order_id' => $params['id'],'store_id'=>$params['store_id']]]); $order = Order::getOne([['mall_id' => $params['mall_id'], 'order_no' => $store_order['order_no'],'status'=>[StatusEnum::ENABLED]]]); if(empty($order)) throw new Exception('订单不存在或已删除'); $trans = Yii::$app->db->beginTransaction(); $model = new OrderExpress(); $params['order_id'] = $order['id']; $params['buyer_id'] = $order['user_id']; $params['buyer_name'] = $order['receiver_name']; $params['operator_username'] = $params['operator_name']; if(!$model->load($params,'') || !$model->save()) throw new \Exception($model->getErrorMessage()); //保存物流轨迹记录 $params['order_express_id'] = $model->id; $res = OrderExpressLog::saveLog($params); if ($res == false) throw new Exception(OrderExpressLog::getStaticError()); $condition['where'] = [['in', 'order_id', $order['id']],['is_feedback'=>[OrderStatusEnum::FEEDBACKING,OrderStatusEnum::FEEDBACKED]]]; $res = OrderDetail::lists($condition); if(!empty($res)) throw new \Exception('订单申请售后中,请先处理售后问题'); // 订单商品的物流状态改变 OrderDetail::updateAll(['shipping_status' => StatusEnum::ENABLED], ['in', 'order_id', $order['id']]); // 自动维护订单状态 $res = Order::autoUpdateStatus($order); if($res === false) throw new \Exception(Order::getStaticError()); $trans->commit(); if ($trigger_event) { // 发货成功触发事件,事务提交完成后触发 $event = new \common\events\order\OrderDeliveryEvent($params['mall_id']); $data['id'] = $model->id; $data['operator_id'] = $params['operator_id']; $data['operator_name'] = $params['operator_name']; Yii::$app->services->events->dispatch($event,$data, $event->listeners); } return true; }catch(Exception $e){ if(isset($trans)) $trans->rollback(); self::$static_error = $e->getMessage().$e->getFile().$e->getLine(); return false; } } public static function takeDelivery($params) { try { $cond = ['mall_id' => $params['mall_id'],'id' => $params['id'], 'status' => [StatusEnum::ENABLED]]; // isset($params['user_id']) && $cond['user_id'] = $params['user_id']; // isset($params['store_id']) && $cond['store_id'] = $params['store_id']; $model = Order::findOne($cond); if (empty($model)) throw new Exception('订单不存在或已删除'); if ($model->order_status == OrderStatusEnum::SING) throw new Exception('订单已经签收'); if ($model->order_status != OrderStatusEnum::PAY) throw new Exception('订单已经被处理'); if (OrderDetail::getAfterSaleCountByOrderId($model['id']) > 0) throw new Exception('请先处理或关闭订单售后'); $model->order_status = OrderStatusEnum::SING; $model->sign_time = time(); $model->consign_time = time(); if (!$model->save()) throw new Exception($model->getErrorMessage()); // 触发订单收货事件,事务提交完成后触发 $event = new OrderTakeDeliveryEvent($params['mall_id']); $data['id'] = $model['id']; $data['operator_id'] = $params['operator_id']; $data['operator_name'] = $params['operator_name']; Yii::$app->services->events->dispatch($event, $data, $event->listeners); return $model; } catch (Exception $e) { self::$static_error = $e->getMessage(); return false; } } }