quanwei
2 days ago 04102f7237efefa744090ed7c25f7b5d0807b679
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
<?php
 
namespace app\job\model\plus\assemble;
 
use app\common\enum\order\OrderSourceEnum;
use app\common\library\helper;
use app\common\model\plus\assemble\Bill as BillModel;
use app\common\model\plus\assemble\BillUser as BillUserModel;
use app\common\model\order\Order as OrderModel;
use app\common\service\order\OrderRefundService;
 
/**
 * 参与记录模型
 */
class Bill extends BillModel
{
    /**
     * 获取待关闭订单
     */
    public function getCloseIds($fail_type = 0){
        return $this->alias('bill')
            ->join('assemble_activity activity', 'bill.assemble_activity_id = activity.assemble_activity_id','left')
            ->where('bill.status', '=', 10)
            ->where('activity.fail_type', '=', $fail_type)
            ->whereTime('bill.end_time', '<=', time())
            ->select();
    }
 
    /**
     * 关闭订单
     * @param $billIds
     */
    public function close($billIds){
        // 更新记录
        $this->startTrans();
        try {
            //修改拼团状态
            $this->where('assemble_bill_id', 'in', $billIds)->save(['status' => 30]);
            //修改订单状态,并退款
            $bill_user_model = new BillUserModel();
            $orderList = $bill_user_model->field(['order_id'])
                ->where('assemble_bill_id', 'in', $billIds)
                ->select();
            $orderIds = helper::getArrayColumn($orderList, 'order_id');
            //修改订单状态,拼团状态
            (new OrderModel)->where('order_id', 'in', $orderIds)->save([
                'order_status' => 20,
                'assemble_status' => 30
            ]);
            $this->commit();
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
        }
        // 退款
        $this->orderRefund();
    }
 
    /**
     * 拼团失败订单退款
     */
    private function orderRefund(){
        //查找待退款的拼团订单,每次取100条
        $orderList = (new OrderModel)->where('order_source', '=', OrderSourceEnum::ASSEMBLE)
            ->where('order_status', '=', 20)
            ->where('is_refund', '=', 0)
            ->where('pay_status', '=', 20)
            ->where('pay_source', '<>', '')
            ->limit(100)
            ->select();
        foreach ($orderList as $order){
            try {
                // 执行退款操作
                if((new OrderRefundService)->execute($order)){
                    // 更新订单状态
                    $order->save([
                        'is_refund' => 1
                    ]);
                }
            } catch (\Exception $e) {
                $this->error = '订单ID:' . $order['order_id'] . ' 退款失败,错误信息:' . $e->getMessage();
            }
        }
        return true;
    }
 
    /**
     * 拼团成功订单
     * @param $billIds
     */
    public function success($billIds){
        // 更新记录
        $this->startTrans();
        try {
            //修改拼团状态
            $this->where('assemble_bill_id', 'in', $billIds)->save(['status' => 20]);
            $order_list = (new BillUserModel)
                ->field(['order_id'])
                ->where('assemble_bill_id', 'in', $billIds)
                ->select();
            $orderIds = helper::getArrayColumn($order_list, 'order_id');
            //更新主订单表拼团状态
            (new OrderModel)->where('order_id', 'in', $orderIds)
                ->save([
                    'assemble_status' => 20
                ]);
            $this->commit();
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
        }
        // 退款
        $this->orderRefund();
    }
}