| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- <?php
- namespace addons\StarChain\common\models;
- use addons\StarChain\common\enums\StarChainOrderEnum;
- use common\models\order\OrderDetail;
- use common\models\order\OrderExpress;
- use Yii;
- use yii\helpers\Json;
- /**
- * This is the model class for table "{{%addons_star_chain_order}}".
- *
- * @property int $id
- * @property int|null $mall_id
- * @property int|null $user_id
- * @property int|null $order_id 商城订单ID
- * @property string|null $order_no 商城订单编号
- * @property string|null $sku_id sku_id
- * @property string|null $spu_id spu_id
- * @property int|null $num 下单数量
- * @property string|null $chain_order_sn 供应链端订单编号
- * @property string|null $chain_batch_order_sn 返回订单批次号batchOrderSn
- * @property float|null $chain_total_amount 订单总金额
- * @property float|null $chain_pay_amount 应付总金额
- * @property int|null $chain_order_status 10为已提交待付款,20为已付款待发货,30为已发货待收货,40为确认收货交易成功,50、60为已取消,70、80、90、100为已关闭
- * @property string|null $chain_deliver_list 物流
- * @property string|null $dot 下单请求数据
- * @property string|null $error 下单错误信息
- * @property int|null $status 0 未推送 1 下单成功 -1 下单失败
- * @property int|null $updated_at
- * @property int|null $created_at
- * @property array $items
- */
- class Order extends BaseModel
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_star_chain_order}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'order_id', 'num', 'chain_order_status', 'status', 'updated_at', 'created_at'], 'integer'],
- [['chain_total_amount', 'chain_pay_amount'], 'number'],
- [['chain_deliver_list', 'dot'], 'string'],
- [['order_no'], 'string', 'max' => 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);
- }
- }
|