fix
songhuangfeng123
2022-09-02 d59ce37bc27d01da323b79c10200e93796865fee
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
package com.gkhy.safePlatform.incidentManage.service.impl;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.query.PageQuery;
import com.gkhy.safePlatform.commons.utils.BeanCopyUtils;
import com.gkhy.safePlatform.commons.utils.StringUtils;
import com.gkhy.safePlatform.commons.vo.ResultVO;
import com.gkhy.safePlatform.commons.vo.SearchResultVO;
import com.gkhy.safePlatform.incidentManage.entity.*;
import com.gkhy.safePlatform.incidentManage.enums.AccidentResultCodes;
import com.gkhy.safePlatform.incidentManage.exception.AccidentException;
import com.gkhy.safePlatform.incidentManage.model.dto.req.AccidentCaseReqDTO;
import com.gkhy.safePlatform.incidentManage.model.dto.resp.AccidentCaseDetailRespDTO;
import com.gkhy.safePlatform.incidentManage.model.dto.resp.AccidentCasePageRespDTO;
import com.gkhy.safePlatform.incidentManage.query.AccidentCaseQuery;
import com.gkhy.safePlatform.incidentManage.query.db.AccidentCaseDBQuery;
import com.gkhy.safePlatform.incidentManage.service.AccidentCaseService;
import com.gkhy.safePlatform.incidentManage.service.baseService.AccidentCaseInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.*;
 
@Service("accidentCaseService")
public class AccidentCaseServiceImpl implements AccidentCaseService {
 
    @Autowired
    private AccidentCaseInfoService accidentCaseInfoService;
 
 
    @Override
    public SearchResultVO<List<AccidentCasePageRespDTO>> selectAccidentCaseList(PageQuery<AccidentCaseQuery> query) {
        Long pageIndex = query.getPageIndex();
        Long pageSize = query.getPageSize();
        Page<AccidentCaseInfoPageDO> page = new Page<>(pageIndex, pageSize);
 
        AccidentCaseDBQuery accidentCaseDBQuery = new AccidentCaseDBQuery();
        if (query.getSearchParams() != null) {
            BeanUtils.copyProperties(query.getSearchParams(), accidentCaseDBQuery);
        }
 
        List<AccidentCaseInfoPageDO> accidentCaseInfoPageDOList = accidentCaseInfoService.selectAccidentCaseList(page, accidentCaseDBQuery);
        List<AccidentCasePageRespDTO> respList = BeanCopyUtils.copyBeanList(accidentCaseInfoPageDOList, AccidentCasePageRespDTO.class);
 
        return new SearchResultVO<>(
                true,
                pageIndex,
                pageSize,
                page.getPages(),
                page.getTotal(),
                respList,
                ResultCodes.OK
        );
    }
 
    @Override
    public ResultVO addAccidentCase(Long uid, AccidentCaseReqDTO accidentCaseReqDTO) {
        //必填项验证
        checkRequired(accidentCaseReqDTO);
 
        Date nowDate = new Date();
        //1.新增事故案例
        AccidentCaseInfo accidentCaseInfo = new AccidentCaseInfo();
        BeanUtils.copyProperties(accidentCaseReqDTO, accidentCaseInfo);
        accidentCaseInfo.setDelFlag(false);
        accidentCaseInfo.setCreateUid(uid);
        accidentCaseInfo.setGmtCreate(nowDate);
        accidentCaseInfoService.addAccidentCase(accidentCaseInfo);
        return new ResultVO(ResultCodes.OK);
    }
 
    @Override
    public ResultVO<AccidentCaseDetailRespDTO> getAccidentCaseById(Long id) {
        AccidentCaseDetailRespDTO AccidentCaseDetailRespDTO = new AccidentCaseDetailRespDTO();
        //查询是否存在
        AccidentCaseInfoDetailDO AccidentCaseInfoDetailDO = accidentCaseInfoService.selectAccidentCaseById(id);
        if (AccidentCaseInfoDetailDO == null) {
            throw new AccidentException(AccidentResultCodes.CASE_NOT_EXIST);
        } else {
            BeanUtils.copyProperties(AccidentCaseInfoDetailDO, AccidentCaseDetailRespDTO);
 
            return new ResultVO<>(ResultCodes.OK, AccidentCaseDetailRespDTO);
        }
    }
 
    @Override
    public ResultVO updateAccidentCase(Long uid, AccidentCaseReqDTO accidentCaseReqDTO) {
        Date nowDate = new Date();
        //查询是否存在
        AccidentCaseInfoDetailDO AccidentCaseInfoDetailDO = accidentCaseInfoService.selectAccidentCaseById(accidentCaseReqDTO.getId());
        if (AccidentCaseInfoDetailDO == null) {
            throw new AccidentException(AccidentResultCodes.CASE_NOT_EXIST);
        } else {
            AccidentCaseInfo accidentCaseInfo = new AccidentCaseInfo();
            BeanUtils.copyProperties(accidentCaseReqDTO, accidentCaseInfo);
            accidentCaseInfo.setUpdateUid(uid);
            accidentCaseInfo.setGmtModitify(nowDate);
            accidentCaseInfoService.updateAccidentCase(accidentCaseInfo);
 
            return new ResultVO(ResultCodes.OK);
        }
    }
 
    @Override
    public ResultVO batchDeleteAccidentCase(Long[] ids) {
        if (ids == null ||  ids.length==0){
            throw new AccidentException(AccidentResultCodes.CASE_NULL);
        } else {
            for (Long id : ids){
                deleteAccidentCase(id);
            }
            return new ResultVO(ResultCodes.OK);
        }
    }
 
    private void deleteAccidentCase(Long id) {
        //查询是否存在
        AccidentCaseInfoDetailDO AccidentCaseInfoDetailDO = accidentCaseInfoService.selectAccidentCaseById(id);
        if (AccidentCaseInfoDetailDO == null) {
            throw new AccidentException(AccidentResultCodes.CASE_NOT_EXIST);
        } else {
            accidentCaseInfoService.deleteAccidentCaseById(id);
        }
    }
 
    /**
     * 验证必填项
     *
     * @return
     */
    private void checkRequired(AccidentCaseReqDTO AccidentCaseReqDTO) {
        // 事故案例标题
        if (AccidentCaseReqDTO.getCaseTitle() == null) {
            throw new AccidentException(AccidentResultCodes.CASE_TITLE_NULL);
        }
        // 事故案例内容
        if (StringUtils.isBlank(AccidentCaseReqDTO.getCaseContent())) {
            throw new AccidentException(AccidentResultCodes.CASE_CONTENT_NULL);
        }
    }
 
}