package com.aucma.dms.service.impl;

//import com.hw.common.core.exception.ServiceException;
//import com.hw.common.core.utils.DateUtils;
//import com.aucma.dms.domain.DmsBaseDeviceBom;
//import com.aucma.dms.mapper.DmsBaseDeviceBomMapper;
//import com.aucma.dms.service.IDmsBaseDeviceBomService;
//import com.hw.system.api.model.LoginUser;

import com.aucma.common.core.domain.model.LoginUser;
import com.aucma.common.exception.ServiceException;
import com.aucma.common.utils.DateUtils;
import com.aucma.dms.domain.DmsBaseDeviceBom;
import com.aucma.dms.mapper.DmsBaseDeviceBomMapper;
import com.aucma.dms.service.IDmsBaseDeviceBomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 设备BOM信息Service业务层处理
 *
 * @author xins
 * @date 2023-12-28
 */
@Service
public class DmsBaseDeviceBomServiceImpl implements IDmsBaseDeviceBomService
{
    @Autowired
    private DmsBaseDeviceBomMapper dmsBaseDeviceBomMapper;

    /**
     * 查询设备BOM信息
     *
     * @param deviceBomId 设备BOM信息主键
     * @return 设备BOM信息
     */
    @Override
    public DmsBaseDeviceBom selectDmsBaseDeviceBomByDeviceBomId(Long deviceBomId)
    {
        return dmsBaseDeviceBomMapper.selectDmsBaseDeviceBomByDeviceBomId(deviceBomId);
    }

    /**
     * 查询设备BOM信息列表
     *
     * @param dmsBaseDeviceBom 设备BOM信息
     * @return 设备BOM信息
     */
    @Override
    public List<DmsBaseDeviceBom> selectDmsBaseDeviceBomList(DmsBaseDeviceBom dmsBaseDeviceBom)
    {
        return dmsBaseDeviceBomMapper.selectDmsBaseDeviceBomList(dmsBaseDeviceBom);
    }

    /**
     * 新增设备BOM信息
     *
     * @param dmsBaseDeviceBom 设备BOM信息
     * @return 结果
     */
    @Override
    public int insertDmsBaseDeviceBom(DmsBaseDeviceBom dmsBaseDeviceBom)
    {
        LoginUser loginUser = new LoginUser();
        //添加标识
        dmsBaseDeviceBom.setIsFlag(1l);
        dmsBaseDeviceBom.setCreateBy(loginUser.getUserId());
        dmsBaseDeviceBom.setCreateTime(DateUtils.getNowDate());
        //如果有父级 则查询父级祖籍列表 +父级编号
        if (dmsBaseDeviceBom.getParentId()==0){
            dmsBaseDeviceBom.setAncestors("0");
        }
        else {
            DmsBaseDeviceBom selectDeviceBom = new DmsBaseDeviceBom();
            selectDeviceBom.setDeviceBomId(dmsBaseDeviceBom.getParentId());
            String ancestors = dmsBaseDeviceBomMapper.selectDmsBaseDeviceBomList(selectDeviceBom).get(0).getAncestors();
            dmsBaseDeviceBom.setAncestors(ancestors + "," + dmsBaseDeviceBom.getParentId());
            //遍历所有祖先节点，检查设备ID是否重复,设备的bom不能包含其设备本身
            String ancestorsNode = dmsBaseDeviceBom.getAncestors();
            for (String ancestorsOne : ancestorsNode.split(",")) {
                if (ancestorsOne.equals("0")) {
                    continue;
                }
                DmsBaseDeviceBom DeviceBomUpdate = dmsBaseDeviceBomMapper.
                        selectDmsBaseDeviceBomByDeviceBomId(Long.parseLong(ancestorsOne));
                if (dmsBaseDeviceBom.getDeviceId().equals(DeviceBomUpdate.getDeviceId())) {
                    throw new ServiceException("不能添加与祖先节点相同的设备编号");
                }
            }
            //同一双亲节点的设备ID不能重复，不同双亲结点同一层级的兄弟节点可以重复
            //顶级节点可以重复,顶级节点不进else
            DmsBaseDeviceBom query = new DmsBaseDeviceBom();
            query.setParentId(dmsBaseDeviceBom.getParentId());
            // 查询父节点的所有子节点，即兄弟节点，都放到列表sibling中
            List<DmsBaseDeviceBom> siblings = dmsBaseDeviceBomMapper.selectDmsBaseDeviceBomList(query);
            // 遍历兄弟节点，检查设备ID是否重复
            for (DmsBaseDeviceBom sibling : siblings) {
                if (dmsBaseDeviceBom.getDeviceId().equals(sibling.getDeviceId())) {
                    throw new ServiceException("不能添加与兄弟节点相同的设备编号: " + dmsBaseDeviceBom.getDeviceId());
                }
            }
        }
        return dmsBaseDeviceBomMapper.insertDmsBaseDeviceBom(dmsBaseDeviceBom);
    }
    /**
     * 修改设备BOM信息
     *
     * @param dmsBaseDeviceBom 设备BOM信息
     * @return 结果
     */
    @Override
    public int updateDmsBaseDeviceBom(DmsBaseDeviceBom dmsBaseDeviceBom)
    {
        //如果有父级 则查询父级祖籍列表 +父级编号
        if (dmsBaseDeviceBom.getParentId()==0){
            dmsBaseDeviceBom.setAncestors("0");
        }
        else{
            DmsBaseDeviceBom DeviceBom = new DmsBaseDeviceBom();
            DeviceBom.setDeviceBomId(dmsBaseDeviceBom.getParentId());
            String ancestors = dmsBaseDeviceBomMapper.selectDmsBaseDeviceBomList(DeviceBom).get(0).getAncestors();
            dmsBaseDeviceBom.setAncestors(ancestors+","+dmsBaseDeviceBom.getParentId());
            //遍历所有祖先节点，检查设备ID是否重复,设备的bom不能包含其设备本身
            String ancestorsNode = dmsBaseDeviceBom.getAncestors();
            for (String ancestorsOne : ancestorsNode.split(",")) {
                if (ancestorsOne.equals("0")) {
                    continue;
                }
                DmsBaseDeviceBom DeviceBomUpdate = dmsBaseDeviceBomMapper.
                        selectDmsBaseDeviceBomByDeviceBomId(Long.parseLong(ancestorsOne));
                if (dmsBaseDeviceBom.getDeviceId().equals(DeviceBomUpdate.getDeviceId())) {
                    throw new ServiceException("不能添加与祖先节点相同的设备编号");
                }
            }
            //同一双亲节点的设备ID不能重复，不同双亲结点同一层级的兄弟节点可以重复
            //顶级节点可以重复,顶级节点不进else
            DmsBaseDeviceBom query = new DmsBaseDeviceBom();
            query.setParentId(dmsBaseDeviceBom.getParentId());
            // 查询父节点的所有子节点
            List<DmsBaseDeviceBom> siblings = dmsBaseDeviceBomMapper.selectDmsBaseDeviceBomList(query);
            // 查询父节点的所有子节点，即兄弟节点，都放到列表sibling中
            for (DmsBaseDeviceBom sibling : siblings) {
                if (dmsBaseDeviceBom.getDeviceId().equals(sibling.getDeviceId())) {
                    throw new ServiceException("不能添加与兄弟节点相同的设备编号: " + dmsBaseDeviceBom.getDeviceId());
                }
            }
        }
        dmsBaseDeviceBom.setUpdateTime(DateUtils.getNowDate());
        return dmsBaseDeviceBomMapper.updateDmsBaseDeviceBom(dmsBaseDeviceBom);
    }

    /**
     * 批量删除设备BOM信息
     *
     * @param deviceBomIds 需要删除的设备BOM信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsBaseDeviceBomByDeviceBomIds(Long[] deviceBomIds)
    {
        return dmsBaseDeviceBomMapper.deleteDmsBaseDeviceBomByDeviceBomIds(deviceBomIds);
    }

    /**
     * 删除设备BOM信息信息
     *
     * @param deviceBomId 设备BOM信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsBaseDeviceBomByDeviceBomId(Long deviceBomId)
    {
        return dmsBaseDeviceBomMapper.deleteDmsBaseDeviceBomByDeviceBomId(deviceBomId);
    }

    @Override
    public List<DmsBaseDeviceBom> selectBomByDeviceType(String deviceType) {
        return dmsBaseDeviceBomMapper.selectBomByDeviceType(deviceType);
    }


}
