| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- namespace addons\QiJuhe\common\models;
- use common\models\order\OrderDetail;
- use addons\QiJuhe\common\enums\OrderEnum;
- use Exception;
- use Yii;
- use yii\db\ActiveQuery;
- use yii\helpers\Json;
- /**
- * This is the model class for table "{{%addons_qijuhe_order}}".
- *
- * @property int $id
- * @property int $mall_id
- * @property int $supply_id 中台ID
- * @property int $order_id 订单ID
- * @property int $user_id 用户ID
- * @property string $order_no 商城订单号
- * @property string|null $supply_order_sn 聚合订单号
- * @property int $goods_id 商品ID
- * @property int $sku_id 聚合商品SKUID
- * @property string|null $source 来源
- * @property int $status 1未推单 2推单中 3推单成功(支付成功) 4推单失败(支付失败) 5已退单
- * @property int $push_num 推单次数
- * @property string|null $order_data 订单信息
- * @property string|null $error 失败信息
- * @property int|null $created_at
- * @property int|null $updated_at
- * @property int|null $is_refund 是否系统退款 1未退 2已退 只有退单该字段才有用
- * @property int|null $refund_id 售后ID
- * @property float|null $refund_money 退款金额
- * @property int|null $err_id 推单失败的错误ID,重新发起的时候需要
- * @property string|null $order_json
- */
- class OrderModel extends \yii\db\ActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_qijuhe_order}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'supply_id', 'order_id', 'user_id', 'order_no', 'goods_id', 'sku_id'], 'required'],
- [['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'],
- [['order_data','order_json'], 'string'],
- [['refund_money'], 'number'],
- [['order_no', 'supply_order_sn', 'source', 'error'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'supply_id' => 'Supply ID',
- 'order_id' => 'Order ID',
- 'user_id' => 'User ID',
- 'order_no' => 'Order No',
- 'supply_order_sn' => 'Supply Order Sn',
- 'goods_id' => 'Goods ID',
- 'sku_id' => 'Sku ID',
- 'source' => 'Source',
- 'status' => 'Status',
- 'push_num' => 'Push Num',
- 'order_data' => 'Order Data',
- 'error' => 'Error',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- 'is_refund' => 'Is Refund',
- 'refund_id' => 'Refund ID',
- 'refund_money' => 'Refund Money',
- 'err_id' => 'Err ID',
- 'order_json' => 'order json',
- ];
- }
-
- /**
- * 关联系统订单表
- *
- * @return ActiveQuery
- */
- public function getMallOrder()
- {
- return $this->hasOne(MallOrderModel::class, ['id' => 'order_id']);
- }
- /**
- * 关联系统订单详情表
- *
- * @return ActiveQuery
- */
- public function getMallOrderDetail()
- {
- return $this->hasOne(OrderDetail::class, ['order_id' => 'order_id']);
- }
- /**
- * 商城订单ID获取订单信息
- *
- * @param integer $order_id
- * @return static
- */
- public static function getOrderByOrderId(int $order_id, string $field = '*')
- {
- return static::find()->where(['order_id' => $order_id])->select($field)->one();
- }
- /**
- * 订单推送失败
- *
- * @return OrderModel
- */
- public function pushFail($msg)
- {
- $this->updated_at = time();
- $this->status = OrderEnum::PUSH_FAIL;
- $this->error = $msg;
- return $this->save();
- }
-
- /**
- * 推送成功
- *
- * @param string $supply_order_sn
- * @return OrderModel
- */
- public function payOk($supply_order_sn = '', $order_data = [])
- {
- $this->updated_at = time();
- $this->supply_order_sn = $supply_order_sn;
- !empty($order_data) && $this->order_data = Json::encode($order_data);
- $this->status = OrderEnum::PUSH_OK;
- $this->error = '';
- return $this->save();
- }
- /**
- * 退单
- *
- * @return OrderModel
- */
- public function refund()
- {
- $this->updated_at = time();
- $this->status = OrderEnum::PUSH_REFUND;
- return $this->save();
- }
- /**
- * 获取供应链子订单ID
- *
- * @return int
- */
- public function getOrderItemId()
- {
- $order_data = json_decode($this->order_data, true);
- if (empty($order_data)) {
- throw new Exception('订单未推送, 无法售后');
- }
- if(empty($order_data['Orders'])){
- throw new Exception('订单数据异常');
- }
- $order_items_id = 0;
- foreach($order_data['Orders'] as $order_items){
- $order_item = $order_items['order_items'];
- foreach($order_item as $item){
- if($item['sku_id'] == $this->sku_id){
- $order_items_id = $item['id'];
- }
- }
- }
- // 是否拥有ItemId
- if (empty($order_items_id)) {
- throw new Exception('子订单ID不存在');
- }
- return $order_items_id;
- }
- }
|