| | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.StringUtils; |
| | | 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.poi.ss.usermodel.*; |
| | | import org.apache.poi.ss.util.CellRangeAddress; |
| | | import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
| | | import org.apache.shiro.SecurityUtils; |
| | | import org.jeecg.common.api.vo.Result; |
| | | import org.jeecg.common.aspect.annotation.AutoLog; |
| | | import org.jeecg.common.constant.CommonConstant; |
| | |
| | | import org.jeecg.common.system.base.controller.JeecgController; |
| | | import org.jeecg.common.system.query.QueryGenerator; |
| | | //import org.jeecg.modules.base.service.IUnitService; |
| | | import org.jeecg.common.system.vo.LoginUser; |
| | | import org.jeecg.modules.eam.entity.*; |
| | | import org.jeecg.modules.eam.service.*; |
| | | import org.jeecgframework.poi.excel.entity.ImportParams; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | import org.springframework.web.multipart.MultipartHttpServletRequest; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | */ |
| | | @ApiOperation(value = "精度参数模板-添加", notes = "精度参数模板-添加") |
| | | @PostMapping(value = "/addNew") |
| | | @Transactional(rollbackFor = { Exception.class }) |
| | | public Result<?> addNew(@RequestBody PrecisionParametersTemplate precisionParametersTemplate) { |
| | | precisionParametersTemplateService.save(precisionParametersTemplate); |
| | | String id= precisionParametersTemplate.getId(); |
| | | List<PrecisionParametersTemplateDetail> details = precisionParametersTemplate.getPrecisionParametersTemplateDetailList(); |
| | | for(PrecisionParametersTemplateDetail precisionParametersTemplateDetail:details){ |
| | | precisionParametersTemplateDetail.setId(""); |
| | | precisionParametersTemplateDetail.setPrecisionParametersTemplateId(id); |
| | | } |
| | | precisionParametersTemplateDetailService.saveBatch(details); |
| | |
| | | .collect(Collectors.toList()); |
| | | return Result.OK(nums); |
| | | } |
| | | /** |
| | | * 导入 |
| | | * @return |
| | | */ |
| | | @RequestMapping(value = "/importExcel", method = RequestMethod.POST) |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) { |
| | | MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; |
| | | LoginUser user= (LoginUser) SecurityUtils.getSubject().getPrincipal(); |
| | | Map<String, MultipartFile> fileMap = multipartRequest.getFileMap(); |
| | | String exceptionInfo = ""; |
| | | int exceptionNum = 0; |
| | | for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) { |
| | | MultipartFile file = entity.getValue(); |
| | | ImportParams params = new ImportParams(); |
| | | params.setNeedSave(true); |
| | | try { |
| | | InputStream fis = file.getInputStream(); |
| | | Workbook workbook = new XSSFWorkbook(fis); |
| | | Sheet sheet = workbook.getSheetAt(0); |
| | | Row row1 = sheet.getRow(1); |
| | | Cell cell15 = row1.getCell(5); |
| | | if(ObjectUtils.isNotNull(cell15)&&cell15.getCellType()==CellType.NUMERIC){ |
| | | String num = (int)cell15.getNumericCellValue()+""; |
| | | PrecisionParametersTemplate precisionParametersTemplate = precisionParametersTemplateService.getOne(new QueryWrapper<PrecisionParametersTemplate>().eq("num",num).eq("del_flag",0),false); |
| | | if(ObjectUtils.isNull(precisionParametersTemplate)){ |
| | | precisionParametersTemplate = new PrecisionParametersTemplate(); |
| | | precisionParametersTemplate.setNum(num); |
| | | precisionParametersTemplate.setName(num+"精度检验单"); |
| | | precisionParametersTemplate.setDelFlag(0); |
| | | precisionParametersTemplateService.save(precisionParametersTemplate); |
| | | } |
| | | precisionParametersTemplateDetailService.remove(new QueryWrapper<PrecisionParametersTemplateDetail>().eq("precision_parameters_template_id",precisionParametersTemplate.getId())); |
| | | for (Row row : sheet) { |
| | | if(row.getRowNum()>3&&row.getRowNum()<sheet.getLastRowNum()){ |
| | | PrecisionParametersTemplateDetail precisionParametersTemplateDetail = new PrecisionParametersTemplateDetail(); |
| | | Cell cell1 = row.getCell(1); |
| | | String cellValue1 = ""; |
| | | if(isMergedCell(sheet,cell1)){ |
| | | cellValue1 = getMergedCellValue(sheet,cell1); |
| | | } |
| | | else if (cell1.getCellType() == CellType.STRING) { |
| | | cellValue1 = cell1.getStringCellValue(); |
| | | } |
| | | Cell cell3 = row.getCell(3); |
| | | String cellValue3 = ""; |
| | | if (cell3.getCellType() == CellType.STRING) { |
| | | cellValue3 = cell3.getStringCellValue(); |
| | | } |
| | | Cell cell4 = row.getCell(4); |
| | | String cellValue4 = ""; |
| | | if(isMergedCell(sheet,cell4)){ |
| | | cellValue4 = getMergedCellValue(sheet,cell4); |
| | | } |
| | | else if (cell4.getCellType() == CellType.STRING) { |
| | | cellValue4 = cell4.getStringCellValue(); |
| | | }else if(cell4.getCellType() == CellType.NUMERIC){ |
| | | cellValue4 = String.valueOf(cell4.getNumericCellValue()); |
| | | } |
| | | PrecisionParameters precisionParameters = precisionParametersService.getOne(new QueryWrapper<PrecisionParameters>().eq("name",cellValue1).eq("precision_parameters_uda1",cellValue3).eq("del_flag",0)); |
| | | if(ObjectUtils.isNull(precisionParameters)){ |
| | | precisionParameters = new PrecisionParameters(); |
| | | precisionParameters.setName(cellValue1); |
| | | precisionParameters.setPrecisionParametersUda1(cellValue3); |
| | | precisionParameters.setDelFlag(0); |
| | | precisionParametersService.save(precisionParameters); |
| | | } |
| | | precisionParametersTemplateDetail.setPrecisionParametersTemplateId(precisionParametersTemplate.getId()); |
| | | precisionParametersTemplateDetail.setPrecisionParametersId(precisionParameters.getId()); |
| | | precisionParametersTemplateDetail.setPrecisionParametersUda1(cellValue3); |
| | | precisionParametersTemplateDetail.setTolerance(cellValue4); |
| | | String[] needs = cellValue4.split("/"); |
| | | if(needs.length==2){ |
| | | precisionParametersTemplateDetail.setDistance(Double.valueOf(needs[1])); |
| | | } |
| | | if(StringUtils.isNotBlank(needs[0])){ |
| | | precisionParametersTemplateDetail.setBase(Double.valueOf(needs[0])); |
| | | } |
| | | precisionParametersTemplateDetail.setSort(row.getRowNum()-4); |
| | | precisionParametersTemplateDetailService.save(precisionParametersTemplateDetail); |
| | | } |
| | | } |
| | | Equipment equipment = equipmentService.getOne(new QueryWrapper<Equipment>().eq("num",num).eq("del_flag",0),false); |
| | | if(ObjectUtils.isNotNull(equipment)&&StringUtils.isBlank(equipment.getPrecisionParametersTemplateId())){ |
| | | equipment.setPrecisionParametersTemplateId(precisionParametersTemplate.getId()); |
| | | equipmentService.updateById(equipment); |
| | | } |
| | | return Result.OK("导入成功"); |
| | | } |
| | | else { |
| | | return Result.OK("导入失败,统一编码格式错误,请正确填写表头"); |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | } |
| | | return Result.error("文件导入失败!"); |
| | | } |
| | | /** |
| | | * |
| | | * 检查单元格是否在合并单元格范围内. |
| | | * |
| | | */ |
| | | private boolean isMergedCell(Sheet sheet, Cell cell) { |
| | | for (CellRangeAddress mergedRegion : sheet.getMergedRegions()) { |
| | | if (mergedRegion.isInRange(cell.getRowIndex(), cell.getColumnIndex())) { |
| | | return true; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | /** |
| | | * |
| | | * 获取合并单元格的值. |
| | | * |
| | | */ |
| | | private String getMergedCellValue(Sheet sheet, Cell cell) { |
| | | for (CellRangeAddress mergedRegion : sheet.getMergedRegions()) { |
| | | if (mergedRegion.isInRange(cell.getRowIndex(), cell.getColumnIndex())) { |
| | | Row firstRow = sheet.getRow(mergedRegion.getFirstRow()); |
| | | Cell firstCell = firstRow.getCell(mergedRegion.getFirstColumn()); |
| | | if (firstCell != null) { |
| | | if (firstCell.getCellType() == CellType.STRING) { |
| | | return firstCell.getStringCellValue(); |
| | | } else if (firstCell.getCellType() == CellType.NUMERIC) { |
| | | return String.valueOf(firstCell.getNumericCellValue()); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | } |