quanwei
2025-10-31 f226d5fe6327e31bb471a96b7370cf94689c6608
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
<?php
 
namespace app\job\model\order;
 
use app\common\model\order\OrderAdvance as OrderAdvanceModel;
use app\common\service\product\factory\ProductFactory;
use app\common\model\order\Order as OrderModel;
use app\common\library\helper;
use app\common\service\order\OrderRefundService;
 
/**
 * 预售定金订单模型
 */
class OrderAdvance extends OrderAdvanceModel
{
    /**
     * 获取订单列表
     */
    public function getCloseList($with = [])
    {
        return $this->with($with)
            ->where('pay_status', '=', 10)
            ->where('order_status', '=', 10)
            ->where('pay_end_time', '<', time())
            ->where('pay_end_time', '>', 0)
            ->select();
    }
 
    /**
     * 获取退定金订单列表
     */
    public function getReturnList()
    {
        return $this->where('pay_status', '=', 20)
            ->where('order_status', '=', 20)
            ->where('is_refund', '=', 0)
            ->where('money_return', '=', 1)
            ->select();
    }
 
    /**
     * 未支付订单自动关闭
     */
    public function close()
    {
        // 查询截止时间未支付的订单
        $list = $this->getCloseList(['order.product', 'advance']);
        $closeOrderAdvanceIds = helper::getArrayColumn($list, 'order_advance_id');
        $this->startTrans();
        try {
            // 取消订单事件
            if (!empty($closeOrderAdvanceIds)) {
                $closeOrderIds = helper::getArrayColumn($list, 'order_id');
                foreach ($list as &$order) {
                    // 回退商品库存
                    ProductFactory::getFactory($order['order']['order_source'])->backProductStock($order['order']['product'], false);
                }
                // 批量更新订单状态为已取消
                (new OrderModel)->onBatchUpdate($closeOrderIds, ['order_status' => 20]);
                $this->where('order_advance_id', 'in', $closeOrderAdvanceIds)->update(['order_status' => 20]);
            }
            $this->commit();
            return $closeOrderAdvanceIds;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
        }
 
    }
 
    /**
     * 已取消订单退定金
     */
    public function return()
    {
        // 查询截止时间未支付的订单
        $list = $this->getReturnList();
        $closeOrderAdvanceIds = helper::getArrayColumn($list, 'order_advance_id');
        // 取消订单事件
        if (!empty($closeOrderAdvanceIds)) {
            foreach ($list as &$item) {
                try {
                    if ((new OrderRefundService)->execute($item)) {
                        // 更新订单状态
                        $item->save([
                            'is_refund' => 1,
                            'refund_money' => $item['pay_price'],
                        ]);
                    }
                } catch (\Exception $e) {
                    $this->error = '订单ID:' . $item['order_advance_id'] . ' 退款失败,错误信息:' . $e->getMessage();
                }
            }
        }
        return $closeOrderAdvanceIds;
    }
 
}