zf
2024-03-25 cd02923a7ce1ffa004b3abbb7af67ab6173dd99d
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package com.gkhy.safePlatform.incidentManage.service.impl;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gkhy.safePlatform.account.rpc.apimodel.AccountDepartmentService;
import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.DepInfoRPCRespDTO;
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.enums.WorkInjuryTypeEnum;
import com.gkhy.safePlatform.incidentManage.exception.AccidentException;
import com.gkhy.safePlatform.incidentManage.model.dto.req.WorkInjuryDeclarationFileReqDTO;
import com.gkhy.safePlatform.incidentManage.model.dto.req.WorkInjuryDeclarationReqDTO;
import com.gkhy.safePlatform.incidentManage.model.dto.resp.WorkInjuryDeclarationDetailRespDTO;
import com.gkhy.safePlatform.incidentManage.model.dto.resp.WorkInjuryDeclarationFileRespDTO;
import com.gkhy.safePlatform.incidentManage.model.dto.resp.WorkInjuryDeclarationPageRespDTO;
import com.gkhy.safePlatform.incidentManage.query.WorkInjuryDeclarationQuery;
import com.gkhy.safePlatform.incidentManage.query.db.WorkInjuryDeclarationDBQuery;
import com.gkhy.safePlatform.incidentManage.service.WorkInjuryDeclarationService;
import com.gkhy.safePlatform.incidentManage.service.baseService.AccidentExpressInfoService;
import com.gkhy.safePlatform.incidentManage.service.baseService.WorkInjuryDeclarationFileInfoService;
import com.gkhy.safePlatform.incidentManage.service.baseService.WorkInjuryDeclarationInfoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.*;
import java.util.stream.Collectors;
 
@Service("workInjuryDeclarationService")
public class WorkInjuryDeclarationServiceImpl implements WorkInjuryDeclarationService {
 
    @Autowired
    private WorkInjuryDeclarationInfoService workInjuryDeclarationInfoService;
 
    @Autowired
    private WorkInjuryDeclarationFileInfoService workInjuryDeclarationFileInfoService;
 
    @DubboReference(check = false)
    private AccountDepartmentService accountDepartmentService;
 
    @Autowired
    private AccidentExpressInfoService accidentExpressInfoService;
 
 
    @Override
    public SearchResultVO<List<WorkInjuryDeclarationPageRespDTO>> selectWorkInjuryDeclarationList(PageQuery<WorkInjuryDeclarationQuery> query) {
        Long pageIndex = query.getPageIndex();
        Long pageSize = query.getPageSize();
        Page<WorkInjuryDeclarationInfoPageDO> page = new Page<>(pageIndex, pageSize);
 
        WorkInjuryDeclarationDBQuery WorkInjuryDeclarationDBQuery = new WorkInjuryDeclarationDBQuery();
        if (query.getSearchParams() != null) {
            BeanUtils.copyProperties(query.getSearchParams(), WorkInjuryDeclarationDBQuery);
        }
 
 
        List<WorkInjuryDeclarationInfoPageDO> WorkInjuryDeclarationInfoPageDOList = workInjuryDeclarationInfoService.selectWorkInjuryDeclarationList(page, WorkInjuryDeclarationDBQuery);
        List<WorkInjuryDeclarationPageRespDTO> respList = BeanCopyUtils.copyBeanList(WorkInjuryDeclarationInfoPageDOList, WorkInjuryDeclarationPageRespDTO.class);
 
 
        Map<Long, String> depPool = new HashMap<>();
 
        respList.forEach(WorkInjuryDeclarationPageRespDTO -> {
            // 设置部门名称
            if (!depPool.containsKey(WorkInjuryDeclarationPageRespDTO.getDeclareDepartmentId())) {
                ResultVO<DepInfoRPCRespDTO> rpcResult = accountDepartmentService.getDepInfoByDepId(WorkInjuryDeclarationPageRespDTO.getDeclareDepartmentId());
                if (rpcResult != null && rpcResult.getCode().equals(ResultCodes.OK.getCode())) {
                    if (rpcResult.getData() != null) {
                        DepInfoRPCRespDTO dep = (DepInfoRPCRespDTO) rpcResult.getData();
 
                        depPool.put(dep.getDepId(), dep.getDepName());
                    }
                }
            }
 
            String depName = depPool.get(WorkInjuryDeclarationPageRespDTO.getDeclareDepartmentId());
            WorkInjuryDeclarationPageRespDTO.setDeptName(depName);
        });
 
        return new SearchResultVO<>(
                true,
                pageIndex,
                pageSize, page.getPages(),
                page.getTotal(),
                respList,
                ResultCodes.OK
        );
    }
 
    @Override
    public ResultVO addWorkInjuryDeclaration(Long uid, WorkInjuryDeclarationReqDTO WorkInjuryDeclarationReqDTO) {
        //必填项验证
        checkRequired(WorkInjuryDeclarationReqDTO);
 
        Date nowDate = new Date();
        //1.新增工伤申报
        WorkInjuryDeclarationInfo WorkInjuryDeclarationInfo = new WorkInjuryDeclarationInfo();
        BeanUtils.copyProperties(WorkInjuryDeclarationReqDTO, WorkInjuryDeclarationInfo);
        WorkInjuryDeclarationInfo.setDelFlag(false);
        WorkInjuryDeclarationInfo.setCreateUid(uid);
        WorkInjuryDeclarationInfo.setGmtCreate(nowDate);
        workInjuryDeclarationInfoService.addWorkInjuryDeclaration(WorkInjuryDeclarationInfo);
        //2.新增工伤申报附件
        if (!CollectionUtils.isEmpty(WorkInjuryDeclarationReqDTO.getFileList())) {
            addWorkInjuryDeclarationFile(WorkInjuryDeclarationInfo.getId(), uid, nowDate, WorkInjuryDeclarationReqDTO.getFileList());
        }
        return new ResultVO(ResultCodes.OK);
    }
 
    private void addWorkInjuryDeclarationFile(Long WorkInjuryDeclarationId, Long uid, Date nowDate, List<WorkInjuryDeclarationFileReqDTO> WorkInjuryDeclarationFileReqDTOList) {
        List<WorkInjuryDeclarationFileInfo> fileInfoList = BeanCopyUtils.copyBeanList(WorkInjuryDeclarationFileReqDTOList, WorkInjuryDeclarationFileInfo.class);
        fileInfoList.forEach(WorkInjuryDeclarationFileInfo -> {
            WorkInjuryDeclarationFileInfo.setWorkInjuryDeclarationId(WorkInjuryDeclarationId);
            WorkInjuryDeclarationFileInfo.setDelFlag(false);
            WorkInjuryDeclarationFileInfo.setCreateUid(uid);
            WorkInjuryDeclarationFileInfo.setGmtCreate(nowDate);
        });
        for (WorkInjuryDeclarationFileInfo WorkInjuryDeclarationFileInfo : fileInfoList) {
            workInjuryDeclarationFileInfoService.addWorkInjuryDeclarationFile(WorkInjuryDeclarationFileInfo);
        }
    }
 
    @Override
    public ResultVO<WorkInjuryDeclarationDetailRespDTO> getWorkInjuryDeclarationById(Long id) {
        WorkInjuryDeclarationDetailRespDTO WorkInjuryDeclarationDetailRespDTO = new WorkInjuryDeclarationDetailRespDTO();
        //查询是否存在
        WorkInjuryDeclarationInfoDetailDO WorkInjuryDeclarationInfoDetailDO = workInjuryDeclarationInfoService.selectWorkInjuryDeclarationById(id);
        if (WorkInjuryDeclarationInfoDetailDO == null) {
            throw new AccidentException(AccidentResultCodes.WORK_INJURY_DECLARATION_NOT_EXIST);
        } else {
            BeanUtils.copyProperties(WorkInjuryDeclarationInfoDetailDO, WorkInjuryDeclarationDetailRespDTO);
            //查找对应的附件
            List<WorkInjuryDeclarationFileInfoDO> WorkInjuryDeclarationFileInfoDOList = workInjuryDeclarationFileInfoService.selectByWorkInjuryDeclarationId(id);
            if (!CollectionUtils.isEmpty(WorkInjuryDeclarationFileInfoDOList)) {
                List<WorkInjuryDeclarationFileRespDTO> WorkInjuryDeclarationFileRespDTOList = BeanCopyUtils.copyBeanList(WorkInjuryDeclarationFileInfoDOList, WorkInjuryDeclarationFileRespDTO.class);
                WorkInjuryDeclarationDetailRespDTO.setFileList(WorkInjuryDeclarationFileRespDTOList);
            }
            return new ResultVO<>(ResultCodes.OK, WorkInjuryDeclarationDetailRespDTO);
        }
    }
 
    @Override
    public ResultVO updateWorkInjuryDeclaration(Long uid, WorkInjuryDeclarationReqDTO WorkInjuryDeclarationReqDTO) {
        Date nowDate = new Date();
        //查询是否存在
        WorkInjuryDeclarationInfoDetailDO WorkInjuryDeclarationInfoDetailDO = workInjuryDeclarationInfoService.selectWorkInjuryDeclarationById(WorkInjuryDeclarationReqDTO.getId());
        if (WorkInjuryDeclarationInfoDetailDO == null) {
            throw new AccidentException(AccidentResultCodes.WORK_INJURY_DECLARATION_NOT_EXIST);
        } else {
            WorkInjuryDeclarationInfo WorkInjuryDeclarationInfo = new WorkInjuryDeclarationInfo();
            BeanUtils.copyProperties(WorkInjuryDeclarationReqDTO, WorkInjuryDeclarationInfo);
            WorkInjuryDeclarationInfo.setUpdateUid(uid);
            WorkInjuryDeclarationInfo.setGmtModitify(nowDate);
            workInjuryDeclarationInfoService.updateWorkInjuryDeclaration(WorkInjuryDeclarationInfo);
            //修改工伤申报附件
            updateWorkInjuryDeclarationFile(uid, WorkInjuryDeclarationReqDTO.getId(), nowDate, WorkInjuryDeclarationReqDTO.getFileList());
            return new ResultVO(ResultCodes.OK);
        }
    }
 
    private void updateWorkInjuryDeclarationFile(Long uid, Long WorkInjuryDeclarationId, Date nowDate, List<WorkInjuryDeclarationFileReqDTO> WorkInjuryDeclarationFileReqDTOList) {
 
        List<WorkInjuryDeclarationFileInfoDO> WorkInjuryDeclarationFileInfoDOList = workInjuryDeclarationFileInfoService.selectByWorkInjuryDeclarationId(WorkInjuryDeclarationId);
        List<Long> oldIdsList = WorkInjuryDeclarationFileInfoDOList.stream().map(WorkInjuryDeclarationFileInfoDO::getId).collect(Collectors.toList());
        List<Long> newIdsList = new ArrayList<>();
 
        //新增的附件集合
        List<WorkInjuryDeclarationFileInfo> addList = new ArrayList<>();
        //删除的附件集合(id)
        List<Long> deleteList = new ArrayList<>();
        for (WorkInjuryDeclarationFileReqDTO WorkInjuryDeclarationFileReqDTO : WorkInjuryDeclarationFileReqDTOList) {
            //如果不存在id则表示页面新增的附件
            if (WorkInjuryDeclarationFileReqDTO.getId() == null) {
                WorkInjuryDeclarationFileInfo WorkInjuryDeclarationFileInfo = new WorkInjuryDeclarationFileInfo();
                BeanUtils.copyProperties(WorkInjuryDeclarationFileReqDTO, WorkInjuryDeclarationFileInfo);
                WorkInjuryDeclarationFileInfo.setDelFlag(false);
                WorkInjuryDeclarationFileInfo.setGmtCreate(nowDate);
                WorkInjuryDeclarationFileInfo.setCreateUid(uid);
                WorkInjuryDeclarationFileInfo.setWorkInjuryDeclarationId(WorkInjuryDeclarationId);
                addList.add(WorkInjuryDeclarationFileInfo);
            }
            //如果存在id则判断页面是否删除
            else {
                newIdsList.add(WorkInjuryDeclarationFileReqDTO.getId());
            }
        }
        for (Long oldId : oldIdsList) {
            if (!newIdsList.contains(oldId)) {
                deleteList.add(oldId);
            }
        }
        if (!CollectionUtils.isEmpty(addList)) {
            for (WorkInjuryDeclarationFileInfo WorkInjuryDeclarationFileInfo : addList) {
                workInjuryDeclarationFileInfoService.addWorkInjuryDeclarationFile(WorkInjuryDeclarationFileInfo);
            }
        }
        if (!CollectionUtils.isEmpty(deleteList)) {
            workInjuryDeclarationFileInfoService.deleteWorkInjuryDeclarationFileByIds(deleteList);
        }
    }
 
 
    @Override
    public ResultVO batchDeleteWorkInjuryDeclaration(Long[] ids) {
 
        if (ids == null || ids.length == 0) {
            throw new AccidentException(AccidentResultCodes.WORK_INJURY_DECLARATION_NULL);
        } else {
            for (Long id : ids) {
                deleteWorkInjuryDeclaration(id);
            }
            return new ResultVO(ResultCodes.OK);
        }
    }
 
    private void deleteWorkInjuryDeclaration(Long id) {
        //查询是否存在
        WorkInjuryDeclarationInfoDetailDO WorkInjuryDeclarationInfoDetailDO = workInjuryDeclarationInfoService.selectWorkInjuryDeclarationById(id);
        if (WorkInjuryDeclarationInfoDetailDO == null) {
            throw new AccidentException(AccidentResultCodes.WORK_INJURY_DECLARATION_NOT_EXIST);
        } else {
            workInjuryDeclarationInfoService.deleteWorkInjuryDeclarationById(id);
            //删除附件
            workInjuryDeclarationFileInfoService.deleteWorkInjuryDeclarationFileByWorkInjuryDeclarationId(id);
        }
    }
 
 
    /**
     * 验证必填项
     *
     * @return
     */
    private void checkRequired(WorkInjuryDeclarationReqDTO workInjuryDeclarationReqDTO) {
        //申报人姓名
        if (StringUtils.isBlank(workInjuryDeclarationReqDTO.getDeclareUserName())) {
            throw new AccidentException(AccidentResultCodes.WORK_INJURY_DECLARATION_USERNAME_NULL);
        }
        //事故性别
        if (workInjuryDeclarationReqDTO.getDeclareUserGender() == null) {
            throw new AccidentException(AccidentResultCodes.WORK_INJURY_DECLARATION_GENDER_NULL);
        }
        //申报人部门
        if (workInjuryDeclarationReqDTO.getDeclareDepartmentId() == null) {
            throw new AccidentException(AccidentResultCodes.WORK_INJURY_DECLARATION_USERNAME_NOT_EXIST);
        }
        //事故名称
        if (workInjuryDeclarationReqDTO.getAccidentExpressId() == null) {
            throw new AccidentException(AccidentResultCodes.ACCIDENT_EXPRESS_NULL);
        }
        AccidentExpressInfoDetailDO accidentExpressInfo = accidentExpressInfoService.selectAccidentExpressById(workInjuryDeclarationReqDTO.getAccidentExpressId());
        if (accidentExpressInfo == null) {
            throw new AccidentException(AccidentResultCodes.ACCIDENT_EXPRESS_NOT_EXIST);
        }
        //工伤类型
        if (workInjuryDeclarationReqDTO.getWorkInjuryType() == null) {
            throw new AccidentException(AccidentResultCodes.WORK_INJURY_DECLARATION_TYPE_NULL);
        }
        //申报日期
        if (workInjuryDeclarationReqDTO.getDeclareDate() == null) {
            throw new AccidentException(AccidentResultCodes.WORK_INJURY_DECLARATION_DATA_NULL);
        }
    }
 
    /**
     * 枚举验证
     */
    private void checkEnum(WorkInjuryDeclarationReqDTO workInjuryDeclarationReqDTO) {
        if (WorkInjuryTypeEnum.getByCode(workInjuryDeclarationReqDTO.getWorkInjuryType()) == null) {
            throw new AccidentException(AccidentResultCodes.WORK_INJURY_DECLARATION_ENUMS_TYPE_NOT_EXIST);
        }
    }
}