package com.ruoyi.ems.service.impl;




import com.huashe.common.exception.Assert;
import com.ruoyi.ems.domain.Area;
import com.ruoyi.ems.domain.ElecAttr;
import com.ruoyi.ems.mapper.ElecAttrMapper;
import com.ruoyi.ems.service.IAreaService;
import com.ruoyi.ems.service.IElecAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 服务区用电属性Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-23
 */
@Service
public class AreaElectricityAttrServiceImpl implements IElecAttrService {
    @Autowired
    private ElecAttrMapper areaAttrMapper;

    @Autowired
    private IAreaService areaService;

    /**
     * 查询服务区用电属性
     *
     * @param id 服务区用电属性主键
     * @return 服务区用电属性
     */
    @Override
    public ElecAttr selectElecAttrById(Long id) {
        return areaAttrMapper.selectElecAttrById(id);
    }

    @Override
    public List<ElecAttr> getEffectiveListByArea(String areaCode) {
        List<ElecAttr> elecAttrs = areaAttrMapper.selectElecAttrList(new ElecAttr());
        Map<String, ElecAttr> elecAttrMap = elecAttrs.stream()
            .collect(Collectors.toMap(ElecAttr::getAreaCode, Function.identity()));

        List<Area> areas = areaService.selectArea(new Area());
        Map<String, Area> areaMap = areas.stream().collect(Collectors.toMap(Area::getAreaCode, Function.identity()));

        LinkedList<ElecAttr> retList = new LinkedList<>();
        Area area = areaMap.get(areaCode);
        Assert.notNull(area, -1, "区域不存在!");

        do {
            ElecAttr elecAttr = elecAttrMap.get(area.getAreaCode());

            if (null != elecAttr) {
                if (retList.isEmpty()) {
                    retList.add(elecAttr);
                }
                else {
                    retList.addFirst(elecAttr);
                }
            }

            area = areaMap.get(area.getParentCode());
        }
        while (null != area);

        return retList;
    }

    @Override
    public ElecAttr getEffectiveByArea(String areaCode) {
        ElecAttr elecAttr = null;
        List<ElecAttr> elecAttrs = areaAttrMapper.selectElecAttrList(new ElecAttr());
        Map<String, ElecAttr> elecAttrMap = elecAttrs.stream()
            .collect(Collectors.toMap(ElecAttr::getAreaCode, Function.identity()));

        List<Area> areas = areaService.selectArea(new Area());
        Map<String, Area> areaMap = areas.stream().collect(Collectors.toMap(Area::getAreaCode, Function.identity()));

        Area area = areaMap.get(areaCode);
        Assert.notNull(area, -1, "区域不存在!");

        do {
            elecAttr = elecAttrMap.get(area.getAreaCode());

            if (null != elecAttr) {
                break;
            }

            area = areaMap.get(area.getParentCode());
        }
        while (null != area);

        return elecAttr;
    }

    /**
     * 查询服务区用电属性列表
     *
     * @param elecAttr 服务区用电属性
     * @return 服务区用电属性
     */
    @Override
    public List<ElecAttr> selectElecAttrList(ElecAttr elecAttr) {
        return areaAttrMapper.selectElecAttrList(elecAttr);
    }

    /**
     * 新增服务区用电属性
     *
     * @param elecAttr 服务区用电属性
     * @return 结果
     */
    @Override
    public int insertElecAttr(ElecAttr elecAttr) {
        return areaAttrMapper.insertElecAttr(elecAttr);
    }

    /**
     * 修改服务区用电属性
     *
     * @param elecAttr 服务区用电属性
     * @return 结果
     */
    @Override
    public int updateElecAttr(ElecAttr elecAttr) {
        return areaAttrMapper.updateElecAttr(elecAttr);
    }

    /**
     * 批量删除服务区用电属性
     *
     * @param ids 需要删除的服务区用电属性主键
     * @return 结果
     */
    @Override
    public int deleteElecAttrByIds(Long[] ids) {
        return areaAttrMapper.deleteElecAttrByIds(ids);
    }

    /**
     * 删除服务区用电属性信息
     *
     * @param id 服务区用电属性主键
     * @return 结果
     */
    @Override
    public int deleteElecAttrById(Long id) {
        return areaAttrMapper.deleteElecAttrById(id);
    }
}
