OrderItem.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace addons\StarChain\common\models;
  3. use common\helpers\ArrayHelper;
  4. use Yii;
  5. /**
  6. * This is the model class for table "{{%addons_star_chain_order_item}}".
  7. *
  8. * @property int $id
  9. * @property int $mall_id Mall Id
  10. * @property int $order_id 供应链订单ID
  11. * @property int $mall_order_id 主商城订单ID
  12. * @property string $chain_batch_order_sn 返回订单批次号batchOrderSn
  13. * @property string $chain_order_sn 供应链端订单号
  14. * @property int $order_detail_id 主商城订单详情ID
  15. * @property string $spu_id 供应链端商品ID
  16. * @property string $sku_id 供应链端Sku Id
  17. * @property int $num 购买数量
  18. * @property string|null $delivery_sn 物流编号
  19. * @property string|null $delivery_corp_sn 物流公司编号
  20. * @property int $send_status 发货状态 0未发货 1已发货
  21. * @property int|null $updated_at
  22. * @property int|null $created_at
  23. * @property Order|null $order
  24. */
  25. class OrderItem extends BaseModel
  26. {
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public static function tableName()
  31. {
  32. return '{{%addons_star_chain_order_item}}';
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function rules()
  38. {
  39. return [
  40. [['mall_id', 'order_id', 'mall_order_id', 'order_detail_id', 'spu_id', 'sku_id', 'num'], 'required'],
  41. [['mall_id', 'order_id', 'mall_order_id', 'order_detail_id', 'num', 'send_status', 'updated_at', 'created_at'], 'integer'],
  42. [['chain_batch_order_sn', 'chain_order_sn', 'delivery_sn', 'delivery_corp_sn'], 'string', 'max' => 64],
  43. [['spu_id', 'sku_id'], 'string', 'max' => 32],
  44. ];
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function attributeLabels()
  50. {
  51. return [
  52. 'id' => 'ID',
  53. 'mall_id' => 'Mall ID',
  54. 'order_id' => 'Order ID',
  55. 'mall_order_id' => 'Mall Order ID',
  56. 'chain_batch_order_sn' => 'Chain Batch Order Sn',
  57. 'chain_order_sn' => 'Chain Order Sn',
  58. 'order_detail_id' => 'Order Detail ID',
  59. 'spu_id' => 'Spu ID',
  60. 'sku_id' => 'Sku ID',
  61. 'num' => 'Num',
  62. 'delivery_sn' => 'Delivery Sn',
  63. 'delivery_corp_sn' => 'Delivery Corp Sn',
  64. 'send_status' => 'Send Status',
  65. 'updated_at' => 'Updated At',
  66. 'created_at' => 'Created At',
  67. ];
  68. }
  69. /**
  70. * @return ActiveQueryInterface the relational query object.
  71. */
  72. public function getOrder()
  73. {
  74. return $this->hasOne(Order::class, ['id' => 'order_id']);
  75. }
  76. /**
  77. * 创建子订单
  78. *
  79. * @param Order $order
  80. * @param array $goods_list
  81. * @return boolean
  82. */
  83. public static function createOrderItem(Order $order, array $goods_list)
  84. {
  85. foreach ($goods_list as $item) {
  86. $model = new self();
  87. $model->mall_id = $order->mall_id;
  88. $model->order_id = $order->id;
  89. $model->spu_id = $item['spuId'];
  90. $model->sku_id = $item['skuId'];
  91. $model->num = $item['goodsQty'];
  92. $model->mall_order_id = $item['mall_order_id'];
  93. $model->order_detail_id = $item['order_detail_id'];
  94. if (!$model->save()) throw new \Exception("创建子订单失败");
  95. }
  96. return true;
  97. }
  98. /**
  99. * 编辑子订单
  100. *
  101. * @param integer $order_id
  102. * @param array $data
  103. * @return boolean
  104. */
  105. public static function editOrderItem(int $order_id, array $data)
  106. {
  107. $order_list = self::find()->where(['order_id' => $order_id])->all();
  108. foreach ($order_list as $item) {
  109. $order_item = ArrayHelper::find($data, function($i) use ($item) {
  110. return in_array($item->sku_id, explode(',', $i['skuIds']));
  111. });
  112. $item->chain_order_sn = $order_item['orderSn'];
  113. $item->chain_batch_order_sn = $order_item['batchOrderSn'];
  114. if (!$item->save()) throw new \Exception("编辑子订单失败");
  115. }
  116. return true;
  117. }
  118. /**
  119. * 获取item by order_detail_id
  120. *
  121. * @param integer $order_detail_id
  122. * @return static|null
  123. */
  124. public static function getOrderItemByDetailId(int $order_detail_id)
  125. {
  126. return self::findOne([
  127. 'order_detail_id' => $order_detail_id,
  128. ]);
  129. }
  130. /**
  131. * 获取子订单详情ID列表
  132. *
  133. * @param integer $order_id
  134. * @param array $sku_ids
  135. * @return array
  136. */
  137. public static function getDetailIdsBySkuIds(int $order_id, array $sku_ids)
  138. {
  139. return self::find()->select('order_detail_id')->where([
  140. 'order_id' => $order_id,
  141. 'sku_id' => $sku_ids,
  142. ])->column();
  143. }
  144. /**
  145. * 获取子订单详情ID列表
  146. *
  147. * @param integer $order_id
  148. * @param array $sku_ids
  149. * @return array
  150. */
  151. public static function getDetailBySkuIds(int $order_id, array $sku_ids)
  152. {
  153. return self::find()->where([
  154. 'order_id' => $order_id,
  155. 'sku_id' => $sku_ids,
  156. ])->all();
  157. }
  158. /**
  159. * @param integer $mall_id
  160. * @param string $order_sn
  161. * @return self|null
  162. */
  163. public static function getOrderByOrderSn(int $mall_id, string $order_sn)
  164. {
  165. return static::find()->where(['mall_id' => $mall_id, 'chain_order_sn' => $order_sn])
  166. ->with(['order'])
  167. ->one();
  168. }
  169. }