| | |
| | | package org.jeecg.modules.eam.controller; |
| | | |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.io.IOException; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.net.URLDecoder; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | import org.jeecg.common.api.vo.Result; |
| | | import org.jeecg.common.system.query.QueryGenerator; |
| | | import org.jeecg.common.aspect.annotation.AutoLog; |
| | | import org.jeecg.common.util.oConvertUtils; |
| | | import org.jeecg.modules.eam.constant.BusinessCodeConst; |
| | | import org.jeecg.modules.eam.entity.EamMaintenanceStandard; |
| | | import org.jeecg.modules.eam.request.EamMaintenanceStandardRequest; |
| | | import org.jeecg.modules.eam.service.IEamMaintenanceStandardService; |
| | | import java.util.Date; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.poi.hssf.usermodel.HSSFSheet; |
| | | import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
| | | import org.apache.poi.ss.usermodel.*; |
| | | import org.apache.poi.xssf.usermodel.XSSFSheet; |
| | | import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
| | | import org.jeecg.common.api.vo.FileUploadResult; |
| | | import org.jeecg.common.api.vo.Result; |
| | | import org.jeecg.common.aspect.annotation.AutoLog; |
| | | import org.jeecg.common.exception.JeecgBootException; |
| | | import org.jeecg.common.system.base.controller.JeecgController; |
| | | import org.jeecg.common.system.query.QueryGenerator; |
| | | import org.jeecg.common.util.FileUtil; |
| | | import org.jeecg.modules.eam.constant.BusinessCodeConst; |
| | | import org.jeecg.modules.eam.constant.MaintenanceCategoryEnum; |
| | | import org.jeecg.modules.eam.dto.MaintenanceStandardImport; |
| | | import org.jeecg.modules.eam.entity.EamEquipment; |
| | | import org.jeecg.modules.eam.entity.EamMaintenanceStandard; |
| | | import org.jeecg.modules.eam.entity.EamMaintenanceStandardDetail; |
| | | import org.jeecg.modules.eam.request.EamMaintenanceStandardRequest; |
| | | import org.jeecg.modules.eam.service.IEamEquipmentService; |
| | | import org.jeecg.modules.eam.service.IEamMaintenanceStandardService; |
| | | import org.jeecg.modules.system.service.ISysBusinessCodeRuleService; |
| | | 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.jeecgframework.poi.util.PoiPublicUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | import org.springframework.web.multipart.MultipartHttpServletRequest; |
| | | import org.springframework.web.servlet.ModelAndView; |
| | | import com.alibaba.fastjson.JSON; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | |
| | | /** |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Paths; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @Description: 保养标准 |
| | | * @Author: jeecg-boot |
| | | * @Date: 2025-03-26 |
| | | * @Date: 2025-03-26 |
| | | * @Version: V1.0 |
| | | */ |
| | | @Slf4j |
| | | @Api(tags="保养标准") |
| | | @Api(tags = "保养标准") |
| | | @RestController |
| | | @RequestMapping("/eam/maintenanceStandard") |
| | | public class EamMaintenanceStandardController extends JeecgController<EamMaintenanceStandard, IEamMaintenanceStandardService> { |
| | | @Autowired |
| | | private IEamMaintenanceStandardService eamMaintenanceStandardService; |
| | | @Autowired |
| | | private ISysBusinessCodeRuleService businessCodeRuleService; |
| | | |
| | | /** |
| | | * 分页列表查询 |
| | | * |
| | | * @param eamMaintenanceStandard |
| | | * @param pageNo |
| | | * @param pageSize |
| | | * @param req |
| | | * @return |
| | | */ |
| | | @AutoLog(value = "保养标准-分页列表查询") |
| | | @ApiOperation(value="保养标准-分页列表查询", notes="保养标准-分页列表查询") |
| | | @GetMapping(value = "/list") |
| | | public Result<?> queryPageList(EamMaintenanceStandard eamMaintenanceStandard, |
| | | @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, |
| | | @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, |
| | | HttpServletRequest req) { |
| | | QueryWrapper<EamMaintenanceStandard> queryWrapper = QueryGenerator.initQueryWrapper(eamMaintenanceStandard, req.getParameterMap()); |
| | | Page<EamMaintenanceStandard> page = new Page<EamMaintenanceStandard>(pageNo, pageSize); |
| | | IPage<EamMaintenanceStandard> pageList = eamMaintenanceStandardService.page(page, queryWrapper); |
| | | return Result.OK(pageList); |
| | | } |
| | | |
| | | /** |
| | | * 添加 |
| | | * |
| | | * @param standardRequest |
| | | * @return |
| | | */ |
| | | @AutoLog(value = "保养标准-添加") |
| | | @ApiOperation(value="保养标准-添加", notes="保养标准-添加") |
| | | @PostMapping(value = "/add") |
| | | public Result<?> add(@RequestBody EamMaintenanceStandardRequest standardRequest) { |
| | | if(standardRequest == null){ |
| | | return Result.error("添加的对象不能为空!"); |
| | | } |
| | | if(CollectionUtil.isEmpty(standardRequest.getTableDetailList())) { |
| | | return Result.error("保养项不能为空!"); |
| | | } |
| | | String codeSeq = businessCodeRuleService.generateBusinessCodeSeq(BusinessCodeConst.MAINTENANCE_STANDARD_CODE_RULE); |
| | | standardRequest.setStandardCode(codeSeq); |
| | | boolean b = eamMaintenanceStandardService.addMaintenanceStandard(standardRequest); |
| | | if(!b) { |
| | | return Result.error("添加失败!"); |
| | | } |
| | | return Result.OK("添加成功!"); |
| | | } |
| | | |
| | | /** |
| | | * 编辑 |
| | | * |
| | | * @param standardRequest |
| | | * @return |
| | | */ |
| | | @AutoLog(value = "保养标准-编辑") |
| | | @ApiOperation(value="保养标准-编辑", notes="保养标准-编辑") |
| | | @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST}) |
| | | public Result<?> edit(@RequestBody EamMaintenanceStandardRequest standardRequest) { |
| | | // eamMaintenanceStandardService.updateById(eamMaintenanceStandard); |
| | | return Result.OK("编辑成功!"); |
| | | } |
| | | |
| | | /** |
| | | * 通过id删除 |
| | | * |
| | | * @param id |
| | | * @return |
| | | */ |
| | | @AutoLog(value = "保养标准-通过id删除") |
| | | @ApiOperation(value="保养标准-通过id删除", notes="保养标准-通过id删除") |
| | | @DeleteMapping(value = "/delete") |
| | | public Result<?> delete(@RequestParam(name="id",required=true) String id) { |
| | | eamMaintenanceStandardService.removeById(id); |
| | | return Result.OK("删除成功!"); |
| | | } |
| | | |
| | | /** |
| | | * 批量删除 |
| | | * |
| | | * @param ids |
| | | * @return |
| | | */ |
| | | @AutoLog(value = "保养标准-批量删除") |
| | | @ApiOperation(value="保养标准-批量删除", notes="保养标准-批量删除") |
| | | @DeleteMapping(value = "/deleteBatch") |
| | | public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) { |
| | | this.eamMaintenanceStandardService.removeByIds(Arrays.asList(ids.split(","))); |
| | | return Result.OK("批量删除成功!"); |
| | | } |
| | | |
| | | /** |
| | | * 通过id查询 |
| | | * |
| | | * @param id |
| | | * @return |
| | | */ |
| | | @AutoLog(value = "保养标准-通过id查询") |
| | | @ApiOperation(value="保养标准-通过id查询", notes="保养标准-通过id查询") |
| | | @GetMapping(value = "/queryById") |
| | | public Result<?> queryById(@RequestParam(name="id",required=true) String id) { |
| | | EamMaintenanceStandard eamMaintenanceStandard = eamMaintenanceStandardService.getById(id); |
| | | return Result.OK(eamMaintenanceStandard); |
| | | } |
| | | @Autowired |
| | | private IEamMaintenanceStandardService eamMaintenanceStandardService; |
| | | @Autowired |
| | | private ISysBusinessCodeRuleService businessCodeRuleService; |
| | | @Autowired |
| | | private IEamEquipmentService eamEquipmentService; |
| | | |
| | | /** |
| | | * 导出excel |
| | | * |
| | | * @param request |
| | | * @param eamMaintenanceStandard |
| | | */ |
| | | @RequestMapping(value = "/exportXls") |
| | | public ModelAndView exportXls(HttpServletRequest request, EamMaintenanceStandard eamMaintenanceStandard) { |
| | | return super.exportXls(request, eamMaintenanceStandard, EamMaintenanceStandard.class, "保养标准"); |
| | | } |
| | | /** |
| | | * 分页列表查询 |
| | | * |
| | | * @param eamMaintenanceStandard |
| | | * @param pageNo |
| | | * @param pageSize |
| | | * @param req |
| | | * @return |
| | | */ |
| | | @ApiOperation(value = "保养标准-分页列表查询", notes = "保养标准-分页列表查询") |
| | | @GetMapping(value = "/list") |
| | | public Result<?> queryPageList(EamMaintenanceStandard eamMaintenanceStandard, |
| | | @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, |
| | | @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, |
| | | HttpServletRequest req) { |
| | | QueryWrapper<EamMaintenanceStandard> queryWrapper = QueryGenerator.initQueryWrapper(eamMaintenanceStandard, req.getParameterMap()); |
| | | Page<EamMaintenanceStandard> page = new Page<EamMaintenanceStandard>(pageNo, pageSize); |
| | | IPage<EamMaintenanceStandard> pageList = eamMaintenanceStandardService.page(page, queryWrapper); |
| | | return Result.OK(pageList); |
| | | } |
| | | |
| | | /** |
| | | * 通过excel导入数据 |
| | | * |
| | | * @param request |
| | | * @param response |
| | | * @return |
| | | */ |
| | | @RequestMapping(value = "/importExcel", method = RequestMethod.POST) |
| | | public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) { |
| | | return super.importExcel(request, response, EamMaintenanceStandard.class); |
| | | } |
| | | /** |
| | | * 添加 |
| | | * |
| | | * @param standardRequest |
| | | * @return |
| | | */ |
| | | @AutoLog(value = "保养标准-添加") |
| | | @ApiOperation(value = "保养标准-添加", notes = "保养标准-添加") |
| | | @PostMapping(value = "/add") |
| | | public Result<?> add(@RequestBody EamMaintenanceStandardRequest standardRequest) { |
| | | if (standardRequest == null) { |
| | | return Result.error("添加的对象不能为空!"); |
| | | } |
| | | if (CollectionUtil.isEmpty(standardRequest.getTableDetailList())) { |
| | | return Result.error("保养项不能为空!"); |
| | | } |
| | | String codeSeq = businessCodeRuleService.generateBusinessCodeSeq(BusinessCodeConst.MAINTENANCE_STANDARD_CODE_RULE); |
| | | standardRequest.setStandardCode(codeSeq); |
| | | boolean b = eamMaintenanceStandardService.addMaintenanceStandard(standardRequest); |
| | | if (!b) { |
| | | return Result.error("添加失败!"); |
| | | } |
| | | return Result.OK("添加成功!"); |
| | | } |
| | | |
| | | /** |
| | | * 编辑 |
| | | * |
| | | * @param standardRequest |
| | | * @return |
| | | */ |
| | | @AutoLog(value = "保养标准-编辑") |
| | | @ApiOperation(value = "保养标准-编辑", notes = "保养标准-编辑") |
| | | @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST}) |
| | | public Result<?> edit(@RequestBody EamMaintenanceStandardRequest standardRequest) { |
| | | if (standardRequest == null) { |
| | | return Result.error("添加的对象不能为空!"); |
| | | } |
| | | if (CollectionUtil.isEmpty(standardRequest.getTableDetailList())) { |
| | | return Result.error("保养项不能为空!"); |
| | | } |
| | | boolean b = eamMaintenanceStandardService.editMaintenanceStandard(standardRequest); |
| | | if (!b) { |
| | | return Result.error("编辑失败!"); |
| | | } |
| | | return Result.OK("编辑成功!"); |
| | | } |
| | | |
| | | /** |
| | | * 通过id删除 |
| | | * |
| | | * @param id |
| | | * @return |
| | | */ |
| | | @AutoLog(value = "保养标准-通过id删除") |
| | | @ApiOperation(value = "保养标准-通过id删除", notes = "保养标准-通过id删除") |
| | | @DeleteMapping(value = "/delete") |
| | | public Result<?> delete(@RequestParam(name = "id", required = true) String id) { |
| | | eamMaintenanceStandardService.removeById(id); |
| | | return Result.OK("删除成功!"); |
| | | } |
| | | |
| | | /** |
| | | * 批量删除 |
| | | * |
| | | * @param ids |
| | | * @return |
| | | */ |
| | | @AutoLog(value = "保养标准-批量删除") |
| | | @ApiOperation(value = "保养标准-批量删除", notes = "保养标准-批量删除") |
| | | @DeleteMapping(value = "/deleteBatch") |
| | | public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) { |
| | | this.eamMaintenanceStandardService.removeByIds(Arrays.asList(ids.split(","))); |
| | | return Result.OK("批量删除成功!"); |
| | | } |
| | | |
| | | /** |
| | | * 通过id查询 |
| | | * |
| | | * @param id |
| | | * @return |
| | | */ |
| | | @ApiOperation(value = "保养标准-通过id查询", notes = "保养标准-通过id查询") |
| | | @GetMapping(value = "/queryById") |
| | | public Result<?> queryById(@RequestParam(name = "id", required = true) String id) { |
| | | EamMaintenanceStandard eamMaintenanceStandard = eamMaintenanceStandardService.getById(id); |
| | | return Result.OK(eamMaintenanceStandard); |
| | | } |
| | | |
| | | /** |
| | | * 导出excel |
| | | * |
| | | * @param request |
| | | * @param eamMaintenanceStandard |
| | | */ |
| | | @RequestMapping(value = "/exportXls") |
| | | public ModelAndView exportXls(HttpServletRequest request, EamMaintenanceStandard eamMaintenanceStandard) { |
| | | return super.exportXls(request, eamMaintenanceStandard, EamMaintenanceStandard.class, "保养标准"); |
| | | } |
| | | |
| | | /** |
| | | * 通过excel导入数据 |
| | | * |
| | | * @param request |
| | | * @param response |
| | | * @return |
| | | */ |
| | | @RequestMapping(value = "/inspectionImportExcel", method = RequestMethod.POST) |
| | | public Result<?> inspectionImportExcel(HttpServletRequest request, HttpServletResponse response) { |
| | | MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; |
| | | Map<String, MultipartFile> fileMap = multipartRequest.getFileMap(); |
| | | for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) { |
| | | // 获取上传文件对象 |
| | | MultipartFile file = entity.getValue(); |
| | | ImportParams params = new ImportParams(); |
| | | params.setTitleRows(2); |
| | | params.setHeadRows(1); |
| | | params.setSheetNum(1); |
| | | params.setNeedSave(true); |
| | | EamMaintenanceStandardRequest standardRequest = new EamMaintenanceStandardRequest(); |
| | | try { |
| | | //读取设备编号,图片等 |
| | | readExcel(file, standardRequest); |
| | | EamEquipment equipment = eamEquipmentService.selectByEquipmentCode(standardRequest.getEquipmentCode()); |
| | | if(equipment == null) { |
| | | log.error("设备不存在:{}", standardRequest.getEquipmentCode()); |
| | | continue; |
| | | } |
| | | standardRequest.setStandardName(standardRequest.getEquipmentName() + "点检标准"); |
| | | standardRequest.setMaintenanceCategory(MaintenanceCategoryEnum.POINT_INSPECTION.name()); |
| | | standardRequest.setEquipmentId(equipment.getId()); |
| | | //读取保养明细内容 |
| | | List<MaintenanceStandardImport> list = ExcelImportUtil.importExcel(file.getInputStream(), MaintenanceStandardImport.class, params); |
| | | //明细项 |
| | | List<EamMaintenanceStandardDetail> tableList = list.stream().map(EamMaintenanceStandardDetail::new).collect(Collectors.toList()); |
| | | standardRequest.setTableDetailList(tableList); |
| | | String codeSeq = businessCodeRuleService.generateBusinessCodeSeq(BusinessCodeConst.MAINTENANCE_STANDARD_CODE_RULE); |
| | | standardRequest.setStandardCode(codeSeq); |
| | | boolean b = eamMaintenanceStandardService.addMaintenanceStandard(standardRequest); |
| | | if (!b) { |
| | | log.error("保存失败! {}", standardRequest.getEquipmentCode()); |
| | | } |
| | | } catch (Exception e) { |
| | | //update-begin-author:taoyan date:20211124 for: 导入数据重复增加提示 |
| | | String msg = e.getMessage(); |
| | | log.error("文件 {} 处理异常: {}", file.getOriginalFilename(), msg, e); |
| | | //update-end-author:taoyan date:20211124 for: 导入数据重复增加提示 |
| | | } finally { |
| | | try { |
| | | file.getInputStream().close(); |
| | | } catch (IOException e) { |
| | | log.error(e.getMessage(), e); |
| | | } |
| | | } |
| | | } |
| | | return Result.ok("文件导入完成!"); |
| | | } |
| | | |
| | | /** |
| | | * 读取Excel 第一行, 第二行的信息,包括图片信息 |
| | | * @param file |
| | | * @param request |
| | | */ |
| | | private void readExcel(MultipartFile file, EamMaintenanceStandardRequest request) { |
| | | Workbook book = null; |
| | | boolean isXSSFWorkbook = false; |
| | | try { |
| | | book = WorkbookFactory.create(file.getInputStream()); |
| | | if (book instanceof XSSFWorkbook) { |
| | | isXSSFWorkbook = true; |
| | | } |
| | | |
| | | Sheet sheet = book.getSheetAt(0); |
| | | //第一行读取 |
| | | Row row = sheet.getRow(0); |
| | | //设备编码 |
| | | Cell equipmentCode = row.getCell(15); |
| | | if(CellType.NUMERIC.equals(equipmentCode.getCellType())) { |
| | | request.setEquipmentCode(String.valueOf((int) equipmentCode.getNumericCellValue())); |
| | | }else if(CellType.STRING.equals(equipmentCode.getCellType())) { |
| | | request.setEquipmentCode(equipmentCode.getStringCellValue()); |
| | | } |
| | | if (StringUtils.isBlank(request.getEquipmentCode())) { |
| | | throw new JeecgBootException("Excel【 " + file.getOriginalFilename() + "】没有读取到设备编号,导入失败!"); |
| | | } |
| | | //初始日期 |
| | | Cell initialDate = row.getCell(11); |
| | | if (DateUtil.isCellDateFormatted(initialDate)) { |
| | | request.setInitialDate(initialDate.getDateCellValue()); |
| | | } else { |
| | | request.setInitialDate(new Date()); |
| | | } |
| | | //设备名称 |
| | | Cell equipmentName = row.getCell(13); |
| | | request.setEquipmentName(equipmentName.getStringCellValue()); |
| | | |
| | | //第二行读取 |
| | | row = sheet.getRow(1); |
| | | //保养周期 |
| | | Cell period = row.getCell(11); |
| | | if (CellType.NUMERIC.equals(period.getCellType())) { |
| | | request.setMaintenancePeriod((int) period.getNumericCellValue()); |
| | | } else { |
| | | //默认点检周期 1 |
| | | request.setMaintenancePeriod(1); |
| | | } |
| | | //文件编码 |
| | | Cell fileCode = row.getCell(13); |
| | | request.setFileCode(fileCode.getStringCellValue()); |
| | | |
| | | Map<String, PictureData> pictures; |
| | | if (isXSSFWorkbook) { |
| | | pictures = PoiPublicUtil.getSheetPictrues07((XSSFSheet) book.getSheetAt(0), (XSSFWorkbook) book); |
| | | } else { |
| | | pictures = PoiPublicUtil.getSheetPictrues03((HSSFSheet) book.getSheetAt(0), (HSSFWorkbook) book); |
| | | } |
| | | |
| | | if (CollectionUtil.isNotEmpty(pictures)) { |
| | | //只会存在一张图片 |
| | | PictureData pictureData = pictures.get(pictures.keySet().iterator().next()); |
| | | byte[] data = pictureData.getData(); |
| | | String fileName = request.getEquipmentCode() + "[" + request.getFileCode() + "]" + "." + pictureData.suggestFileExtension(); |
| | | FileUploadResult fileUploadResult = FileUtil.uploadFile(data, fileName); |
| | | if(fileUploadResult != null) { |
| | | List<FileUploadResult> fileList = request.getFileList(); |
| | | if(fileList == null) { |
| | | fileList = new ArrayList<FileUploadResult>(); |
| | | } |
| | | fileList.add(fileUploadResult); |
| | | request.setFileList(fileList); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("读取Excel信息失败:{}", e.getMessage(), e); |
| | | } |
| | | } |
| | | } |