package jnpf.limsService.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import jnpf.base.service.SuperServiceImpl; import jnpf.enums.LimsSampleEnum; import jnpf.limsEntity.*; import jnpf.limsMapper.LimsApplyTestMapper; import jnpf.limsService.*; import jnpf.util.DateUtil; import jnpf.util.StringUtil; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RequestBody; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; @Service public class LimsApplyTestServiceImpl extends SuperServiceImpl implements LimsApplyTestService { @Autowired private LimsApplyTestItemService limsApplyTestItemService; @Autowired private LimsSampleInfoService limsSampleInfoService; @Autowired private LimsSamplingRecordService limsSamplingRecordService; @Autowired private LimsTestTaskService limsTestTaskService; @Autowired private JdbcTemplate jdbcTemplate; /** * 根据检验信息生成样品信息 * @param limsApplyTestEntity */ @Override @Transactional(rollbackFor = Exception.class) public void createSampleInfo(LimsApplyTestEntity limsApplyTestEntity) { LimsSampleInfoEntity limsSampleInfoEntity = new LimsSampleInfoEntity(); LimsSamplingRecordEntity limsSamplingRecordEntity = new LimsSamplingRecordEntity(); // 获取样品编号 String sampleCode = creatSampleCode(); limsApplyTestEntity.setSampleCode(sampleCode); // 查询sampleId 是否有值 ,避免重复生成 String sampleId = limsApplyTestEntity.getSampleId(); if (StringUtil.isNotEmpty(sampleId)) { limsSampleInfoEntity = limsSampleInfoService.getById(sampleId); } // 复制信息 BeanUtils.copyProperties(limsApplyTestEntity, limsSampleInfoEntity,"fId"); BeanUtils.copyProperties(limsApplyTestEntity, limsSamplingRecordEntity,"fId"); // 回写请检ID limsSampleInfoEntity.setApplyId(limsApplyTestEntity.getId()); //获取对应流程信息 LimsSampleEnum sampleRecord = LimsSampleEnum.SAMPLE_RECORD; // 请检已完成 状态为待取样 limsSampleInfoEntity.setSampleStatus(String.valueOf(sampleRecord.getCode())); //limsSamplingRecordEntity.setApplyId(limsApplyTestEntity.getFId()); // 保存对应信息 limsSampleInfoService.save(limsSampleInfoEntity); // 回写SampleId limsSamplingRecordEntity.setSampleId(limsSampleInfoEntity.getId()); limsSamplingRecordService.save(limsSamplingRecordEntity); // 请检已完成 回写SampleId limsApplyTestEntity.setSampleId(limsSampleInfoEntity.getId()); limsApplyTestEntity.setApplyStatus("2"); this.updateById(limsApplyTestEntity); // 生成对应的检验任务 LambdaQueryWrapper itemQueryWrapper = new LambdaQueryWrapper<>(); itemQueryWrapper.eq(LimsApplyTestItemEntity::getFForeignId, limsApplyTestEntity.getId()); // 获取检验项目信息 List itemList = limsApplyTestItemService.list(itemQueryWrapper); // 赋值 List taskEntityList = itemList.stream().map(item -> { LimsTestTaskEntity limsTestTaskEntity = new LimsTestTaskEntity(); BeanUtils.copyProperties(item, limsTestTaskEntity, "id"); BeanUtils.copyProperties(limsApplyTestEntity, limsTestTaskEntity, "id"); limsTestTaskEntity.setCheckStatus(String.valueOf(sampleRecord.getItemCode())); return limsTestTaskEntity; }).collect(Collectors.toList()); // 将 null 值排在最后面 taskEntityList = taskEntityList.stream() .sorted(Comparator.nullsLast( Comparator.comparing(LimsTestTaskEntity::getItemSort))) .collect(Collectors.toList()); // 保存信息 limsTestTaskService.saveBatch(taskEntityList); } public String creatSampleCode() { String sql = "SELECT MAX(SAMPLE_CODE) FROM lims_sample_info"; String maxCode; try { maxCode = jdbcTemplate.queryForObject(sql, String.class); } catch (EmptyResultDataAccessException e) { maxCode = null; } // 获取年份 String year = DateUtil.getNow().split("-")[0]; if (StringUtil.isEmpty(maxCode)) { maxCode = "SAMPLE-" + year + "-001"; } else { // 判断是否为新的一年 if (year.equals(maxCode.split("-")[1])){ maxCode = "SAMPLE-" + year + "-" + String.format("%03d", Integer.parseInt(maxCode.split("-")[2] + 1)); } else { maxCode = "SAMPLE-" + year + "-001"; } } return maxCode; } }