ReceiptApiService.java 14.5 KB
package com.huaheng.api.general.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.api.general.controller.ReceiptApi;
import com.huaheng.api.general.domain.ReceiptDomain;
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.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.FilterConfigDetail.domain.FilterConfigDetail;
import com.huaheng.pc.config.FilterConfigDetail.service.FilterConfigDetailService;
import com.huaheng.pc.config.company.domain.Company;
import com.huaheng.pc.config.company.service.CompanyService;
import com.huaheng.pc.config.material.domain.Material;
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.statusFlow.domain.StatusFlowHeader;
import com.huaheng.pc.config.statusFlow.service.StatusFlowHeaderService;
import com.huaheng.pc.config.supplier.domain.Supplier;
import com.huaheng.pc.config.supplier.service.SupplierService;
import com.huaheng.pc.config.warehouse.domain.Warehouse;
import com.huaheng.pc.config.warehouse.service.WarehouseService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.receipt.receiptDetailHistory.domain.ReceiptDetailHistory;
import com.huaheng.pc.receipt.receiptDetailHistory.service.ReceiptDetailHistoryService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
import com.huaheng.pc.receipt.receiptHeaderHistory.domain.ReceiptHeaderHistory;
import com.huaheng.pc.receipt.receiptHeaderHistory.service.ReceiptHeaderHistoryService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
@Transactional
public class ReceiptApiService {
    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private ReceiptTypeService receiptTypeService;
    @Resource
    private WarehouseService warehouseService;
    @Resource
    private CompanyService companyService;
    @Resource
    private MaterialService materialService;
    @Resource
    private SupplierService supplierService;
    @Resource
    private FilterConfigDetailService filterConfigDetailService;
    @Resource
    private StatusFlowHeaderService statusFlowHeaderService;
    @Resource
    private ReceiptHeaderHistoryService receiptHeaderHistoryService;
    @Resource
    private ReceiptDetailHistoryService receiptDetailHistoryService;
    /**
     * 入库单下发
     * @param receipt 入库单
     * @return 是否下发成功
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult receipt(ReceiptDomain receipt){

        /* 0.step 获取入库头表,获取入库明细*/
        ReceiptHeader receiptHeader = receipt.getReceiptHeader();
        List<ReceiptDetail> receiptDetails = receipt.getReceiptDetails();
        /* 1.step 校验入库单是否为空*/
        if (receiptHeader == null || receiptDetails.isEmpty()){
            throw new ServiceException("入库主单或入库子单为空");
        }
        /* 2.step 检查入库头表合法性*/
        AjaxResult checkReceiptHeaderResult = checkReceiptHeader(receiptHeader);
        if (checkReceiptHeaderResult.hasErr() ){
            return AjaxResult.error(checkReceiptHeaderResult.getMsg());
        }

        /* 3.step 检查入库明细合法性*/
        AjaxResult checkReceiptDetailResult = checkReceiptDetail(receiptDetails);
        if (checkReceiptDetailResult.hasErr() ){
            return AjaxResult.error(checkReceiptDetailResult.getMsg());
        }

        /* 4.step 计算入库明细总行数、总数量*/
        BigDecimal totalQty = new BigDecimal(0);
        for (ReceiptDetail receiptDetail: receiptDetails) {
            totalQty.add(receiptDetail.getTotalQty());
        }
        receiptHeader.setTotalLines(receiptDetails.size());
        receiptHeader.setTotalQty(totalQty);

        /* 5.step 保存的入库头表*/
        if (!receiptHeaderService.save(receiptHeader)){
            throw new ServiceException("保存入库头表失败");
        }

        /* 6.step 保存入库明细*/
        LambdaQueryWrapper<ReceiptHeader> lambda = Wrappers.lambdaQuery();
        lambda.eq(ReceiptHeader::getWarehouseCode, receiptHeader.getWarehouseCode())
                .eq(ReceiptHeader::getCode, receiptHeader.getCode());
        receiptHeader = receiptHeaderService.getOne(lambda);
        List<ReceiptDetail> jsonListDetail= new ArrayList<ReceiptDetail>();
        for (ReceiptDetail receiptDetail : receiptDetails) {
            receiptDetail.setReceiptId(receiptHeader.getId());
            receiptDetail.setReceiptCode(receiptHeader.getCode());
            receiptDetail.setWarehouseCode(receiptHeader.getWarehouseCode());
            receiptDetail.setCompanyCode(receiptHeader.getWarehouseCode());
            Material material = materialService.findAllByCode(receiptDetail.getMaterialCode());
            receiptDetail.setMaterialName(material.getName());
            receiptDetail.setMaterialSpec(material.getSpec());
            receiptDetail.setMaterialUnit(material.getUnit());
            if (!receiptDetailService.save(receiptDetail)){
                throw new ServiceException("保存入库明细失败");
            }
                jsonListDetail.add(receiptDetail);

        }
        /* 6.step回传入库单和入库明细单 */
        ReceiptDomain receiptDomain =new ReceiptDomain();
        receiptDomain.setReceiptHeader(receiptHeader);
        receiptDomain.setReceiptDetails(jsonListDetail);

        return AjaxResult.success(receiptDomain);
    }

    /**
     * 检查入库头表合法性
     * @param receiptHeader 入库头表
     * @return 检查结果
     */
    private AjaxResult checkReceiptHeader(ReceiptHeader receiptHeader){
        /* 0.step 必填项是否为空*/
        if (StringUtils.isEmpty(receiptHeader.getCode()) || StringUtils.isEmpty(receiptHeader.getCompanyCode()) ||
                StringUtils.isEmpty(receiptHeader.getWarehouseCode()) || StringUtils.isEmpty(receiptHeader.getReceiptType())){
            return AjaxResult.error("入库主单字段有误");
        }

        /* 1.step 查询该单据编码是否已存在*/
        LambdaQueryWrapper<ReceiptHeader> receiptHeaderLambda = Wrappers.lambdaQuery(receiptHeader);
        if (receiptHeaderService.getOne(receiptHeaderLambda) != null){
            return AjaxResult.error("该单据已存在:"+receiptHeader.getCode());
        }

        /* 2.step 判断入库类型是否匹配*/
        LambdaQueryWrapper<ReceiptType> receiptTypeLambda = Wrappers.lambdaQuery();
        receiptTypeLambda.eq(ReceiptType::getCode, receiptHeader.getReceiptType())
                         .eq(ReceiptType::getWarehouseCode,receiptHeader.getWarehouseCode())
                          .eq(ReceiptType::getCompanyCode,receiptHeader.getCompanyCode());
        if (receiptTypeService.getOne(receiptTypeLambda) == null){
            return AjaxResult.error("没有对应的入库单类型");
        }

        /* 3.step 判断仓库是否存在*/
        LambdaQueryWrapper<Warehouse> warehouseLambda = Wrappers.lambdaQuery();
        warehouseLambda.eq(Warehouse::getCode, receiptHeader.getWarehouseCode());
        if (warehouseService.getOne(warehouseLambda) == null){
            return AjaxResult.error("该仓库不存在");
        }

        /* 4.step 判断货主是否存在*/
        LambdaQueryWrapper<Company> companyLambda = Wrappers.lambdaQuery();
        companyLambda.eq(Company::getCode, receiptHeader.getCompanyCode());
        if (companyService.getOne(companyLambda) == null){
            return AjaxResult.error("该货主不存在");
        }

        return AjaxResult.success("");
    }

    /**
     * 检查入库明细合法性
     * @param receiptDetails 入库明细
     * @return 检查结果
     */
    private AjaxResult checkReceiptDetail(List<ReceiptDetail> receiptDetails){

        for (ReceiptDetail receiptDetail: receiptDetails) {
            /* 0.step 判断必填字段是否为空且总数量不能为0*/
            if (receiptDetail.getMaterialCode() == null ||
                    new BigDecimal(0).compareTo(receiptDetail.getTotalQty() != null ?
                            receiptDetail.getTotalQty() : new BigDecimal(0)) == 0 ){
                return AjaxResult.error("入库明细字段有误");
            }

            /* 1.step 判断供应商是否存在*/
            if (receiptDetail.getSupplierCode() != null){
                LambdaQueryWrapper<Supplier> supplierLambda = Wrappers.lambdaQuery();
                supplierLambda.eq(Supplier::getCode, receiptDetail.getSupplierCode());
                if (supplierService.getOne(supplierLambda) == null) {
                    return AjaxResult.error("供应商不存在");
                }
            }

            /* 2.step 判断定位规则是否存在*/
            if (receiptDetail.getLocatingRule() != null){
                LambdaQueryWrapper<FilterConfigDetail> filterConfigDetailLambda = Wrappers.lambdaQuery();
                filterConfigDetailLambda.eq(FilterConfigDetail::getCode, receiptDetail.getLocatingRule())
                        .eq(FilterConfigDetail::getModuleType, "receipt")
                        .eq(FilterConfigDetail::getRecordType, "locationRule");
                FilterConfigDetail filterConfigDetail = filterConfigDetailService.getOne(filterConfigDetailLambda);
                if (filterConfigDetail == null){
                    return AjaxResult.error(receiptDetail.getLocatingRule()+"定位规则不存在");
                }
            }

            /* 3.step 判断流程编码是否存在*/
            if (receiptDetail.getStatusFlowCode() != null){
                LambdaQueryWrapper<StatusFlowHeader> statusFlowHeaderLambda = Wrappers.lambdaQuery();
                statusFlowHeaderLambda.eq(StatusFlowHeader::getCode, receiptDetail.getStatusFlowCode())
                        .eq(StatusFlowHeader::getModuleType, "receipt")
                        .eq(StatusFlowHeader::getRecordType, "receivingFlow");
                StatusFlowHeader statusFlowHeader = statusFlowHeaderService.getOne(statusFlowHeaderLambda);
                if (statusFlowHeader == null){
                    return AjaxResult.error(receiptDetail.getLocatingRule()+"该流程不存在");
                }
            }
        }

        return AjaxResult.success("");
    }


    @Transactional
    public AjaxResult remove(Integer[] ids){
        List<Integer> idList = Arrays.asList(ids);
        for (Integer id : idList) {
            ReceiptHeader receiptHeader = receiptHeaderService.getById(id);
            if (receiptHeader == null)  {
                return AjaxResult.success("");
            }
            if((receiptHeader.getFirstStatus()>= QuantityConstant.RECEIPT_HEADER_POSTING && receiptHeader.getLastStatus()>=QuantityConstant.RECEIPT_HEADER_POSTING)||
                    (receiptHeader.getFirstStatus()<QuantityConstant.RECEIPT_HEADER_POOL &receiptHeader.getLastStatus()<QuantityConstant.RECEIPT_HEADER_POOL)){
                ReceiptHeaderHistory receiptHeaderHistory = new ReceiptHeaderHistory();
                List<ReceiptDetailHistory> receiptDetailHistoryList = new ArrayList<>();
                //查询入库单明细
                LambdaQueryWrapper<ReceiptDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
                lambdaQueryWrapper.eq(ReceiptDetail::getReceiptId, id);
                List<ReceiptDetail> list = receiptDetailService.list(lambdaQueryWrapper);
                try {
                    //复制到入库历史实体
                    BeanUtils.copyProperties(receiptHeaderHistory, receiptHeader);
                    for (ReceiptDetail receiptDetail: list) {
                        ReceiptDetailHistory receiptDetailHistory = new ReceiptDetailHistory();
                        BeanUtils.copyProperties(receiptDetailHistory, receiptDetail);
                        receiptDetailHistoryList.add(receiptDetailHistory);
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }

                receiptHeaderHistory.setLastUpdatedBy(ShiroUtils.getLoginName());
                if (!receiptHeaderService.removeById(receiptHeader.getId())){
                    throw new ServiceException("删除头表失败");
                }
                if(!receiptHeaderHistoryService.save(receiptHeaderHistory)){
                    throw new ServiceException("新增历史入库单失败");
                }
                // 当存在明细时删除
                if (list.size()!= 0){
                    //删除入库明细
                    List<Integer> receiptDetailIds = new ArrayList<>();
                    for (int i=0; i<receiptDetailHistoryList.size();i++){
                        receiptDetailHistoryList.get(i).setLastUpdatedBy(ShiroUtils.getLoginName());
                        receiptDetailHistoryList.get(i).setReceiptId(receiptHeaderHistory.getId());
                        receiptDetailIds.add(receiptDetailHistoryList.get(i).getId());
                    }
                    if (!receiptDetailService.removeByIds(receiptDetailIds)) {
                        throw new ServiceException("删除明细表失败");
                    }
                    if (!receiptDetailHistoryService.saveBatch(receiptDetailHistoryList)){
                        throw new ServiceException("新增明细失败");
                    }                }

            }else {
                return AjaxResult.success("入库单没有完成,无法删除");
            }
        }
        return AjaxResult.success("删除成功");
    }
}