package com.aucma.production.service.impl;

import java.util.List;

import com.aucma.common.exception.base.BaseException;
import com.aucma.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aucma.production.mapper.BaseBomInfoMapper;
import com.aucma.production.domain.BaseBomInfo;
import com.aucma.production.service.IBaseBomInfoService;

/**
 * 生产BOMService业务层处理
 * 
 * @author Yinq
 * @date 2023-09-28
 */
@Service
public class BaseBomInfoServiceImpl implements IBaseBomInfoService 
{
    @Autowired
    private BaseBomInfoMapper baseBomInfoMapper;

    /**
     * 查询生产BOM
     * 
     * @param objId 生产BOM主键
     * @return 生产BOM
     */
    @Override
    public BaseBomInfo selectBaseBomInfoByObjId(Long objId)
    {
        return baseBomInfoMapper.selectBaseBomInfoByObjId(objId);
    }

    /**
     * 查询生产BOM列表
     * 
     * @param baseBomInfo 生产BOM
     * @return 生产BOM
     */
    @Override
    public List<BaseBomInfo> selectBaseBomInfoList(BaseBomInfo baseBomInfo)
    {
        return baseBomInfoMapper.selectBaseBomInfoList(baseBomInfo);
    }

    /**
     * 新增生产BOM
     * 
     * @param baseBomInfo 生产BOM
     * @return 结果
     */
    @Override
    public int insertBaseBomInfo(BaseBomInfo baseBomInfo)
    {
        BaseBomInfo bomInfo = baseBomInfoMapper.selectBaseBomInfoByMaterialCode(baseBomInfo.getMaterialCode());
        if (StringUtils.isNotNull(bomInfo)){
            throw new BaseException("该物料编号：" + baseBomInfo.getMaterialCode() + "已存在！");
        }
        if (StringUtils.isNotNull(baseBomInfo.getParentId())){
            BaseBomInfo info = baseBomInfoMapper.selectBaseBomInfoByMaterialCode(baseBomInfo.getParentId());
            baseBomInfo.setAncestors(info.getAncestors() + "," + baseBomInfo.getMaterialCode());
        }else {
            baseBomInfo.setAncestors(baseBomInfo.getMaterialCode());
        }
        return baseBomInfoMapper.insertBaseBomInfo(baseBomInfo);
    }

    /**
     * 修改生产BOM
     * 
     * @param baseBomInfo 生产BOM
     * @return 结果
     */
    @Override
    public int updateBaseBomInfo(BaseBomInfo baseBomInfo)
    {
        String parentId = baseBomInfo.getParentId();
        if (StringUtils.isNull(parentId)){
            baseBomInfo.setAncestors(baseBomInfo.getMaterialCode());
        }else {
            BaseBomInfo newParentBomInfo = baseBomInfoMapper.selectBaseBomInfoByMaterialCode(parentId);
            BaseBomInfo oldBomInfo = baseBomInfoMapper.selectBaseBomInfoByMaterialCode(baseBomInfo.getMaterialCode());
            if (StringUtils.isNotNull(newParentBomInfo) && StringUtils.isNotNull(oldBomInfo))
            {
                String newAncestors = newParentBomInfo.getAncestors() + "," + baseBomInfo.getMaterialCode();
                String oldAncestors = oldBomInfo.getAncestors();
                baseBomInfo.setAncestors(newAncestors);
                updateBomChildren(baseBomInfo.getMaterialCode(), newAncestors, oldAncestors);
            }
        }
        return baseBomInfoMapper.updateBaseBomInfo(baseBomInfo);
    }

    /**
     * 修改子元素关系
     *
     * @param materialCode 被修改的部门ID
     * @param newAncestors 新的父ID集合
     * @param oldAncestors 旧的父ID集合
     */
    private void updateBomChildren(String materialCode, String newAncestors, String oldAncestors) {
        List<BaseBomInfo> children = baseBomInfoMapper.selectChildrenBomById(materialCode);
        for (BaseBomInfo child : children)
        {
            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
        }
        if (children.size() > 0)
        {
            baseBomInfoMapper.updateBomChildren(children);
        }

    }

    /**
     * 批量删除生产BOM
     * 
     * @param objIds 需要删除的生产BOM主键
     * @return 结果
     */
    @Override
    public int deleteBaseBomInfoByObjIds(Long[] objIds)
    {
        return baseBomInfoMapper.deleteBaseBomInfoByObjIds(objIds);
    }

    /**
     * 删除生产BOM信息
     * 
     * @param objId 生产BOM主键
     * @return 结果
     */
    @Override
    public int deleteBaseBomInfoByObjId(Long objId)
    {
        return baseBomInfoMapper.deleteBaseBomInfoByObjId(objId);
    }

    /**
     * 检查生产BOM是否满足下计划条件
     * @param baseBomInfoList
     */
    @Override
    public void checkBomInfo(List<BaseBomInfo> baseBomInfoList) {
        for (BaseBomInfo baseBomInfo : baseBomInfoList) {
            if (baseBomInfo.getStandardAmount() < 1){
                throw new BaseException("生产BOM：" + baseBomInfo.getMaterialName() + " 标准数量小于1！");
            }
            if (StringUtils.isEmpty(baseBomInfo.getProductLineCode())){
                throw new BaseException("生产BOM：" + baseBomInfo.getMaterialName() + " 未分配工位！");
            }
        }
    }

}
