eight
2024-11-19 d5ed847bb09ca9245e7ea47524269ed46cefa7d5
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
package cn.lihu.jh.module.ecg.service.queuesequence;
 
import cn.lihu.jh.module.ecg.Utils;
import cn.lihu.jh.module.ecg.dal.dataobject.checktype.CheckTypeDO;
import cn.lihu.jh.module.ecg.dal.mysql.checktype.CheckTypeMapper;
import cn.lihu.jh.module.infra.api.config.ConfigApi;
import cn.lihu.jh.module.system.api.dict.DictDataApi;
import cn.lihu.jh.module.system.api.dict.dto.DictDataRespDTO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;
 
import java.time.LocalTime;
import java.util.*;
import cn.lihu.jh.module.ecg.controller.admin.queuesequence.vo.*;
import cn.lihu.jh.module.ecg.dal.dataobject.queuesequence.QueueSequenceDO;
import cn.lihu.jh.framework.common.pojo.PageResult;
import cn.lihu.jh.framework.common.pojo.PageParam;
import cn.lihu.jh.framework.common.util.object.BeanUtils;
 
import cn.lihu.jh.module.ecg.dal.mysql.queuesequence.QueueSequenceMapper;
 
import javax.annotation.Resource;
 
import static cn.lihu.jh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.lihu.jh.module.ecg.Constants.*;
import static cn.lihu.jh.module.ecg.enums.ErrorCodeConstants.*;
 
/**
 * 当天序号 Service 实现类
 *
 * @author 金华医院
 */
@Service
@Validated
public class QueueSequenceServiceImpl implements QueueSequenceService {
 
    @Resource
    private ConfigApi configApi;
 
    @Resource
    private DictDataApi dictDataApi;
 
    @Resource
    private QueueSequenceMapper queueSequenceMapper;
 
    @Resource
    private CheckTypeMapper checkTypeMapper;
 
    @Override
    public void initQueueSequenceTable() {
        queueSequenceMapper.clearQueueSequenceTableNotCurrent();
        Integer rowCount = queueSequenceMapper.getQueueSequenceTableRowCount();
        if (null == rowCount || 0 == rowCount) {
            resetQueueSequence();
        }
    }
 
    @Override
    public Integer createQueueSequence(QueueSequenceSaveReqVO createReqVO) {
        // 插入
        QueueSequenceDO queueSequence = BeanUtils.toBean(createReqVO, QueueSequenceDO.class);
        queueSequenceMapper.insert(queueSequence);
        // 返回
        return queueSequence.getId();
    }
 
    @Override
    public void updateQueueSequence(QueueSequenceSaveReqVO updateReqVO) {
        // 校验存在
        validateQueueSequenceExists(updateReqVO.getId());
        // 更新
        QueueSequenceDO updateObj = BeanUtils.toBean(updateReqVO, QueueSequenceDO.class);
        queueSequenceMapper.updateById(updateObj);
    }
 
    @Override
    public void deleteQueueSequence(Integer id) {
        // 校验存在
        validateQueueSequenceExists(id);
        // 删除
        queueSequenceMapper.deleteById(id);
    }
 
    private void validateQueueSequenceExists(Integer id) {
        if (queueSequenceMapper.selectById(id) == null) {
            throw exception(QUEUE_SEQUENCE_NOT_EXISTS);
        }
    }
 
    @Override
    public QueueSequenceDO getQueueSequence(Integer id) {
        return queueSequenceMapper.selectById(id);
    }
 
    @Override
    public List<QueueSequenceDO> selectGivenCheckTypeTimeslot(Integer checkType) {
        return queueSequenceMapper.selectTimeslotByCheckType( checkType );
    }
 
    @Override
    public PageResult<QueueSequenceDO> getQueueSequencePage(QueueSequencePageReqVO pageReqVO) {
        return queueSequenceMapper.selectPage(pageReqVO);
    }
 
    @Override
    public void resetQueueSequence() {
        queueSequenceMapper.clearQueueSequenceTable();
 
        // 读取时段配置
        String strBookTimeslotLength = configApi.getConfigValueByKey(BOOK_TIMESLOT_LENGTH);
 
        // 方案1:配置中取    7:30,8:30,9:30,10:30,11:30,12:30,13:30,14:30,15:30
        //String strBookTimeslotList = configApi.getConfigValueByKey(BOOK_TIMESLOT_LIST);
        //List<LocalTime> timeslotList = Utils.parseTimeSlotList(strBookTimeslotList, Integer.valueOf(strBookTimeslotLength));
 
        List<DictDataRespDTO> dictBookTimeslotList = dictDataApi.getDictDataList(ECG_BOOK_TIMESLOT);
 
        // 读取所有的检查类型
        List<CheckTypeDO> checkTypeDOList =  checkTypeMapper.simpleCheckTypeList();
        checkTypeDOList.forEach( checkTypeDO -> {
 
            for (int i=0; i < dictBookTimeslotList.size(); i++) {
                QueueSequenceDO queueSequenceDO = new QueueSequenceDO();
                queueSequenceDO.setCheckType( checkTypeDO.getValue() );
                queueSequenceDO.setTimeSlot( Integer.valueOf(dictBookTimeslotList.get(i).getValue()) );
                queueSequenceDO.setQueueNo( i * checkTypeDO.getTimeslotBookNum() + checkTypeDO.getTimeslotReservedNum());
                queueSequenceDO.setQueueVipNo( i * checkTypeDO.getTimeslotBookNum());
                queueSequenceDO.setQueueFull( i * checkTypeDO.getTimeslotBookNum() + checkTypeDO.getTimeslotBookNum());
                queueSequenceDO.setQueueVipFull( i * checkTypeDO.getTimeslotBookNum() + checkTypeDO.getTimeslotReservedNum());
                queueSequenceMapper.insert( queueSequenceDO );
            };
        });
    }
 
    @Override
    @Transactional
    public Integer distributeSeqNo(Integer checkType, Integer timeslot) {
        Integer curSeqNo = queueSequenceMapper.selectQueueNoForUpdate(checkType, timeslot);
        if (null == curSeqNo) {
            throw exception(QUEUE_SEQUENCE_TIMESLOT_FULL);
        }
 
        Integer updateRowNum = queueSequenceMapper.updateGivenCheckTypeTimeslotSeqNo(checkType, timeslot, curSeqNo);
        if (null == updateRowNum || updateRowNum == 0) {
            throw exception(QUEUE_SEQUENCE_TIMESLOT_EXCEPTION);
        }
 
        return curSeqNo + 1;
    }
 
    @Override
    @Transactional
    public Integer distributeVipSeqNo(Integer checkType, Integer timeslot) {
        Integer curVipSeqNo = queueSequenceMapper.selectQueueVipNoForUpdate(checkType, timeslot);
        if (null == curVipSeqNo) {
            throw exception(QUEUE_SEQUENCE_TIMESLOT_FULL);
        }
 
        Integer updateRowNum = queueSequenceMapper.updateGivenCheckTypeTimeslotVipSeqNo(checkType, timeslot, curVipSeqNo);
        if (null == updateRowNum || updateRowNum == 0) {
            throw exception(QUEUE_SEQUENCE_TIMESLOT_EXCEPTION);
        }
 
        return curVipSeqNo + 1;
    }
 
}