Commit 5ceb20e7 by lijingjing

样品编号可保留备用功能;

parent 9f5600cd
package com.patzn.cloud.service.lims.hmhj.service;
import com.baomidou.mybatisplus.plugins.Page;
import com.patzn.cloud.commons.controller.Account;
import com.patzn.cloud.commons.service.IBaseService;
import com.patzn.cloud.service.BaseEntity;
import com.patzn.cloud.service.hmhj.entity.CodeRule;
import com.patzn.cloud.service.hmhj.enums.CodeTypeEnum;
import com.patzn.cloud.service.lims.base.dto.CodeTypeDTO;
import java.util.List;
......@@ -25,4 +27,6 @@ public interface ICodeRuleService extends IBaseService<CodeRule> {
String getKey(Integer codeType, String codeName, BaseEntity entity);
String getKey(String prefix, String rule, BaseEntity entity);
void recycleCode(CodeTypeEnum codeTypeEnum, List<String> codeList, Account account);
}
......@@ -2,10 +2,13 @@ package com.patzn.cloud.service.lims.hmhj.service.impl;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.SqlRunner;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.patzn.cloud.commons.api.RestAssert;
import com.patzn.cloud.commons.constant.CommonConstants;
import com.patzn.cloud.commons.controller.Account;
import com.patzn.cloud.commons.service.impl.BaseServiceImpl;
import com.patzn.cloud.commons.toolkit.CodeKeyUtils;
import com.patzn.cloud.commons.toolkit.FreemarkerUtils;
......@@ -14,21 +17,25 @@ import com.patzn.cloud.feign.lims.base.client.LmsBaseDictClient;
import com.patzn.cloud.service.BaseEntity;
import com.patzn.cloud.service.CodeRuleHelper;
import com.patzn.cloud.service.hmhj.entity.CodeRule;
import com.patzn.cloud.service.hmhj.entity.CodeRuleRecord;
import com.patzn.cloud.service.hmhj.enums.CodeTypeEnum;
import com.patzn.cloud.service.lims.base.dto.CodeTypeDTO;
import com.patzn.cloud.service.lims.base.entity.LmsBaseDict;
import com.patzn.cloud.service.lims.base.entity.LmsCodeRuleRecord;
import com.patzn.cloud.service.lims.common.StringHandleUtils;
import com.patzn.cloud.service.lims.hmhj.mapper.CodeRuleMapper;
import com.patzn.cloud.service.lims.hmhj.service.ICodeRuleRecordService;
import com.patzn.cloud.service.lims.hmhj.service.ICodeRuleService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* 编号规则 服务实现类
......@@ -178,4 +185,128 @@ public class CodeRuleServiceImpl extends BaseServiceImpl<CodeRuleMapper, CodeRul
RestAssert.fail(null == codeRule, "隐性编号规则不存在");
return getCodeKey(codeRule, "隐性规则" + prefix, entity);
}
private static CodeRule getCodeRule(Map<String, Object> map) {
if (map != null && null != map.get("rule") && null != map.get("length")) {
CodeRule rule = new CodeRule();
rule.setCompanyId((Long) map.get("company_id"));
rule.setContinueSn((Integer) map.get("continue_sn"));
rule.setCtime((Date) map.get("ctime"));
rule.setIgnoreNum((String) map.get("ignore_num"));
rule.setLength((Integer) map.get("length"));
rule.setPre((String) map.get("pre"));
rule.setRepeat((Integer) map.get("repeat"));
rule.setRule((String) map.get("rule"));
rule.setSn((Integer) map.get("sn"));
rule.setSublength((Integer) map.get("sublength"));
rule.setType(CodeTypeEnum.get((Integer) map.get("type")));
rule.setId((Long) map.get("id"));
return rule;
} else {
return null;
}
}
private static CodeRuleRecord getCodeRuleRecord(Map<String, Object> map) {
if (map == null) {
return null;
}
CodeRuleRecord record = new CodeRuleRecord();
record.setCompanyId((Long) map.get("company_id"));
record.setCtime((Date) map.get("ctime"));
record.setSn((Integer) map.get("sn"));
record.setId((Long) map.get("id"));
record.setCodeRuleId((Long) map.get("code_rule_id"));
record.setVary((String) map.get("vary"));
return record;
}
@Override
public void recycleCode(CodeTypeEnum codeTypeEnum, List<String> codeList, Account account) {
codeList.removeIf(StringUtils::isBlank);
if (CollectionUtils.isEmpty(codeList)) {
return;
}
// 根据编号规则枚举查询已经存在的编号前缀
List<Map<String, Object>> lmsCodeList;
lmsCodeList = SqlRunner.db().selectList("select * from code_rule where type = {0} and company_id = {1}",
codeTypeEnum.getValue(), account.getCompanyId());
RestAssert.fail(CollectionUtils.isEmpty(lmsCodeList), "未配置编号规则");
// 将 map 转换为编号规则对象
List<CodeRule> codeRuleList = lmsCodeList.stream().map(CodeRuleServiceImpl::getCodeRule)
.collect(Collectors.toList());
// 将编号规则的 id 和对象本身组成一个 map
Map<Long, CodeRule> ruleMap = codeRuleList.stream()
.collect(Collectors.toMap(CodeRule::getId, Function.identity()));
List<Long> codeIdList = new ArrayList<>();
codeRuleList.forEach((lmsCode -> codeIdList.add(lmsCode.getId())));
// 查询编号记录表
List<Map<String, Object>> recordList = SqlRunner.db().
selectList("select * from code_rule_record where company_id = {0} and code_rule_id in {1} and vary like '%#SN#'",
account.getCompanyId(), codeIdList);
if (CollectionUtils.isEmpty(recordList)) {
// 如果没有配置顺序号,不予处理
return;
}
// 将map转换成编号记录的实体的list
List<CodeRuleRecord> ruleRecordList = recordList.stream().map(CodeRuleServiceImpl::getCodeRuleRecord)
.collect(Collectors.toList());
// 创建一个map,因为vary部分可能有多个
Map<String, List<Integer>> codeMap = new HashMap<>(16);
Map<String, Long> varyIdMap = new HashMap<>(16);
for (String code : codeList) {
// 内层遍历编号记录
Iterator<CodeRuleRecord> iterator = ruleRecordList.iterator();
while (iterator.hasNext()) {
// 数据库中的编号记录
CodeRuleRecord codeRuleRecord = iterator.next();
// 这是数据库中存储的编号前缀
String codeRulePrefix = codeRuleRecord.getVary().replace("#SN#", "");
// 先将实际编号后面的 sn 的长度删除, 删除之后就是实际编号的前缀了
CodeRule lmsCodeRule = ruleMap.get(codeRuleRecord.getCodeRuleId());
// tmpCode 就是实际的编号
String tmpCode = code;
if (lmsCodeRule != null) {
// length 是编号规则中配置的长度字段
Integer length = lmsCodeRule.getLength();
if (length != null) {
if (tmpCode.length() - length <= 0) {
// 如果实际编号长度减去配置的编号长度 <= 0, 说明编号的前缀极有可能不存在, 所以没有回收依据, 不予回收
continue;
}
tmpCode = tmpCode.substring(0, tmpCode.length() - length);
}
}
// 这里的 tmpCode 就是实际编号的前缀了
// 判断删除顺序号后的编号和编号前缀是否相同
if (Objects.equals(tmpCode, codeRulePrefix)
|| (!"".equals(codeRulePrefix) && tmpCode.startsWith(codeRulePrefix))) {
// 相同
String sn = code.replace(codeRulePrefix, "");
if (NumberUtils.isDigits(sn)) {
if (codeMap.containsKey(codeRulePrefix)) {
codeMap.get(codeRulePrefix).add(Integer.valueOf(sn));
} else {
List<Integer> list = new ArrayList<>();
list.add(Integer.valueOf(sn));
codeMap.put(codeRulePrefix, list);
}
// 放入 map
varyIdMap.put(codeRulePrefix, codeRuleRecord.getCodeRuleId());
}
// 下一层外循环
break;
}
}
}
codeMap.forEach((vary, snList) -> {
Collections.sort(snList);
LmsCodeRuleRecord record = new LmsCodeRuleRecord();
record.setCodeRuleId(varyIdMap.get(vary));
record.setVary(vary + "#SN#");
record.setSn(snList.get(0) - 1);
SqlRunner.db().update("update code_rule_record set sn = {0} where code_rule_id = {1} and vary = {2}",
snList.get(0) - 1, varyIdMap.get(vary), vary + "#SN#");
});
}
}
......@@ -62,6 +62,9 @@ import java.util.stream.Collectors;
public class EntrustSampleServiceImpl extends BaseServiceImpl<EntrustSampleMapper, EntrustSample> implements IEntrustSampleService {
@Autowired
private ICodeRuleService codeRuleService;
@Autowired
private SysFileTemplateClient sysFileTemplateClient;
@Autowired
......@@ -671,6 +674,14 @@ public class EntrustSampleServiceImpl extends BaseServiceImpl<EntrustSampleMappe
@Override
public boolean removeByIds(List<Long> ids) {
entrustSampleItemService.remove(Condition.create().in("entrust_sample_id", ids));
// 查询要删除的样品编号
List<EntrustSample> samples = super.getBatchIds(ids);
List<String> sampleCodes = samples.stream().map(EntrustSample::getCode).collect(Collectors.toList());
Integer codeRuleType = samples.get(0).getCodeRuleType();
// 不为空的情下触发编号回收机制
if (CollectionUtils.isNotEmpty(sampleCodes) && null != codeRuleType) {
codeRuleService.recycleCode(CodeTypeEnum.get(codeRuleType), sampleCodes, LoginHelper.getAccount());
}
return baseMapper.deleteBatchIds(ids) > 0;
}
......@@ -2259,13 +2270,27 @@ public class EntrustSampleServiceImpl extends BaseServiceImpl<EntrustSampleMappe
if (CollectionUtils.isEmpty(ids)) {
return true;
}
// 删除备样
entrustSampleBackupService.deletePhysicalByEntrustIds(ids);
// ==
List<EntrustSample> sampleList = list(Condition.create().in("entrust_id", ids));
if (CollectionUtils.isEmpty(sampleList)) {
return true;
}
ids.forEach(id -> {
// 只查询当前委托下的样品
List<EntrustSample> samples = sampleList.stream().filter(t -> Objects.equals(t.getEntrustId(), id)).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(samples)) {
List<String> sampleCodes = samples.stream().map(EntrustSample::getCode).collect(Collectors.toList());
Integer codeRuleType = samples.get(0).getCodeRuleType();
if (null != codeRuleType && org.apache.commons.collections.CollectionUtils.isNotEmpty(sampleCodes)) {
codeRuleService.recycleCode(CodeTypeEnum.get(codeRuleType), sampleCodes, LoginHelper.getAccount());
}
}
});
// 删除备样
entrustSampleBackupService.deletePhysicalByEntrustIds(ids);
List<Long> sampleIds = sampleList.stream().map(EntrustSample::getId).collect(Collectors.toList());
// 删除样品接收信息
entrustSamplePrepareService.deletePhysicalBySampleIds(sampleIds);
......
......@@ -138,6 +138,22 @@ public class EntrustServiceImpl extends BaseServiceImpl<EntrustMapper, Entrust>
@Override
public boolean removeByIds(List<Long> ids) {
if (CollectionUtils.isEmpty(ids)) {
return false;
}
ids.forEach(id -> {
List<EntrustSample> samples = entrustSampleService.list(Condition.create().eq("entrust_id", id));
if (CollectionUtils.isNotEmpty(samples)) {
List<String> sampleCodes = samples.stream().map(EntrustSample::getCode).collect(Collectors.toList());
Integer codeRuleType = samples.get(0).getCodeRuleType();
if (null != codeRuleType && CollectionUtils.isNotEmpty(sampleCodes)) {
codeRuleService.recycleCode(CodeTypeEnum.get(codeRuleType), sampleCodes, LoginHelper.getAccount());
}
}
});
entrustSampleService.remove(Condition.create().in("entrust_id", ids));
entrustSampleItemService.remove(Condition.create().in("entrust_id", ids));
return baseMapper.deleteBatchIds(ids) > 0;
}
......@@ -242,7 +258,10 @@ public class EntrustServiceImpl extends BaseServiceImpl<EntrustMapper, Entrust>
if (HmConst.EXIST_CODE_MATERIALS.equals(parentCategory)) {
sampleCode = sample.getCode();
} else {
sampleCode = codeRuleService.getKey((Integer) codeType.getValue(), codeType.getName(), sample);
Integer codeTypeVal = (Integer) codeType.getValue();
sampleCode = codeRuleService.getKey(codeTypeVal, codeType.getName(), sample);
// 生成编号,赋予编号规则类型
sample.setCodeRuleType(codeTypeVal);
// 若为外委检测和三级编码,则查询质检编号
if (CodeTypeEnum.SAMPLE_WWJC_CODE == codeType || CodeTypeEnum.SAMPLE_YFL_CODE == codeType) {
List<UserInspectionCode> userInspectionCodeList = userInspectionCodeService.list(Condition.create().eq("user_id", account.getUserId()).eq("type", "ZJ").eq("deleted", 0));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment