package com.aucma.base.service.impl;

import java.util.List;

import com.aucma.common.exception.base.BaseException;
import com.aucma.common.utils.DateUtils;
import com.aucma.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aucma.base.mapper.OrderBomInfoMapper;
import com.aucma.base.domain.OrderBomInfo;
import com.aucma.base.service.IOrderBomInfoService;

/**
 * 订单BOMService业务层处理
 * 
 * @author Yinq
 * @date 2023-09-28
 */
@Service
public class OrderBomInfoServiceImpl implements IOrderBomInfoService 
{
    @Autowired
    private OrderBomInfoMapper orderBomInfoMapper;

    /**
     * 查询订单BOM
     * 
     * @param objId 订单BOM主键
     * @return 订单BOM
     */
    @Override
    public OrderBomInfo selectOrderBomInfoByObjId(Long objId)
    {
        return orderBomInfoMapper.selectOrderBomInfoByObjId(objId);
    }

    /**
     * 查询订单BOM列表
     * 
     * @param orderBomInfo 订单BOM
     * @return 订单BOM
     */
    @Override
    public List<OrderBomInfo> selectOrderBomInfoList(OrderBomInfo orderBomInfo)
    {
        return orderBomInfoMapper.selectOrderBomInfoList(orderBomInfo);
    }

    /**
     * 新增订单BOM
     * 
     * @param orderBomInfo 订单BOM
     * @return 结果
     */
    @Override
    public int insertOrderBomInfo(OrderBomInfo orderBomInfo)
    {
        OrderBomInfo bomInfo = orderBomInfoMapper.selectOrderBomInfoByMaterialCode(orderBomInfo.getMaterialCode());
        if (StringUtils.isNotNull(bomInfo)){
            throw new BaseException("该物料编号：" + orderBomInfo.getMaterialCode() + "已存在！");
        }
        if (StringUtils.isNotNull(orderBomInfo.getParentId())){
            OrderBomInfo info = orderBomInfoMapper.selectOrderBomInfoByMaterialCode(orderBomInfo.getParentId());
            orderBomInfo.setAncestors(info.getAncestors() + "," + orderBomInfo.getMaterialCode());
        }else {
            orderBomInfo.setAncestors(orderBomInfo.getMaterialCode());
        }
        orderBomInfo.setCreatedTime(DateUtils.getNowDate());
        return orderBomInfoMapper.insertOrderBomInfo(orderBomInfo);
    }

    /**
     * 修改订单BOM
     * 
     * @param orderBomInfo 订单BOM
     * @return 结果
     */
    @Override
    public int updateOrderBomInfo(OrderBomInfo orderBomInfoa)
    {
        List<OrderBomInfo> orderBomInfos = orderBomInfoMapper.selectOrderBomInfoList(new OrderBomInfo());
        for (OrderBomInfo orderBomInfo : orderBomInfos) {

        String parentId = orderBomInfo.getParentId();
        if (StringUtils.isNull(parentId)){
            orderBomInfo.setAncestors(orderBomInfo.getMaterialCode());
        }else {
            OrderBomInfo newParentBomInfo = orderBomInfoMapper.selectOrderBomInfoByMaterialCode(parentId);
            OrderBomInfo oldBomInfo = orderBomInfoMapper.selectOrderBomInfoByMaterialCode(orderBomInfo.getMaterialCode());
            if (StringUtils.isNotNull(newParentBomInfo) && StringUtils.isNotNull(oldBomInfo))
            {
                String newAncestors = newParentBomInfo.getAncestors() + "," + orderBomInfo.getMaterialCode();
                String oldAncestors = oldBomInfo.getAncestors();
                orderBomInfo.setAncestors(newAncestors);
                updateBomChildren(orderBomInfo.getMaterialCode(), newAncestors, oldAncestors);
            }
        }
        orderBomInfo.setUpdatedTime(DateUtils.getNowDate());
            orderBomInfoMapper.updateOrderBomInfo(orderBomInfo);
        }
        return orderBomInfoMapper.updateOrderBomInfo(orderBomInfoa);
    }

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

    /**
     * 批量删除订单BOM
     * 
     * @param objIds 需要删除的订单BOM主键
     * @return 结果
     */
    @Override
    public int deleteOrderBomInfoByObjIds(Long[] objIds)
    {
        return orderBomInfoMapper.deleteOrderBomInfoByObjIds(objIds);
    }

    /**
     * 删除订单BOM信息
     * 
     * @param objId 订单BOM主键
     * @return 结果
     */
    @Override
    public int deleteOrderBomInfoByObjId(Long objId)
    {
        return orderBomInfoMapper.deleteOrderBomInfoByObjId(objId);
    }
}
