ZQN
2024-06-17 b1ff19545212508d3f65741ab889f0b6df82a511
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.project.system.service.impl;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.common.constant.OrderNoConstants;
import com.project.common.enums.OrderPre;
import com.project.common.exception.base.BaseException;
import com.project.common.utils.DateUtils;
import com.project.common.utils.StringUtils;
import com.project.system.domain.SysOrderNo;
import com.project.system.mapper.SysOrderNoMapper;
import com.project.system.service.ISysOrderNoService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.Date;
 
 
/**
 * 订单编号Service业务层处理
 *
 * @author manton
 * @date 2023-03-07
 */
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class SysOrderNoServiceImpl extends ServiceImpl<SysOrderNoMapper, SysOrderNo> implements ISysOrderNoService {
 
 
    @Override
    public String getOrderNo(int type)
    {
 
        return getOrderNumberByType(type);
    }
 
 
 
    private String getOrderNumberByType(int type)
    {
        String prefix = OrderPre.getNameByIndex(type);//需通过类型转换成前缀
        if(StringUtils.isEmpty(prefix)){ throw new BaseException("获取单据参数异常!"); }
        synchronized ((String.valueOf(type)).intern())
        {
            //系统时间:六位时间(年月日)
            String exactDate = DateUtils.dateNow();
            Integer newCurrentNo = 0;// 当前流水号
            //1 先查询
            SysOrderNo orderNo = this.getOne(this.lq().eq(SysOrderNo::getType, type));
            if(orderNo!=null && orderNo.getExactDate().equals(exactDate)){
                newCurrentNo = orderNo.getCurrentNo();
            } else {
                newCurrentNo = OrderNoConstants.INIT_CURRENT_NO;
            }
            if(orderNo==null){
                //生成单据编号
                orderNo = new SysOrderNo();
                orderNo.setType(type);
                orderNo.setExactDate(exactDate);
                orderNo.setCurrentNo(newCurrentNo+1);// 更新流水号+1
                orderNo.setUpdateTime(new Date());
                if(!this.save(orderNo)){
                    throw new BaseException("单据编号生成失败!");
                }
            } else {
                //sql +1
                if(!this.baseMapper.updateCurrentNo(orderNo.getId(), exactDate, new Date(), newCurrentNo)){// 更新单据编号 +1
                    throw new BaseException("更新单据编号失败!");
                }
                //  再次查询
                SysOrderNo newOrderNo = this.getOne(this.lq().eq(SysOrderNo::getType, type));
                newCurrentNo = newOrderNo.getCurrentNo();
            }
 
            return prefix + "-"  + exactDate + newCurrentNo;
        }
    }
}