车间能级提升-智能设备管理系统
zhuguifei
2025-03-10 6648e74a007254e167c5508de5d25402cd4bb63b
eims/ruoyi-modules/lb-eims/src/main/java/org/dromara/eims/controller/GenerateCodeController.java
@@ -5,6 +5,8 @@
import org.dromara.common.core.constant.CacheConstants;
import org.dromara.common.core.domain.R;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.eims.service.IEimsRepairResService;
import org.dromara.eims.service.IGenerateCodeService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@@ -26,6 +28,8 @@
@RestController
@RequestMapping("/eims/generate")
public class GenerateCodeController {
    private final IGenerateCodeService  eimsRepairResService;
    /**
     * 根据前缀生成各种编码
     *
@@ -35,25 +39,9 @@
    @GetMapping("/{prefix}")
    public R<String> generateCode(@NotNull(message = "类型不能为空")
                                      @PathVariable String prefix) {
        String todayStr = DateTimeFormatter.ofPattern("yyyyMMdd").format(LocalDate.now());
        String key = CacheConstants.EIMS_GENERATE_CODE + ":" + prefix;
        String code;
        // 使用Redis的原子性操作避免并发问题
        String oldCode = RedisUtils.getCacheObject(key);
        if (oldCode != null && oldCode.contains(todayStr)) {
            int no = Integer.parseInt(oldCode.substring(oldCode.length() - 4));
            code = String.format("%s%s%04d", prefix, todayStr, no + 1);
        } else {
            code = String.format("%s%s%04d", prefix, todayStr, 1);
        }
        // 更新缓存
        try {
            RedisUtils.setCacheObject(key, code);
        } catch (Exception e) {
            return R.fail("生成编码失败,请稍后重试!");
        }
        return R.ok("生成成功!", code);
        String code = eimsRepairResService.generateCode(prefix);
        return code == null ? R.fail("生成失败!") :R.ok("生成成功!", code);
    }
}