diff --git a/hzims-service/ticket/src/main/java/com/hnac/hzims/ticket/standardTicket/controller/StandardWorkTicketController.java b/hzims-service/ticket/src/main/java/com/hnac/hzims/ticket/standardTicket/controller/StandardWorkTicketController.java index df0db77..a34f050 100644 --- a/hzims-service/ticket/src/main/java/com/hnac/hzims/ticket/standardTicket/controller/StandardWorkTicketController.java +++ b/hzims-service/ticket/src/main/java/com/hnac/hzims/ticket/standardTicket/controller/StandardWorkTicketController.java @@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.toolkit.IdWorker; import com.hnac.hzims.ticket.response.WorkTicketMessageResponse; import com.hnac.hzims.ticket.standardTicket.service.StandardWorkTicketService; +import com.hnac.hzims.ticket.standardTicket.utils.StandardTicketIdWorker; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; @@ -11,6 +12,7 @@ import lombok.extern.slf4j.Slf4j; import org.springblade.core.tool.api.R; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -36,6 +38,10 @@ public class StandardWorkTicketController { @Autowired private RedisTemplate redisTemplate; + + @Autowired + private StandardTicketIdWorker standardTicketIdWorker; + @ApiOperation("测试开票") @PostMapping("/createTicket") public R createTicket() { @@ -45,8 +51,16 @@ public class StandardWorkTicketController { String jsonString = JSON.toJSONString(workTicketMessageResponse); redisTemplate.convertAndSend("blade:queue:flow", jsonString); standardWorkTicketService.createTicket(); + redisTemplate.opsForValue().set("1", "2"); return R.success("成功"); } + @ApiOperation("测试生成唯一编码") + @GetMapping("/idWored") + public R idWored() { + String ticketCode = standardTicketIdWorker.getTicketCode("aa", "bb", "2304"); + return R.data(ticketCode); + } + } diff --git a/hzims-service/ticket/src/main/java/com/hnac/hzims/ticket/standardTicket/utils/StandardTicketIdWorker.java b/hzims-service/ticket/src/main/java/com/hnac/hzims/ticket/standardTicket/utils/StandardTicketIdWorker.java new file mode 100644 index 0000000..c74ab25 --- /dev/null +++ b/hzims-service/ticket/src/main/java/com/hnac/hzims/ticket/standardTicket/utils/StandardTicketIdWorker.java @@ -0,0 +1,199 @@ +package com.hnac.hzims.ticket.standardTicket.utils; + +import com.alibaba.fastjson.JSON; +import com.baomidou.mybatisplus.core.toolkit.StringUtils; +import lombok.Data; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.data.redis.core.ValueOperations; +import org.springframework.stereotype.Component; + +import java.text.NumberFormat; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; + + +/** + * 生成票据编码工具类 + * + * @Author WL + * @Version v1.0 + * @Serial 1.0 + * @Date 2023/3/27 10:11 + */ +@Component +public class StandardTicketIdWorker { + + private static Map cacheMap = new HashMap<>(); + + + @Autowired + private StringRedisTemplate redisTemplate; + + private static NumberFormat numberFormatter; + + + static { + //初始化 + numberFormatter = NumberFormat.getInstance(); + numberFormatter.setGroupingUsed(false); + numberFormatter.setMinimumIntegerDigits(3); + numberFormatter.setMaximumFractionDigits(3); + } + + /** + * 按规则生成票据编码:票据编码:AA-BB-CCCC-DDD格式 + * + * @param staff AA在建站之初由接站人员填写要求唯一 + * @param typeInstrument 票据种类,第一种票,第二种票 + * @param YearMonth 开票时间年月 + */ + public String getTicketCode(String staff, String typeInstrument, String YearMonth) { + ValueOperations operations = redisTemplate.opsForValue(); + AtomicInteger atomicInteger = new AtomicInteger(1); + String key = staff + ":" + typeInstrument + ":" + YearMonth; + // TicketCode ticketCode1 = cacheMap.get(key); + TicketCode ticketCode = JSON.parseObject(operations.get(key),TicketCode.class); + if (ticketCode == null) { + ticketCode = new TicketCode(); + ticketCode.setYearMonth(YearMonth); + ticketCode.setSequencing(atomicInteger); + // cacheMap.put(key, ticketCode); + operations.set(key,JSON.toJSONString(ticketCode)); + String result = getTicketCode(staff, typeInstrument, YearMonth, atomicInteger.get()); + return result; + } + + String originalYearMonth = ticketCode.getYearMonth(); + + AtomicInteger sequencing = ticketCode.getSequencing(); + + //生成 票据排序 格式 001 010 111 + int code = getYearMonthCode(originalYearMonth, YearMonth, key, sequencing); + + String result = getTicketCode(staff, typeInstrument, YearMonth, code); + return result; + } + + + /** + * 生成的票据 编号 + */ + public String getTicketCode(String staff, String typeInstrument, String YearMonth, int sequencing) { + return staff.concat("-").concat(typeInstrument).concat("-").concat(YearMonth).concat("-").concat(numberFormatter.format(sequencing)); + } + + + /** + * 返回票据排序 + * + * @param + * @param originalYearMonth 上次的年月 年的后2位 + 月 + * @param nextYearMonth 现在的年月 年的后2位 + 月 + * @param key + * @param sequencing + * @return + */ + private int getYearMonthCode(String originalYearMonth, String nextYearMonth, String key, AtomicInteger sequencing) { + ValueOperations operations = redisTemplate.opsForValue(); + //1. 累计票据 + sequencing.incrementAndGet(); + //3. 判断年是否为空 + if (StringUtils.isEmpty(originalYearMonth) && StringUtils.isEmpty(nextYearMonth)) { + return sequencing.get(); + } + //4. 验证年 最后一位数 + String year1 = getSubstring(originalYearMonth, 1); + String year2 = getSubstring(nextYearMonth, 1); + if (!year1.equals(year2)) { + sequencing.compareAndSet(sequencing.get(), 1); + return sequencing.get(); + } + + //4. 验证倒数第二位数 + year1 = getSubstring(originalYearMonth, 2); + year2 = getSubstring(nextYearMonth, 2); + if (!year1.equals(year2)) { + sequencing.compareAndSet(sequencing.get(), 1); + return sequencing.get(); + } + + //4. 验证倒数第三位数 + year1 = getSubstring(originalYearMonth, 3); + year2 = getSubstring(nextYearMonth, 3); + if (!year1.equals(year2)) { + sequencing.compareAndSet(sequencing.get(), 1); + return sequencing.get(); + } + + + //6. sequencing 大于等于1000 的话,从头开始 + if (sequencing.get() >= 1000) { + sequencing.compareAndSet(sequencing.get(), 1); + System.out.println("sequencing >= 1000 被执行呢"); + redisTemplate.delete(key); + // cacheMap.remove(key); + return sequencing.get(); + } + + TicketCode ticketCode = new TicketCode(); + ticketCode.setYearMonth(nextYearMonth); + ticketCode.setSequencing(sequencing); + operations.set(key, JSON.toJSONString(ticketCode)); + //cacheMap.put(key, ticketCode); + + return sequencing.get(); + } + + /** + * 截取字符串 + * + * @param originalYearMonth + * @param digit 位数 + * @return + */ + private static String getSubstring(String originalYearMonth, int digit) { + return originalYearMonth.substring(originalYearMonth.length() - digit); + } + + + @Data + public static class TicketCode { + + /** + * 月份 + */ + private String yearMonth; + /** + * 记录数 + */ + private AtomicInteger sequencing = new AtomicInteger(1); + } + + public static void main(String[] args) { + + StandardTicketIdWorker standardTicketIdWorker = new StandardTicketIdWorker(); + int count = 1; + while (count <= 1000) { + + System.out.println("1 ===> " + standardTicketIdWorker.getTicketCode("aa", "bb", "2304")); + System.out.println("================================="); + //System.out.println("2 ===》" + standardTicketIdWorker.getTicketCode("aa", "bb", "2204")); + count++; + + } + + // NumberFormat numberFormatter = NumberFormat.getInstance(); + // numberFormatter.setGroupingUsed(false); + // numberFormatter.setMinimumIntegerDigits(3); + // numberFormatter.setMaximumFractionDigits(3); + // + // String format = numberFormatter.format(2); + // System.out.println(format); + + } + + +}