OrderModel.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace addons\QiJuhe\common\models;
  3. use common\models\order\OrderDetail;
  4. use addons\QiJuhe\common\enums\OrderEnum;
  5. use Exception;
  6. use Yii;
  7. use yii\db\ActiveQuery;
  8. use yii\helpers\Json;
  9. /**
  10. * This is the model class for table "{{%addons_qijuhe_order}}".
  11. *
  12. * @property int $id
  13. * @property int $mall_id
  14. * @property int $supply_id 中台ID
  15. * @property int $order_id 订单ID
  16. * @property int $user_id 用户ID
  17. * @property string $order_no 商城订单号
  18. * @property string|null $supply_order_sn 聚合订单号
  19. * @property int $goods_id 商品ID
  20. * @property int $sku_id 聚合商品SKUID
  21. * @property string|null $source 来源
  22. * @property int $status 1未推单 2推单中 3推单成功(支付成功) 4推单失败(支付失败) 5已退单
  23. * @property int $push_num 推单次数
  24. * @property string|null $order_data 订单信息
  25. * @property string|null $error 失败信息
  26. * @property int|null $created_at
  27. * @property int|null $updated_at
  28. * @property int|null $is_refund 是否系统退款 1未退 2已退 只有退单该字段才有用
  29. * @property int|null $refund_id 售后ID
  30. * @property float|null $refund_money 退款金额
  31. * @property int|null $err_id 推单失败的错误ID,重新发起的时候需要
  32. * @property string|null $order_json
  33. */
  34. class OrderModel extends \yii\db\ActiveRecord
  35. {
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public static function tableName()
  40. {
  41. return '{{%addons_qijuhe_order}}';
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function rules()
  47. {
  48. return [
  49. [['mall_id', 'supply_id', 'order_id', 'user_id', 'order_no', 'goods_id', 'sku_id'], 'required'],
  50. [['mall_id', 'supply_id', 'order_id', 'user_id', 'goods_id', 'sku_id', 'status', 'push_num', 'created_at', 'updated_at', 'is_refund', 'refund_id', 'err_id','order_detail_id'], 'integer'],
  51. [['order_data','order_json'], 'string'],
  52. [['refund_money'], 'number'],
  53. [['order_no', 'supply_order_sn', 'source', 'error'], 'string', 'max' => 255],
  54. ];
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function attributeLabels()
  60. {
  61. return [
  62. 'id' => 'ID',
  63. 'mall_id' => 'Mall ID',
  64. 'supply_id' => 'Supply ID',
  65. 'order_id' => 'Order ID',
  66. 'user_id' => 'User ID',
  67. 'order_no' => 'Order No',
  68. 'supply_order_sn' => 'Supply Order Sn',
  69. 'goods_id' => 'Goods ID',
  70. 'sku_id' => 'Sku ID',
  71. 'source' => 'Source',
  72. 'status' => 'Status',
  73. 'push_num' => 'Push Num',
  74. 'order_data' => 'Order Data',
  75. 'error' => 'Error',
  76. 'created_at' => 'Created At',
  77. 'updated_at' => 'Updated At',
  78. 'is_refund' => 'Is Refund',
  79. 'refund_id' => 'Refund ID',
  80. 'refund_money' => 'Refund Money',
  81. 'err_id' => 'Err ID',
  82. 'order_json' => 'order json',
  83. ];
  84. }
  85. /**
  86. * 关联系统订单表
  87. *
  88. * @return ActiveQuery
  89. */
  90. public function getMallOrder()
  91. {
  92. return $this->hasOne(MallOrderModel::class, ['id' => 'order_id']);
  93. }
  94. /**
  95. * 关联系统订单详情表
  96. *
  97. * @return ActiveQuery
  98. */
  99. public function getMallOrderDetail()
  100. {
  101. return $this->hasOne(OrderDetail::class, ['order_id' => 'order_id']);
  102. }
  103. /**
  104. * 商城订单ID获取订单信息
  105. *
  106. * @param integer $order_id
  107. * @return static
  108. */
  109. public static function getOrderByOrderId(int $order_id, string $field = '*')
  110. {
  111. return static::find()->where(['order_id' => $order_id])->select($field)->one();
  112. }
  113. /**
  114. * 订单推送失败
  115. *
  116. * @return OrderModel
  117. */
  118. public function pushFail($msg)
  119. {
  120. $this->updated_at = time();
  121. $this->status = OrderEnum::PUSH_FAIL;
  122. $this->error = $msg;
  123. return $this->save();
  124. }
  125. /**
  126. * 推送成功
  127. *
  128. * @param string $supply_order_sn
  129. * @return OrderModel
  130. */
  131. public function payOk($supply_order_sn = '', $order_data = [])
  132. {
  133. $this->updated_at = time();
  134. $this->supply_order_sn = $supply_order_sn;
  135. !empty($order_data) && $this->order_data = Json::encode($order_data);
  136. $this->status = OrderEnum::PUSH_OK;
  137. $this->error = '';
  138. return $this->save();
  139. }
  140. /**
  141. * 退单
  142. *
  143. * @return OrderModel
  144. */
  145. public function refund()
  146. {
  147. $this->updated_at = time();
  148. $this->status = OrderEnum::PUSH_REFUND;
  149. return $this->save();
  150. }
  151. /**
  152. * 获取供应链子订单ID
  153. *
  154. * @return int
  155. */
  156. public function getOrderItemId()
  157. {
  158. $order_data = json_decode($this->order_data, true);
  159. if (empty($order_data)) {
  160. throw new Exception('订单未推送, 无法售后');
  161. }
  162. if(empty($order_data['Orders'])){
  163. throw new Exception('订单数据异常');
  164. }
  165. $order_items_id = 0;
  166. foreach($order_data['Orders'] as $order_items){
  167. $order_item = $order_items['order_items'];
  168. foreach($order_item as $item){
  169. if($item['sku_id'] == $this->sku_id){
  170. $order_items_id = $item['id'];
  171. }
  172. }
  173. }
  174. // 是否拥有ItemId
  175. if (empty($order_items_id)) {
  176. throw new Exception('子订单ID不存在');
  177. }
  178. return $order_items_id;
  179. }
  180. }