guohongjin
2024-04-17 67c5f30d9a0d96746a423fbaf4c843593801a28a
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package cn.stylefeng.rest.modular.order.service;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjUtil;
import cn.stylefeng.guns.modular.business.dto.ImPushDataDTO;
import cn.stylefeng.guns.modular.business.dto.request.CreateOrderConsultOneRequest;
import cn.stylefeng.guns.modular.business.entity.*;
import cn.stylefeng.guns.modular.business.service.*;
import cn.stylefeng.guns.modular.business.service.impl.ImBizService;
import cn.stylefeng.guns.utils.GeneralUtil;
import cn.stylefeng.rest.core.exception.BusinessException;
import cn.stylefeng.roses.kernel.customer.api.pojo.CustomerInfo;
import cn.stylefeng.roses.kernel.customer.modular.entity.Customer;
import cn.stylefeng.roses.kernel.customer.modular.service.CustomerService;
import cn.stylefeng.roses.kernel.rule.enums.*;
import cn.stylefeng.roses.kernel.rule.exception.enums.defaults.DefaultBusinessExceptionEnum;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
 
@Slf4j
@Service
public class MentalTestOrderBizService {
 
    @Resource
    ImBizService imBizService;
 
    @Resource
    IMentalTestTopicService mentalTestTopicService;
 
    @Resource
    IMentalTestRecordService mentalTestRecordService;
 
    @Resource
    IOrderMentalTestService orderMentalTestService;
 
    @Resource
    IOrderConsultOneService orderConsultOneService;
 
    @Resource
    IMentalAppointmentService mentalAppointmentService;
 
    @Resource
    CustomerService customerService;
 
    @Transactional(rollbackFor = Exception.class)
    public Boolean paySuccessOrderConsultOne(String orderNo, String tradeNo, String totalAmount, Integer payType) {
        // 获取订单信息
        OrderConsultOne order = orderConsultOneService.getOrderByNo(orderNo);
        Assert.notNull(order, "订单不存在");
        Assert.isTrue(order.getPayAmount().compareTo(new BigDecimal(totalAmount)) == 0, "订单支付金额错误!");
 
        // 待支付订单修改状态
        if (OrderStatusFlagEnum.WAIT_PAY.getCode().equals(order.getStatusFlag())) {
            orderConsultOneService.update(
                    Wrappers.<OrderConsultOne>lambdaUpdate()
                            .set(OrderConsultOne::getStatusFlag, OrderStatusFlagEnum.PAY_SUCCESS.getCode())
                            .set(OrderConsultOne::getPayType, payType)
                            .set(OrderConsultOne::getTransactionNo, tradeNo)
                            .eq(OrderConsultOne::getOrderNo, orderNo)
            );
 
            boolean update = mentalAppointmentService.update(
                    Wrappers.<MentalAppointment>lambdaUpdate()
                            .set(MentalAppointment::getStatusFlag, MentalAppointmentStatusEnum.WAIT_SERVICE.getCode())
                            .set(MentalAppointment::getConsultStatus, MentalAppointmentConsultStatusEnum.NOT_START.getCode())
                            .eq(MentalAppointment::getId, order.getMentalAppointmentId())
            );
            if (update) {
                // 性格分析预约
                MentalAppointment mentalAppointment = mentalAppointmentService.getById(order.getMentalAppointmentId());
 
                // 推送消息内容
                String pushContent = "你有新的预约,请注意查收。" +
                        "预约用户:" + mentalAppointment.getUserName() +
                        "预约时间:" + DateUtil.format(mentalAppointment.getAppointmentDay(), DatePattern.NORM_DATE_PATTERN) +
                        " " + mentalAppointment.getBeginTimePoint() +
                        " ~ " + mentalAppointment.getEndTimePoint();
                // IM推送数据json
                ImPushDataDTO pushData = ImPushDataDTO.builder()
                        .type(ImPushTypeEnum.S_TO_W_TIP_MENTAL_APPOINTMENT.getCode())
                        .objId(ObjUtil.toString(mentalAppointment.getId()))
                        .title("通知")
                        .content(pushContent)
                        .data1(ObjUtil.toString(mentalAppointment.getUserId()))
                        .data2(ObjUtil.toString(mentalAppointment.getWorkerId()))
                        .build();
                imBizService.messageSendSystem(mentalAppointment.getUserId()+"", new String[]{mentalAppointment.getWorkerId()+""}, pushData, ImUserTypeEnum.WORKER, PostIdEnum.PO_31, true);
            }
 
            return true;
        }
        return false;
    }
 
    @Transactional(rollbackFor = Exception.class)
    public OrderConsultOne createOrderConsultOne(CreateOrderConsultOneRequest req) {
        // 生成了订单但未支付
        OrderConsultOne oo = orderConsultOneService.getOne(
                Wrappers.<OrderConsultOne>lambdaQuery()
                        .eq(OrderConsultOne::getUserId, req.getUserId())
                        .eq(OrderConsultOne::getMentalTestRecordId, req.getMentalTestRecordId())
                        .eq(OrderConsultOne::getStatusFlag, OrderStatusFlagEnum.WAIT_PAY.getCode())
                        .eq(OrderConsultOne::getIsDelete, YesOrNotEnum.N.getCode())
                        .last("LIMIT 1")
        );
        if (oo != null) {
            // 可以提示用户
            return oo;
        }
 
        // 心理测试记录
        MentalTestRecord mentalTestRecord = mentalTestRecordService.getById(req.getMentalTestRecordId());
        Assert.notNull(mentalTestRecord, "心理测试记录不存在");
        // 题库信息
        MentalTestTopic topic = mentalTestTopicService.getById(mentalTestRecord.getTopicId());
        Assert.notNull(topic, "题库不存在");
 
        // 设置下单信息
        OrderConsultOne o = OrderConsultOne.builder()
                .orderNo(GeneralUtil.generateOrderNo(OrderTypeEnum.CONSULT_ONE))
                .userId(req.getUserId())
                //.goodsId(consultWorkerId) // 下单支付后自动匹配1v1性格分析师
                .mentalTestRecordId(req.getMentalTestRecordId())
                .mentalTestOrderNo(mentalTestRecord.getAnswerNo())
                .appointmentDay(DateUtil.parse(req.getAppointmentDay(), DatePattern.NORM_DATE_PATTERN))
                .beginTimePoint(req.getBeginTimePoint())
                .endTimePoint(req.getEndTimePoint())
                .orderAmount(topic.getConsultAmount())
                .payAmount(topic.getConsultAmount())
                .build();
 
        // 有咨询费用需要支付
        boolean isNeedPay = topic.getConsultAmount().compareTo(BigDecimal.ZERO) > 0;
        // 后台创建的订单
        boolean isBack = false;
        if (isNeedPay) {
            // 获取心理测试订单
            OrderMentalTest orderMentalTest = orderMentalTestService.getOrderByNo(mentalTestRecord.getAnswerNo());
            Assert.notNull(orderMentalTest, "心理测试订单不存在");
            isBack = orderMentalTest.getIsBack();
            if (isBack) {
                // 后台创建的订单,不需要支付咨询费
                isNeedPay = false;
            }
        }
 
        // 判断订单是否需要支付
        if (isNeedPay) {
            o.setStatusFlag(OrderStatusFlagEnum.WAIT_PAY.getCode());
        } else {
            o.setStatusFlag(OrderStatusFlagEnum.PAY_SUCCESS.getCode());
            if (!isBack) {
                // 后台添加的需统计
                o.setPayAmount(BigDecimal.ZERO);
                o.setOrderAmount(BigDecimal.ZERO);
            }
        }
 
        // 保存1v1性格分析预约,并推送消息给工作人员
        MentalAppointment mentalAppointment = saveMentalAppointmentPushMessage(o.getStatusFlag(), o);
        if (mentalAppointment == null) {
            throw new BusinessException(DefaultBusinessExceptionEnum.MENTAL_APPOINTMENT_NO_WORKER_ERROR);
        }
        o.setGoodsId(mentalAppointment.getWorkerId());
        o.setMentalAppointmentId(mentalAppointment.getId());
 
        // 更新用户现状
        Customer customer = BeanUtil.toBean(req, Customer.class);
        customer.setCustomerId(req.getUserId());
        // 修改用户信息
        customerService.updateCustomerRemoveCache(customer);
 
        // 保存订单信息
        orderConsultOneService.save(o);
 
        // 更新心理测试结果-1v1咨询性格分析师
        mentalTestRecordService.update(
                Wrappers.<MentalTestRecord>lambdaUpdate()
                        .set(MentalTestRecord::getConsultWorkerId, mentalAppointment.getWorkerId())
                        .set(MentalTestRecord::getOrderConsultOneId, o.getId())
                        .set(MentalTestRecord::getMentalAppointmentId, mentalAppointment.getId())
                        .eq(MentalTestRecord::getId, req.getMentalTestRecordId())
        );
        return o;
    }
 
    /**
     * 保存1v1性格分析预约,并推送消息给工作人员
     * @param o
     * @return
     */
    public MentalAppointment saveMentalAppointmentPushMessage(Integer orderConsultOneStatusFlag, OrderConsultOne o) {
        // 性格分析预约
        MentalAppointment mentalAppointment = MentalAppointment.builder()
                .userId(o.getUserId())
                .appointmentDay(o.getAppointmentDay())
                .beginTimePoint(o.getBeginTimePoint())
                .endTimePoint(o.getEndTimePoint())
                .build();
 
        // 用户信息
        CustomerInfo customerInfo = customerService.getCustomerInfoById(o.getUserId());
        mentalAppointment.setUserName(customerInfo.getRealName());
        mentalAppointment.setPhone(customerInfo.getLinkPhone());
 
        // 分配性格分析师
        Long consultWorkerId = mentalAppointmentService.assignMentalAppointmentWorkerId(o.getAppointmentDay(), o.getBeginTimePoint(), o.getEndTimePoint());
        if (consultWorkerId != null) {
            mentalAppointment.setWorkerId(consultWorkerId);
            if (orderConsultOneStatusFlag != null && OrderStatusFlagEnum.PAY_SUCCESS.getCode().equals(orderConsultOneStatusFlag)) {
                // 已分配(不要支付的)
                mentalAppointment.setStatusFlag(MentalAppointmentStatusEnum.WAIT_SERVICE.getCode());
            } else {
                // 等待1v1咨询单支付后,需支付回调改为已分配
                mentalAppointment.setStatusFlag(MentalAppointmentStatusEnum.WAIT_PAY.getCode());
                mentalAppointment.setConsultStatus(MentalAppointmentConsultStatusEnum.NOT_START.getCode());
            }
 
            // 保存性格分析预约
            mentalAppointmentService.save(mentalAppointment);
 
            if (MentalAppointmentStatusEnum.WAIT_SERVICE.getCode().equals(mentalAppointment.getStatusFlag())) {
                // 推送消息内容
                String pushContent = "你有新的预约,请注意查收。" +
                        "预约用户:" + mentalAppointment.getUserName() +
                        "预约时间:" + DateUtil.format(mentalAppointment.getAppointmentDay(), DatePattern.NORM_DATE_PATTERN) +
                        " " + mentalAppointment.getBeginTimePoint() +
                        " ~ " + mentalAppointment.getEndTimePoint();
                // IM推送数据json
                ImPushDataDTO pushData = ImPushDataDTO.builder()
                        .type(ImPushTypeEnum.S_TO_W_TIP_MENTAL_APPOINTMENT.getCode())
                        .objId(ObjUtil.toString(mentalAppointment.getId()))
                        .title("通知")
                        .content(pushContent)
                        .data1(ObjUtil.toString(mentalAppointment.getUserId()))
                        .data2(ObjUtil.toString(mentalAppointment.getWorkerId()))
                        .build();
                imBizService.messageSendSystem(mentalAppointment.getUserId()+"", new String[]{mentalAppointment.getWorkerId()+""}, pushData, ImUserTypeEnum.WORKER, PostIdEnum.PO_31, true);
            }
        } else {
            return null;
        }
        return mentalAppointment;
    }
 
}