ReceiptHeaderService.java 17.9 KB
package com.huaheng.pc.receipt.receiptHeader.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.check.checkDetail.domain.CheckDetail;
import com.huaheng.pc.check.checkDetail.service.CheckDetailService;
import com.huaheng.pc.check.checkHeader.domain.CheckHeader;
import com.huaheng.pc.check.checkHeader.service.CheckHeaderService;
import com.huaheng.pc.config.company.service.CompanyService;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.receiptType.domain.ReceiptType;
import com.huaheng.pc.config.receiptType.service.ReceiptTypeService;
import com.huaheng.pc.config.sendMail.service.SendMailService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.mapper.ReceiptHeaderMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class ReceiptHeaderService extends ServiceImpl<ReceiptHeaderMapper, ReceiptHeader> {

    @Resource
    private ReceiptTypeService receiptTypeService;
    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private CheckHeaderService checkHeaderService;
    @Resource
    private CheckDetailService checkDetailService;
    @Resource
    private SendMailService sendMailService;
    @Resource
    private ReceiptHeaderMapper receiptHeaderMapper;
    @Resource
    private CompanyService companyService;
    @Resource
    private MaterialService materialService;

    /**
     * 保存入库单
     * @param receiptHeader
     * @return
     */
    public AjaxResult saveReceiptHeader(ReceiptHeader receiptHeader){
        //根据仓库和货主以及传入单据类型编码判断单据类型是否存在
        LambdaQueryWrapper<ReceiptType> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ReceiptType::getCode, receiptHeader.getReceiptType())
                 .eq(ReceiptType::getWarehouseCode,ShiroUtils.getWarehouseCode())
                 .eq(ReceiptType::getCompanyCode, receiptHeader.getCompanyCode());
        ReceiptType receiptType = receiptTypeService.getOne(lambdaQueryWrapper);
        if(receiptType == null) {
            throw new ServiceException(ShiroUtils.getWarehouseCode(),receiptHeader.getCompanyCode(),"没有对应的入库单类型");
        }

        //根据单据类型生成单号
        String code = createCode(receiptHeader.getReceiptType());
        if(StringUtils.isEmpty(code)){
            throw new ServiceException("根据单据类型"+receiptHeader.getReceiptType()+"生成单号失败");
        }
        receiptHeader.setCode(code);
        receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_BUILD);
        receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_BUILD);
        receiptHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
        receiptHeader.setCreatedBy(ShiroUtils.getLoginName());
        receiptHeader.setLastUpdatedBy(ShiroUtils.getLoginName());
        return AjaxResult.toAjax(this.save(receiptHeader));
    }

    /**
     * 根据单据类型生成单号  规则:入库单类型 + 年月日 + 5位(排序号 + 1)
     * @param receiptType
     * @return
     */
    //todo:huhai 单据生成规则,后续版本计划追加一个序列号生成规则表:统一设定前缀,填充位,流水码等
    public String createCode(String receiptType) {
        String code = null;
        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        LambdaQueryWrapper<ReceiptHeader> lambda = Wrappers.lambdaQuery();
        lambda.select(ReceiptHeader::getCode).eq(ReceiptHeader::getReceiptType, receiptType)
            .orderByDesc(ReceiptHeader::getId).last("Limit 1");
        //如果指定类型的最后的code存在,并且日期一致。那么 code = 入库单类型 + 年月日 + (排序号 + 1)
        String maxCode = null;
        ReceiptHeader receiptHeader = this.getOne(lambda);
        if ( receiptHeader != null){
            maxCode = this.getOne(lambda).getCode();
        }
        //todo:隐藏了BUG,请修正。 修改好  去掉todo
        if (maxCode != null && maxCode.substring(maxCode.length() - 13, maxCode.length() - 5).equals(df.format(now))) {
            Integer Count = Integer.valueOf(maxCode.substring(maxCode.length() - 5, maxCode.length()));
            code = receiptType + df.format(now) + String.format("%05d", Count + 1);
        } else {
            code = receiptType + df.format(now) + "00001";
        }
        return code;
    }

    /**
     * 入库预约
     * @param receiptHeader
     * @return
     */
    public AjaxResult reservation(ReceiptHeader receiptHeader) {
        LambdaQueryWrapper<ReceiptDetail> receiptDetailLambda = Wrappers.lambdaQuery();
        receiptDetailLambda.eq(ReceiptDetail::getReceiptId, receiptHeader.getId());
        List<ReceiptDetail> receiptDetails = receiptDetailService.list(receiptDetailLambda);
        //更新入库明细流程
        for (ReceiptDetail receiptDetail : receiptDetails) {
            receiptDetail.setProcessStamp(QuantityConstant.RECEIPT_HEADER_RESERVATION.toString());
            if (!receiptDetailService.updateById(receiptDetail)){
                throw new ServiceException("更新入库明细处理标记失败");
            }
            receiptDetail = receiptDetailService.queryflow(receiptDetail);
            if (!receiptDetailService.updateById(receiptDetail)){
               throw new ServiceException("更新入库明细流程失败");
           }
        }
        receiptHeader.setLastUpdatedBy(ShiroUtils.getLoginName());
        //更新入库单预约时间
        if (!this.updateById(receiptHeader)){
            throw new ServiceException("更新入库单预约时间失败");
        }
        receiptDetailService.updateReceiptHeaderLastStatus(receiptHeader.getId());
        return AjaxResult.success("预约成功");
    }

    /**
     * 入库到货
     * @param id
     * @return
     */
    public AjaxResult arrival(Integer id) {
        LambdaQueryWrapper<ReceiptDetail> receiptDetailLambda = Wrappers.lambdaQuery();
        receiptDetailLambda.eq(ReceiptDetail::getReceiptId, id)
                .eq(ReceiptDetail::getProcessStamp, QuantityConstant.RECEIPT_HEADER_ARRIVAL.toString());
        List<ReceiptDetail> receiptDetails = receiptDetailService.list(receiptDetailLambda);
        //更新入库明细流程
        for (ReceiptDetail receiptDetail : receiptDetails) {
            receiptDetailService.queryflow(receiptDetail);
        }
        //更新入库单头表
        receiptDetailService.updateReceiptHeaderLastStatus(id);
        return AjaxResult.success("预约成功");
    }

    /**
     * 入库质检
     */
    public AjaxResult check(Integer id){

        ReceiptHeader receiptHeader = this.getById(id);
        LambdaQueryWrapper<ReceiptDetail> receiptDetailLambda = Wrappers.lambdaQuery();
        receiptDetailLambda.eq(ReceiptDetail::getReceiptId, id);
        List<ReceiptDetail> receiptDetails = receiptDetailService.list(receiptDetailLambda);

        //判断是否已经生成了质检单
        LambdaQueryWrapper<CheckHeader> checkHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        checkHeaderLambdaQueryWrapper.eq(CheckHeader::getCode, receiptHeader.getCode());
        CheckHeader checkHeaderQuery = checkHeaderService.getOne(checkHeaderLambdaQueryWrapper);
        if (checkHeaderQuery != null){
            throw new ServiceException("质检单已生成");
        }
        //创建质检头表
        CheckHeader checkHeader = new CheckHeader();
        checkHeader.setCode(receiptHeader.getCode());
        checkHeader.setType(QuantityConstant.CHECK_TYPE_RECEIPT.toString());
        checkHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
        checkHeader.setReferCode(receiptHeader.getReferCode());
        checkHeader.setEnable(1);
        checkHeader.setStatus(QuantityConstant.CHECK_STATUS_BUILD.toString());
        checkHeader.setCreatedBy(ShiroUtils.getLoginName());
        checkHeader.setLastUpdatedBy(ShiroUtils.getLoginName());

        if (!checkHeaderService.save(checkHeader)){
            throw new ServiceException("添加质检头表错误");
        }

        //查询保存后的质检头表
        LambdaQueryWrapper<CheckHeader> checkHeaderLambda = Wrappers.lambdaQuery(checkHeader);
        checkHeader = checkHeaderService.getOne(checkHeaderLambda);

        for (ReceiptDetail receiptDetail : receiptDetails) {
            CheckDetail checkDetail = new CheckDetail();
            checkDetail.setCheckHeaderId(checkHeader.getId());
            checkDetail.setWarehouseCode(ShiroUtils.getWarehouseCode());
            checkDetail.setCheckCode(checkHeader.getCode());
            checkDetail.setReceiptDetailId(receiptDetail.getId());
            checkDetail.setReceiptCode(receiptDetail.getReceiptCode());
            checkDetail.setReferCode(checkHeader.getReferCode());
            checkDetail.setMaterialCode(receiptDetail.getMaterialCode());
            checkDetail.setMaterialName(receiptDetail.getMaterialName());
            checkDetail.setMaterialSpec(receiptDetail.getMaterialSpec());
            checkDetail.setMaterialUnit(receiptDetail.getMaterialUnit());
            checkDetail.setCompanyCode(receiptDetail.getCompanyCode());
            checkDetail.setInventorySts(receiptDetail.getInventorySts());
            checkDetail.setStatus(QuantityConstant.CHECK_STATUS_BUILD.toString());
            checkDetail.setQty(receiptDetail.getTotalQty());
            checkDetail.setCheckBy(ShiroUtils.getLoginName());
            checkDetail.setCreatedBy(ShiroUtils.getLoginName());
            checkDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
            if ( !checkDetailService.save(checkDetail) ){
                throw new ServiceException("保存质检明细失败");
            }
        }
        return AjaxResult.success("创建质检单成功");
    }

    /**
     * 提交审核入库单
     */
    @Transactional
    public AjaxResult submitReview(String ids){
        String warehouseCode=ShiroUtils.getWarehouseCode();
        if (StringUtils.isEmpty(ids)){
            return AjaxResult.error("仓库:"+warehouseCode+",提交入库单审核,id不能为空");
        }
        Integer[] idArray = Convert.toIntArray(ids);
        for (int i = 0; i<idArray.length; i++){
            ReceiptHeader receiptHeader = this.getById(idArray[i]);
            if (receiptHeader.getFirstStatus()>QuantityConstant.RECEIPT_HEADER_VERIFYING
                    ||receiptHeader.getLastStatus()>QuantityConstant.RECEIPT_HEADER_VERIFYING){
                return AjaxResult.error("仓库:"+warehouseCode+",单据号:"+receiptHeader.getCode()+"入库单不需要审核");
            }
            LambdaQueryWrapper<ReceiptDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
            lambdaQueryWrapper.eq(ReceiptDetail::getReceiptId, idArray[i]);
            List<ReceiptDetail> receiptDetailList = receiptDetailService.list(lambdaQueryWrapper);
            if (receiptDetailList.isEmpty()){
                return AjaxResult.error("仓库:"+warehouseCode+",单据号:"+receiptHeader.getCode()+"入库单没有明细");
            }
            receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_VERIFYING);
            receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_VERIFYING);
            if (!this.updateById(receiptHeader)){
                throw new ServiceException("仓库:"+warehouseCode+",单据号:"+receiptHeader.getCode()+" 失败");
            }
        }
        return AjaxResult.success("仓库:"+warehouseCode+",提交审核成功");
    }

    /**
     * 到货通知
     * @param id
     * @return
     */
    public AjaxResult arrivalNotice(String id){
        LambdaQueryWrapper<ReceiptDetail> lambda = Wrappers.lambdaQuery();
        lambda.eq(ReceiptDetail::getReceiptId, id);
        List<ReceiptDetail> receiptDetails = receiptDetailService.list(lambda);
        for (ReceiptDetail receiptDetail : receiptDetails) {
            receiptDetail.setProcessStamp(QuantityConstant.RECEIPT_HEADER_ARRIVAL.toString());
            receiptDetailService.updateById(receiptDetail);
            if (!receiptDetailService.updateById(receiptDetailService.queryflow(receiptDetail))){
                throw new ServiceException("更新入库明细处理标记出错");
            }
            receiptDetailService.updateReceiptHeaderLastStatus(receiptDetail.getReceiptId());
        }
        return AjaxResult.success("");
    }

    /**
     * 加入订单池
     * @param ids 头表id字符串
     * @return
     */
    @Transactional
    public AjaxResult addPool(String ids){
        List<Integer> idList = Arrays.asList(Convert.toIntArray(ids));
        for (Integer id : idList){
            LambdaQueryWrapper<ReceiptDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
            lambdaQueryWrapper.eq(ReceiptDetail::getReceiptId, id);
            List<ReceiptDetail> list = receiptDetailService.list(lambdaQueryWrapper);
            if (list.size() != 0){
                for (ReceiptDetail receiptDetail : list){
                    if (Integer.parseInt(receiptDetail.getProcessStamp()) < QuantityConstant.RECEIPT_HEADER_POOL){
                        //判断入库明细是否需要审核
                        ReceiptDetail beforeReceiptDetail = receiptDetailService.queryflow(receiptDetail);
                        if (Integer.parseInt(beforeReceiptDetail.getProcessStamp()) == 5 || Integer.parseInt(beforeReceiptDetail.getProcessStamp()) == 100){
                            throw new ServiceException("请先提交审核");
                        }
                        //加入订单池
                        receiptDetail.setProcessStamp(QuantityConstant.RECEIPT_HEADER_POOL.toString());
                        ReceiptDetail receiptDetail1 = receiptDetailService.queryflow(receiptDetail);
                        if (!receiptDetailService.updateById(receiptDetail1)){
                            throw new ServiceException("更新入库明细状态失败");
                        }
                    } else {
                        receiptDetailService.updateReceiptHeaderLastStatus(receiptDetail.getReceiptId());
                    }
                }
            }
            //更新头表状态
            receiptDetailService.updateReceiptHeaderLastStatus(id);
        }
        return AjaxResult.success("加入成功");
    }


    public List<ReceiptHeader> selectListByCreated() {
        return receiptHeaderMapper.selectListByCreated();
    }


    @Transactional(rollbackFor = Exception.class)
    public Integer createTodayHeader(String companyCode) {
        String receiptType = "PPI";
        String code = null;
        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        String maxCode = receiptHeaderMapper.createCode(receiptType);
        //如果指定类型的最后的code存在,并且日期一致。那么 code = 入库单类型 + 年月日 + (排序号 + 1)
        if (maxCode != null && maxCode.substring(maxCode.length() - 13, maxCode.length() - 5).equals(df.format(now)))
        {
            Integer Count = Integer.valueOf(maxCode.substring(maxCode.length() - 5, maxCode.length()));
            code = receiptType + df.format(now) + String.format("%05d", Count + 1);
        }
        else
        {
            code = receiptType + df.format(now) + "00001";
        }
        ReceiptHeader receiptHeader = new ReceiptHeader();
        receiptHeader.setId(null);
        receiptHeader.setCode(code);
        receiptHeader.setReceiptType(receiptType);
        receiptHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
        receiptHeader.setCreated(null);
        receiptHeader.setCreatedBy(ShiroUtils.getLoginName());
        receiptHeader.setLastUpdated(null);
        receiptHeader.setLastUpdatedBy(ShiroUtils.getLoginName());
        receiptHeader.setFirstStatus(200);
        receiptHeader.setLastStatus(200);
        receiptHeader.setCompanyCode(companyCode);
        this.save(receiptHeader);
        return receiptHeader.getId();
    }

    @Transactional(rollbackFor = Exception.class)
    public List<ReceiptHeader> getLatestReceipt(String warehouseCode, String companyCode) {
        return receiptHeaderMapper.getLatestReceipt(warehouseCode, companyCode);
    }

    /**
     * 修改回传后状态
     * @param id
     * @return
     */
    private AjaxResult editReceiveStatus(Integer id) {
        ReceiptHeader receiptHeader = new ReceiptHeader();
        receiptHeader.setId(id);
        receiptHeader.setFirstStatus(900);
        receiptHeader.setLastStatus(900);
        if (!updateById(receiptHeader)) {
            throw new ServiceException("更新头表状态失败");
        } else {
            LambdaQueryWrapper<ReceiptDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
            lambdaQueryWrapper.eq(ReceiptDetail::getReceiptId, id);
            List<ReceiptDetail> receiptDetails = receiptDetailService.list(lambdaQueryWrapper);
            for (ReceiptDetail receiptDetail : receiptDetails) {
                receiptDetail.setProcessStamp("900");
            }
            if (!receiptDetailService.updateBatchById(receiptDetails)) {
                throw new ServiceException("更新明细失败");
            }
        }
        return AjaxResult.success("");
    }
}