CycleCountService.java 8.08 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.CycCountDomain;
import com.huaheng.common.constant.QuantityConstant;
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.config.configValue.domain.ConfigValue;
import com.huaheng.pc.config.configValue.service.ConfigValueService;
import com.huaheng.pc.config.cycleCountPreference.service.CycleCountPreferenceService;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.inventory.cycleCountDetail.domain.CycleCountDetail;
import com.huaheng.pc.inventory.cycleCountDetail.service.CycleCountDetailService;
import com.huaheng.pc.inventory.cycleCountHeader.domain.CycleCountHeader;
import com.huaheng.pc.inventory.cycleCountHeader.service.CycleCountHeaderService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
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 CycleCountService {

    @Resource
    private CycleCountHeaderService cycleCountHeaderService;
    @Resource
    private CycleCountDetailService cycleCountDetailService;
    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private LocationService locationService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private TaskDetailService taskDetailService;
    @Resource
    private ConfigValueService configValueService;
    @Resource
    private CycleCountPreferenceService cycleCountPreferenceService;
    /**
     * 盘点单下发
     * @param cycCountDomain 盘点单
     * @return 是否下发成功
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertCycleCount(CycCountDomain cycCountDomain){
        CycleCountHeader cycleCountHeader =cycCountDomain.getCycleCountHeader();

        if (cycleCountHeader==null || cycCountDomain.getInventoryId()==null){
            return AjaxResult.error("主单据或库存id不能为空");
        }

        if (StringUtils.isEmpty(cycCountDomain.getCycleCountHeader().getCountType())){
            return AjaxResult.error("盘点类型不能为空");
        }
        if (StringUtils.isEmpty(cycleCountHeader.getWarehouseCode())){
            return AjaxResult.error("仓库编码不能为空");
        }
        if (StringUtils.isEmpty(cycleCountHeader.getCompanyCode())){
            return AjaxResult.error("货主编码不能为空");
        }

        cycleCountHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
        cycleCountHeader.setCompanyCode(cycleCountHeader.getCompanyCode());
        cycleCountHeader.setCreated(new Date());
        cycleCountHeader.setCreatedBy(ShiroUtils.getLoginName());
        cycleCountHeader.setCode(cycleCountHeaderService.createCode());
        if (!cycleCountHeaderService.save(cycleCountHeader)){
            return AjaxResult.error("新增盘点单失败");
        }
        this.addDetails(cycleCountHeader.getCode(),cycCountDomain.getInventoryId());

        return AjaxResult.success("成功");
    }

    /**
     * 新增盘点明细
     * */
    @Transactional
    public AjaxResult addDetails(String cycleCountHeadCode, Integer[] inventoryDetailIdIds) {

        if(cycleCountHeadCode == null){
            throw new ServiceException("盘点主单编码不能为空!");
        }
        CycleCountHeader cyclecountHeader =new CycleCountHeader();
        cyclecountHeader.setCode(cycleCountHeadCode);
        LambdaQueryWrapper<CycleCountHeader> ch = Wrappers.lambdaQuery(cyclecountHeader);
        cyclecountHeader = cycleCountHeaderService.getOne(ch);
        //可能存在并发删除的情况
        if(cyclecountHeader==null){
            return AjaxResult.error("盘点主单据不存在");
        }
        if(cyclecountHeader.getStatusCyc() > QuantityConstant.CYCLECOUNT_STATUS_BUILD){
            return AjaxResult.error("盘点单非新建状态,无法再添加明细");
        }
        //默认盘点配置,首选项
        ConfigValue configValue = new ConfigValue();
        configValue.setModuleType("cyclecount");
        configValue.setWarehouseCode(ShiroUtils.getWarehouseCode());
        LambdaQueryWrapper<ConfigValue> configValueLambdaQueryWrapper = Wrappers.lambdaQuery(configValue);
        configValue = configValueService.getOne(configValueLambdaQueryWrapper);
        if(configValue == null){
            throw new SecurityException("请先在配置中添加盘点配置");
        }
        String preferenceCode = configValue.getIdentifier();
        //获取已经存在的明细
        CycleCountDetail cycleCountDetail = new CycleCountDetail();
        cycleCountDetail.setWarehouseCode(cyclecountHeader.getWarehouseCode());
        cycleCountDetail.setCompanyCode(cyclecountHeader.getCompanyCode());
        cycleCountDetail.setCycleCountHeadCode(cyclecountHeader.getCode());
        LambdaQueryWrapper<CycleCountDetail> cycleCountDetailLambd = Wrappers.lambdaQuery(cycleCountDetail);
        List<CycleCountDetail> cyclecountDetailList = cycleCountDetailService.list(cycleCountDetailLambd);
        if(cyclecountDetailList == null){
            cyclecountDetailList = new ArrayList<>();
        }
        for(int inventoryDetailId : inventoryDetailIdIds){
            //当前有盘点明细时验证是否已经生成盘点细单,生成则跳出该条循环
            if(cyclecountDetailList.stream().anyMatch(t -> t.getInventoryDetailId()!=null && t.getInventoryDetailId() == inventoryDetailId)){
                continue;
            }
            //查询该条库存明细
            InventoryDetail inventoryDetail = inventoryDetailService.getById(inventoryDetailId);
            //写入盘点明细
            CycleCountDetail ccd = new CycleCountDetail();
            ccd.setPreferenceCode(preferenceCode);
            ccd.setCycleCountHeadCode(cyclecountHeader.getCode());
            ccd.setInventoryDetailId(inventoryDetailId);
            ccd.setWarehouseCode(inventoryDetail.getWarehouseCode());
            ccd.setCompanyCode(inventoryDetail.getCompanyCode());
            ccd.setInventorySts(inventoryDetail.getInventorySts());
            ccd.setCountId(inventoryDetail.getId());
            //ccd.setEnableStatus(1);
            ccd.setLocationCode(inventoryDetail.getLocationCode());
            ccd.setContainerCode(inventoryDetail.getContainerCode());
            ccd.setMaterialCode(inventoryDetail.getMaterialCode());
            ccd.setMaterialName(inventoryDetail.getMaterialName());
            ccd.setMaterialSpec(inventoryDetail.getMaterialSpec());
            ccd.setMaterialUnit(inventoryDetail.getMaterialUnit());
            ccd.setSystemQty(inventoryDetail.getQty());
            ccd.setBatch(inventoryDetail.getBatch());
            ccd.setLot(inventoryDetail.getLot());
            ccd.setProjectNo(inventoryDetail.getProjectNo());
            ccd.setCountedBy(cyclecountHeader.getCreatedBy());
            ccd.setCountedAt(cyclecountHeader.getCreated());
            ccd.setCreated(new Date());
            ccd.setCreatedBy(ShiroUtils.getLoginName());
            ccd.setLastUpdated(new Date());
            ccd.setLastUpdatedBy(ShiroUtils.getLoginName());
            cycleCountDetailService.save(ccd);
            //更新库存盘点锁
            inventoryDetail.setLockCode("cyclecount");
            inventoryDetailService.saveOrUpdate(inventoryDetail);
        }
        return AjaxResult.success("生成盘点明细成功");
    }

}