| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- namespace addons\StarChain\common\models;
- use common\helpers\ArrayHelper;
- use Yii;
- /**
- * This is the model class for table "{{%addons_star_chain_order_item}}".
- *
- * @property int $id
- * @property int $mall_id Mall Id
- * @property int $order_id 供应链订单ID
- * @property int $mall_order_id 主商城订单ID
- * @property string $chain_batch_order_sn 返回订单批次号batchOrderSn
- * @property string $chain_order_sn 供应链端订单号
- * @property int $order_detail_id 主商城订单详情ID
- * @property string $spu_id 供应链端商品ID
- * @property string $sku_id 供应链端Sku Id
- * @property int $num 购买数量
- * @property string|null $delivery_sn 物流编号
- * @property string|null $delivery_corp_sn 物流公司编号
- * @property int $send_status 发货状态 0未发货 1已发货
- * @property int|null $updated_at
- * @property int|null $created_at
- * @property Order|null $order
- */
- class OrderItem extends BaseModel
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_star_chain_order_item}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'order_id', 'mall_order_id', 'order_detail_id', 'spu_id', 'sku_id', 'num'], 'required'],
- [['mall_id', 'order_id', 'mall_order_id', 'order_detail_id', 'num', 'send_status', 'updated_at', 'created_at'], 'integer'],
- [['chain_batch_order_sn', 'chain_order_sn', 'delivery_sn', 'delivery_corp_sn'], 'string', 'max' => 64],
- [['spu_id', 'sku_id'], 'string', 'max' => 32],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'order_id' => 'Order ID',
- 'mall_order_id' => 'Mall Order ID',
- 'chain_batch_order_sn' => 'Chain Batch Order Sn',
- 'chain_order_sn' => 'Chain Order Sn',
- 'order_detail_id' => 'Order Detail ID',
- 'spu_id' => 'Spu ID',
- 'sku_id' => 'Sku ID',
- 'num' => 'Num',
- 'delivery_sn' => 'Delivery Sn',
- 'delivery_corp_sn' => 'Delivery Corp Sn',
- 'send_status' => 'Send Status',
- 'updated_at' => 'Updated At',
- 'created_at' => 'Created At',
- ];
- }
- /**
- * @return ActiveQueryInterface the relational query object.
- */
- public function getOrder()
- {
- return $this->hasOne(Order::class, ['id' => 'order_id']);
- }
- /**
- * 创建子订单
- *
- * @param Order $order
- * @param array $goods_list
- * @return boolean
- */
- public static function createOrderItem(Order $order, array $goods_list)
- {
- foreach ($goods_list as $item) {
- $model = new self();
- $model->mall_id = $order->mall_id;
- $model->order_id = $order->id;
- $model->spu_id = $item['spuId'];
- $model->sku_id = $item['skuId'];
- $model->num = $item['goodsQty'];
- $model->mall_order_id = $item['mall_order_id'];
- $model->order_detail_id = $item['order_detail_id'];
- if (!$model->save()) throw new \Exception("创建子订单失败");
- }
- return true;
- }
- /**
- * 编辑子订单
- *
- * @param integer $order_id
- * @param array $data
- * @return boolean
- */
- public static function editOrderItem(int $order_id, array $data)
- {
- $order_list = self::find()->where(['order_id' => $order_id])->all();
- foreach ($order_list as $item) {
- $order_item = ArrayHelper::find($data, function($i) use ($item) {
- return in_array($item->sku_id, explode(',', $i['skuIds']));
- });
- $item->chain_order_sn = $order_item['orderSn'];
- $item->chain_batch_order_sn = $order_item['batchOrderSn'];
- if (!$item->save()) throw new \Exception("编辑子订单失败");
- }
- return true;
- }
- /**
- * 获取item by order_detail_id
- *
- * @param integer $order_detail_id
- * @return static|null
- */
- public static function getOrderItemByDetailId(int $order_detail_id)
- {
- return self::findOne([
- 'order_detail_id' => $order_detail_id,
- ]);
- }
- /**
- * 获取子订单详情ID列表
- *
- * @param integer $order_id
- * @param array $sku_ids
- * @return array
- */
- public static function getDetailIdsBySkuIds(int $order_id, array $sku_ids)
- {
- return self::find()->select('order_detail_id')->where([
- 'order_id' => $order_id,
- 'sku_id' => $sku_ids,
- ])->column();
- }
- /**
- * 获取子订单详情ID列表
- *
- * @param integer $order_id
- * @param array $sku_ids
- * @return array
- */
- public static function getDetailBySkuIds(int $order_id, array $sku_ids)
- {
- return self::find()->where([
- 'order_id' => $order_id,
- 'sku_id' => $sku_ids,
- ])->all();
- }
- /**
- * @param integer $mall_id
- * @param string $order_sn
- * @return self|null
- */
- public static function getOrderByOrderSn(int $mall_id, string $order_sn)
- {
- return static::find()->where(['mall_id' => $mall_id, 'chain_order_sn' => $order_sn])
- ->with(['order'])
- ->one();
- }
- }
|