AdjustService.java 9.63 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.domain.AdjustDomain;
import com.huaheng.common.exception.service.ServiceException;
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.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.inventory.adjustDetail.domain.AdjustDetail;
import com.huaheng.pc.inventory.adjustDetail.service.AdjustDetailService;
import com.huaheng.pc.inventory.adjustHeader.domain.AdjustHeader;
import com.huaheng.pc.inventory.adjustHeader.mapper.AdjustHeaderMapper;
import com.huaheng.pc.inventory.adjustHeader.service.AdjustHeaderService;
import com.huaheng.pc.inventory.cycleCountDetail.domain.CycleCountDetail;
import com.huaheng.pc.inventory.cycleCountDetail.service.CycleCountDetailService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * Created by Enzo Cotter on 2019/11/4.
 * @author mahuandong
 */
@Component
@Transactional(rollbackFor = Exception.class)
public class AdjustService {

    @Resource
    private AdjustHeaderMapper adjustHeaderMapper;
    @Resource
    private AdjustDetailService adjustDetailService;
    @Resource
    private AdjustHeaderService adjustHeaderService;
    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private CycleCountDetailService cycleCountDetailService;
    @Resource
    private CheckDetailService checkDetailService;
    @Resource
    private MaterialService materialService;
    /**
     * 调整单下发
     * @param adjustDomain 调整单
     * @return 是否下发成功
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertAdjust(AdjustDomain adjustDomain){
        AdjustHeader adjustHeader =adjustDomain.getAdjustHeader();

        List<AdjustDetail> adjustDetailList =adjustDomain.getAdjustDetailList();

        if (adjustHeader==null || adjustDetailList==null){
            return AjaxResult.error("主单据和单据明细信息不能为空");
        }

        if (StringUtils.isEmpty(adjustDomain.getAdjustHeader().getProblemType())){
            return AjaxResult.error("调整类型不能为空");
        }
        if (StringUtils.isEmpty(adjustDomain.getAdjustHeader().getWarehouseCode())){
            return AjaxResult.error("仓库编码不能为空");
        }
        if (StringUtils.isEmpty(adjustDomain.getAdjustHeader().getCompanyCode())){
            return AjaxResult.error("货主编码不能为空");
        }
        AdjustHeader adjustHeaderCode= this.addSave(adjustDomain.getAdjustHeader());

        //查询主单及加入仓库和货主,盘点,质检单编码
        if(adjustHeaderCode == null){
            return AjaxResult.error("没有对应的主单据!");
        }
        List<AdjustDetail> adjustDetails =new ArrayList<>();
        for (AdjustDetail detail:adjustDetailList) {
            detail.setWarehouseCode(adjustHeaderCode.getWarehouseCode());
            detail.setAdjustHeaderCode(adjustHeaderCode.getCode());
            detail.setCompanyCode(adjustHeaderCode.getCompanyCode());
            detail.setCycleCountHeaderCode(adjustHeaderCode.getCycleCountHeaderCode());
            detail.setCheckHeaderCode(adjustHeaderCode.getCheckHeaderCode());
             this.addDetails(detail);
            adjustDetails.add(detail);

        }
        AdjustDomain adjustDomainAjax =new AdjustDomain();
        adjustDomainAjax.setAdjustHeader(adjustHeaderCode);
        adjustDomainAjax.setAdjustDetailList(adjustDetails);

        return AjaxResult.success(adjustDomainAjax);
    }

    /**
     * 保存新增调整头
     * @param adjustHeader
     * @return
     */
    @Transactional
    public AdjustHeader addSave(AdjustHeader adjustHeader) {
        //校验调整类型
        //盘点调整,质检调整需要和盘点和质检单据确认。
        switch (adjustHeader.getProblemType()){
            case "checkAdjust":   //质检调整
                if(StringUtils.isEmpty(adjustHeader.getCheckHeaderCode())){
                    throw new SecurityException("选择单据类型为质检调整时,质检单编码不能为空!");
                }
                break;
            case "cyclecountAdjust":   //盘点调整
                if(StringUtils.isEmpty(adjustHeader.getCycleCountHeaderCode())){
                    throw new SecurityException("选择单据类型为盘点调整时,盘点单编码不能为空!");
                }
                break;
            case "adjust":   //普通调整
                if(StringUtils.isEmpty(adjustHeader.getCycleCountHeaderCode())){
                    throw new SecurityException("选择单据类型为普通调整时,盘点单编码不能为空!");
                }
                break;
            default:
                throw new ServiceException("类型错误!");

        }

        adjustHeader.setCreated(new Date());
        adjustHeader.setCreatedBy(ShiroUtils.getLoginName());
        adjustHeader.setCode(this.createCode());
        if (!adjustHeaderService.save(adjustHeader)){
            throw new SecurityException("新增调整单失败!");
        }
        return adjustHeader;
    }


    @Transactional
    public AjaxResult addDetails(AdjustDetail adjustDetail) {

        //查询主单据
        AdjustHeader adjustHeader = new AdjustHeader();
        adjustHeader.setCode(adjustDetail.getAdjustHeaderCode());
        adjustHeader.setWarehouseCode(adjustDetail.getWarehouseCode());
        adjustHeader.setCompanyCode(adjustDetail.getCompanyCode());
        LambdaQueryWrapper<AdjustHeader> lambdaQueryWrapper = Wrappers.lambdaQuery(adjustHeader);
        adjustHeader = adjustHeaderService.getOne(lambdaQueryWrapper);
        //检查库存
        if((adjustDetail.getInventoryDetailId()) != null){
            InventoryDetail inventoryDetail = inventoryDetailService.getById(adjustDetail.getInventoryDetailId());
            if(inventoryDetail == null){
                return AjaxResult.error("没有该条库存明细");
            }
        }
        //明细单据的上层单编码需要和主单一直,主单空值则不判断
        if(StringUtils.isNotEmpty(adjustHeader.getCycleCountHeaderCode()) || StringUtils.isNotEmpty(adjustHeader.getCheckHeaderCode())){
            adjustDetail.setCycleCountHeaderCode(adjustHeader.getCycleCountHeaderCode());
            adjustDetail.setCheckHeaderCode(adjustHeader.getCheckHeaderCode());
        }
        //检查盘点单
        if(StringUtils.isNotEmpty(adjustDetail.getCycleCountHeaderCode())){
            LambdaQueryWrapper<CycleCountDetail> queryCycleCountCode = Wrappers.lambdaQuery();
            queryCycleCountCode.eq(CycleCountDetail::getCycleCountHeadCode,adjustDetail.getCycleCountHeaderCode());
            CycleCountDetail cycleCountDetail = cycleCountDetailService.list(queryCycleCountCode).get(0);
            if(cycleCountDetail == null || !cycleCountDetail.getCycleCountHeadCode().equals(adjustDetail.getCycleCountHeaderCode())){
                return AjaxResult.error("盘点单错误,请核对盘点单据!");
            }
        }
        //检查质检单
        if(StringUtils.isNotEmpty(adjustDetail.getCheckHeaderCode())){
            CheckDetail checkDetail = checkDetailService.getById(adjustDetail.getCheckDetailId());
            if(checkDetail == null || !checkDetail.getCheckCode().equals(adjustDetail.getCheckHeaderCode())){
                return AjaxResult.error("质检单错误,请核对质检单据!");
            }
        }
        //检查物料
        Material material = materialService.findAllByCode(adjustDetail.getMaterialCode());
        if(material == null){
            return AjaxResult.error("物料编码错误!");
        }

        adjustDetail.setProblemType(adjustHeader.getProblemType());//调整类型
        adjustDetail.setMaterialUnit(material.getUnit());
        adjustDetail.setMaterialSpec(material.getSpec());
        adjustDetail.setCreated(new Date());
        adjustDetail.setCreatedBy(ShiroUtils.getLoginName());
        adjustDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
        adjustDetail.setLastUpdated(new Date());
        adjustDetailService.saveOrUpdate(adjustDetail);

        return AjaxResult.success("新增调整明细成功!");
    }



    //生成差异单号
    public String createCode() {
        String code;
        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        String maxCode =  adjustHeaderMapper.getLastCode();;
        //如果指定类型的最后的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 = "AD" + df.format(now) + String.format("%05d", Count + 1);
        }
        else
        {
            code = "AD" + df.format(now) + "00001";
        }
        return code;
    }

}