数据编码服务中新增序列号生成逻辑

新增序列号生成服务,支持三种不同的编码规则:
1. 日期+递增数字(年月日或年月)。
2. 随机生成的8位序列号,包含大写字母和数字。
3. 默认规则,自增序列号。

实现具有不同前缀和后缀的序列号生成,包括年月日或年月作为前缀,校验位作为后缀。
This commit is contained in:
lijiaqi 2024-09-12 09:39:19 +08:00
parent 6e6e9e321a
commit 4df8eb13ca
4 changed files with 110 additions and 2 deletions

View File

@ -0,0 +1,23 @@
package cn.workde.module.drone.coding.dto;
import cn.workde.module.drone.coding.entity.Batches;
import cn.workde.module.drone.coding.entity.Code;
import cn.workde.module.drone.coding.entity.Mfc;
import cn.workde.module.drone.coding.entity.Rule;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
public class CodeDto extends Code {
@Schema(description = "批次")
private Batches batches;
@Schema(description = "MFC")
private Mfc mfc;
@Schema(description = "规则")
private Rule rule;
}

View File

@ -5,21 +5,21 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.workde.core.base.BaseService;
import cn.workde.core.data.dto.OptionResult;
import cn.workde.core.data.dto.Result;
import cn.workde.module.drone.coding.dto.AttributeValueDto;
import cn.workde.module.drone.coding.dto.BatchesDto;
import cn.workde.module.drone.coding.entity.*;
import cn.workde.module.drone.coding.mapper.BatchesMapper;
import cn.workde.module.drone.coding.vo.AttributeValueVo;
import cn.workde.module.drone.coding.vo.AttributeValueVo2;
import cn.workde.module.drone.coding.vo.BatchesVo;
import cn.workde.module.drone.coding.wrapper.BatchesWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
@ -56,6 +56,8 @@ public class BatchesService extends BaseService<BatchesMapper, Batches> {
@Resource
@Lazy
private CodeService codeService;
@Autowired
private RuleService ruleService;
public Result addBatches(BatchesVo vo) {
Long count = this.getBaseMapper().selectCount(new QueryWrapper<Batches>()
@ -234,5 +236,26 @@ public class BatchesService extends BaseService<BatchesMapper, Batches> {
return flag ? Result.success("操作成功") : Result.fail("操作失败");
}
@Transactional(rollbackFor = Exception.class)
public Result generateCode(String batchesId, Integer count) {
if (ObjUtil.isNull(batchesId)) {
return Result.fail("批次ID不能为空");
}
if (ObjUtil.isNull(count)) {
return Result.fail("生成数量不能为空");
}
if (count < 1){
return Result.fail("生成数量不能小于1");
}
Batches batches = getById(batchesId);
if (ObjUtil.isNull(batches)) {
return Result.fail("批次不存在");
}
Mfc mfc = mfcService.getById(batches.getMfcId());
Rule rule = ruleService.getById(batches.getRuleId());
codeService.generateCode(batchesId, batches.getBatchesCode(), mfc.getId(), mfc.getMfcCode(), rule.getId(), rule.getRuleCode(), count);
return Result.success("操作成功");
}
}

View File

@ -2,11 +2,18 @@ package cn.workde.module.drone.coding.service;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.workde.core.base.BaseService;
import cn.workde.core.data.dto.Result;
import cn.workde.core.exception.ResultException;
import cn.workde.module.drone.coding.dto.AttributeValueDto;
import cn.workde.module.drone.coding.dto.CodeDto;
import cn.workde.module.drone.coding.entity.Code;
import cn.workde.module.drone.coding.mapper.CodeMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -25,6 +32,10 @@ public class CodeService extends BaseService<CodeMapper, Code>{
private static final String VALID_CHARS = UPPER_CASE_LETTERS + NUMBERS;
private static final SecureRandom random = new SecureRandom();
@Resource
@Lazy
private BatchesService batchesService;
public static String generateSNCode(int totalLength, int upperCaseCount, int numberCount) {
if (totalLength < upperCaseCount + numberCount) {
throw new IllegalArgumentException("Total length must be greater than or equal to the sum of upperCaseCount and numberCount.");
@ -222,6 +233,27 @@ public class CodeService extends BaseService<CodeMapper, Code>{
}
}
public Result<AttributeValueDto> getAttributeValueDetail(String code) {
if (StrUtil.isBlank(code)) throw new ResultException("参数错误");
// 获取批次
Code batches = getOne(new QueryWrapper<Code>()
.lambda()
.eq(Code::getCode, code)
);
if (ObjUtil.isNull(batches)) throw new ResultException("无人机编码不存在");
return batchesService.getAttributeValueDetail(batches.getBatchesId());
}
public Result<Page<CodeDto>> getCodePageList(Page page, String batchesId, String mfcId, String ruleId, String code) {
Page<Code> paged = page(page, new QueryWrapper<Code>()
.lambda()
.eq(StrUtil.isNotBlank(batchesId), Code::getBatchesId, batchesId)
.eq(StrUtil.isNotBlank(mfcId), Code::getMfcId, mfcId)
.eq(StrUtil.isNotBlank(ruleId), Code::getRuleId, ruleId)
.like(StrUtil.isNotBlank(code), Code::getCode, code));
return Result.data(null);
}
}

View File

@ -0,0 +1,30 @@
package cn.workde.module.drone.coding.wrapper;
import cn.hutool.extra.spring.SpringUtil;
import cn.workde.core.base.BaseWrapper;
import cn.workde.module.drone.coding.dto.CodeDto;
import cn.workde.module.drone.coding.entity.Code;
import cn.workde.module.drone.coding.service.BatchesService;
import cn.workde.module.drone.coding.service.MfcService;
import cn.workde.module.drone.coding.service.RuleService;
import org.apache.ibatis.annotations.Param;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
import org.mapstruct.factory.Mappers;
@Mapper
public interface CodeWrapper extends BaseWrapper<Code, CodeDto> {
CodeWrapper INSTANCE = Mappers.getMapper(CodeWrapper.class);
@AfterMapping
default void setOtherField(@Param("obj") Code obj, @MappingTarget @Param("dto") CodeDto dto) {
BatchesService batchesService = SpringUtil.getBean(BatchesService.class);
dto.setBatches(batchesService.getById(obj.getBatchesId()));
MfcService mfcService = SpringUtil.getBean(MfcService.class);
dto.setMfc(mfcService.getById(obj.getMfcId()));
RuleService ruleService = SpringUtil.getBean(RuleService.class);
dto.setRule(ruleService.getById(obj.getRuleId()));
}
}