32], [['sku_id', 'spu_id', 'chain_order_sn', 'chain_batch_order_sn'], 'string', 'max' => 64], [['error'], 'string', 'max' => 255], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'mall_id' => 'Mall ID', 'user_id' => 'User ID', 'order_id' => 'Order ID', 'order_no' => 'Order No', 'sku_id' => 'Sku ID', 'spu_id' => 'Spu ID', 'num' => 'Num', 'chain_order_sn' => 'Chain Order Sn', 'chain_batch_order_sn' => 'Chain Batch Order Sn', 'chain_total_amount' => 'Chain Total Amount', 'chain_pay_amount' => 'Chain Pay Amount', 'chain_order_status' => 'Chain Order Status', 'chain_deliver_list' => 'Chain Deliver List', 'dot' => 'Dot', 'error' => 'Error', 'status' => 'Status', 'updated_at' => 'Updated At', 'created_at' => 'Created At', ]; } /** * 获取商品详情 * * @return ActiveQueryInterface */ public function getGoods() { return $this->hasOne(Goods::class, ['spu_id' => 'spu_id']); } /** * 获取订单详情 * * @return ActiveQueryInterface */ public function getDetail() { return $this->hasOne(OrderDetail::class, ['order_id' => 'order_id']); } /** * 获取订单详情列表 * * @return ActiveQueryInterface */ public function getDetails() { return $this->hasMany(OrderDetail::class, ['order_id' => 'order_id']); } /** * 获取订单信息 * * @return ActiveQueryInterface */ public function getOrder() { return $this->hasOne(MallOrder::class, ['id' => 'order_id']); } /** * @return ActiveQueryInterface */ public function getItem() { return $this->hasOne(OrderItem::class, ['order_id' => 'id']); } /** * @return ActiveQueryInterface the relational query object. */ public function getItems() { return $this->hasMany(OrderItem::class, ['order_id' => 'id']); } /** * 新增订单 * * @param array $data * @return boolean|Order */ public static function addOrder($data) { $model = new self(); if ($model->load($data, '') && $model->save()) { return $model; } return false; } /** * 创建订单 * * @param MallOrder $order * @param array $dto * @param integer $status * @return boolean|Order * @throws Exception */ public static function createOrder(MallOrder $order, array $dto, int $status = 0) { $model = new self(); $model->mall_id = $order->mall_id; $model->user_id = $order->user_id; $model->order_id = $order->id; $model->order_no = $order->order_no; $model->sku_id = ''; // 使用item表 当前字段已遗弃 $model->spu_id = ''; // 使用item表 当前字段已遗弃 $model->num = 0; // 使用item表 当前字段已遗弃 $model->dot = Json::encode($dto); $model->status = $status; if (!$model->save()) throw new \Exception('Order::createOrder 创建订单失败'); // 添加子订单 OrderItem::createOrderItem($model, $dto['goodsList']); return $model; } /** * 订单成功 * * @param array $data * @return boolean */ public function orderSuccess(array $data) { $first = reset($data); $this->chain_batch_order_sn = $first['batchOrderSn']; $this->chain_total_amount = $first['totalAmount']; $this->chain_pay_amount = $first['payAmount']; $this->chain_order_status = $first['orderStatus']; $this->status = 1; // 子订单完成 OrderItem::editOrderItem($this->id, $data); return $this->save(); } /** * 订单失败 * * @param string $error * @return boolean */ public function orderError($error) { $this->status = -1; $this->error = $error; return $this->save(); } /** * 订单发货 * * @return void */ public function delivery($order_data) { $this->chain_deliver_list = Json::encode($order_data['deliverList']); $this->chain_order_status = StarChainOrderEnum::STATUS_30; $this->updated_at = time(); $this->save(); $params = $this->_getExpressParams(current(($order_data['deliverList']))); if (OrderExpress::delivery($params) === false) { throw new \Exception( sprintf('OrderExpress::delivery发货失败, params:%s msg: %s', json_encode($params), OrderExpress::getStaticError()) ); } } /** * 通过消息推送数据进行发货 * * @param string $delivery_sn 消息推送过来的物流单号 * @param string $delivery_corp_sn 消息推送过来的物流公司编号 * @param array $sku_ids 消息推送过来的发货的商品id * @return void */ public function deliveryByMsg( string $delivery_sn, string $delivery_corp_sn, array $sku_ids ) { // 主订单设置发货状态 $params = $this->_getExpressParamsByMsg($delivery_sn, $delivery_corp_sn, $sku_ids); $this->chain_order_status = StarChainOrderEnum::STATUS_30; $this->updated_at = time(); $this->save(); if (OrderExpress::delivery($params) === false) { Yii::$app->custom->logs( 'OrderExpress::deliveryByMsg发货失败', [ 'params' => $params, 'msg' => OrderExpress::getStaticError(), ] ); throw new \Exception( sprintf('OrderExpress::deliveryByMsg发货失败, params:%s msg: %s', json_encode($params), OrderExpress::getStaticError()) ); } } /** * 获取发货信息 * * @param array $logistic * @return array */ private function _getExpressParams($logistic) { $express = Express::getExpressByCode($logistic['deliveryCorpSn']); $express_no = $logistic['deliverySn'] ?? ''; $params['id'] = $this->order_id; foreach ($this->details as $detail) { $params['order_detail_ids'][] = $detail->id; } $params['shipping_type'] = 0; if (!empty($express)) { $params['shipping_type'] = 1; $params['express_id'] = $express->express_id; $params['express_name'] = $express->name; $params['express_no'] = $express_no; } $params['mall_id'] = $this->mall_id; $params['mch_id'] = 0; $params['operator_id'] = 0; $params['operator_name'] = '怡亚通供应方'; return $params; } /** * 获取发货信息(通过消息推送) * * @param string $delivery_sn * @param string $delivery_corp_sn * @return array */ private function _getExpressParamsByMsg(string $delivery_sn, string $delivery_corp_sn, array $sku_ids) { $express = Express::getExpressByCode($delivery_corp_sn); $express_no = $delivery_sn; $params['id'] = $this->order_id; $params['order_detail_ids'] = $this->getDetailIdsBySkuIds($sku_ids); $params['shipping_type'] = 0; if (!empty($express)) { $params['shipping_type'] = 1; $params['express_id'] = $express->express_id; $params['express_name'] = $express->name; $params['express_no'] = $express_no; } $params['mall_id'] = $this->mall_id; $params['mch_id'] = 0; $params['operator_id'] = 0; $params['operator_name'] = '怡亚通供应方'; return $params; } /** * 通过sku_id获取订单详情id * * @param array $sku_ids * @return array */ public function getDetailIdsBySkuIds(array $sku_ids) { return OrderItem::getDetailIdsBySkuIds($this->id, $sku_ids); } /** * 查询未发货的订单 * * @param int $mall_id * @param string|array $order_sn * @return array */ public static function getUnDeliveryOrderByOrderSn($mall_id, $order_sn) { return static::find()->where( ['mall_id' => $mall_id, 'chain_order_status' => StarChainOrderEnum::STATUS_20, 'chain_order_sn' => $order_sn] )->all(); } /** * 通过订单号查询订单 * * @param integer $mall_id * @param string $order_sn * @return static|null */ public static function getOrderByOrderSn(int $mall_id, string $order_sn) { return static::find()->where( ['mall_id' => $mall_id, 'chain_order_sn' => $order_sn] )->one(); } /** * @param integer $mall_id * @param integer $order_id * @param array $fields * @return self|null */ public static function getOrderAndItemByOrderId( int $mall_id, int $order_id, $fields = ['id', 'chain_order_sn', 'sku_id', 'chain_deliver_list'] ) { return static::find() ->where(['order_id' => $order_id, 'mall_id' => $mall_id]) ->with(['items']) ->select($fields) ->one(); } /** * @param integer $mall_id * @param integer $order_id * @param array $fields * @return self|null */ public static function getOrderAndItemById( int $mall_id, int $id ) { return static::find() ->where(['id' => $id, 'mall_id' => $mall_id]) ->with(['items']) ->one(); } /** * 迁移数据维护 * * @return void */ public static function maintainData() { $order_list = static::find() ->select('id, order_id, mall_id, sku_id, spu_id, num, created_at') ->with([ 'detail' => function ($query) { $query->select('id, order_id, mall_id'); }, ]) ->andWhere(['>', 'sku_id', 0]) ->andWhere(['>', 'spu_id', 0]) ->andWhere(['>', 'num', 0]) ->all(); $data = array_map(function ($item) { return [ 'order_id' => $item->id, 'mall_id' => $item->mall_id, 'sku_id' => $item->sku_id, 'spu_id' => $item->spu_id, 'num' => $item->num, 'mall_order_id' => $item->order_id, 'order_detail_id' => $item->detail->id, 'updated_at' => $item->created_at, 'created_at' => $item->created_at, ]; }, $order_list); OrderItem::batchInsert($data, false); } }