quanwei
18 hours ago c441dea81bd86bdfb12dff35821fed51f4cc91c2
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
176
177
178
<?php
 
namespace app\operations\model\plus\release;
 
use app\common\model\plus\release\Order as OrderModel;
use app\common\model\plus\release\SupplyUser as SupplyUserModel;
use app\operations\service\order\ExportService;
 
/**
 * 订单模型
 */
class Order extends OrderModel
{
    // 定义全局的查询范围
    protected $globalScope = ['status'];
    public function scopeStatus($query)
    {
        $shop_supplier_ids = \app\operations\model\supplier\Supplier::getSupplierIdsByUser(session('jjjshop_operations')['user']);
        if (empty($shop_supplier_ids)){
            $query->where($query->getTable() . '.shop_supplier_id', -1);
        }else{
            $query->where($query->getTable() . '.shop_supplier_id', 'in', $shop_supplier_ids);
        }
    }
    /**
     * 获取订单列表
     */
    public function getList($search)
    {
        $model = $this
            ->with(['demanduser','supplyuser',"project"])
            ->order(['create_time' => 'desc']);
 
        if (!empty($search['supply_user_id'])) {
            $model = $model->where('supply_user_id', '=', $search['supply_user_id']);
        }
        if (!empty($search['demand_user_id'])) {
            $model = $model->where('demand_user_id', '=', $search['demand_user_id']);
        }
        if ($search['pay_status'] > -1) {
            $model = $model->where('pay_status', '=', $search['pay_status']);
        }
        if ($search['order_status'] > -1) {
            $model = $model->where('order_status', '=', $search['order_status']);
        }
        if (!empty($search['pay_type'])) {
            $model = $model->where('pay_type', '=', $search['pay_type']);
        }
        //搜索时间段
        if (isset($search['create_time']) && $search['create_time'] != '') {
            $sta_time = array_shift($search['create_time']);
            $end_time = array_pop($search['create_time']);
            $model = $model->whereBetweenTime('create_time', $sta_time, date('Y-m-d 23:59:59', strtotime($end_time)));
        }
   
        // if (!empty($search['nick_name'])) {
        //     $model = $model->where('user.nickName|order.real_name|order.mobile', 'like', '%' . $search['nick_name'] . '%');
        // }
        // 获取列表数据
        return $model->paginate($search);
    }
 
    /**
     * 编辑用户
     * @param $data
     * @return bool
     */
    public function edit($data)
    {
        // 开启事务
        $this->startTrans();
        try {
            $this->save($data);
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    /**
     * 获取微信支付的金额
     */
    public function getTotalPayByDate($day,$user_ids=[],$in='true',$pay_type=10,$shop_supplier_id='')
    {
        if(is_array($day)){
            $start = $day[0];
            $end = $day[1];
        }else{
            $start = strtotime($day);
            $end = strtotime($day) + 86399;
        }
        $model = $this;
        if(!empty($user_ids)){
            if($in){
                $model = $model->where('user_id', 'in', $user_ids);
            }else{
                $model = $model->where('user_id', 'not in', $user_ids);
            }
        }
        $money = $model->where('create_time', 'between', "$start,$end")
            ->where('pay_status', '=', 20)
            ->where('pay_type', '=', $pay_type)
            ->sum("pay_price");
 
        return $money;
    }
 
    /**
     * 确认线下已支付
     */
    public function onCash($data)
    {
        if (
            $this['pay_status'] != 10
        ) {
            $this->error = '该订单不满足线下支付条件';
            return false;
        }
        return $this->transaction(function () use ($data) {
            $save_data=[
                'pay_status'=>20,
                'pay_time'=>time(),
                'pay_type'=>40,
            ];
            return $this->where("id","=",$data["id"])->save($save_data);
        });
    }
 
    /**
     * 确认已完成订单
     */
    public function onFinish($data)
    {
        if (
            $this['pay_status'] != 20
            || $this['order_status'] != 10
        ) {
            $this->error = '该订单不满足完成的条件';
            return false;
        }
        return $this->transaction(function () use ($data) {
            //给供应方发放佣金
            (new SupplyUserModel())->where("user_id","=",$this["supply_user_id"])->inc("money",$this["money"])->update();
 
            $save_data=[
                'order_status'=>30,
                'finish_time'=>time(),
            ];
            return $this->where("id","=",$data["id"])->save($save_data);
        });
    }
 
    /**
     * 取消订单
     */
    public function confirmCancel($user)
    {
        if ($this['order_status'] == 30) {
            $this->error = '已完成不可取消';
            return false;
        }
        //订单不能取消
        if($this['order_status'] != 10){
            $this->error = '该订单取消失败';
            return false;
        }
 
        // 订单取消事件
        return $this->transaction(function () use ($user) {
            // 更新订单状态
            return $this->save(['order_status' => 20]);
        });
    }
 
}