diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/controller/SysCodeRuleController.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/controller/SysCodeRuleController.java new file mode 100644 index 00000000..e8c12f04 --- /dev/null +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/controller/SysCodeRuleController.java @@ -0,0 +1,127 @@ +package org.dromara.system.controller; + +import java.util.List; + +import lombok.RequiredArgsConstructor; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.validation.constraints.*; +import cn.dev33.satoken.annotation.SaCheckPermission; +import org.springframework.web.bind.annotation.*; +import org.springframework.validation.annotation.Validated; +import org.dromara.common.idempotent.annotation.RepeatSubmit; +import org.dromara.common.log.annotation.Log; +import org.dromara.common.web.core.BaseController; +import org.dromara.common.mybatis.core.page.PageQuery; +import org.dromara.common.core.domain.R; +import org.dromara.common.core.validate.AddGroup; +import org.dromara.common.core.validate.EditGroup; +import org.dromara.common.log.enums.BusinessType; +import org.dromara.common.excel.utils.ExcelUtil; +import org.dromara.system.domain.vo.SysCodeRuleVo; +import org.dromara.system.domain.bo.SysCodeRuleBo; +import org.dromara.system.service.ISysCodeRuleService; +import org.dromara.common.mybatis.core.page.TableDataInfo; + +/** + * 编码规则 + * 前端访问路由地址为:/system/codeRule + * + * @author Yinq + * @date 2025-02-11 + */ +@Validated +@RequiredArgsConstructor +@RestController +@RequestMapping("/codeRule") +public class SysCodeRuleController extends BaseController { + + private final ISysCodeRuleService sysCodeRuleService; + + /** + * 查询编码规则列表 + */ + @SaCheckPermission("system:codeRule:list") + @GetMapping("/list") + public TableDataInfo list(SysCodeRuleBo bo, PageQuery pageQuery) { + return sysCodeRuleService.queryPageList(bo, pageQuery); + } + + /** + * 导出编码规则列表 + */ + @SaCheckPermission("system:codeRule:export") + @Log(title = "编码规则", businessType = BusinessType.EXPORT) + @PostMapping("/export") + public void export(SysCodeRuleBo bo, HttpServletResponse response) { + List list = sysCodeRuleService.queryList(bo); + ExcelUtil.exportExcel(list, "编码规则", SysCodeRuleVo.class, response); + } + + /** + * 获取编码规则详细信息 + * + * @param codeRuleId 主键 + */ + @SaCheckPermission("system:codeRule:query") + @GetMapping("/{codeRuleId}") + public R getInfo(@NotNull(message = "主键不能为空") + @PathVariable Long codeRuleId) { + return R.ok(sysCodeRuleService.queryById(codeRuleId)); + } + + /** + * 新增编码规则 + */ + @SaCheckPermission("system:codeRule:add") + @Log(title = "编码规则", businessType = BusinessType.INSERT) + @RepeatSubmit() + @PostMapping() + public R add(@Validated(AddGroup.class) @RequestBody SysCodeRuleBo bo) { + return toAjax(sysCodeRuleService.insertByBo(bo)); + } + + /** + * 修改编码规则 + */ + @SaCheckPermission("system:codeRule:edit") + @Log(title = "编码规则", businessType = BusinessType.UPDATE) + @RepeatSubmit() + @PutMapping() + public R edit(@Validated(EditGroup.class) @RequestBody SysCodeRuleBo bo) { + return toAjax(sysCodeRuleService.updateByBo(bo)); + } + + /** + * 删除编码规则 + * + * @param codeRuleIds 主键串 + */ + @SaCheckPermission("system:codeRule:remove") + @Log(title = "编码规则", businessType = BusinessType.DELETE) + @DeleteMapping("/{codeRuleIds}") + public R remove(@NotEmpty(message = "主键不能为空") + @PathVariable Long[] codeRuleIds) { + return toAjax(sysCodeRuleService.deleteWithValidByIds(List.of(codeRuleIds), true)); + } + + + /** + * 下拉框查询编码规则列表 + */ + @GetMapping("/getSysCodeRuleList") + public R> getSysCodeRuleList(SysCodeRuleBo bo) { + List list = sysCodeRuleService.queryList(bo); + return R.ok(list); + } + + /** + * 获取生成的编码规则 + * @param bo 编码规则编码 + * @return + */ + @GetMapping("/getRuleGenerateCode") + public R getRuleGenerateCode(SysCodeRuleBo bo) { + return R.ok(sysCodeRuleService.getRuleGenerateCode(bo)); + } + +} diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/SysCodeRule.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/SysCodeRule.java new file mode 100644 index 00000000..81731629 --- /dev/null +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/SysCodeRule.java @@ -0,0 +1,80 @@ +package org.dromara.system.domain; + +import org.dromara.common.tenant.core.TenantEntity; +import com.baomidou.mybatisplus.annotation.*; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.io.Serial; + +/** + * 编码规则对象 sys_code_rule + * + * @author Yinq + * @date 2025-02-11 + */ +@Data +@EqualsAndHashCode(callSuper = true) +@TableName("sys_code_rule") +public class SysCodeRule extends TenantEntity { + + @Serial + private static final long serialVersionUID = 1L; + + /** + * 编码规则ID + */ + @TableId(value = "code_rule_id", type = IdType.AUTO) + private Long codeRuleId; + + /** + * 编码规则编码 + */ + private String codeRuleCode; + + /** + * 编码规则字段 + */ + private String codeRuleName; + + /** + * 规则前缀 + */ + private String rulePrefix; + + /** + * 序列号初始长度 + */ + private Long sequenceLength; + + /** + * 规则模板 + */ + private String ruleTemplate; + + /** + * 更新标识(1不更新 2每日更新) + */ + private String renewFlag; + + /** + * 当前编码 + */ + private String currentCode; + + /** + * 下个编码 + */ + private String nextCode; + + /** + * 激活标识(1是 0否) + */ + private String activeFlag; + + /** + * 备注 + */ + private String remark; + +} diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/bo/SysCodeRuleBo.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/bo/SysCodeRuleBo.java new file mode 100644 index 00000000..ed07c925 --- /dev/null +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/bo/SysCodeRuleBo.java @@ -0,0 +1,84 @@ +package org.dromara.system.domain.bo; + +import org.dromara.system.domain.SysCodeRule; +import org.dromara.common.mybatis.core.domain.BaseEntity; +import org.dromara.common.core.validate.AddGroup; +import org.dromara.common.core.validate.EditGroup; +import io.github.linpeilie.annotations.AutoMapper; +import lombok.Data; +import lombok.EqualsAndHashCode; +import jakarta.validation.constraints.*; + +/** + * 编码规则业务对象 sys_code_rule + * + * @author Yinq + * @date 2025-02-11 + */ +@Data +@EqualsAndHashCode(callSuper = true) +@AutoMapper(target = SysCodeRule.class, reverseConvertGenerate = false) +public class SysCodeRuleBo extends BaseEntity { + + /** + * 编码规则ID + */ + private Long codeRuleId; + + /** + * 编码规则编码 + */ + @NotBlank(message = "编码规则编码不能为空", groups = {AddGroup.class, EditGroup.class}) + private String codeRuleCode; + + /** + * 编码规则字段 + */ + @NotBlank(message = "编码规则字段不能为空", groups = {AddGroup.class, EditGroup.class}) + private String codeRuleName; + + /** + * 规则前缀 + */ + @NotBlank(message = "规则前缀不能为空", groups = {AddGroup.class, EditGroup.class}) + private String rulePrefix; + + /** + * 序列号初始长度 + */ + private Long sequenceLength; + + /** + * 规则模板 + */ + private String ruleTemplate; + + /** + * 更新标识(1不更新 2每日更新) + */ + @NotBlank(message = "更新标识(1不更新 2每日更新)不能为空", groups = {AddGroup.class, EditGroup.class}) + private String renewFlag; + + /** + * 当前编码 + */ + private String currentCode; + + /** + * 下个编码 + */ + private String nextCode; + + /** + * 激活标识(1是 0否) + */ + @NotBlank(message = "激活标识(1是 0否)不能为空", groups = {AddGroup.class, EditGroup.class}) + private String activeFlag; + + /** + * 备注 + */ + private String remark; + + +} diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysCodeRuleVo.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysCodeRuleVo.java new file mode 100644 index 00000000..aaf6c63f --- /dev/null +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/vo/SysCodeRuleVo.java @@ -0,0 +1,100 @@ +package org.dromara.system.domain.vo; + +import org.dromara.system.domain.SysCodeRule; +import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; +import com.alibaba.excel.annotation.ExcelProperty; +import org.dromara.common.excel.annotation.ExcelDictFormat; +import org.dromara.common.excel.convert.ExcelDictConvert; +import io.github.linpeilie.annotations.AutoMapper; +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; +import java.util.Date; + + + +/** + * 编码规则视图对象 sys_code_rule + * + * @author Yinq + * @date 2025-02-11 + */ +@Data +@ExcelIgnoreUnannotated +@AutoMapper(target = SysCodeRule.class) +public class SysCodeRuleVo implements Serializable { + + @Serial + private static final long serialVersionUID = 1L; + + /** + * 编码规则ID + */ + @ExcelProperty(value = "编码规则ID") + private Long codeRuleId; + + /** + * 编码规则编码 + */ + @ExcelProperty(value = "编码规则编码") + private String codeRuleCode; + + /** + * 编码规则字段 + */ + @ExcelProperty(value = "编码规则字段") + private String codeRuleName; + + /** + * 规则前缀 + */ + @ExcelProperty(value = "规则前缀") + private String rulePrefix; + + /** + * 序列号初始长度 + */ + @ExcelProperty(value = "序列号初始长度") + private Long sequenceLength; + + /** + * 规则模板 + */ + @ExcelProperty(value = "规则模板") + private String ruleTemplate; + + /** + * 更新标识(1不更新 2每日更新) + */ + @ExcelProperty(value = "更新标识(1不更新 2每日更新)", converter = ExcelDictConvert.class) + @ExcelDictFormat(dictType = "renew_flag") + private String renewFlag; + + /** + * 当前编码 + */ + @ExcelProperty(value = "当前编码") + private String currentCode; + + /** + * 下个编码 + */ + @ExcelProperty(value = "下个编码") + private String nextCode; + + /** + * 激活标识(1是 0否) + */ + @ExcelProperty(value = "激活标识(1是 0否)", converter = ExcelDictConvert.class) + @ExcelDictFormat(dictType = "active_flag") + private String activeFlag; + + /** + * 备注 + */ + @ExcelProperty(value = "备注") + private String remark; + + +} diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/mapper/SysCodeRuleMapper.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/mapper/SysCodeRuleMapper.java new file mode 100644 index 00000000..4bd84b4b --- /dev/null +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/mapper/SysCodeRuleMapper.java @@ -0,0 +1,15 @@ +package org.dromara.system.mapper; + +import org.dromara.system.domain.SysCodeRule; +import org.dromara.system.domain.vo.SysCodeRuleVo; +import org.dromara.common.mybatis.core.mapper.BaseMapperPlus; + +/** + * 编码规则Mapper接口 + * + * @author Yinq + * @date 2025-02-11 + */ +public interface SysCodeRuleMapper extends BaseMapperPlus { + +} diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/ISysCodeRuleService.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/ISysCodeRuleService.java new file mode 100644 index 00000000..d5c6b578 --- /dev/null +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/ISysCodeRuleService.java @@ -0,0 +1,77 @@ +package org.dromara.system.service; + +import org.dromara.system.domain.SysCodeRule; +import org.dromara.system.domain.vo.SysCodeRuleVo; +import org.dromara.system.domain.bo.SysCodeRuleBo; +import org.dromara.common.mybatis.core.page.TableDataInfo; +import org.dromara.common.mybatis.core.page.PageQuery; + +import java.util.Collection; +import java.util.List; + +/** + * 编码规则Service接口 + * + * @author Yinq + * @date 2025-02-11 + */ +public interface ISysCodeRuleService { + + /** + * 查询编码规则 + * + * @param codeRuleId 主键 + * @return 编码规则 + */ + SysCodeRuleVo queryById(Long codeRuleId); + + /** + * 分页查询编码规则列表 + * + * @param bo 查询条件 + * @param pageQuery 分页参数 + * @return 编码规则分页列表 + */ + TableDataInfo queryPageList(SysCodeRuleBo bo, PageQuery pageQuery); + + /** + * 查询符合条件的编码规则列表 + * + * @param bo 查询条件 + * @return 编码规则列表 + */ + List queryList(SysCodeRuleBo bo); + + /** + * 新增编码规则 + * + * @param bo 编码规则 + * @return 是否新增成功 + */ + Boolean insertByBo(SysCodeRuleBo bo); + + /** + * 修改编码规则 + * + * @param bo 编码规则 + * @return 是否修改成功 + */ + Boolean updateByBo(SysCodeRuleBo bo); + + /** + * 校验并批量删除编码规则信息 + * + * @param ids 待删除的主键集合 + * @param isValid 是否进行有效性校验 + * @return 是否删除成功 + */ + Boolean deleteWithValidByIds(Collection ids, Boolean isValid); + + /** + * 获取生成的编码规则 + * @param bo 编码规则编码 + * @return + */ + String getRuleGenerateCode(SysCodeRuleBo bo); + +} diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysCodeRuleServiceImpl.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysCodeRuleServiceImpl.java new file mode 100644 index 00000000..4ba6880d --- /dev/null +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysCodeRuleServiceImpl.java @@ -0,0 +1,196 @@ +package org.dromara.system.service.impl; + +import org.dromara.common.core.utils.DateUtils; +import org.dromara.common.core.utils.MapstructUtils; +import org.dromara.common.core.utils.StringUtils; +import org.dromara.common.mybatis.core.page.TableDataInfo; +import org.dromara.common.mybatis.core.page.PageQuery; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.github.yulichang.toolkit.JoinWrappers; +import com.github.yulichang.wrapper.MPJLambdaWrapper; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.dromara.system.domain.bo.SysCodeRuleBo; +import org.dromara.system.domain.vo.SysCodeRuleVo; +import org.dromara.system.domain.SysCodeRule; +import org.dromara.system.mapper.SysCodeRuleMapper; +import org.dromara.system.service.ISysCodeRuleService; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.Collection; + +/** + * 编码规则Service业务层处理 + * + * @author Yinq + * @date 2025-02-11 + */ +@RequiredArgsConstructor +@Service +public class SysCodeRuleServiceImpl implements ISysCodeRuleService { + + private final SysCodeRuleMapper baseMapper; + + /** + * 查询编码规则 + * + * @param codeRuleId 主键 + * @return 编码规则 + */ + @Override + public SysCodeRuleVo queryById(Long codeRuleId) { + return baseMapper.selectVoById(codeRuleId); + } + + /** + * 分页查询编码规则列表 + * + * @param bo 查询条件 + * @param pageQuery 分页参数 + * @return 编码规则分页列表 + */ + @Override + public TableDataInfo queryPageList(SysCodeRuleBo bo, PageQuery pageQuery) { + MPJLambdaWrapper lqw = buildQueryWrapper(bo); + Page result = baseMapper.selectVoPage(pageQuery.build(), lqw); + return TableDataInfo.build(result); + } + + /** + * 查询符合条件的编码规则列表 + * + * @param bo 查询条件 + * @return 编码规则列表 + */ + @Override + public List queryList(SysCodeRuleBo bo) { + MPJLambdaWrapper lqw = buildQueryWrapper(bo); + return baseMapper.selectVoList(lqw); + } + + private MPJLambdaWrapper buildQueryWrapper(SysCodeRuleBo bo) { + Map params = bo.getParams(); + MPJLambdaWrapper lqw = JoinWrappers.lambda(SysCodeRule.class) + .selectAll(SysCodeRule.class) + .eq(bo.getCodeRuleId() != null, SysCodeRule::getCodeRuleId, bo.getCodeRuleId()) + .eq(StringUtils.isNotBlank(bo.getCodeRuleCode()), SysCodeRule::getCodeRuleCode, bo.getCodeRuleCode()) + .like(StringUtils.isNotBlank(bo.getCodeRuleName()), SysCodeRule::getCodeRuleName, bo.getCodeRuleName()) + .eq(StringUtils.isNotBlank(bo.getRulePrefix()), SysCodeRule::getRulePrefix, bo.getRulePrefix()) + .eq(StringUtils.isNotBlank(bo.getRuleTemplate()), SysCodeRule::getRuleTemplate, bo.getRuleTemplate()) + .eq(StringUtils.isNotBlank(bo.getRenewFlag()), SysCodeRule::getRenewFlag, bo.getRenewFlag()) + .eq(StringUtils.isNotBlank(bo.getCurrentCode()), SysCodeRule::getCurrentCode, bo.getCurrentCode()) + .eq(StringUtils.isNotBlank(bo.getNextCode()), SysCodeRule::getNextCode, bo.getNextCode()) + .eq(StringUtils.isNotBlank(bo.getActiveFlag()), SysCodeRule::getActiveFlag, bo.getActiveFlag()) + .orderByAsc(SysCodeRule::getCreateTime); + return lqw; + } + + /** + * 新增编码规则 + * + * @param bo 编码规则 + * @return 是否新增成功 + */ + @Override + public Boolean insertByBo(SysCodeRuleBo bo) { + SysCodeRule add = MapstructUtils.convert(bo, SysCodeRule.class); + validEntityBeforeSave(add); + generateCode(add); + boolean flag = baseMapper.insert(add) > 0; + if (flag) { + bo.setCodeRuleId(add.getCodeRuleId()); + } + return flag; + } + + /** + * 修改编码规则 + * + * @param bo 编码规则 + * @return 是否修改成功 + */ + @Override + public Boolean updateByBo(SysCodeRuleBo bo) { + SysCodeRule update = MapstructUtils.convert(bo, SysCodeRule.class); + validEntityBeforeSave(update); + return baseMapper.updateById(update) > 0; + } + + /** + * 保存前的数据校验 + */ + private void validEntityBeforeSave(SysCodeRule entity) { + //TODO 做一些数据校验,如唯一约束 + } + + /** + * 校验并批量删除编码规则信息 + * + * @param ids 待删除的主键集合 + * @param isValid 是否进行有效性校验 + * @return 是否删除成功 + */ + @Override + public Boolean deleteWithValidByIds(Collection ids, Boolean isValid) { + if (isValid) { + //TODO 做一些业务上的校验,判断是否需要校验 + } + return baseMapper.deleteByIds(ids) > 0; + } + + /** + * 获取生成的编码规则 + * @param bo 编码规则编码 + * @return + */ + @Override + @Transactional(rollbackFor = Exception.class) + public String getRuleGenerateCode(SysCodeRuleBo bo) { + MPJLambdaWrapper lqw = buildQueryWrapper(bo); + SysCodeRule sysCodeRule = baseMapper.selectOne(lqw); + SysCodeRule codeRule = generateCode(sysCodeRule); + baseMapper.updateById(codeRule); + return codeRule.getCurrentCode(); + } + + public SysCodeRule generateCode(SysCodeRule sysCodeRule) { + if (StringUtils.isNull(sysCodeRule)){ + return null; + } + Long sequenceLength = sysCodeRule.getSequenceLength(); + String ruleTemplate = sysCodeRule.getRuleTemplate(); + String rulePrefix = sysCodeRule.getRulePrefix(); + String nextCode = sysCodeRule.getNextCode(); + StringBuilder code = new StringBuilder(); + code.append(rulePrefix); + if (sysCodeRule.getRenewFlag().equals("2")){ + //每日更新 + String dateToStr = DateUtils.parseDateToStr(ruleTemplate, new Date()); + code.append(dateToStr); + } + long seq = 1L; + // 截取序列号的前 sequenceLength 位 + if (StringUtils.isNotEmpty(nextCode)){ + String substring = nextCode.substring(nextCode.length() - sequenceLength.intValue()); + seq = Long.parseLong(substring) + 1; + } + //序列号 + code.append(formatSequence(sequenceLength, seq)); + sysCodeRule.setCurrentCode(sysCodeRule.getNextCode()); + sysCodeRule.setNextCode(code.toString()); + return sysCodeRule; + } + + public static String formatSequence(Long sequenceLength, Long seq) { + String seqStr = String.valueOf(seq); + if (seqStr.length() >= sequenceLength) { + return seqStr; + } + return String.format("%0" + sequenceLength + "d", seq); + } + +} diff --git a/ruoyi-modules/ruoyi-system/src/main/resources/mapper/system/SysCodeRuleMapper.xml b/ruoyi-modules/ruoyi-system/src/main/resources/mapper/system/SysCodeRuleMapper.xml new file mode 100644 index 00000000..ea6f5a54 --- /dev/null +++ b/ruoyi-modules/ruoyi-system/src/main/resources/mapper/system/SysCodeRuleMapper.xml @@ -0,0 +1,7 @@ + + + + +