mitao
2024-12-19 d4f8d4b7b624628fbcaf3459546aaa55334a9922
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
package com.sinata.system.service.impl;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinata.common.entity.PageDTO;
import com.sinata.system.enums.BoxProcessEnum;
import com.sinata.system.enums.BoxStatusEnum;
import com.sinata.common.exception.ServiceException;
import com.sinata.common.utils.BeanUtils;
import com.sinata.common.utils.CollUtils;
import com.sinata.common.utils.StringUtils;
import com.sinata.system.domain.MwBox;
import com.sinata.system.domain.dto.MwBoxDTO;
import com.sinata.system.domain.query.MwBoxPageQuery;
import com.sinata.system.domain.vo.BoxStatisticsVO;
import com.sinata.system.domain.vo.MwBoxVO;
import com.sinata.system.mapper.MwBoxMapper;
import com.sinata.system.service.MwBoxService;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
 
/**
 * <p>
 * 转运箱 服务实现类
 * </p>
 *
 * @author mitao
 * @since 2024-12-02
 */
@Service
public class MwBoxServiceImpl extends ServiceImpl<MwBoxMapper, MwBox> implements MwBoxService {
    /**
     * 转运箱数据统计
     *
     * @return
     */
    @Override
    public BoxStatisticsVO getBoxStatistics() {
        BoxStatisticsVO boxStatisticsVO = new BoxStatisticsVO();
        List<MwBox> boxList = this.list();
        if (CollUtils.isNotEmpty(boxList)) {
            boxStatisticsVO.setTotal(boxList.size());
            boxStatisticsVO.setBrokenCount(boxList.stream().filter(box -> BoxStatusEnum.BROKEN.getCode().equals(box.getStatus())).count());
            boxStatisticsVO.setLostCount(boxList.stream().filter(box -> BoxStatusEnum.LOST.getCode().equals(box.getStatus())).count());
            boxStatisticsVO.setDisposeCount(boxList.stream().filter(box -> BoxProcessEnum.DISPOSAL.getCode().equals(box.getStatus())).count());
            boxStatisticsVO.setHospitalUsingCount(boxList.stream().filter(box -> BoxProcessEnum.HOSPITAL_USING.getCode().equals(box.getStatus())).count());
            boxStatisticsVO.setTransitingCount(boxList.stream().filter(box -> BoxProcessEnum.TRANSITING.getCode().equals(box.getStatus())).count());
        }
        return boxStatisticsVO;
    }
 
    /**
     * 转运箱分页列表
     *
     * @param query
     * @return
     */
    @Override
    public PageDTO<MwBoxVO> pageList(MwBoxPageQuery query) {
        Page<MwBox> page = this.lambdaQuery()
                .between(StringUtils.isNotBlank(query.getBoxNumberStart())
                                && StringUtils.isNotBlank(query.getBoxNumberEnd()),
                        MwBox::getBoxNumber, query.getBoxNumberStart(), query.getBoxNumberEnd())
                .eq(Objects.nonNull(query.getStatus()), MwBox::getStatus, query.getStatus())
                .eq(Objects.nonNull(query.getLink()), MwBox::getLink, query.getLink())
                .between(Objects.nonNull(query.getLastUseTimeStart())
                                && Objects.nonNull(query.getLastUseTimeEnd()),
                        MwBox::getLastUseTime, query.getLastUseTimeStart(), query.getLastUseTimeEnd())
                .orderByDesc(MwBox::getBoxNumber)
                .page(new Page<>(query.getPageCurr(), query.getPageSize()));
        return PageDTO.of(page, MwBoxVO.class);
    }
 
    /**
     * 新增转运箱
     *
     * @param boxNumberStart
     * @param boxNumberEnd
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(String boxNumberStart, String boxNumberEnd) {
        String regx = "\\d+";
        if (boxNumberStart.length() != 11 || boxNumberEnd.length() != 11 || !boxNumberStart.matches(regx) || !boxNumberEnd.matches(regx)) {
            throw new ServiceException("请输入有效的11位数字编号!");
        }
        BigDecimal start = new BigDecimal(boxNumberStart);
        BigDecimal end = new BigDecimal(boxNumberEnd);
        if (start.compareTo(end) > 0) {
            throw new ServiceException("开始编号不能大于结束编号");
        }
        List<MwBox> boxList = new ArrayList<>();
        for (BigDecimal i = start; i.compareTo(end) <= 0; i = i.add(BigDecimal.ONE)) {
            MwBox mwBox = new MwBox();
            mwBox.setBoxNumber(i.toString());
            mwBox.setStatus(BoxStatusEnum.NORMAL.getCode());
            boxList.add(mwBox);
        }
        try {
            this.saveBatch(boxList);
        } catch (DuplicateKeyException e) {
            throw new ServiceException("转运箱编号重复");
        }
    }
 
    /**
     * 编辑转运箱状态
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editBatch(List<MwBoxDTO> dtoList) {
        List<MwBox> mwBoxes = BeanUtils.copyToList(dtoList, MwBox.class);
        this.updateBatchById(mwBoxes);
    }
}