郑永安
2023-06-19 7a6abd05683528032687c75e80e0bd2030a3e46c
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
package com.gkhy.safePlatform.specialWork.service.impl;
 
import cn.hutool.core.util.IdUtil;
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.account.rpc.apimodel.model.resp.DepRPCRespDTO;
import com.gkhy.safePlatform.commons.co.ContextCacheUser;
import com.gkhy.safePlatform.commons.enums.E;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.exception.AusinessException;
import com.gkhy.safePlatform.commons.exception.BusinessException;
import com.gkhy.safePlatform.commons.query.PageQuery;
import com.gkhy.safePlatform.commons.vo.ResultVO;
import com.gkhy.safePlatform.commons.vo.SearchResultVO;
import com.gkhy.safePlatform.specialWork.entity.SpecialWorkAppointmentInfo;
import com.gkhy.safePlatform.specialWork.model.bo.WorkStatisticsBO;
import com.gkhy.safePlatform.specialWork.model.dto.req.DeleteForm;
import com.gkhy.safePlatform.specialWork.model.dto.req.SpecialWorkAppointmentAddReqDTO;
import com.gkhy.safePlatform.specialWork.model.dto.req.SpecialWorkAppointmentModReqDTO;
import com.gkhy.safePlatform.specialWork.model.dto.resp.SpecialWorkAppointmentRespDTO;
import com.gkhy.safePlatform.specialWork.model.dto.resp.WorkStatisticsRespDTO;
import com.gkhy.safePlatform.specialWork.model.query.SpecialWorkAppointmentQuery;
import com.gkhy.safePlatform.specialWork.service.SpecialWorkAppointmentService;
import com.gkhy.safePlatform.specialWork.service.baseService.SpecialWorkAppointmentInfoService;
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 java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
@Service("SpecialWorkAppointmentService")
public class SpecialWorkAppointmentServiceImpl implements SpecialWorkAppointmentService {
 
    @DubboReference(check = false)
    private AccountDepartmentService accountDepartmentService;
    @Autowired
    private SpecialWorkAppointmentInfoService appointmentInfoService;
    /**
     * 新增
     * @param currentUser
     * @param addReqDTO
     * @return
     */
    @Override
    public ResultVO save(ContextCacheUser currentUser, SpecialWorkAppointmentAddReqDTO addReqDTO) {
        //先获取当前申请人所在的部门当天是否已预约
        Long count = appointmentInfoService.getCountByDepAndTime(currentUser.getDepId(), addReqDTO.getAppointmentTime());
        if(count>0){
            throw new AusinessException(E.DATA_DATABASE_EXIST,"该部门下已有当天预约,不可重复预约");
        }
        //获取部门相关信息
        DepInfoRPCRespDTO depInfo = (DepInfoRPCRespDTO)accountDepartmentService.getDepInfoByDepId(currentUser.getDepId()).getData();
        if(null == depInfo){
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT,"该申请部门不存在");
        }
        //复制数据
        SpecialWorkAppointmentInfo specialWorkAppointmentInfo = new SpecialWorkAppointmentInfo();
        BeanUtils.copyProperties(addReqDTO,specialWorkAppointmentInfo);
        //填充
        specialWorkAppointmentInfo.setId(IdUtil.getSnowflake(0,0).nextId());
        specialWorkAppointmentInfo.setApplyDepId(currentUser.getDepId());
        specialWorkAppointmentInfo.setApplyDepName(depInfo.getDepName());
        specialWorkAppointmentInfo.setCreateUname(currentUser.getUsername());
        specialWorkAppointmentInfo.setCreateUid(currentUser.getUid());
        int i = appointmentInfoService.saveOne(specialWorkAppointmentInfo);
        if(i > 0){
           return ResultVO.success();
        }
        return new ResultVO(ResultCodes.SERVER_ADD_ERROR);
    }
 
    /**
     * 修改
     * @param currentUser
     * @param modReqDTO
     * @return
     */
    @Override
    public ResultVO update(ContextCacheUser currentUser, SpecialWorkAppointmentModReqDTO modReqDTO) {
        //复制数据
        SpecialWorkAppointmentInfo specialWorkAppointmentInfo = new SpecialWorkAppointmentInfo();
        BeanUtils.copyProperties(modReqDTO,specialWorkAppointmentInfo);
        //填充
        specialWorkAppointmentInfo.setModifiedUname(currentUser.getUsername());
        specialWorkAppointmentInfo.setModifiedUid(currentUser.getUid());
        int i = appointmentInfoService.updateOne(specialWorkAppointmentInfo);
        if(i > 0){
            return ResultVO.success();
        }
        return new ResultVO(ResultCodes.SERVER_UPDATE_ERROR);
    }
 
    /**
     * 删除
     * @param currentUser
     * @param id
     * @return
     */
    @Override
    public ResultVO deleteOne(ContextCacheUser currentUser, Long id) {
        if(null == id){
            throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
        }
        int i = appointmentInfoService.updateStatus(id);
        if(i > 0){
            return ResultVO.success();
        }
        return new ResultVO(ResultCodes.SERVER_DEL_ERROR);
    }
 
    @Override
    public ResultVO batchDelete(ContextCacheUser currentUser, DeleteForm deleteForm) {
        List<Long> ids = deleteForm.getIds();
        int i = appointmentInfoService.updateStatusByIds(ids);
        if(i > 0){
            return ResultVO.success();
        }
        return new ResultVO(ResultCodes.SERVER_DEL_ERROR);
    }
 
    @Override
    public SearchResultVO<List<SpecialWorkAppointmentRespDTO>> listAll(ContextCacheUser currentUser, PageQuery<SpecialWorkAppointmentQuery> pageQuery) {
        Page<SpecialWorkAppointmentInfo> page = new Page(pageQuery.getPageIndex(),pageQuery.getPageSize());
        List<SpecialWorkAppointmentInfo> specialWorkAppointmentInfos = appointmentInfoService.listByPage(page, pageQuery.getSearchParams());
        List<SpecialWorkAppointmentRespDTO> respDTOList = new ArrayList<>();
        specialWorkAppointmentInfos.forEach(item -> {
            SpecialWorkAppointmentRespDTO respDTO = new SpecialWorkAppointmentRespDTO();
            BeanUtils.copyProperties(item,respDTO);
            respDTOList.add(respDTO);
        });
 
        return new SearchResultVO<>(true,
                page.getCurrent(),
                page.getSize(),
                page.getPages(),
                page.getTotal(),
                respDTOList,
                ResultCodes.OK);
    }
 
    /**
     * 单条查询
     * @param currentUser
     * @param id
     * @return
     */
    @Override
    public ResultVO<SpecialWorkAppointmentRespDTO> queryById(ContextCacheUser currentUser, Long id) {
        if(null == id){
            throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
        }
        SpecialWorkAppointmentInfo specialWorkAppointmentInfo = appointmentInfoService.queryById(id);
        SpecialWorkAppointmentRespDTO respDTO = new SpecialWorkAppointmentRespDTO();
        if(null != specialWorkAppointmentInfo){
            BeanUtils.copyProperties(specialWorkAppointmentInfo,respDTO);
        }
        return new ResultVO<>(ResultCodes.OK,respDTO);
    }
 
    @Override
    public SearchResultVO<List<SpecialWorkAppointmentRespDTO>> listByDep(ContextCacheUser currentUser, PageQuery<SpecialWorkAppointmentQuery> pageQuery) {
 
        Page<SpecialWorkAppointmentInfo> page = new Page(pageQuery.getPageIndex(),pageQuery.getPageSize());
        SpecialWorkAppointmentQuery query = pageQuery.getSearchParams();
        query.setApplyDepId(currentUser.getDepId());
        List<SpecialWorkAppointmentInfo> specialWorkAppointmentInfos = appointmentInfoService.listByPage(page, query);
        List<SpecialWorkAppointmentRespDTO> respDTOList = new ArrayList<>();
        specialWorkAppointmentInfos.forEach(item -> {
            SpecialWorkAppointmentRespDTO respDTO = new SpecialWorkAppointmentRespDTO();
            BeanUtils.copyProperties(item,respDTO);
            respDTOList.add(respDTO);
        });
 
        return new SearchResultVO<>(true,
                page.getCurrent(),
                page.getSize(),
                page.getPages(),
                page.getTotal(),
                respDTOList,
                ResultCodes.OK);
    }
 
    @Override
    public ResultVO<List<WorkStatisticsRespDTO>> statisticsAppointment(SpecialWorkAppointmentQuery query) {
        List<WorkStatisticsRespDTO> respDTOList = new ArrayList<>();
        //获取所有部门
        List<DepRPCRespDTO> depList = getDepList();
        //统计各部门每种作业数量
        List<WorkStatisticsBO> workStatisticsBOList = appointmentInfoService.statisticsAppointment(query);
        //遍历部门
        for(DepRPCRespDTO dep : depList){
            WorkStatisticsRespDTO respDTO = new WorkStatisticsRespDTO();
            respDTO.setApplyDepId(dep.getDepId());
            respDTO.setApplyDepName(dep.getDepName());
            List<WorkStatisticsBO> selectList = workStatisticsBOList
                    .stream()
                    .filter(bo -> bo.getApplyDepId().equals(dep.getDepId()))
                    .collect(Collectors.toList());
           if(selectList.size()>0){
               WorkStatisticsBO workStatisticsBO = selectList.get(0);
               respDTO.setBlindPlatePluggingCount(workStatisticsBO.getBlindPlatePluggingCount());
               respDTO.setConfinedSpaceCount(workStatisticsBO.getConfinedSpaceCount());
               respDTO.setHeightCount(workStatisticsBO.getHeightCount());
               respDTO.setHotCount(workStatisticsBO.getHotCount());
               respDTO.setLiftingCount(workStatisticsBO.getLiftingCount());
               respDTO.setOpenCircuitCout(workStatisticsBO.getOpenCircuitCout());
               respDTO.setGroundBreakingCount(workStatisticsBO.getGroundBreakingCount());
               respDTO.setTemporaryPowerCount(workStatisticsBO.getTemporaryPowerCount());
               respDTO.setTotalCount(workStatisticsBO.getTotalCount());
               respDTOList.add(respDTO);
           }
 
           //子级
            List<DepRPCRespDTO> children = dep.getChildren();
           if(null != children && children.size()>0){
               traverseChildren(children,workStatisticsBOList,respDTOList);
           }
 
        }
        return new ResultVO<>(ResultCodes.OK,respDTOList);
    }
 
    public void traverseChildren(List<DepRPCRespDTO> depRPCRespDTOList,List<WorkStatisticsBO> workStatisticsBOList,List<WorkStatisticsRespDTO> respDTOList){
        for (DepRPCRespDTO depRPCRespDTO : depRPCRespDTOList) {
            WorkStatisticsRespDTO respDTO = new WorkStatisticsRespDTO();
            respDTO.setApplyDepId(depRPCRespDTO.getDepId());
            respDTO.setApplyDepName(depRPCRespDTO.getDepName());
            List<WorkStatisticsBO> selectList = workStatisticsBOList
                    .stream()
                    .filter(bo -> bo.getApplyDepId().equals(depRPCRespDTO.getDepId()))
                    .collect(Collectors.toList());
            if(selectList.size()>0){
                WorkStatisticsBO workStatisticsBO = selectList.get(0);
                respDTO.setBlindPlatePluggingCount(workStatisticsBO.getBlindPlatePluggingCount());
                respDTO.setConfinedSpaceCount(workStatisticsBO.getConfinedSpaceCount());
                respDTO.setHeightCount(workStatisticsBO.getHeightCount());
                respDTO.setHotCount(workStatisticsBO.getHotCount());
                respDTO.setLiftingCount(workStatisticsBO.getLiftingCount());
                respDTO.setOpenCircuitCout(workStatisticsBO.getOpenCircuitCout());
                respDTO.setGroundBreakingCount(workStatisticsBO.getGroundBreakingCount());
                respDTO.setTemporaryPowerCount(workStatisticsBO.getTemporaryPowerCount());
                respDTO.setTotalCount(workStatisticsBO.getTotalCount());
                respDTOList.add(respDTO);
            }
            //子级
            List<DepRPCRespDTO> children = depRPCRespDTO.getChildren();
            if(null != children && children.size()>0){
                traverseChildren(children,workStatisticsBOList,respDTOList);
            }
        }
    }
    public List<DepRPCRespDTO> getDepList(){
        List<DepRPCRespDTO> depList = new ArrayList<>();
        try{
            ResultVO<List<DepRPCRespDTO>> listResultVO = accountDepartmentService.depList();
            if (listResultVO != null && listResultVO.getCode().equals(ResultCodes.OK.getCode())) {
                if (listResultVO.getData() != null) {
                    depList = (List<DepRPCRespDTO>) listResultVO.getData();
                }else{
                    throw new BusinessException(ResultCodes.CLIENT_DEP_NOT_EXIST);
                }
            } else {
                throw new BusinessException(ResultCodes.RPC_ACCESS_EXCEPTION);
            }
 
        }catch (Exception e){
            throw new BusinessException(ResultCodes.RPC_ACCESS_EXCEPTION);
        }
        return depList;
    }
}