From 4df8eb13cad992716c781d2792895fad9ae1ab18 Mon Sep 17 00:00:00 2001 From: lijiaqi Date: Thu, 12 Sep 2024 09:39:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BC=96=E7=A0=81=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E4=B8=AD=E6=96=B0=E5=A2=9E=E5=BA=8F=E5=88=97=E5=8F=B7?= =?UTF-8?q?=E7=94=9F=E6=88=90=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增序列号生成服务,支持三种不同的编码规则: 1. 日期+递增数字(年月日或年月)。 2. 随机生成的8位序列号,包含大写字母和数字。 3. 默认规则,自增序列号。 实现具有不同前缀和后缀的序列号生成,包括年月日或年月作为前缀,校验位作为后缀。 --- .../module/drone/coding/dto/CodeDto.java | 23 +++++++++++++ .../drone/coding/service/BatchesService.java | 27 ++++++++++++++-- .../drone/coding/service/CodeService.java | 32 +++++++++++++++++++ .../drone/coding/wrapper/CodeWrapper.java | 30 +++++++++++++++++ 4 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 src/main/java/cn/workde/module/drone/coding/dto/CodeDto.java create mode 100644 src/main/java/cn/workde/module/drone/coding/wrapper/CodeWrapper.java diff --git a/src/main/java/cn/workde/module/drone/coding/dto/CodeDto.java b/src/main/java/cn/workde/module/drone/coding/dto/CodeDto.java new file mode 100644 index 0000000..49a2bcf --- /dev/null +++ b/src/main/java/cn/workde/module/drone/coding/dto/CodeDto.java @@ -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; +} diff --git a/src/main/java/cn/workde/module/drone/coding/service/BatchesService.java b/src/main/java/cn/workde/module/drone/coding/service/BatchesService.java index 0f27899..a60490c 100644 --- a/src/main/java/cn/workde/module/drone/coding/service/BatchesService.java +++ b/src/main/java/cn/workde/module/drone/coding/service/BatchesService.java @@ -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 { @Resource @Lazy private CodeService codeService; + @Autowired + private RuleService ruleService; public Result addBatches(BatchesVo vo) { Long count = this.getBaseMapper().selectCount(new QueryWrapper() @@ -234,5 +236,26 @@ public class BatchesService extends BaseService { 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("操作成功"); + } + } diff --git a/src/main/java/cn/workde/module/drone/coding/service/CodeService.java b/src/main/java/cn/workde/module/drone/coding/service/CodeService.java index 2abb2b6..837b2ba 100644 --- a/src/main/java/cn/workde/module/drone/coding/service/CodeService.java +++ b/src/main/java/cn/workde/module/drone/coding/service/CodeService.java @@ -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{ 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{ } } + public Result getAttributeValueDetail(String code) { + if (StrUtil.isBlank(code)) throw new ResultException("参数错误"); + // 获取批次 + Code batches = getOne(new QueryWrapper() + .lambda() + .eq(Code::getCode, code) + ); + if (ObjUtil.isNull(batches)) throw new ResultException("无人机编码不存在"); + return batchesService.getAttributeValueDetail(batches.getBatchesId()); + } + + public Result> getCodePageList(Page page, String batchesId, String mfcId, String ruleId, String code) { + Page paged = page(page, new QueryWrapper() + .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); + } + } diff --git a/src/main/java/cn/workde/module/drone/coding/wrapper/CodeWrapper.java b/src/main/java/cn/workde/module/drone/coding/wrapper/CodeWrapper.java new file mode 100644 index 0000000..dc7a81b --- /dev/null +++ b/src/main/java/cn/workde/module/drone/coding/wrapper/CodeWrapper.java @@ -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 { + 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())); + } + +}