package org.jeecg.modules.lims.testing.controller; 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.jeecg.common.api.vo.Result; import org.jeecg.common.aspect.annotation.AutoLog; import org.jeecg.common.system.base.controller.JeecgController; import org.jeecg.common.system.query.QueryGenerator; import org.jeecg.common.system.vo.DictModel; import org.jeecg.modules.lims.testing.dto.LimsTestingBatchDTO; import org.jeecg.modules.lims.testing.entity.*; import org.jeecg.modules.lims.testing.service.*; import org.jeecg.modules.lims.testing.vo.LimsTestingBatchVo; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.text.DecimalFormat; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; /** * @Description: lims_testing_batch * @Author: jeecg-boot * @Date: 2023-05-11 * @Version: V1.0 */ @Api(tags = "lims_testing_batch") @RestController @RequestMapping("/lims/testing/batch") @Slf4j public class LimsTestingBatchController extends JeecgController { @Autowired private ILimsTestingBatchService limsTestingBatchService; @Autowired private ILimsBatchSampleService limsBatchSampleService; @Autowired private ILimsTestingProductService limsTestingProductService; @Autowired private ILimsTestingStandardService limsTestingStandardService; @Autowired private ILimsItemTargetService limsItemTargetService; @Autowired private ILimsBatchResultService limsBatchResultService; @Autowired private ILimsTestingStandCondService limsTestingStandCondService; @Autowired private ILimsBatchDetailService limsBatchDetailService; @Autowired private ILimsTestingItemService itemService; @Autowired private ILimsTestingTypeService typService; @Autowired private ILimsTestingCategoryService categoryService; /** * 分页列表查询 * * @param limsTestingBatch * @param pageNo * @param pageSize * @param req * @return */ @AutoLog(value = "lims_testing_batch-分页列表查询") @ApiOperation(value = "lims_testing_batch-分页列表查询", notes = "lims_testing_batch-分页列表查询") @GetMapping(value = "/list") public Result queryPageList(LimsTestingBatch limsTestingBatch, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) { QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(limsTestingBatch, req.getParameterMap()); Page page = new Page(pageNo, pageSize); Page page2 = new Page(pageNo, pageSize); IPage pageList = limsTestingBatchService.page(page, queryWrapper); comp(pageList, page2); return Result.OK(page2); } private void comp(IPage pageList, Page page) { List collect = pageList.getRecords().stream().map(item -> { LimsTestingBatchVo vo = new LimsTestingBatchVo(); BeanUtils.copyProperties(item, vo); //产品名称 LimsTestingProduct product = limsTestingProductService.getById(item.getProdId()); if (product != null) { vo.setProdName(product.getName()); } LimsTestingStandard stand = limsTestingStandardService.getById(item.getStandardId()); if (stand != null) { vo.setStandName(stand.getName()); } //计算每个批次的完成进度 QueryWrapper detailQueryWrapper = new QueryWrapper<>(); detailQueryWrapper.lambda().eq(LimsBatchDetail::getBatchId, item.getId()); List detailList = limsBatchDetailService.list(detailQueryWrapper); vo.setProgress(0d); if (detailList.size() > 0) { //已完成检验数据 List finishDetailList = detailList.stream().filter(detail -> detail.getValue() != null).collect(Collectors.toList()); vo.setBatchDetailTotal(detailList.size()); vo.setBatchDetailFinish(finishDetailList.size()); if (finishDetailList.size() > 0) { double progress = finishDetailList.size() / 1.0 / detailList.size(); progress = progress * 100; DecimalFormat decimalFormat = new DecimalFormat("#.0"); String pro = decimalFormat.format(progress); vo.setProgress(Double.parseDouble(pro)); } } return vo; }).collect(Collectors.toList()); BeanUtils.copyProperties(pageList, page); page.setRecords(collect); } /** * 添加 * * @param limsTestingBatch * @return */ @AutoLog(value = "lims_testing_batch-添加") @ApiOperation(value = "lims_testing_batch-添加", notes = "lims_testing_batch-添加") @PostMapping(value = "/add") @Transactional public Result add(@RequestBody LimsTestingBatchDTO limsTestingBatch) { //添加批次后需要生成样本信息 limsTestingBatch.setBatchDate(new Date()); QueryWrapper queryWrapper = new QueryWrapper<>(); //查询产品发布技术标准发布的 queryWrapper.lambda().eq(LimsTestingStandard::getProdId, limsTestingBatch.getProdId()) .eq(LimsTestingStandard::getStatus, 1).orderByDesc(LimsTestingStandard::getCreateTime); //查询产品的技术标准 List standList = limsTestingStandardService.list(queryWrapper); if (!standList.isEmpty() && standList.size() > 0) { String standId = standList.get(0).getId(); limsTestingBatch.setStandardId(standId); } else { return Result.error("当前产品没有发布技术标准,请维护后重试!"); } //1.生成批次 limsTestingBatchService.save(limsTestingBatch); //2.生成样本数据 generateSimpleData(limsTestingBatch); //3.生成检测记录汇总结果 Integer integer = generateInspectRecord(limsTestingBatch); if (integer == -1) { return Result.error("当前产品未设置鉴定项目,请维护技术标准后重试!"); } //4.生成检测明细 generateInspectDetail(limsTestingBatch); return Result.OK("添加成功!"); } //生成样本数据 private void generateSimpleData(LimsTestingBatchDTO limsTestingBatch) { List channelInfoList = limsTestingBatch.getChannelInfoList(); if (channelInfoList.isEmpty()) return; AtomicInteger sampleSum = new AtomicInteger(); channelInfoList.stream().forEach(item -> { String channels = item.getChannels(); Integer sampleNum = item.getSampleNum(); //通道 和 样本数 为空时跳过 if (StringUtils.isEmpty(channels) || sampleNum == null) { return; } String[] channelArray = channels.split(","); //通道数 for (int i = 0; i < channelArray.length; i++) { String channel = channelArray[i]; //样本数 for (int j = 0; j < sampleNum; j++) { sampleSum.getAndIncrement(); LimsBatchSample sample = new LimsBatchSample(); sample.setBatchId(limsTestingBatch.getId()); sample.setModelId(item.getModelId()); sample.setChannel(channel); sample.setProdId(limsTestingBatch.getProdId()); sample.setCode(item.getModelCode() + "-s" + (j + 1) + "-c" + channel); limsBatchSampleService.save(sample); } } }); //生成样本后更新统计数据 limsTestingBatch.setSampleNum(sampleSum.get()); limsTestingBatchService.updateById(limsTestingBatch); } //生成检验记录 private Integer generateInspectRecord(LimsTestingBatchDTO limsTestingBatch) { String standardId = limsTestingBatch.getStandardId(); QueryWrapper itemTargetQueryWrapper = new QueryWrapper<>(); itemTargetQueryWrapper.lambda().eq(LimsItemTarget::getStandardId, standardId); itemTargetQueryWrapper.lambda().eq(LimsItemTarget::getEnabled,1); List targetList = limsItemTargetService.list(itemTargetQueryWrapper); Map> typGroupMap = targetList.stream().collect(Collectors.groupingBy(LimsItemTarget::getTypeId)); Map> itmGroupMap = targetList.stream().collect(Collectors.groupingBy(LimsItemTarget::getItemId)); if (itmGroupMap.isEmpty()) { return -1; } //根据typ分组 typGroupMap.forEach((typ, value) -> { LimsBatchResult result = new LimsBatchResult(); result.setBatchId(limsTestingBatch.getId()); result.setTestingCat(value.get(0).getCategoryId()); result.setTestingType(typ); LimsTestingCategory category = categoryService.getById(result.getTestingCat()); LimsTestingType t = typService.getById(result.getTestingType()); double sort = category.getSort() * 100 + t.getSort() * 1; result.setSort(sort); limsBatchResultService.save(result); }); return 1; } /** * 生成检验明细 */ private void generateInspectDetail(LimsTestingBatchDTO limsTestingBatch) { /* //1.查询批次检测项目 String standardId = limsTestingBatch.getStandardId(); QueryWrapper itemTargetQueryWrapper = new QueryWrapper<>(); itemTargetQueryWrapper.lambda().eq(LimsItemTarget::getStandardId, standardId); List targetList = limsItemTargetService.list(itemTargetQueryWrapper); Map> typGroupMap = targetList.stream().collect(Collectors.groupingBy(LimsItemTarget::getTypeId)); //2.查询批次所有样本 QueryWrapper sampleQueryWrapper = new QueryWrapper<>(); sampleQueryWrapper.lambda().eq(LimsBatchSample::getBatchId,limsTestingBatch.getId()); List sampleList = limsBatchSampleService.list(sampleQueryWrapper); //遍历技术标准下鉴定类别-type(查询各type的检测条件) typGroupMap.forEach((type,list)->{ //查询批次下技术标准所有检测条件 QueryWrapper condQueryWrapper = new QueryWrapper<>(); condQueryWrapper.lambda().eq(LimsTestingStandCond::getTypeId,type); List conds = limsTestingStandCondService.list(condQueryWrapper); //如果没有检验条件,则添加一行检验数据 if(conds ==null || conds.size() < 1) { LimsTestingStandCond cond = new LimsTestingStandCond(); cond.setCond("-"); conds.add(cond); } for (int i = 0; i resultQueryWrapper = new QueryWrapper<>(); resultQueryWrapper.lambda().eq(LimsBatchResult::getBatchId, limsTestingBatch.getId()); List resultList = limsBatchResultService.list(resultQueryWrapper); Map resultMap = resultList.stream().collect(Collectors.toMap(LimsBatchResult::getTestingType, LimsBatchResult::getId)); //2.通过type 查询item List typList = resultList.stream().map(LimsBatchResult::getTestingType).collect(Collectors.toList()); //3.根据typ查询需检验的所有鉴定项 QueryWrapper targetQueryWrapper = new QueryWrapper<>(); targetQueryWrapper.lambda().eq(LimsItemTarget::getStandardId, limsTestingBatch.getStandardId()); targetQueryWrapper.lambda().in(LimsItemTarget::getTypeId, typList.toArray()); targetQueryWrapper.lambda().eq(LimsItemTarget::getEnabled, 1); List targetList = limsItemTargetService.list(targetQueryWrapper); List typeIds = new ArrayList<>(); List itemIds = new ArrayList<>(); //4.根据type分类item Map> typGroupMap = targetList.stream().collect(Collectors.groupingBy(LimsItemTarget::getTypeId)); //5.查询type的测试条件 Map> typCondMap1 = new HashMap<>(); Map> typCondMap2 = new HashMap<>(); typGroupMap.forEach((typ, itmList) -> { //保存typid和itemid 供生成破坏性使用 typeIds.add(typ); List itmIds = itmList.stream().filter(item->item.getEnabled()!=null && item.getEnabled()==1).map(LimsItemTarget::getItemId).collect(Collectors.toList()); itemIds.addAll(itmIds); //保存typid和itemid 供生成破坏性使用 QueryWrapper condQueryWrapper1 = new QueryWrapper<>(); condQueryWrapper1.lambda().eq(LimsTestingStandCond::getStandardId, limsTestingBatch.getStandardId()); condQueryWrapper1.lambda().eq(LimsTestingStandCond::getTypeId, typ); //测试电压 condQueryWrapper1.lambda().eq(LimsTestingStandCond::getType, 1); List typCondList1 = limsTestingStandCondService.list(condQueryWrapper1); if (typCondList1 == null || typCondList1.size() < 1) { LimsTestingStandCond cond = new LimsTestingStandCond(); cond.setCond("-"); typCondList1.add(cond); } typCondMap1.put(typ, typCondList1); //测试温度 QueryWrapper condQueryWrapper2 = new QueryWrapper<>(); condQueryWrapper2.lambda().eq(LimsTestingStandCond::getStandardId, limsTestingBatch.getStandardId()); condQueryWrapper2.lambda().eq(LimsTestingStandCond::getTypeId, typ); condQueryWrapper2.lambda().eq(LimsTestingStandCond::getType, 2); List typCondList2 = limsTestingStandCondService.list(condQueryWrapper2); if (typCondList2 == null || typCondList2.size() < 1) { LimsTestingStandCond cond = new LimsTestingStandCond(); cond.setCond("-"); typCondList2.add(cond); } typCondMap2.put(typ, typCondList2); }); //6.查询批次所有样本 QueryWrapper sampleQueryWrapper = new QueryWrapper<>(); sampleQueryWrapper.lambda().eq(LimsBatchSample::getBatchId, limsTestingBatch.getId()); List sampleList = limsBatchSampleService.list(sampleQueryWrapper); List saveList = new ArrayList<>(); //组装数据 targetList.stream().forEach(itm -> { String typeId = itm.getTypeId(); //查询typ是否破坏性实验 LimsTestingType typ = typService.getById(typeId); sampleList.forEach(sample -> { //测试电压 List conds1 = typCondMap1.get(typeId); //测试温度 List conds2 = typCondMap2.get(typeId); conds1.forEach(cond1 -> { conds2.forEach(cond2 -> { LimsBatchDetail detail = new LimsBatchDetail(); detail.setResultId(resultMap.get(typeId)); detail.setSampleId(sample.getId()); detail.setItemId(itm.getItemId()); detail.setCond(cond1.getCond()); detail.setCond2(cond2.getCond()); detail.setBatchId(limsTestingBatch.getId()); //limsBatchDetailService.save(detail); saveList.add(detail); }); }); }); }); //10 补充破坏性实验需要数据 //10.1 查询所有typ项 QueryWrapper typeQueryWrapper = new QueryWrapper<>(); typeQueryWrapper.lambda().in(LimsTestingType::getId,typeIds); List tList = typService.list(typeQueryWrapper); //10.2 根据typ查询所有itm并筛选出常规项 QueryWrapper itemQueryWrapper = new QueryWrapper<>(); itemQueryWrapper.lambda().in(LimsTestingItem::getId,itemIds); itemQueryWrapper.lambda().eq(LimsTestingItem::getGengeral,1); itemQueryWrapper.lambda().eq(LimsTestingItem::getEnabled,1); List itmGengeralList = itemService.list(itemQueryWrapper); tList.forEach(typ->{ if(typ.getDestructive()==null || typ.getDestructive()==0) return; sampleList.forEach(sample -> { itmGengeralList.forEach(itm->{ //fix 破坏性实验不需要添加条件 // //测试电压 // List conds1 = typCondMap1.get(itm.getTypeId()); // //测试温度 // List conds2 = typCondMap2.get(itm.getTypeId()); // // conds1.forEach(cond1 -> { // conds2.forEach(cond2 -> { LimsBatchDetail detail2 = new LimsBatchDetail(); detail2.setResultId(resultMap.get(typ.getId())); detail2.setSampleId(sample.getId()); detail2.setItemId(itm.getId()); detail2.setCond("-"); detail2.setCond2("-"); detail2.setBatchId(limsTestingBatch.getId()); detail2.setDestroy(1); detail2.setRemark(typ.getName()+"破坏性实验检测项"); detail2.setTid(typ.getId()); saveList.add(detail2); //limsBatchDetailService.save(detail2); LimsBatchDetail detail3 = new LimsBatchDetail(); detail3.setResultId(resultMap.get(typ.getId())); detail3.setSampleId(sample.getId()); detail3.setItemId(itm.getId()); detail3.setCond("-"); detail3.setCond2("-"); detail3.setDestroy(2); detail3.setRemark(typ.getName()+"破坏性实验检测项"); detail3.setBatchId(limsTestingBatch.getId()); detail3.setTid(typ.getId()); //limsBatchDetailService.save(detail3); saveList.add(detail3); }); // }); // }); }); }); limsBatchDetailService.saveBatch(saveList); } /** * 编辑 * * @param limsTestingBatch * @return */ @AutoLog(value = "lims_testing_batch-编辑") @ApiOperation(value = "lims_testing_batch-编辑", notes = "lims_testing_batch-编辑") @PutMapping(value = "/edit") public Result edit(@RequestBody LimsTestingBatch limsTestingBatch) { limsTestingBatchService.updateById(limsTestingBatch); return Result.OK("编辑成功!"); } /** * 通过id删除 * * @param id * @return */ @AutoLog(value = "lims_testing_batch-通过id删除") @ApiOperation(value = "lims_testing_batch-通过id删除", notes = "lims_testing_batch-通过id删除") @DeleteMapping(value = "/delete") @Transactional public Result delete(@RequestParam(name = "id", required = true) String id) { limsTestingBatchService.removeById(id); //删除批次时还需删除批次下生成的数据 //1.删除批次检验结果记录 QueryWrapper resultQueryWrapper = new QueryWrapper<>(); resultQueryWrapper.lambda().eq(LimsBatchResult::getBatchId, id); limsBatchResultService.remove(resultQueryWrapper); //2.删除批次检验结果记录明细 QueryWrapper detailQueryWrapper = new QueryWrapper<>(); detailQueryWrapper.lambda().eq(LimsBatchDetail::getBatchId, id); limsBatchDetailService.remove(detailQueryWrapper); //3.删除批次下生成样本数据 QueryWrapper sampleQueryWrapper = new QueryWrapper<>(); sampleQueryWrapper.lambda().eq(LimsBatchSample::getBatchId, id); limsBatchSampleService.remove(sampleQueryWrapper); return Result.OK("删除成功!"); } /** * 批量删除 * * @param ids * @return */ @AutoLog(value = "lims_testing_batch-批量删除") @ApiOperation(value = "lims_testing_batch-批量删除", notes = "lims_testing_batch-批量删除") @DeleteMapping(value = "/deleteBatch") @Transactional public Result deleteBatch(@RequestParam(name = "ids", required = true) String ids) { List idList = Arrays.asList(ids.split(",")); this.limsTestingBatchService.removeByIds(idList); //删除批次时还需删除批次下生成的数据 //1.删除批次检验结果记录 QueryWrapper resultQueryWrapper = new QueryWrapper<>(); resultQueryWrapper.lambda().in(LimsBatchResult::getBatchId, idList); limsBatchResultService.remove(resultQueryWrapper); //2.删除批次检验结果记录明细 QueryWrapper detailQueryWrapper = new QueryWrapper<>(); detailQueryWrapper.lambda().in(LimsBatchDetail::getBatchId, idList); limsBatchDetailService.remove(detailQueryWrapper); //3.删除批次下生成样本数据 QueryWrapper sampleQueryWrapper = new QueryWrapper<>(); sampleQueryWrapper.lambda().in(LimsBatchSample::getBatchId, idList); limsBatchSampleService.remove(sampleQueryWrapper); return Result.OK("批量删除成功!"); } /** * 通过id查询 * * @param id * @return */ @AutoLog(value = "lims_testing_batch-通过id查询") @ApiOperation(value = "lims_testing_batch-通过id查询", notes = "lims_testing_batch-通过id查询") @GetMapping(value = "/queryById") public Result queryById(@RequestParam(name = "id", required = true) String id) { LimsTestingBatch limsTestingBatch = limsTestingBatchService.getById(id); if (limsTestingBatch == null) { return Result.error("未找到对应数据"); } return Result.OK(limsTestingBatch); } /** * 导出excel * * @param request * @param limsTestingBatch */ @RequestMapping(value = "/exportXls") public ModelAndView exportXls(HttpServletRequest request, LimsTestingBatch limsTestingBatch) { return super.exportXls(request, limsTestingBatch, LimsTestingBatch.class, "lims_testing_batch"); } /** * 通过excel导入数据 * * @param request * @param response * @return */ @RequestMapping(value = "/importExcel", method = RequestMethod.POST) public Result importExcel(HttpServletRequest request, HttpServletResponse response) { return super.importExcel(request, response, LimsTestingBatch.class); } }