goupan
2024-04-29 727d4605fd6826ee9078553b5c7626b7803629d8
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
package cn.stylefeng.guns.modular.business.service.impl;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.http.HttpUtil;
import cn.stylefeng.guns.modular.business.dto.MentalAppointmentPageDTO;
import cn.stylefeng.guns.modular.business.dto.MentalTestMyMentalAppointmentDTO;
import cn.stylefeng.guns.modular.business.dto.UserMentalAppointmentPageResponseDTO;
import cn.stylefeng.guns.modular.business.entity.MentalAnalysisSpecialTimeConfig;
import cn.stylefeng.guns.modular.business.entity.MentalAnalysisTimeConfig;
import cn.stylefeng.guns.modular.business.entity.MentalAppointment;
import cn.stylefeng.guns.modular.business.entity.OrderConsultOne;
import cn.stylefeng.guns.modular.business.mapper.MentalAppointmentMapper;
import cn.stylefeng.guns.modular.business.service.IMentalAnalysisSpecialTimeConfigService;
import cn.stylefeng.guns.modular.business.service.IMentalAnalysisTimeConfigService;
import cn.stylefeng.guns.modular.business.service.IMentalAppointmentService;
import cn.stylefeng.guns.modular.business.service.IOrderConsultOneService;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.customer.modular.service.CustomerService;
import cn.stylefeng.roses.kernel.rule.enums.*;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 性格分析预约 服务实现类
 * </p>
 *
 * @author goupan
 * @since 2024-01-01
 */
@Slf4j
@Service
public class MentalAppointmentServiceImpl extends ServiceImpl<MentalAppointmentMapper, MentalAppointment> implements IMentalAppointmentService {
 
    @Value("${refund.alipay-url}")
    private String refundAlipayUrl;
    @Value("${refund.wxpay-url}")
    private String refundWxpayUrl;
 
    @Resource
    private CustomerService customerService;
 
    @Resource
    private IMentalAppointmentService mentalAppointmentService;
 
    @Resource
    private IMentalAnalysisTimeConfigService mentalAnalysisTimeConfigService;
 
    @Resource
    private IMentalAnalysisSpecialTimeConfigService mentalAnalysisSpecialTimeConfigService;
 
    @Resource
    private IOrderConsultOneService orderConsultOneService;
 
    @Override
    public List<MentalTestMyMentalAppointmentDTO> myMentalAppointment(Long userId, String consultantName, List<Integer> statusFlagList) {
        return this.baseMapper.myMentalAppointment(userId, consultantName, statusFlagList);
    }
 
    @Override
    public Page<UserMentalAppointmentPageResponseDTO> userMentalAppointmentPage(Page<Object> page, Long userId, String name, String telephone, Integer statusFlag) {
        return this.baseMapper.userMentalAppointmentPage(page, userId, name, telephone, statusFlag);
    }
 
    @Override
    public Page<MentalAppointmentPageDTO> getPage(Page<Object> page, Long counsellingInfoId, String workerNickName, String userNickName, String userTelephone, Integer statusFlag, List<Integer> statusFlagList) {
        return this.baseMapper.mentalAnalysisTimeConfigSchedule(page, null, null, counsellingInfoId, workerNickName, userNickName, userTelephone, statusFlag, statusFlagList);
    }
 
    @Override
    public List<MentalAppointmentPageDTO> mentalAnalysisTimeConfigSchedule(String searchBeginTime, String searchEndTime, Long counsellingInfoId, String workerNickName, String userNickName, String userTelephone, Integer statusFlag, List<Integer> statusFlagList) {
        return this.baseMapper.mentalAnalysisTimeConfigSchedule(searchBeginTime, searchEndTime, counsellingInfoId, workerNickName, userNickName, userTelephone, statusFlag, statusFlagList);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean mentalAppointmentCancel(Long id) {
        // 获取当前登录用户信息
        LoginUser loginUser = LoginContext.me().getLoginUser();
        Long userId = loginUser.getUserId();
 
        // 性格分析预约
        MentalAppointment mentalAppointment = this.getById(id);
        Assert.isTrue(userId.equals(mentalAppointment.getUserId()), "预约用户信息错误");
 
        // 服务中可取消
        if (mentalAppointment != null
                && MentalAppointmentStatusEnum.WAIT_SERVICE.getCode().equals(mentalAppointment.getStatusFlag())) {
            // 1V1咨询订单信息
            OrderConsultOne orderConsultOne = orderConsultOneService.getOne(
                    Wrappers.<OrderConsultOne>lambdaQuery()
                            .eq(OrderConsultOne::getMentalAppointmentId, id)
                            .gt(OrderConsultOne::getPayAmount, 0)
                            .isNotNull(OrderConsultOne::getPayType)
            );
 
            boolean update = orderConsultOneService.update(
                    Wrappers.<OrderConsultOne>lambdaUpdate()
                            .set(OrderConsultOne::getStatusFlag, OrderStatusFlagEnum.CANCEL.getCode())
                            .set(OrderConsultOne::getRefundAmount, orderConsultOne.getPayAmount())
                            .set(OrderConsultOne::getRefundTime, new Date())
                            .set(OrderConsultOne::getCancelTime, new Date())
                            .set(OrderConsultOne::getCancelRemark, "申请取消")
                            .eq(OrderConsultOne::getId, orderConsultOne.getId())
                            .eq(OrderConsultOne::getStatusFlag, OrderStatusFlagEnum.PAY_SUCCESS.getCode())
            );
            if (update) {
                this.update(
                        Wrappers.<MentalAppointment>lambdaUpdate()
                                .set(MentalAppointment::getStatusFlag, MentalAppointmentStatusEnum.CANCEL.getCode())
                                .eq(MentalAppointment::getId, mentalAppointment.getId())
                );
            }
            Assert.isTrue(update, "订单取消失败");
 
            Boolean isRefund = true;
            if (PayTypeEnum.ALIPAY.getCode().toString().equals(orderConsultOne.getPayType())) {
                Map<String, Object> map = new HashMap(2);
                map.put("outTradeNo", orderConsultOne.getOrderNo());
                map.put("tradeNo", orderConsultOne.getTransactionNo());
                String result = HttpUtil.createGet(refundAlipayUrl).form(map).execute().body();
                log.info("1v1咨询订单退款支付宝:{},{},{}", orderConsultOne.getOrderNo(), orderConsultOne.getTransactionNo(), result);
                isRefund = BooleanUtil.toBoolean(result);
            } else if (PayTypeEnum.WXPAY.getCode().toString().equals(orderConsultOne.getPayType())) {
                // TODO: 1v1咨询订单退款微信
                String result = "";
                log.info("1v1咨询订单退款微信:{},{},{}", orderConsultOne.getOrderNo(), orderConsultOne.getTransactionNo(), result);
                isRefund = BooleanUtil.toBoolean(result);
            }
            Assert.isTrue(isRefund, "退款失败");
            return true;
        }
 
        return false;
    }
 
    @Override
    public Long assignMentalAppointmentWorkerId(Date appointmentDay, String beginTimePoint, String endTimePoint) {
        // 预约时间日期
        String appointmentDayYmd = DateUtil.format(appointmentDay, DatePattern.NORM_DATE_PATTERN);
 
        // 获取预约时段有服务的分析师(不可预约)
        List<MentalAppointment> sameTimeAppointmentList = mentalAppointmentService.list(
                Wrappers.<MentalAppointment>lambdaQuery()
                        .isNull(MentalAppointment::getType)
                        .eq(MentalAppointment::getAppointmentDay, appointmentDayYmd)
                        .eq(MentalAppointment::getBeginTimePoint, beginTimePoint)
                        .eq(MentalAppointment::getEndTimePoint, endTimePoint).ne(MentalAppointment::getType,1)
                        .in(MentalAppointment::getStatusFlag, Arrays.asList(
                                MentalAppointmentStatusEnum.WAIT_PAY.getCode(),
                                MentalAppointmentStatusEnum.WAIT_SERVICE.getCode(),
                                MentalAppointmentStatusEnum.IN_SERVICE.getCode()
                        ))
        );
        List<Long> neWorkerIdList = sameTimeAppointmentList.stream().map(MentalAppointment::getWorkerId).collect(Collectors.toList());
 
        // 查询特殊时间段,取消当天的分析师(不可预约)
        List<Long> isCancelDaySpecialTimeConfigList = mentalAnalysisSpecialTimeConfigService.listObjs(
                Wrappers.<MentalAnalysisSpecialTimeConfig>lambdaQuery()
                        .select(MentalAnalysisSpecialTimeConfig::getCounsellingInfoId)
                        .eq(MentalAnalysisSpecialTimeConfig::getSpecialDay, appointmentDayYmd)
                        .eq(MentalAnalysisSpecialTimeConfig::getIsCancelDay, 1)
                , obj -> {
                    Long counsellingInfoId = (Long) obj;
                    return counsellingInfoId;
                }
        );
        neWorkerIdList.addAll(isCancelDaySpecialTimeConfigList);
        neWorkerIdList.stream().distinct();
 
        // 获取当前星期
        Integer weekDay = DateUtil.dayOfWeekEnum(appointmentDay).getIso8601Value();
        // 预约时间段配置匹配的性格分析师
        List<MentalAnalysisTimeConfig> eqWorkerTimeConfigList = mentalAnalysisTimeConfigService.getWorkerListByAppointmentTime(CustomerWorkStatusEnum.ON_WORK.getCode(), CustomerMentalAnalysisStatusEnum.ON_WORK.getCode(), weekDay, appointmentDayYmd, beginTimePoint, endTimePoint);
        // 分析师ID(预约时间段配置匹配)
        List<Long> eqWorkerIdByTimeConfigList = eqWorkerTimeConfigList.isEmpty() ? Collections.singletonList(0L) :
                eqWorkerTimeConfigList.stream()
                        .map(MentalAnalysisTimeConfig::getCounsellingInfoId)
                        .distinct()
                        .collect(Collectors.toList());
 
        // 查询有特殊时间段配置,分析师(可预约)
        List<MentalAnalysisSpecialTimeConfig> specialTimeConfigList = mentalAnalysisSpecialTimeConfigService.list(
                Wrappers.<MentalAnalysisSpecialTimeConfig>lambdaQuery()
                        .in(MentalAnalysisSpecialTimeConfig::getCounsellingInfoId, eqWorkerIdByTimeConfigList)
                        .eq(MentalAnalysisSpecialTimeConfig::getSpecialDay, appointmentDayYmd)
                        .eq(MentalAnalysisSpecialTimeConfig::getIsCancelDay, 0)
        );
 
        // 根据预约时间段、特殊时间段(优先)判断是否符合预约时间
        List<Long> eqWorkerIdList = eqWorkerTimeConfigList.stream()
                .filter(wtc -> {
                    // 特殊时间段(优先),存在判断是否符合时间段,不存在直接过
                    boolean isPresentSpecialTimeConfig = specialTimeConfigList.stream().filter(stc -> stc.getCounsellingInfoId().equals(wtc.getCounsellingInfoId())).findFirst().isPresent();
                    if (isPresentSpecialTimeConfig) {
                        // 如果存在特殊时间段,判断是否有匹配预约时间段的特殊时段设置,未设置(不可预约)
                        return specialTimeConfigList.stream().filter(stc -> stc.getSpecialBeginTimePoint().equals(beginTimePoint)
                                        && stc.getSpecialEndTimePoint().equals(endTimePoint)
                                )
                                .findFirst()
                                .isPresent();
                    } else {
                        return true;
                    }
                })
                .map(MentalAnalysisTimeConfig::getCounsellingInfoId)
                .collect(Collectors.toList());
 
        // 特殊时间段符合的分析师(可预约)
        List<MentalAnalysisSpecialTimeConfig> specialTimePointList = mentalAnalysisSpecialTimeConfigService.list(
                Wrappers.<MentalAnalysisSpecialTimeConfig>lambdaQuery()
                        .select(MentalAnalysisSpecialTimeConfig::getCounsellingInfoId)
                        .eq(MentalAnalysisSpecialTimeConfig::getSpecialDay, appointmentDayYmd)
                        .eq(MentalAnalysisSpecialTimeConfig::getSpecialBeginTimePoint, beginTimePoint)
                        .eq(MentalAnalysisSpecialTimeConfig::getSpecialEndTimePoint, endTimePoint)
                        .eq(MentalAnalysisSpecialTimeConfig::getIsCancelDay, 0)
        );
        // 特殊时间段符合的分析师ID
        List<Long> specialTimePointWorkerIdList = specialTimePointList.stream().map(MentalAnalysisSpecialTimeConfig::getCounsellingInfoId).collect(Collectors.toList());
        // 特殊时间段的补充
        eqWorkerIdList.addAll(specialTimePointWorkerIdList);
 
        if (CollUtil.isEmpty(eqWorkerIdList)) {
            // 没有分配
            eqWorkerIdList = Collections.singletonList(0L);
        } else {
            // 去重
            eqWorkerIdList.stream().distinct();
        }
 
        // 分配性格分析师
        Long consultWorkerId = customerService.randomWorkerIdByLineStatusAndPostNeWorkerId(ImStatusEnum.ON_LINE.getCode(), null, PostIdEnum.PO_31.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode(), CustomerMentalAnalysisStatusEnum.ON_WORK.getCode(), eqWorkerIdList, neWorkerIdList);
        if (consultWorkerId == null) {
            consultWorkerId = customerService.randomWorkerIdByLineStatusAndPostNeWorkerId(null, null, PostIdEnum.PO_31.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode(), CustomerMentalAnalysisStatusEnum.ON_WORK.getCode(), eqWorkerIdList, neWorkerIdList);
        }
 
        return consultWorkerId;
    }
}