|
@@ -0,0 +1,75 @@
|
|
|
+package com.qs.mp.admin.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.qs.mp.admin.domain.TicketBox;
|
|
|
+import com.qs.mp.admin.mapper.TicketBoxMapper;
|
|
|
+import com.qs.mp.admin.service.ITicketBoxSerialService;
|
|
|
+import com.qs.mp.common.core.redis.RedisCache;
|
|
|
+import com.qs.mp.common.enums.TicketTypeEnum;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author zhongcp
|
|
|
+ * @Date 2022/3/11
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class TicketBoxSerialServiceImpl implements ITicketBoxSerialService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisCache redisCache;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate redisTemplate;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TicketBoxMapper ticketBoxMapper;
|
|
|
+
|
|
|
+ public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String generateSerial(TicketTypeEnum ticketType) {
|
|
|
+ //年份后两位
|
|
|
+ String prefix = sdf.format(new Date()).substring(2);
|
|
|
+
|
|
|
+ //如果日期前缀未过期,则序号自增
|
|
|
+ //否则,将日期作为Key,1作为Value重置,并设置第二年0点过期
|
|
|
+ if (redisCache.hasKey(prefix)) {
|
|
|
+ redisTemplate.opsForValue().increment(prefix, 1);
|
|
|
+
|
|
|
+ } else {
|
|
|
+ int start = 1;
|
|
|
+ List<TicketBox> ticketBoxList = ticketBoxMapper.selectList(new QueryWrapper<TicketBox>().orderByDesc("created_time").last("limit 1"));
|
|
|
+ if (!CollectionUtils.isEmpty(ticketBoxList)) {
|
|
|
+ String boxNo = ticketBoxList.get(0).getBoxNo();
|
|
|
+ int year = Integer.valueOf(boxNo.substring(1, 3));
|
|
|
+ // 同一年份,做累加
|
|
|
+ if (year == Integer.valueOf(prefix)) {
|
|
|
+ start = Integer.valueOf(boxNo.substring(3)) + 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ redisTemplate.opsForValue().set(prefix, start);
|
|
|
+ redisTemplate.expireAt(prefix, getNextYearDate());
|
|
|
+
|
|
|
+ }
|
|
|
+ return "T" + prefix + String.format("%1$05d", redisTemplate.opsForValue().get(prefix));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取第二年 1月 1日 00:00的时间
|
|
|
+ private static Date getNextYearDate() {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.set(Calendar.MONTH, 1);
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ calendar.add(Calendar.YEAR, 1);
|
|
|
+ return calendar.getTime();
|
|
|
+ }
|
|
|
+}
|