ReceiptHeaderService.java 13.2 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.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.receiptType.domain.ReceiptType;
import com.huaheng.pc.config.receiptType.service.ReceiptTypeService;
import com.huaheng.pc.config.sendMail.service.SendMailService;
import com.huaheng.pc.config.statusFlow.domain.StatusFlowDetail;
import com.huaheng.pc.config.statusFlow.service.StatusFlowDetailService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailServiceImpl;
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.lang.ref.WeakReference;
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;

    /**
     * 保存入库单
     * @param receiptHeader
     * @return
     */
    public AjaxResult saveReceiptHeader(ReceiptHeader receiptHeader){
        LambdaQueryWrapper<ReceiptType> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ReceiptType::getCode, receiptHeader.getReceiptType());
        ReceiptType receiptType = receiptTypeService.getOne(lambdaQueryWrapper);

        //流程详情查询
        LambdaQueryWrapper<StatusFlowDetail> lambdaStatus = Wrappers.lambdaQuery();
        lambdaStatus.orderByAsc(StatusFlowDetail::getSequence);

        if(receiptType == null) {
            throw new ServiceException("没有对应的入库单类型");
        }

        String code = createCode(receiptHeader.getReceiptType());
        receiptHeader.setCode(code);
        receiptHeader.setFirstStatus(0);
        receiptHeader.setLastStatus(0);
        receiptHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
        receiptHeader.setCreatedBy(ShiroUtils.getLoginName());
        receiptHeader.setLastUpdatedBy(ShiroUtils.getLoginName());
        return AjaxResult.toAjax(this.save(receiptHeader));
    }

    /**
     * 生成单号
     * @param receiptType
     * @return
     */
    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();
        }
        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("120");
            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, "150");
        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("100");
        checkHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
        checkHeader.setReferCode(receiptHeader.getReferCode());
        checkHeader.setEnable(1);
        checkHeader.setStatus("0");
        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("0");
            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("创建质检单成功");
    }

    /**
     * 提交审核入库单
     */
    public AjaxResult submitReview(String ids){
        if (StringUtils.isEmpty(ids)){
            return AjaxResult.error("id不能为空");
        }
        Integer[] idArray = Convert.toIntArray(ids);
        for (int i = 0; i<idArray.length; i++){
            ReceiptHeader receiptHeader = this.getById(idArray[i]);
            if (receiptHeader.getFirstStatus()>5||receiptHeader.getLastStatus()>5){
                return AjaxResult.error("单据号为"+receiptHeader.getCode()+"入库单不需要审核");
            }
            receiptHeader.setFirstStatus(5);
            receiptHeader.setLastStatus(5);
            if (!this.updateById(receiptHeader)){
                throw new ServiceException("更新入库头表失败");
            } else {
                return AjaxResult.success("提交审核成功,请等待审核");
            }
        }
        return AjaxResult.success("提交审核成功");
    }

    /**
     * 到货通知
     * @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(String.valueOf(150));
            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()) < 100){
                        //判断入库明细是否需要审核
                        ReceiptDetail beforeReceiptDetail = receiptDetailService.queryflow(receiptDetail);
                        if (Integer.parseInt(beforeReceiptDetail.getProcessStamp()) == 5 || Integer.parseInt(beforeReceiptDetail.getProcessStamp()) == 100){
                            throw new ServiceException("请先提交审核");
                        }
                        //加入订单池
                        receiptDetail.setProcessStamp("100");
                        ReceiptDetail receiptDetail1 = receiptDetailService.queryflow(receiptDetail);
                        if (!receiptDetailService.updateById(receiptDetail1)){
                            throw new ServiceException("更新入库明细状态失败");
                        }
                    } else {
                        throw new ServiceException("已有入库明细进入订单池");
                    }
                }
            }
            //更新头表状态
            receiptDetailService.updateReceiptHeaderLastStatus(id);
        }
        return AjaxResult.success("加入成功");
    }
}