生成订单编号工具

订单编号工具类

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * 生成订单工具
 */
public class OrderNoUtil {

  //订单编号前缀
  public static final String PREFIX = "QX-";


  /**
   * description : 生成订单编号(前缀+yyyyMMddHHmmss+digit位随机数)
   * @param digit : 生成几位随机数
   * @return : 订单编号(String)
   */
  public static synchronized String getOrderNo(int digit) {
    StringBuilder resultBuffer = new StringBuilder();
    resultBuffer.append(PREFIX)
        .append(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
    Random random = new Random();
    for (int i = 0; i < digit; i++) {
      resultBuffer.append(random.nextInt(10));
    }
    return resultBuffer.toString();
  }
}
/**
 * 创建申请单
 * @param applyOrderDTO 申请信息
 * @return OrderId
 */
@Override
public Object createApplyOrder(ApplyOrderDTO applyOrderDTO) {
    UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
    JWTUser jwtUser = (JWTUser) authentication.getPrincipal();
    //创建时,OrderStatus,APPLYING;DealStatus,NOT_ACCEPTED;OrderProcessNode,AUDIT_ORDER
    //ProcessId,CREATE_ORDER
    ApplyOrderDO applyOrderDO = new ApplyOrderDO();
    applyOrderDO.setOrderId(OrderNoUtil.getOrderNo(4));
    applyOrderDO.setUserId(jwtUser.getUserDO().getUuid());
    applyOrderDO.setUserName(jwtUser.getUserDO().getUsername());
    applyOrderDO.setUserPhoneNum(jwtUser.getUserDO().getMobile());
    applyOrderDO.setUserCompany(applyOrderDTO.getUserCompany()==null?"无":applyOrderDTO.getUserCompany());
    applyOrderDO.setOrderStatus(OrderStatus.APPLYING);
    applyOrderDO.setDealStatus(DealStatus.NOT_ACCEPTED);
    applyOrderDO.setCreateTime(new Date());
    applyOrderDO.setUserRealName(jwtUser.getUserDO().getUserRealName());
    applyOrderDO.setUserNackName(jwtUser.getUserDO().getNickName());
    applyOrderDO.setOrderProcessNode(OrderProcessNode.AUDIT_ORDER);
    LambdaQueryWrapper<ApplyOrderDO> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(ApplyOrderDO::getOrderId, applyOrderDO.getOrderId());
    long existsRows = applyOrderMapper.selectCount(queryWrapper);

    List<ApplyOrderDetailDO> applyOrderDetailList = applyOrderDTO.getApplyOrderDetailList();
    LambdaQueryWrapper<ApplyOrderDetailDO> queryWrapper1 = new LambdaQueryWrapper<>();
    queryWrapper1.eq(ApplyOrderDetailDO::getOrderId, applyOrderDO.getOrderId());
    long existsRows1 = applyOrderDetailMapper.selectCount(queryWrapper1);

    if (existsRows == 0) {
        applyOrderMapper.insert(applyOrderDO);
        for (ApplyOrderDetailDO detailDO : applyOrderDetailList) {
            detailDO.setOrderId(applyOrderDO.getOrderId());
            if (existsRows1 == 0) {
                applyOrderDetailMapper.insert(detailDO);
            }
        }
        ApplyOrderProcessDO applyOrderProcessDO = new ApplyOrderProcessDO();
        applyOrderProcessDO.setOrderId(applyOrderDO.getOrderId());
        // 这里的是正在进行中的流程,而不是执行完后显示下一步流程。
        applyOrderProcessDO.setProcessId(OrderProcessNode.CREATE_ORDER);
        applyOrderProcessDO.setProcessResult("PASS");
        applyOrderProcessDO.setProcessRemarks("申请单已提交");
        applyOrderProcessDO.setProcessTime(new Date());
        applyOrderProcessDO.setProcessDealUser(jwtUser.getUserDO().getUsername());
        applyOrderProcessDO.setProcessDealMobile(jwtUser.getUserDO().getMobile());
        applyOrderProcessMapper.insert(applyOrderProcessDO);

        Map<String,String> map = new HashMap<>();
        map.put("orderId",applyOrderDO.getOrderId());
        return map;
    } else {
        return "创建申请单失败";
    }

}

你可能感兴趣的:(Java基础,java)