AgvBillServiceImpl.java 4.23 KB
package com.huaheng.api.acs.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.api.acs.domain.AgvBill;
import com.huaheng.api.acs.domain.AgvBillDetail;
import com.huaheng.api.acs.mapper.AgvBillDetailMapper;
import com.huaheng.api.acs.mapper.AgvBillMapper;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.DateUtils;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.warehouse.domain.Warehouse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class AgvBillServiceImpl implements AgvBillService{
    @Resource
    private AgvBillDetailMapper agvBillDetailMapper;
    @Resource
    private AgvBillMapper agvBillMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addBill(List<AgvBillDetail> agvBillDetails) {
        String taskNo = UUID.randomUUID().toString();
        String billCode = createCode();
        //校验参数
        agvBillDetails.forEach(a->{ agvbillCheck(a); });
        BigDecimal taskNum = agvBillDetails.stream().map(AgvBillDetail::getQty).reduce(BigDecimal::add).get();
        taskNum= taskNum.setScale(1,BigDecimal.ROUND_DOWN);
        //添加订单
        AgvBill agvBill=new AgvBill();
        agvBill.setCode(billCode);
        agvBill.setCreated(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",new Date()));
        agvBill.setCreatedBy(agvBillDetails.get(0).getCreatedBy());
        agvBill.setFirstStatus(1);
        agvBill.setLastStatus(1);
        agvBill.setNum(agvBillDetails.size());
        agvBill.setTaskNum(taskNum);
        agvBillMapper.insert(agvBill);
        //添加明细
        agvBillDetails.forEach(a->{
            a.setBillId(agvBill.getId());
            a.setStatus(1);
            a.setCreated(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",new Date()));
            agvBillDetailMapper.insert(a);
        });
        //生成立库的出库任务或者平库的出库任务
        //生成agv任务
        return AjaxResult.success();

    }

    private String createCode() {
        //B2022070100001
        String billCode=null;
        LambdaQueryWrapper<AgvBill> lambdaQuery= Wrappers.lambdaQuery();
        lambdaQuery.orderByDesc(AgvBill::getId).last("limit 1");
        AgvBill agvBill = agvBillMapper.selectOne(lambdaQuery);
        String code=null;
        if(StringUtils.isNotNull(agvBill)){
             code= agvBill.getCode();
        }
        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        if(code!=null && code.substring(code.length()-13,code.length()-5).equals(df.format(now))){
            Integer count = Integer.valueOf(code.substring(code.length() - 5, code.length()));
            billCode = "B" + df.format(now) + String.format("%05d", count + 1);
        }else {
            billCode = "B" + df.format(now) + String.format("%05d",  1);
        }
        return billCode;
    }

    private void agvbillCheck(AgvBillDetail agvBillDetail) {
        if(StringUtils.isEmpty(agvBillDetail.getMaterialCode())){
            throw new ServiceException("没有物料编码");
        }
        if(StringUtils.isEmpty(agvBillDetail.getMaterialName())){
            throw new ServiceException("没有物料名称");
        }
        if(StringUtils.isNull(agvBillDetail.getQty())){
            throw new ServiceException("没有明细数量");
        }
        if(StringUtils.isNull(agvBillDetail.getFromPort())){
            throw new ServiceException("没有起始点位");
        }
        if(StringUtils.isNull(agvBillDetail.getToPort())){
            throw new ServiceException("没有目标点位");
        }
        if(StringUtils.isNull(agvBillDetail.getDetailType())){
            throw new ServiceException("没有类型");
        }
        if(StringUtils.isNull(agvBillDetail.getCreatedBy())){
            throw new ServiceException("没有创建人");
        }
    }
}