quanwei
2 days ago 73b874c72ad55eb9eef21c36160ac0de58f0189e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
 
namespace app\api\model\order;
 
use app\common\model\order\OrderRefund as OrderRefundModel;
use app\common\model\order\Order as OrderModel;
 
/**
 * 售后单模型
 */
class OrderRefund extends OrderRefundModel
{
    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'app_id',
        'update_time'
    ];
 
    /**
     * 追加字段
     * @var array
     */
    protected $append = [
        'state_text',   // 售后单状态文字描述
    ];
 
    /**
     * 售后单状态文字描述
     */
    public function getStateTextAttr($value, $data)
    {
        // 已完成
        if ($data['status'] == 20) {
            $text = [10 => '已同意退货并已退款', 20 => '已同意换货并已收货', '30' => '仅退款并已退款'];
            return $text[$data['type']];
        }
        // 已取消
        if ($data['status'] == 30) {
            return '已取消';
        }
        // 已拒绝
        if ($data['status'] == 10) {
            $text = [10 => '已拒绝退货退款', 20 => '已拒绝换货', 30 => '已拒绝退款'];
            return $text[$data['type']];
        }
        // 进行中
        if ($data['status'] == 0) {
            if ($data['is_agree'] == 0) {
                return '等待审核中';
            }
            if ($data['type'] == 10) {
                return $data['is_user_send'] ? '已发货,待平台确认' : '已同意退货,请及时发货';
            }
            if ($data['type'] == 20) {
                return $data['is_user_send'] ? '已发货,待平台确认' : '已同意换货,请及时发货';
            }
        }
        return $value;
    }
 
    /**
     * 获取用户售后单列表
     */
    public function getList($user_id, $state = -1, $param)
    {
        $model = $this;
        $state > -1 && $model = $model->where('status', '=', $state);
        if (isset($param['shop_supplier_id']) && $param['shop_supplier_id']) {
            $model = $model->where('shop_supplier_id', '=', $param['shop_supplier_id']);
        }
        if (isset($param['type']) && $param['type'] == 1) {
            $model = $model->where('user_id', '=', $user_id);
        }
        $list = $model->with(['order_master.advance', 'orderproduct.image'])
            ->order(['create_time' => 'desc'])
            ->paginate($param);
        foreach ($list as &$item) {
            if ($item['orderMaster']['order_source'] == 70) {
                $item['orderproduct']['total_pay_price'] = round($item['orderproduct']['total_pay_price'] + $item['order_master']['advance']['pay_price'], 2);
            }
        }
        return $list;
    }
 
    /**
     * 用户发货
     */
    public function delivery($data)
    {
        if ($this['is_agree']['value'] != 10 || $this['is_user_send'] != 0) {
            $this->error = '当前售后单不合法,不允许该操作';
            return false;
        }
        if ($data['express_id'] <= 0) {
            $this->error = '请选择物流公司';
            return false;
        }
        if (empty($data['express_no'])) {
            $this->error = '请填写物流单号';
            return false;
        }
        return $this->save([
            'is_user_send' => 1,
            'send_time' => time(),
            'express_id' => (int)$data['express_id'],
            'express_no' => $data['express_no'],
        ]);
    }
 
    /**
     * 新增售后单记录
     */
    public function apply($user, $product, $data)
    {
        $this->startTrans();
        try {
            $shop_supplier_id = (new OrderModel())->where(['order_id' => $product['order_id']])->value('
                shop_supplier_id');
            // 新增售后单记录
            $this->save([
                'order_product_id' => $data['order_product_id'],
                'order_id' => $product['order_id'],
                'user_id' => $user['user_id'],
                'type' => $data['type'],
                'apply_desc' => $data['content'],
                'is_agree' => 0,
                'status' => 0,
                'app_id' => self::$app_id,
                'shop_supplier_id' => $shop_supplier_id
            ]);
            // 记录凭证图片关系
            if (isset($data['images']) && !empty($data['images'])) {
                $this->saveImages($this['order_refund_id'], $data['images']);
            }
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    //新增平台介入
    public function plateapply($order_refund_id)
    {
 
        return $this->where(['order_refund_id' => $order_refund_id])->update(['plate_status' => 10]);
    }
 
    /**
     * 记录售后单图片
     */
    private function saveImages($order_refund_id, $images)
    {
        $images_ids = [];
        foreach (json_decode($images, true) as $val) {
            $images_ids[] = $val['file_id'];
        }
        // 生成评价图片数据
        $data = [];
        foreach ($images_ids as $image_id) {
            $data[] = [
                'order_refund_id' => $order_refund_id,
                'image_id' => $image_id,
                'app_id' => self::$app_id
            ];
        }
        return !empty($data) && (new OrderRefundImage)->saveAll($data);
    }
 
}