guohongjin
2024-05-15 5b7639f0bd9e056738ec15100ed0532e965c6cd5
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
package cn.stylefeng.guns.modular.business.service.impl;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.stylefeng.guns.modular.business.dto.request.MentalAnalysisSpecialTimeConfigEdit2Request;
import cn.stylefeng.guns.modular.business.entity.MentalAnalysisSpecialTimeConfig;
import cn.stylefeng.guns.modular.business.entity.MentalAppointment;
import cn.stylefeng.guns.modular.business.mapper.MentalAnalysisSpecialTimeConfigMapper;
import cn.stylefeng.guns.modular.business.service.IMentalAnalysisSpecialTimeConfigService;
import cn.stylefeng.guns.modular.business.service.IMentalAppointmentService;
import cn.stylefeng.roses.kernel.rule.enums.MentalAppointmentStatusEnum;
import cn.stylefeng.roses.kernel.rule.pojo.response.ErrorResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jetbrains.annotations.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
 
/**
 * <p>
 * 性格分析特殊时间配置 服务实现类
 * </p>
 *
 * @author goupan
 * @since 2024-01-26
 */
@Service
public class MentalAnalysisSpecialTimeConfigServiceImpl extends ServiceImpl<MentalAnalysisSpecialTimeConfigMapper, MentalAnalysisSpecialTimeConfig> implements IMentalAnalysisSpecialTimeConfigService {
 
    @Resource
    private IMentalAppointmentService mentalAppointmentService;
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseData editSpecialTimeConfig(MentalAnalysisSpecialTimeConfigEdit2Request req) {
        List<MentalAnalysisSpecialTimeConfig> batchSpecialTimeConfigList = new ArrayList<>();
 
        // 转日期格式
        Date specialDayDate = DateUtil.parseDate(req.getSpecialDay());
 
        // 判断是设置当天、还是指定时间段
        if (req.getSpecialTimeConfigList() == null || req.getSpecialTimeConfigList().size() == 0) {
            // 获取当天预约(服务中)
            Long inServiceMentalAppointmentCount = mentalAppointmentService.count(
                    Wrappers.<MentalAppointment>lambdaQuery()
                            .eq(MentalAppointment::getWorkerId, req.getCounsellingInfoId())
                            .eq(MentalAppointment::getAppointmentDay, req.getSpecialDay())
                            .eq(MentalAppointment::getStatusFlag, MentalAppointmentStatusEnum.IN_SERVICE.getCode())
            );
            if (inServiceMentalAppointmentCount > 0) {
                return new ErrorResponseData("500", "当天有预约服务中,设置特殊时段失败!");
            }
 
            // 获取当天预约(待服务)
            List<MentalAppointment> todayMentalAppointmentList = mentalAppointmentService.list(
                    Wrappers.<MentalAppointment>lambdaQuery()
                            .eq(MentalAppointment::getWorkerId, req.getCounsellingInfoId())
                            .eq(MentalAppointment::getAppointmentDay, req.getSpecialDay())
                            .eq(MentalAppointment::getStatusFlag, MentalAppointmentStatusEnum.WAIT_SERVICE.getCode())
            );
            // 预约单新分配性格分析师
            ResponseData x = updateMentalAppointmentNewWorkerOperation(todayMentalAppointmentList);
            if (x != null) {
                return x;
            }
 
            // 设置当天
            MentalAnalysisSpecialTimeConfig o = BeanUtil.toBean(req, MentalAnalysisSpecialTimeConfig.class);
            o.setIsCancelDay(1);
            o.setCounsellingInfoId(req.getCounsellingInfoId());
            o.setSpecialDay(specialDayDate);
            batchSpecialTimeConfigList.add(o);
        } else {
            // 特殊时间段,是否未覆盖预约时间
            List<MentalAppointment> todayMentalAppointmentList = mentalAppointmentService.list(
                    Wrappers.<MentalAppointment>lambdaQuery()
                            .eq(MentalAppointment::getWorkerId, req.getCounsellingInfoId())
                            .eq(MentalAppointment::getAppointmentDay, req.getSpecialDay())
                            .in(MentalAppointment::getStatusFlag, Arrays.asList(
                                    MentalAppointmentStatusEnum.WAIT_SERVICE.getCode(),
                                    MentalAppointmentStatusEnum.IN_SERVICE.getCode()
                            ))
                            .and(wand ->
                                    req.getSpecialTimeConfigList().forEach(st -> {
                                        wand.or(wor -> wor.ne(MentalAppointment::getBeginTimePoint, st.getSpecialBeginTimePoint())
                                                .ne(MentalAppointment::getEndTimePoint, st.getSpecialEndTimePoint()));
                                    })
                            )
            );
            // 服务中存在,未覆盖,提示失败
            Boolean inServicePresent = todayMentalAppointmentList.stream().filter(tma -> MentalAppointmentStatusEnum.IN_SERVICE.getCode().equals(tma.getStatusFlag())).findAny().isPresent();
            if (inServicePresent) {
                return new ErrorResponseData("500", "有预约服务中,设置特殊时段失败!");
            }
 
            // 预约单新分配性格分析师
            ResponseData x = updateMentalAppointmentNewWorkerOperation(todayMentalAppointmentList);
            if (x != null) {
                return x;
            }
 
            // 设置时间段特殊时间
            for (MentalAnalysisSpecialTimeConfigEdit2Request.SpecialTimeConfig t : req.getSpecialTimeConfigList()) {
                MentalAnalysisSpecialTimeConfig o = BeanUtil.toBean(t, MentalAnalysisSpecialTimeConfig.class);
                o.setCounsellingInfoId(req.getCounsellingInfoId());
                o.setSpecialDay(specialDayDate);
                batchSpecialTimeConfigList.add(o);
            }
        }
 
        // 清除当天的设置
        this.remove(
                Wrappers.<MentalAnalysisSpecialTimeConfig>lambdaQuery()
                        .eq(MentalAnalysisSpecialTimeConfig::getCounsellingInfoId, req.getCounsellingInfoId())
                        .eq(MentalAnalysisSpecialTimeConfig::getSpecialDay, req.getSpecialDay())
        );
 
        if (CollUtil.isNotEmpty(batchSpecialTimeConfigList)) {
            this.saveOrUpdateBatch(batchSpecialTimeConfigList);
        }
        return new SuccessResponseData();
    }
 
    /**
     * 预约单新分配性格分析师
     * @param todayMentalAppointmentList
     * @return
     */
    @Nullable
    private ResponseData updateMentalAppointmentNewWorkerOperation(List<MentalAppointment> todayMentalAppointmentList) {
        if (CollUtil.isNotEmpty(todayMentalAppointmentList)) {
            // 修改待服务的预约性格分析师
            List<MentalAppointment> waitUpdateMentalAppointmentList = new ArrayList<>();
            for (MentalAppointment o : todayMentalAppointmentList) {
                MentalAppointment newMa = new MentalAppointment();
                newMa.setId(o.getId());
 
                // 预约时间匹配性格分析师
                Long workerId = mentalAppointmentService.assignMentalAppointmentWorkerId(o.getAppointmentDay(), o.getBeginTimePoint(), o.getEndTimePoint());
                if (workerId == null) {
                    return new ErrorResponseData("500", "新分配性格分析师失败!");
                }
 
                // 分配新的性格分析师
                newMa.setWorkerId(workerId);
                waitUpdateMentalAppointmentList.add(newMa);
            }
            if (CollUtil.isNotEmpty(waitUpdateMentalAppointmentList)) {
                mentalAppointmentService.updateBatchById(waitUpdateMentalAppointmentList);
            }
        }
        return null;
    }
 
}