Commit 10722f11 authored by LiuLin's avatar LiuLin

fix(tcm):申请单编码生成代码开发

parent a14c3d28
package com.yeejoin.amos.boot.module.tcm.api.controller;
import com.yeejoin.amos.boot.biz.common.controller.BaseController;
import com.yeejoin.amos.boot.module.tcm.api.dto.AlertCalledDto;
import com.yeejoin.amos.boot.module.tcm.api.service.ICreateCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.typroject.tyboot.core.foundation.enumeration.UserType;
import org.typroject.tyboot.core.restful.doc.TycloudOperation;
import org.typroject.tyboot.core.restful.utils.ResponseHelper;
import org.typroject.tyboot.core.restful.utils.ResponseModel;
import java.util.List;
/**
*
* 生成顺序码
* @author LiuLin
* @date 2023-12-14
*/
@RestController
@Api(tags = "生成顺序码")
@RequestMapping(value = "/code")
public class CreateCodeController extends BaseController {
@Autowired
private ICreateCodeService createCodeService;
/**
* 申请单编号生成
* @param type
* @param num
* @return
*/
@TycloudOperation(ApiLevel = UserType.AGENCY)
@PostMapping(value = "/ANCode")
@ApiOperation(httpMethod = "POST", value = "申请单编号生成", notes = "申请单编号生成")
public ResponseModel<List<String>> CreateANCode(@RequestParam("type") String type,
@RequestParam("num") int num) {
return ResponseHelper.buildResponse(createCodeService.createApplicationFormCode(type,num));
}
/**
* 申请单编号生成
* @param type
* @param num
* @return
*/
@TycloudOperation(ApiLevel = UserType.AGENCY)
@PostMapping(value = "/Device registration code")
@ApiOperation(httpMethod = "POST", value = "申请单编号生成", notes = "申请单编号生成")
public ResponseModel<List<String>> CreateANCode1(@RequestParam("type") String type,
@RequestParam("num") int num) {
return ResponseHelper.buildResponse(createCodeService.CreateANCode(type,num));
}
}
package com.yeejoin.amos.boot.module.tcm.api.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* 申请单枚举
* @author LiuLin
*/
@Getter
@AllArgsConstructor
public enum ApplicationFormTypeEnum {
/**
* 申请单枚举
*/
GZ("GZ"),
JY("JY"),
SY("SY");
/**
* 编号
*/
private final String code;
}
package com.yeejoin.amos.boot.module.tcm.api.service;
import java.util.List;
public interface ICreateCodeService {
/**
* 生成申请单编号
* @param type 枚举类型
* @param num 生成个数
* @return List
*/
List<String> createApplicationFormCode(String type, int num);
}
package com.yeejoin.amos.boot.module.tcm.biz.service.impl;
import com.yeejoin.amos.boot.module.tcm.api.enums.ApplicationFormTypeEnum;
import com.yeejoin.amos.boot.module.tcm.api.service.ICreateCodeService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* @author LiuLin
* @date 2023-12-14
*/
@Service
public class CreateCodeServiceImpl implements ICreateCodeService {
private static final String LOCK_VALUE = "locked";
private static final String LOCK_KEY = "sequence_lock";
private final RedisTemplate<String, String> redisTemplate;
private String rulePrefix;
public CreateCodeServiceImpl(RedisTemplate<String, String> redisTemplate) {
this.redisTemplate = redisTemplate;
}
@Override
public List<String> createApplicationFormCode(String type, int batchSize) {
if (!isValueInEnum(type)) {
return Collections.emptyList();
}
rulePrefix = type + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
return this.generateBatchSequence(type, batchSize);
}
public boolean isValueInEnum(String value) {
return EnumSet.allOf(ApplicationFormTypeEnum.class)
.stream()
.anyMatch(enumValue -> enumValue.name().equals(value));
}
public List<String> generateBatchSequence(String sequenceKey, int batchSize) {
// 使用分布式锁,确保在并发情况下只有一个线程能够生成顺序码
Boolean lockAcquired = redisTemplate.opsForValue().setIfAbsent(LOCK_KEY, LOCK_VALUE);
if (Boolean.TRUE.equals(lockAcquired)) {
try {
// 获取当前顺序码
ValueOperations<String, String> valueOps = redisTemplate.opsForValue();
String currentSequenceStr = valueOps.get(sequenceKey);
// 如果为空,则初始化为0
if (currentSequenceStr == null) {
currentSequenceStr = "0";
}
// 将当前顺序码加1
Long currentSequence = Long.parseLong(currentSequenceStr);
// 生成批量顺序码
List<String> sequenceList = new ArrayList<>();
for (int i = 0; i < batchSize; i++) {
currentSequence++;
// 生成3位顺序码
String formattedSequence = String.format("%03d", currentSequence);
sequenceList.add(rulePrefix + formattedSequence);
// 更新顺序码
setValueWithDailyExpiration(sequenceKey, String.valueOf(formattedSequence));
}
return sequenceList;
} finally {
redisTemplate.delete(LOCK_KEY);
}
} else {
// 获取锁失败,可以选择重试或采取其他策略
throw new RuntimeException("Failed to acquire lock for sequence generation");
}
}
/**
* redis 根据自然月过期
*
* @param key key
* @param value value
*/
public void setValueWithMonthlyExpiration(String key, String value) {
ValueOperations<String, String> valueOps = redisTemplate.opsForValue();
valueOps.set(key, value);
Date currentDate = new Date();
Date endOfMonth = calculateEndOfMonth(currentDate);
long expirationTimeInSeconds = endOfMonth.getTime() - currentDate.getTime();
redisTemplate.expire(key, expirationTimeInSeconds, TimeUnit.MILLISECONDS);
}
public void setValueWithDailyExpiration(String key, String value) {
ValueOperations<String, String> valueOps = redisTemplate.opsForValue();
valueOps.set(key, value);
Date currentDate = new Date();
Date endOfDay = calculateEndOfDay(currentDate);
long expirationTimeInSeconds = endOfDay.getTime() - currentDate.getTime();
redisTemplate.expire(key, expirationTimeInSeconds, TimeUnit.MILLISECONDS);
}
private Date calculateEndOfMonth(Date currentDate) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(currentDate);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
return calendar.getTime();
}
private Date calculateEndOfDay(Date currentDate) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(currentDate);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
return calendar.getTime();
}
}
\ No newline at end of file
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