段飞宇
2 years ago
2 changed files with 213 additions and 0 deletions
@ -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<String, TicketCode> 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<String, String> 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<String, String> 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);
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue