package org.jeecg.modules.lims.testing.controller; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; 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.modules.lims.testing.dto.LimsItemTargetDTO; import org.jeecg.modules.lims.testing.entity.*; import org.jeecg.modules.lims.testing.service.*; 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.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; /** * @Description: lims_testing_standard * @Author: jeecg-boot * @Date: 2023-04-24 * @Version: V1.0 */ @Api(tags = "lims_testing_standard") @RestController @RequestMapping("/lims/testing/stand") @Slf4j public class LimsTestingStandardController extends JeecgController { @Autowired private ILimsTestingStandardService limsTestingStandardService; @Autowired private ILimsTestingCategoryService limsTestingCategoryService; @Autowired private ILimsTestingTypeService limsTestingTypeService; @Autowired private ILimsTestingItemService limsTestingItemService; @Autowired private ILimsItemTargetService limsItemTargetService; @Autowired private ILimsTestingOutlineService outlineService; /** * 分页列表查询 * * @param limsTestingStandard * @param pageNo * @param pageSize * @param req * @return */ @AutoLog(value = "lims_testing_standard-分页列表查询") @ApiOperation(value = "lims_testing_standard-分页列表查询", notes = "lims_testing_standard-分页列表查询") @GetMapping(value = "/list") public Result queryPageList(LimsTestingStandard limsTestingStandard, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) { QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(limsTestingStandard, req.getParameterMap()); Page page = new Page(pageNo, pageSize); IPage pageList = limsTestingStandardService.page(page, queryWrapper); return Result.OK(pageList); } /** * 添加 * * @param limsTestingStandard * @return */ @AutoLog(value = "lims_testing_standard-添加") @ApiOperation(value = "lims_testing_standard-添加", notes = "lims_testing_standard-添加") @PostMapping(value = "/add") @Transactional public Result add(@RequestBody LimsTestingStandard limsTestingStandard) { limsTestingStandardService.save(limsTestingStandard); //添加时,将所有鉴定大纲项添加为技术标准,后续修改启用禁用字段 String outlineId = limsTestingStandard.getOutlineId(); if (StringUtils.isEmpty(outlineId)) return Result.OK("添加成功!"); addStandItmTarget(limsTestingStandard); // return Result.OK("添加成功!"); } /** * 添加技术标准 item */ private List addStandItmTarget(LimsTestingStandard limsTestingStandard) { //1.根据鉴定大纲outlineId查询cat QueryWrapper catWrapper = new QueryWrapper<>(); catWrapper.lambda().eq(LimsTestingCategory::getOutlineId, limsTestingStandard.getOutlineId()); List catList = limsTestingCategoryService.list(catWrapper); List catIds = catList.stream().map(item -> item.getId()).collect(Collectors.toList()); if (catIds.isEmpty()) { catIds.add("empty"); } //2.根据cat查询tye QueryWrapper typWrapper = new QueryWrapper<>(); typWrapper.lambda().in(LimsTestingType::getCategoryId, catIds); List typList = limsTestingTypeService.list(typWrapper); List typIds = typList.stream().map(item -> item.getId()).collect(Collectors.toList()); if (typIds.isEmpty()) { typIds.add("empty"); } //3.根据typ查询父类itm QueryWrapper itmWrapper = new QueryWrapper<>(); itmWrapper.lambda().in(LimsTestingItem::getTypeId, typIds); List itmList = limsTestingItemService.list(itmWrapper); List itmIds = itmList.stream().map(item -> item.getId()).collect(Collectors.toList()); //4.组装insert数据 List targetDTOSList = itmList.stream().map(item -> { //组装ITM LimsItemTargetDTO t = new LimsItemTargetDTO(); t.setItemId(item.getId()); t.setItemName(item.getName()); t.setPid(item.getTypeId()); t.setEnabled(0); t.setIsTech(0); t.setStandardId(limsTestingStandard.getId()); t.setOutlineId(limsTestingStandard.getOutlineId()); return t; }).map(item -> { //组装TYP List selTypList = typList.stream().filter(i -> i.getId().equals(item.getPid())).collect(Collectors.toList()); LimsTestingType type = selTypList.get(0); item.setTypeId(type.getId()); item.setPpid(type.getCategoryId()); return item; }).map(item -> { //组装CAT List selCatList = catList.stream().filter(i -> i.getId().equals(item.getPpid())).collect(Collectors.toList()); LimsTestingCategory cat = selCatList.get(0); item.setCategoryId(cat.getId()); return item; }).collect(Collectors.toList()); //dto转entity List list = targetDTOSList.stream().map(item -> { LimsItemTarget target = new LimsItemTarget(); BeanUtils.copyProperties(item, target); return target; }).collect(Collectors.toList()); limsItemTargetService.saveBatch(list); return list; } /** * 编辑 * * @param stand * @return */ @AutoLog(value = "lims_testing_standard-编辑") @ApiOperation(value = "lims_testing_standard-编辑", notes = "lims_testing_standard-编辑") @PutMapping(value = "/edit") @Transactional public Result edit(@RequestBody LimsTestingStandard stand) { //未发布且修改技术标准大纲时才更新技术标准的大纲内容 //1.查询大纲是否发生更改 LimsTestingStandard ostand = limsTestingStandardService.getById(stand.getId()); if (ostand.getOutlineId() != null && stand.getOutlineId() != null && stand.getStatus() == 0 && !ostand.getOutlineId().equals(stand.getOutlineId())) { //2.大纲更改,删除历史生成 item_target 记录 QueryWrapper targetQueryWrapper = new QueryWrapper<>(); targetQueryWrapper.lambda().eq(LimsItemTarget::getStandardId, ostand.getId()); // 旧的指标 List oldList = limsItemTargetService.list(targetQueryWrapper); limsItemTargetService.remove(targetQueryWrapper); //3.根据新大纲添加技术标准 addStandItmTarget(stand); // 新的指标 List newList = limsItemTargetService.list(targetQueryWrapper); // 拷贝编码或名称相同的指标 for (LimsItemTarget oItem : oldList) { if (oItem.getIsTech() == 1) { newList.add(oItem); } else { for (LimsItemTarget item : newList) { if ((item.getCode() != null && item.getCode().equals(oItem.getCode())) || (item.getName() != null && item.getName().equals(oItem.getName())) || (item.getItemName() != null && item.getItemName().equals(oItem.getItemName()))) { LimsTestingType type = limsTestingTypeService.getById(item.getTypeId()); LimsTestingType oType = limsTestingTypeService.getById(oItem.getTypeId()); if ((type.getCode() != null && type.getCode().equals(oType.getCode())) || (type.getName() != null && type.getName().equals(oType.getName()))) { LimsTestingCategory cate = limsTestingCategoryService.getById(item.getCategoryId()); LimsTestingCategory oCate = limsTestingCategoryService.getById(oItem.getCategoryId()); if ((cate.getCode() != null && cate.getCode().equals(oCate.getCode())) || (cate.getName() != null && cate.getName().equals(oCate.getName()))) { item.setEnabled(oItem.getEnabled()); item.setIsJudge(oItem.getIsJudge()); item.setIsTech(oItem.getIsTech()); item.setMax(oItem.getMax()); item.setTyp(oItem.getTyp()); item.setMin(oItem.getMin()); item.setRemark(oItem.getRemark()); item.setUnit(oItem.getUnit()); item.setValType(oItem.getValType()); item.setVal(oItem.getVal()); item.setJudgment(oItem.getJudgment()); } } } } } } limsItemTargetService.saveOrUpdateBatch(newList); } //发布时查询当前技术标准的产品是否存在已发布版本,存在则更新为废止 if (stand.getStatus() == 1) { queryOldStand(stand); limsTestingStandardService.updateById(stand); } else if (stand.getStatus() == 2) { //生成新版时先查询该产品的大纲是否更新,更新需要重新根据新大纲生成技术标准 LimsTestingOutline oldOutline = outlineService.getById(stand.getOutlineId()); //如果大纲是启用状态,则直接拷贝,否则需要根据新大纲生成 if (oldOutline.getStatus() == 1 && oldOutline.getEnabled() == 1) { //生成新版本时需要复制大纲下所有数据 generateNewVersion(stand); } else { //查询在启用的新大纲 QueryWrapper newOutQuery = new QueryWrapper<>(); newOutQuery.lambda().eq(LimsTestingOutline::getStatus, 1); newOutQuery.lambda().eq(LimsTestingOutline::getEnabled, 1); LimsTestingOutline one = outlineService.getOne(newOutQuery); LimsTestingStandard newStand = new LimsTestingStandard(); BeanUtils.copyProperties(stand, newStand); newStand.setId(null); newStand.setStatus(0); newStand.setCreateTime(null); newStand.setCreateBy(null); newStand.setUpdateBy(null); newStand.setUpdateTime(null); if (stand.getVer() == null) { stand.setVer(1); } else { stand.setVer(stand.getVer() + 1); } newStand.setOutlineId(one.getId()); limsTestingStandardService.save(newStand); List newList = addStandItmTarget(newStand); //生成新大纲后拷贝已录入指标项 QueryWrapper targetQueryWrapper = new QueryWrapper<>(); targetQueryWrapper.lambda().eq(LimsItemTarget::getStandardId, stand.getId()); List oldList = limsItemTargetService.list(targetQueryWrapper); targetQueryWrapper.lambda().eq(LimsItemTarget::getStandardId, newStand.getId()); for (LimsItemTarget oItem : oldList) { for (LimsItemTarget item : newList) { if ((item.getCode() != null && item.getCode().equals(oItem.getCode())) || (item.getName() != null && item.getName().equals(oItem.getName())) || (item.getItemName() != null && item.getItemName().equals(oItem.getItemName()))) { LimsTestingType type = limsTestingTypeService.getById(item.getTypeId()); LimsTestingType oType = limsTestingTypeService.getById(oItem.getTypeId()); if ((type.getCode() != null && type.getCode().equals(oType.getCode())) || (type.getName() != null && type.getName().equals(oType.getName()))) { LimsTestingCategory cate = limsTestingCategoryService.getById(item.getCategoryId()); LimsTestingCategory oCate = limsTestingCategoryService.getById(oItem.getCategoryId()); if ((cate.getCode() != null && cate.getCode().equals(oCate.getCode())) || (cate.getName() != null && cate.getName().equals(oCate.getName()))) { item.setEnabled(oItem.getEnabled()); item.setIsJudge(oItem.getIsJudge()); item.setIsTech(oItem.getIsTech()); item.setMax(oItem.getMax()); item.setTyp(oItem.getTyp()); item.setMin(oItem.getMin()); item.setRemark(oItem.getRemark()); item.setUnit(oItem.getUnit()); item.setValType(oItem.getValType()); item.setVal(oItem.getVal()); item.setJudgment(oItem.getJudgment()); } } } } } limsItemTargetService.saveOrUpdateBatch(newList); } //生成新版本时不更新 技术标准,只执行复制数据, } else { limsTestingStandardService.updateById(stand); } return Result.OK("编辑成功!"); } /** * 查询产品(已发布)技术标准,并更新为废止状态 * * @param stand */ private void queryOldStand(LimsTestingStandard stand) { if (stand.getProdId() == null) return; QueryWrapper standardQueryWrapper = new QueryWrapper<>(); standardQueryWrapper.lambda().eq(LimsTestingStandard::getProdId, stand.getProdId()); //已发布状态 standardQueryWrapper.lambda().eq(LimsTestingStandard::getStatus, 1); List list = limsTestingStandardService.list(standardQueryWrapper); list.forEach(item -> { item.setStatus(2); limsTestingStandardService.updateById(item); }); } /** * 生成(新版)技术标准 * * @param stand */ private void generateNewVersion(LimsTestingStandard stand) { String id = stand.getId(); stand.setId(null); stand.setStatus(0); stand.setCreateTime(null); stand.setCreateBy(null); stand.setUpdateBy(null); stand.setUpdateTime(null); if (stand.getVer() == null) { stand.setVer(1); } else { stand.setVer(stand.getVer() + 1); } limsTestingStandardService.save(stand); QueryWrapper targetQueryWrapper = new QueryWrapper<>(); targetQueryWrapper.lambda().eq(LimsItemTarget::getStandardId, id); List targetList = limsItemTargetService.list(targetQueryWrapper); List tList = targetList.stream().map(item -> { item.setId(null); //设置新的id item.setStandardId(stand.getId()); return item; }).collect(Collectors.toList()); limsItemTargetService.saveBatch(tList); } /** * 通过id删除 * * @param id * @return */ @AutoLog(value = "lims_testing_standard-通过id删除") @ApiOperation(value = "lims_testing_standard-通过id删除", notes = "lims_testing_standard-通过id删除") @DeleteMapping(value = "/delete") @Transactional public Result delete(@RequestParam(name = "id", required = true) String id) { limsTestingStandardService.removeById(id); QueryWrapper targetQueryWrapper = new QueryWrapper<>(); targetQueryWrapper.lambda().eq(LimsItemTarget::getStandardId, id); limsItemTargetService.remove(targetQueryWrapper); return Result.OK("删除成功!"); } /** * 批量删除 * * @param ids * @return */ @AutoLog(value = "lims_testing_standard-批量删除") @ApiOperation(value = "lims_testing_standard-批量删除", notes = "lims_testing_standard-批量删除") @DeleteMapping(value = "/deleteBatch") @Transactional public Result deleteBatch(@RequestParam(name = "ids", required = true) String ids) { List list = Arrays.asList(ids.split(",")); this.limsTestingStandardService.removeByIds(list); for (int i = 0; i < list.size(); i++) { String id = list.get(i); QueryWrapper targetQueryWrapper = new QueryWrapper<>(); targetQueryWrapper.lambda().eq(LimsItemTarget::getStandardId, id); limsItemTargetService.remove(targetQueryWrapper); } return Result.OK("批量删除成功!"); } /** * 通过id查询 * * @param id * @return */ @AutoLog(value = "lims_testing_standard-通过id查询") @ApiOperation(value = "lims_testing_standard-通过id查询", notes = "lims_testing_standard-通过id查询") @GetMapping(value = "/queryById") public Result queryById(@RequestParam(name = "id", required = true) String id) { LimsTestingStandard limsTestingStandard = limsTestingStandardService.getById(id); if (limsTestingStandard == null) { return Result.error("未找到对应数据"); } return Result.OK(limsTestingStandard); } /** * 导出excel * * @param request * @param limsTestingStandard */ @RequestMapping(value = "/exportXls") public ModelAndView exportXls(HttpServletRequest request, LimsTestingStandard limsTestingStandard) { return super.exportXls(request, limsTestingStandard, LimsTestingStandard.class, "lims_testing_standard"); } /** * 通过excel导入数据 * * @param request * @param response * @return */ @RequestMapping(value = "/importExcel", method = RequestMethod.POST) public Result importExcel(HttpServletRequest request, HttpServletResponse response) { return super.importExcel(request, response, LimsTestingStandard.class); } }