package com.ruoyi.ems.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.ems.domain.EmsCls;
import com.ruoyi.ems.domain.PowerBox;
import com.ruoyi.ems.domain.PowerBoxElecEqpt;
import com.ruoyi.ems.mapper.PowerBoxMapper;
import com.ruoyi.ems.service.IPowerBoxElecEqptService;
import com.ruoyi.ems.service.IPowerBoxService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 动力箱柜Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-20
 */
@Service
public class PowerBoxServiceImpl implements IPowerBoxService {
    @Autowired
    private PowerBoxMapper powerBoxMapper;

    @Autowired
    private IPowerBoxElecEqptService elecEqptService;

    /**
     * 查询动力箱柜
     *
     * @param id 动力箱柜主键
     * @return 动力箱柜
     */
    @Override
    public PowerBox selectPowerBoxById(Long id) {
        PowerBox powerBox = powerBoxMapper.selectPowerBoxById(id);

        // 查询箱体内部电气设备
        if (null != powerBox) {
            List<PowerBoxElecEqpt> eqptList = elecEqptService.selectByBoxCode(powerBox.getBoxCode());
            powerBox.setElecEqptList(eqptList);
        }

        return powerBox;
    }

    /**
     * 查询动力箱柜列表
     *
     * @param powerBox 动力箱柜
     * @return 动力箱柜
     */
    @Override
    public List<PowerBox> selectPowerBoxList(PowerBox param) {
        List<PowerBox> retList = powerBoxMapper.selectPowerBoxList(param);

        if (CollectionUtils.isNotEmpty(retList)) {
            List<PowerBoxElecEqpt> eqptList = elecEqptService.selectBoxElecEqptList(new PowerBoxElecEqpt());
            Map<String, List<PowerBoxElecEqpt>> groupedByParent = eqptList.stream()
                .collect(Collectors.groupingBy(PowerBoxElecEqpt::getBoxCode));

            for (PowerBox powerBox : retList) {
                if (groupedByParent.containsKey(powerBox.getBoxCode())) {
                    powerBox.setElecEqptList(groupedByParent.get(powerBox.getBoxCode()));
                }
                else {
                    powerBox.setElecEqptList(new ArrayList<>());
                }
            }
        }

        return retList;
    }

    /**
     * 新增动力箱柜
     *
     * @param powerBox 动力箱柜
     * @return 结果
     */
    @Override
    public int insertPowerBox(PowerBox powerBox) {
        elecEqptService.deleteByBoxCode(powerBox.getBoxCode());
        elecEqptService.insertBatch(powerBox.getElecEqptList());
        return powerBoxMapper.insertPowerBox(powerBox);
    }

    /**
     * 修改动力箱柜
     *
     * @param powerBox 动力箱柜
     * @return 结果
     */
    @Override
    public int updatePowerBox(PowerBox powerBox) {
        List<PowerBoxElecEqpt> eqptList = powerBox.getElecEqptList();

        if (CollectionUtils.isNotEmpty(eqptList)) {
            elecEqptService.deleteByBoxCode(powerBox.getBoxCode());
            elecEqptService.insertBatch(eqptList);
        }
        else {
            elecEqptService.deleteByBoxCode(powerBox.getBoxCode());
        }

        return powerBoxMapper.updatePowerBox(powerBox);
    }

    /**
     * 批量删除动力箱柜
     *
     * @param ids 需要删除的动力箱柜主键
     * @return 结果
     */
    @Override
    public int deletePowerBoxByIds(Long[] ids) {
        for (Long id : ids) {
            PowerBox powerBox = powerBoxMapper.selectPowerBoxById(id);

            if (null != powerBox) {
                elecEqptService.deleteByBoxCode(powerBox.getBoxCode());
            }
        }

        return powerBoxMapper.deletePowerBoxByIds(ids);
    }

    /**
     * 删除动力箱柜信息
     *
     * @param id 动力箱柜主键
     * @return 结果
     */
    @Override
    public int deletePowerBoxById(Long id) {
        PowerBox powerBox = powerBoxMapper.selectPowerBoxById(id);

        if (null != powerBox) {
            elecEqptService.deleteByBoxCode(powerBox.getBoxCode());
        }

        return powerBoxMapper.deletePowerBoxById(id);
    }
}
