quanwei
2025-11-21 1db9a4130699636cabe7e0c9f7f15d004aadada0
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
<?php
 
namespace app\api\service\order\paysuccess\type;
 
use app\api\model\plus\business\Order as OrderModel;
use app\api\model\user\User as UserModel;
use app\common\enum\order\OrderPayTypeEnum;
use app\common\enum\user\balanceLog\BalanceLogSceneEnum;
use app\common\model\user\BalanceLog as BalanceLogModel;
use app\common\service\BaseService;
use app\common\model\plus\business\Business as BusinessModel;
use app\common\model\plus\business\Setting as SettingModel;
use app\api\model\user\PointsLog as PointsLogModel;
 
/**
 * 名片订单支付成功后的回调
 */
class BusinessCardPaySuccessService extends BaseService
{
    // 订单模型
    public $model;
 
    // 当前用户信息
    private $user;
 
    /**
     * 构造函数
     */
    public function __construct($orderNo)
    {
        // 实例化订单模型
        $this->model = OrderModel::getPayDetail($orderNo);
        // 获取用户信息
        $this->user = UserModel::detail($this->model['user_id']);
    }
 
    /**
     * 订单支付成功业务处理
     */
    public function onPaySuccess($payType, $payData = [])
    {
        if (empty($this->model)) {
            $this->error = '未找到该订单信息';
            return false;
        }
        // 更新付款状态
        return $this->updatePayStatus($payType, $payData);
    }
 
    /**
     * 更新付款状态
     */
    private function updatePayStatus($payType, $payData = [])
    {
        // 验证余额支付时用户余额是否满足
        if ($payType == OrderPayTypeEnum::BALANCE) {
            if ($this->user['balance'] < $this->model['pay_price']) {
                $this->error = '用户余额不足,无法使用余额支付';
                return false;
            }
        }
        // 验证余额支付时用户余额是否满足
        if ($payType == OrderPayTypeEnum::POINTS) {
            if ($this->user['points'] < $this->model['pay_price']) {
                $this->error = '用户联盟币不足,无法使用联盟币支付';
                return false;
            }
        }
        // 事务处理
        $this->model->transaction(function () use ($payType, $payData) {
            // 更新订单状态
            $this->updateOrderInfo($payType, $payData);
            if ($payType != OrderPayTypeEnum::POINTS) {
                // 累积用户总消费金额
                $this->user->setIncPayMoney($this->model['pay_price']);
            }
 
            // 记录订单支付信息
            $this->updatePayInfo($payType);
            // 如果是置顶订单,更新名片置顶状态
            if ($this->model['order_source'] == 20) {
                $this->updateBusinessCardTopStatus();
            }
        });
        return true;
    }
 
    /**
     * 更新订单记录
     */
    private function updateOrderInfo($payType, $payData)
    {
        $order = [
            'pay_type' => $payType,
            'pay_status' => 20,
            'pay_time' => time(),
        ];
        if ($payType == OrderPayTypeEnum::WECHAT) {
            $order['transaction_id'] = $payData['transaction_id'];
        }
        // 更新订单状态
        return $this->model->save($order);
    }
 
    /**
     * 记录订单支付信息
     */
    private function updatePayInfo($payType)
    {
        // 余额支付
        if ($payType == OrderPayTypeEnum::BALANCE && $this->model['pay_price'] > 0) {
            // 更新用户余额
            $this->user->where('user_id', '=', $this->user['user_id'])
                ->dec('balance', $this->model['pay_price'])
                ->update();
            BalanceLogModel::add(BalanceLogSceneEnum::CONSUME, [
                'user_id' => $this->user['user_id'],
                'money' => -$this->model['pay_price'],
            ], ['order_no' => $this->model['order_no']]);
        }elseif ($payType == OrderPayTypeEnum::POINTS && $this->model['pay_price'] > 0) {
            // 更新用户联盟币
            $this->user->where('user_id', '=', $this->user['user_id'])
                ->dec('points', $this->model['pay_price'])
                ->update();
            $describe=$this->model['order_source'] == 20?'用户购买名片置顶服务':'用户购买名片服务';
            PointsLogModel::add([
                'user_id' => $this->user['user_id'],
                'value' => $this->model['pay_price'],
                'describe' =>$describe." 订单号:{$this->model['order_no']}",
                'remark' => '',
            ]);
        }
    }
    
    /**
     * 更新名片置顶状态
     */
    private function updateBusinessCardTopStatus()
    {
        // 获取置顶设置
        $setting = SettingModel::getItem('basic');
        $topDays = isset($setting['top_days']) ? $setting['top_days'] : 7; // 默认7天
        
        // 计算置顶到期时间
        $topTime = time() + ($topDays * 86400);
        
        // 更新名片置顶状态
        $businessCard = BusinessModel::detail($this->model['business_card_id']);
        if ($businessCard) {
            $businessCard->save([
                'is_top' => 1,
                'top_time' => $topTime
            ]);
        }
    }
}