“linengliang”
2023-12-13 ae33e68baf21878ce145d75ac377d14c1e2b2a82
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package org.jeecg.modules.eam.controller;
 
import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.ImportExcelUtil;
import org.jeecg.modules.eam.entity.*;
import org.jeecg.modules.eam.service.*;
import org.jeecg.modules.eam.vo.OperationCertificateImportVo;
import org.jeecg.modules.system.entity.SysDict;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDictItemService;
import org.jeecg.modules.system.service.ISysDictService;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.system.service.SysIdentityService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.vo.LoginUser;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.eam.vo.OperationCertificatePage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
 
 /**
 * @Description: 操作证书管理
 * @Author: jeecg-boot
 * @Date:   2023-10-17
 * @Version: V1.0
 */
@Api(tags="操作证书管理")
@RestController
@RequestMapping("/eam/operationCertificate")
@Slf4j
public class OperationCertificateController {
    @Autowired
    private IOperationCertificateService operationCertificateService;
    @Autowired
    private IOperationCertificateDetailService operationCertificateDetailService;
 
     @Autowired
     private IdentityService identityService;
     @Autowired
     private ISysUserService userService;
     @Autowired
     private IEamEquipmentService equipmentService;
     @Autowired
     private IProfessionService professionService;
     @Autowired
     private ISysDictService sysDictService;
     @Autowired
     private ISysDictItemService sysDictItemService;
    
    /**
     * 分页列表查询
     *
     * @param operationCertificate
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "操作证书管理-分页列表查询")
    @ApiOperation(value="操作证书管理-分页列表查询", notes="操作证书管理-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<OperationCertificate>> queryPageList(OperationCertificate operationCertificate,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<OperationCertificate> queryWrapper = QueryGenerator.initQueryWrapper(operationCertificate, req.getParameterMap());
        Page<OperationCertificate> page = new Page<OperationCertificate>(pageNo, pageSize);
        IPage<OperationCertificate> pageList = operationCertificateService.page(page, queryWrapper);
        return Result.OK(pageList);
    }
    
    /**
     *   添加
     *
     * @param operationCertificatePage
     * @return
     */
    @AutoLog(value = "操作证书管理-添加")
    @ApiOperation(value="操作证书管理-添加", notes="操作证书管理-添加")
    //@RequiresPermissions("org.jeecg.modules.mdc:mom_eam_operation_certificate:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody OperationCertificatePage operationCertificatePage) {
        OperationCertificate operationCertificate = new OperationCertificate();
        BeanUtils.copyProperties(operationCertificatePage, operationCertificate);
        operationCertificateService.saveMain(operationCertificate, operationCertificatePage.getOperationCertificateDetailList());
        return Result.OK("添加成功!");
    }
 
     @PostMapping(value = "/addOperationCertificate")
     public Result<String> addOperationCertificate(@RequestBody List<OperationCertificateApplyDetail> operationCertificateList) {
        for(OperationCertificateApplyDetail operationCertificateApplyDetail : operationCertificateList){
            if("pass".equals(operationCertificateApplyDetail.getExaminationConclusion())){
                Date currentDate = new Date();
                Calendar c = Calendar.getInstance();
                c.setTime(currentDate);
                c.add(Calendar.YEAR, 1);
                currentDate = c.getTime();
                OperationCertificate operationCertificate = new OperationCertificate();
                operationCertificate.setNum(identityService.getNumByTypeAndLength("OperationCertificate", 4));
                SysUser sysUser = userService.getById(operationCertificateApplyDetail.getUserId());
                if(sysUser != null){
                    operationCertificate.setUserId(sysUser.getUsername());
                }
                operationCertificate.setEquipmentIds(operationCertificateApplyDetail.getEquipmentIds());
                operationCertificate.setCurrentCycleScore(12);
                operationCertificate.setIssueDate(new Date());
                operationCertificate.setStartTime(new Date());
                operationCertificate.setEndTime(currentDate);
                operationCertificate.setStatus("1");
                operationCertificateService.save(operationCertificate);
            }
        }
         return Result.OK("添加成功!");
     }
    
    /**
     *  编辑
     *
     * @param operationCertificatePage
     * @return
     */
    @AutoLog(value = "操作证书管理-编辑")
    @ApiOperation(value="操作证书管理-编辑", notes="操作证书管理-编辑")
    //@RequiresPermissions("org.jeecg.modules.mdc:mom_eam_operation_certificate:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
    public Result<String> edit(@RequestBody OperationCertificatePage operationCertificatePage) {
        OperationCertificate operationCertificate = new OperationCertificate();
        BeanUtils.copyProperties(operationCertificatePage, operationCertificate);
        OperationCertificate operationCertificateEntity = operationCertificateService.getById(operationCertificate.getId());
        if(operationCertificateEntity==null) {
            return Result.error("未找到对应数据");
        }
        operationCertificateService.updateMain(operationCertificate, operationCertificatePage.getOperationCertificateDetailList());
        return Result.OK("编辑成功!");
    }
    
    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "操作证书管理-通过id删除")
    @ApiOperation(value="操作证书管理-通过id删除", notes="操作证书管理-通过id删除")
    //@RequiresPermissions("org.jeecg.modules.mdc:mom_eam_operation_certificate:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name="id",required=true) String id) {
        operationCertificateService.delMain(id);
        return Result.OK("删除成功!");
    }
    
    /**
     *  批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "操作证书管理-批量删除")
    @ApiOperation(value="操作证书管理-批量删除", notes="操作证书管理-批量删除")
    //@RequiresPermissions("org.jeecg.modules.mdc:mom_eam_operation_certificate:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.operationCertificateService.delBatchMain(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }
    
    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "操作证书管理-通过id查询")
    @ApiOperation(value="操作证书管理-通过id查询", notes="操作证书管理-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<OperationCertificate> queryById(@RequestParam(name="id",required=true) String id) {
        OperationCertificate operationCertificate = operationCertificateService.getById(id);
        if(operationCertificate==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(operationCertificate);
 
    }
    
    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "操作证管理明细通过主表ID查询")
    @ApiOperation(value="操作证管理明细主表ID查询", notes="操作证管理明细-通主表ID查询")
    @GetMapping(value = "/queryOperationCertificateDetailByMainId")
    public Result<List<OperationCertificateDetail>> queryOperationCertificateDetailListByMainId(@RequestParam(name="id",required=true) String id) {
        List<OperationCertificateDetail> operationCertificateDetailList = operationCertificateDetailService.selectByMainId(id);
        return Result.OK(operationCertificateDetailList);
    }
 
    /**
    * 导出excel
    *
    * @param request
    * @param operationCertificate
    */
    //@RequiresPermissions("org.jeecg.modules.mdc:mom_eam_operation_certificate:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, OperationCertificate operationCertificate) {
      // Step.1 组装查询条件查询数据
      QueryWrapper<OperationCertificate> queryWrapper = QueryGenerator.initQueryWrapper(operationCertificate, request.getParameterMap());
      LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
 
      //配置选中数据查询条件
      String selections = request.getParameter("selections");
      if(oConvertUtils.isNotEmpty(selections)) {
         List<String> selectionList = Arrays.asList(selections.split(","));
         queryWrapper.in("id",selectionList);
      }
      //Step.2 获取导出数据
      List<OperationCertificate> operationCertificateList = operationCertificateService.list(queryWrapper);
 
      // Step.3 组装pageList
      List<OperationCertificatePage> pageList = new ArrayList<OperationCertificatePage>();
      for (OperationCertificate main : operationCertificateList) {
          OperationCertificatePage vo = new OperationCertificatePage();
          BeanUtils.copyProperties(main, vo);
          List<OperationCertificateDetail> operationCertificateDetailList = operationCertificateDetailService.selectByMainId(main.getId());
          vo.setOperationCertificateDetailList(operationCertificateDetailList);
          pageList.add(vo);
      }
 
      // Step.4 AutoPoi 导出Excel
      ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
      mv.addObject(NormalExcelConstants.FILE_NAME, "操作证书管理列表");
      mv.addObject(NormalExcelConstants.CLASS, OperationCertificatePage.class);
      mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("操作证书管理数据", "导出人:"+sysUser.getRealname(), "操作证书管理"));
      mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
      return mv;
    }
 
    /**
    * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("org.jeecg.modules.mdc:mom_eam_operation_certificate:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) throws IOException  {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        // 错误信息
        List<String> errorMessage = new ArrayList<>();
        int successLines = 0, errorLines = 0,totalLines = 0,partSuccessLines = 0;
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            // 获取上传文件对象
            MultipartFile file = entity.getValue();
            ImportParams params = new ImportParams();
            params.setNeedSave(true);
            try {
                    List<OperationCertificateImportVo> list = ExcelImportUtil.importExcel(file.getInputStream(), OperationCertificateImportVo.class, params);
                    totalLines = list.size();
                    for (int i = 0; i < list.size(); i++) {
                    OperationCertificateImportVo operationCertificateImportVo = list.get(i);
                    OperationCertificate operationCertificate = new OperationCertificate();
                    //判空
                    if(StringUtils.isBlank(operationCertificateImportVo.getName())){
                        errorMessage.add("第" + (i + 1) + "行导入失败,原因:人员不能为空!");
                        errorLines++;
                        continue;
                    }
                    if(StringUtils.isBlank(operationCertificateImportVo.getTechnicalLevel())){
                        errorMessage.add("第" + (i + 1) + "行导入失败,原因:技能等级不能为空!");
                        errorLines++;
                        continue;
                    }
                    if(StringUtils.isBlank(operationCertificateImportVo.getProfession())){
                        errorMessage.add("第" + (i + 1) + "行导入失败,原因:工种不能为空!");
                        errorLines++;
                        continue;
                    }
                    if(StringUtils.isBlank(operationCertificateImportVo.getWorkNo())){
                        errorMessage.add("第" + (i + 1) + "行导入失败,原因:工作证号不能为空!");
                        errorLines++;
                        continue;
                    }
                    if(StringUtils.isBlank(operationCertificateImportVo.getAppointmentCardNum())){
                        errorMessage.add("第" + (i + 1) + "行导入失败,原因:上岗证号不能为空!");
                        errorLines++;
                        continue;
                    }
                    if(StringUtils.isBlank(operationCertificateImportVo.getEquipmentNum())){
                        errorMessage.add("第" + (i + 1) + "行导入失败,原因:统一编号不能为空!");
                        errorLines++;
                        continue;
                    }
                    if(operationCertificateImportVo.getIssueDate() == null){
                        errorMessage.add("第" + (i + 1) + "行导入失败,原因:发证日期不能为空!");
                        errorLines++;
                        continue;
                    }
                    if(StringUtils.isBlank(operationCertificateImportVo.getNum())){
                        operationCertificate.setNum(identityService.getNumByTypeAndLength("OperationCertificate",4));
                    }else {
                        operationCertificate.setNum(operationCertificateImportVo.getNum());
                    }
                    //根据人员姓名查询人员id
                    List<SysUser> userList = userService.lambdaQuery().eq(SysUser::getRealname,operationCertificateImportVo.getName().trim())
                            .eq(SysUser::getWorkNo,operationCertificateImportVo.getWorkNo().trim()).eq(SysUser::getDelFlag, CommonConstant.DEL_FLAG_0).list();
                    if(CollectionUtils.isEmpty(userList)){
                        errorMessage.add("第" + (i + 1) + "行导入失败,原因:人员不存在或工作证号与人员姓名不匹配,请先维护人员信息;");
                        errorLines++;
                        continue;
                    }else{
                        operationCertificate.setUserId(userList.get(0).getUsername());
                    }
                    //工种查询
                    Profession profession = professionService.lambdaQuery().eq(Profession::getName,operationCertificateImportVo.getProfession().trim()).eq(Profession::getDelFlag,CommonConstant.DEL_FLAG_0).one();
                    if(profession == null){
                        Profession profession1 = new Profession();
                        profession1.setNum(identityService.getNumByTypeAndLength("Profession",4));
                        profession1.setName(operationCertificateImportVo.getProfession().trim());
                        professionService.save(profession1);
                        operationCertificate.setProfession(profession1.getId());
                    }else {
                        operationCertificate.setProfession(profession.getId());
                    }
                    //技能等级判断
                    if(CollectionUtils.isEmpty(sysDictItemService.lambdaQuery().eq(SysDictItem::getDictId,sysDictService.lambdaQuery().eq(SysDict::getDictCode,"technical_level").one().getId()).eq(SysDictItem::getItemText,operationCertificateImportVo.getTechnicalLevel().trim()).list())){
                        errorMessage.add("第" + (i + 1) + "行导入失败,原因:技能等级不存在,请先维护技能等级;");
                        errorLines++;
                        continue;
                    }else{
                        operationCertificate.setTechnicalLevel(sysDictItemService.lambdaQuery().eq(SysDictItem::getDictId,sysDictService.lambdaQuery().eq(SysDict::getDictCode,"technical_level").one().getId()).eq(SysDictItem::getItemText,operationCertificateImportVo.getTechnicalLevel().trim()).one().getItemValue());
                    }
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    operationCertificate.setIssueDate(operationCertificateImportVo.getIssueDate());
                    operationCertificate.setStartTime(operationCertificateImportVo.getIssueDate());
                    Date currentDate = operationCertificateImportVo.getIssueDate();
                    Calendar c = Calendar.getInstance();
                    c.setTime(currentDate);
                    c.add(Calendar.YEAR, 1);
                    currentDate = c.getTime();
                    operationCertificate.setEndTime(currentDate);
                    operationCertificate.setCurrentCycleScore(12);
                    operationCertificate.setRemark(operationCertificateImportVo.getRemark());
                    List<String> equipments  = Arrays.asList(operationCertificateImportVo.getEquipmentNum().split("/"));
                    List<String> equipmentIds = new ArrayList<>();
                    int a = 0;
                    for(int j = 0; j<equipments.size();j++){
                        List<Equipment> equipmentList = equipmentService.lambdaQuery().eq(Equipment::getNum,equipments.get(j)).eq(Equipment::getDelFlag,CommonConstant.DEL_FLAG_0).list();
                        if(CollectionUtils.isNotEmpty(equipmentList)){
                            equipmentIds.add(equipmentList.get(0).getId());
                        }else {
                            errorMessage.add("第" + (i + 1) + "行统一编号为"+equipments.get(j)+"的设备导入失败,原因:设备台账中不存在该设备,请先维护设备信息;");
                            a++;
                            continue;
                        }
                    }
                    if(a == equipments.size()){
                        errorLines++;
                        a = 0;
                        continue;
                    }else if(a > 0 && a < equipments.size()){
                        partSuccessLines++;
                        a = 0;
                    }
                    if(CollectionUtils.isEmpty(equipmentIds)){
                        errorMessage.add("第" + (i + 1) + "行导入失败,原因:设备不存在,请先维护设备信息;");
                        errorLines++;
                        continue;
                    }else{
                        operationCertificate.setEquipmentIds(equipmentIds.stream().map(String::toString).collect(Collectors.joining(",")));
                    }
                    operationCertificate.setStatus("1");
                    //查询当前人员是否已有操作证
                    List<OperationCertificate> operationCertificateList = operationCertificateService.lambdaQuery().eq(OperationCertificate::getUserId,userList.get(0).getUsername()).eq(OperationCertificate::getDelFlag,CommonConstant.DEL_FLAG_0).list();
                    if(CollectionUtils.isEmpty(operationCertificateList)){
                        operationCertificateService.save(operationCertificate);
                    }else {
                        //将设备增加到操作证
                        String newEquipmentIds = operationCertificateList.get(0).getEquipmentIds().concat(",").concat(operationCertificate.getEquipmentIds());
                        operationCertificate.setId(operationCertificateList.get(0).getId());
                        HashSet<String> set = new HashSet<String>(Arrays.asList(newEquipmentIds.split(",")));
                        operationCertificate.setEquipmentIds(set.stream().collect(Collectors.joining(",")));
                        operationCertificateService.updateById(operationCertificate);
                    }
                    successLines++;
                }
            } catch (Exception e) {
                errorMessage.add("发生异常:" + e.getMessage());
                log.error(e.getMessage(), e);
                return Result.error(errorMessage.toString());
                // return (Result<?>) errorMessage;
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
        return ImportExcelUtil.imporReturnOperationCertificate(totalLines,partSuccessLines,errorLines, successLines, errorMessage);
    }
     /**
      * 通过主表ID查询
      * @return
      */
     //@AutoLog(value = "操作证管理明细-通过主表ID查询")
     @ApiOperation(value="操作证管理明细-通过主表ID查询", notes="操作证管理明细-通过主表ID查询")
     @GetMapping(value = "/listOperationCertificateDetailByMainId")
     public Result<IPage<OperationCertificateDetail>> listOperationCertificateDetailByMainId(OperationCertificateDetail operationCertificateDetail,
                                                                                             @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                                             HttpServletRequest req) {
         QueryWrapper<OperationCertificateDetail> queryWrapper = QueryGenerator.initQueryWrapper(operationCertificateDetail, req.getParameterMap());
         Page<OperationCertificateDetail> page = new Page<OperationCertificateDetail>(pageNo, pageSize);
         IPage<OperationCertificateDetail> pageList = operationCertificateDetailService.page(page, queryWrapper);
         return Result.OK(pageList);
     }
}