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 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; } } }