quanwei
2025-12-10 898043fc97d2ab8b793fd317a049b874ed207c6d
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
179
180
181
182
183
184
185
186
187
188
189
190
<?php
 
namespace app\api\model\user;
 
use app\common\enum\order\OrderTypeEnum;
use app\common\model\user\CardRecord as CardRecordModel;
use app\common\enum\order\OrderPayTypeEnum;
use app\api\service\order\PaymentService;
use app\api\service\order\paysuccess\type\CardPaySuccessService;
use app\common\service\qrcode\CardService;
 
/**
 * 会员卡记录模型
 */
class CardRecord extends CardRecordModel
{
    /**
     * 获取我的会员卡
     */
    public function getList($data, $user)
    {
        $model = $this;
        $list = $model->alias('r')
            ->with(['card.cardtype', 'user'])
            ->where('c.is_delete', '=', 0)
            ->where('c.status', '=', 0)
            ->where('r.is_delete', '=', 0)
            ->where('r.pay_status', '=', 20)
            ->where('r.user_id', '=', $user['user_id'])
            ->join('user_card c', 'c.card_id=r.card_id')
            ->where(function ($query) {
                $query->where('expire_time', '=', 0)->whereOr('expire_time', '>', time());
            })
            ->field('r.*')
            ->order(['r.create_time' => 'desc'])
            ->paginate($data);
        return $list;
    }
 
    /**
     * 获取我的会员卡详情
     */
    public static function getDetail($order_id)
    {
        $detail = self::detail($order_id);
        return $detail;
    }
 
    /**
     * 获取我的会员卡
     */
    public static function getCardDetail($user_id)
    {
        $model = new static;
        $detail = $model->with(['card'])
            ->where('is_delete', '=', 0)
            ->where('pay_status', '=', 20)
            ->where('user_id', '=', $user_id)
            ->where(function ($query) {
                $query->where('expire_time', '=', 0)->whereOr('expire_time', '>', time());
            })
            ->find();
        return $detail ? $detail : '';
    }
 
    /**
     * 创建订单
     */
    public function createOrder($user, $param)
    {
        $cardId = self::getCardDetail($user['user_id']);
        if ($cardId) {
            $this->error = '已存在会员卡,不允许购买';
            return false;
        }
        // 获取订单数据
        $detail = (new Card())::detail($param['card_id']);
        //生成记录
        $record = [
            'user_id' => $user['user_id'],
            'card_id' => $param['card_id'],
            'order_no' => $this->orderNo(),
            'expire_time' => $detail['expire'] ? (time() + $detail['expire'] * 86400 * 30) : 0,
            'money' => $detail['money'],
            'discount' => $detail['is_discount'] ? $detail['discount'] : 0,
            'open_points' => $detail['open_points'],
            'open_points_num' => $detail['open_points_num'],
            'open_coupon' => $detail['open_coupon'],
            'open_coupons' => $detail['open_coupons'],
            'open_money' => $detail['open_money'],
            'open_money_num' => $detail['open_money_num'],
            'pay_type' => $param['pay_type'],
            'pay_time' => time(),
            'app_id' => self::$app_id,
        ];
        $status = $this->save($record);
        if ($detail['money'] == 0) {
            $param['pay_type'] = OrderPayTypeEnum::BALANCE;
        }
        // 余额支付标记订单已支付
        if ($status && $param['pay_type'] == OrderPayTypeEnum::BALANCE) {
            if ($user['balance'] < $detail['money']) {
                $this->error = '用户余额不足,无法使用余额支付';
                return false;
            }
            $this->onPaymentByBalance($record['order_no']);
        }
        return $this['order_id'];
    }
 
    /**
     * 余额支付标记订单已支付
     */
    public function onPaymentByBalance($orderNo)
    {
        // 获取订单详情
        $PaySuccess = new CardPaySuccessService($orderNo);
        // 发起余额支付
        return $PaySuccess->onPaySuccess(OrderPayTypeEnum::BALANCE);
        return $status;
    }
 
    /**
     * 待支付订单详情
     */
    public static function getPayDetail($orderNo)
    {
        $model = new static();
        return $model->where(['order_no' => $orderNo, 'pay_status' => 10])->with(['user'])->find();
    }
 
    /**
     * 订单详情
     */
    public static function getUserOrderDetail($order_id, $user_id)
    {
        $model = new static();
        $order = $model->where(['order_id' => $order_id, 'user_id' => $user_id])->find();
        if (empty($order)) {
            throw new BaseException(['msg' => '订单不存在']);
        }
        return $order;
    }
 
    /**
     * 构建支付请求的参数
     */
    public static function onOrderPayment($user, $order, $payType, $pay_source)
    {
        //如果来源是h5,首次不处理,payH5再处理
        if ($pay_source == 'h5') {
            return [];
        }
        if ($payType == OrderPayTypeEnum::WECHAT) {
            return self::onPaymentByWechat($user, $order, $pay_source);
        }
        return [];
    }
 
    /**
     * 构建微信支付请求
     */
    protected static function onPaymentByWechat($user, $order, $pay_source)
    {
        return PaymentService::wechat(
            $user,
            $order,
            OrderTypeEnum::CARD,
            $pay_source
        );
    }
 
    /**
     * 获取会员卡最小折扣
     */
    public function getDiscount($user_id)
    {
        $discount = $this->alias('r')
            ->where('r.is_delete', '=', 0)
            ->where('r.pay_status', '=', 20)
            ->where('r.user_id', '=', $user_id)
            ->where('r.discount', '>', 0)
            ->where(function ($query) {
                $query->where('expire_time', '=', 0)->whereOr('expire_time', '>', time());
            })
            ->order('r.discount asc')
            ->value('r.discount');
        return $discount ? round($discount / 10, 2) : 0;
    }
}