package com.ruoyi.ems.service.impl;

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

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.ems.domain.Area;
import com.ruoyi.ems.domain.AreaBuilding;
import com.ruoyi.ems.domain.AreaBuildingZoning;
import com.ruoyi.ems.domain.EmsDevice;
import com.ruoyi.ems.domain.EmsFacs;
import com.ruoyi.ems.domain.common.AreaTier;
import com.ruoyi.ems.domain.vo.QueryDevice;
import com.ruoyi.ems.domain.vo.TreeEntity;
import com.ruoyi.ems.mapper.EmsDeviceMapper;
import com.ruoyi.ems.service.IAreaBuildingService;
import com.ruoyi.ems.service.IAreaBuildingZoningService;
import com.ruoyi.ems.service.IAreaService;
import com.ruoyi.ems.service.IEmsDeviceService;
import com.ruoyi.ems.service.IEmsFacsService;

/**
 * 能源设备Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-10
 */
@Service
public class EmsDeviceServiceImpl implements IEmsDeviceService {
    @Autowired
    private EmsDeviceMapper emsDeviceMapper;

    @Autowired
    private IAreaService areaService;

    @Autowired
    private IAreaBuildingService buildingService;

    @Autowired
    private IAreaBuildingZoningService zoningService;

    /**
     * 查询能源设备
     *
     * @param id 能源设备主键
     * @return 能源设备
     */
    @Override
    public EmsDevice selectEmsDeviceById(Long id) {
        EmsDevice emsDevice = emsDeviceMapper.selectEmsDeviceById(id);

        if (null != emsDevice) {
            fillAreaName(emsDevice);
        }

        return emsDevice;
    }

    @Override
    public EmsDevice selectByCode(String code) {
        return emsDeviceMapper.selectDeviceByCode(code);
    }

    @Override
    public EmsDevice selectDetailByCode(String code) {
        EmsDevice device = emsDeviceMapper.selectDetailByCode(code);

        if (null != device) {
            fillAreaName(device);
        }

        return device;
    }

    @Override
    public List<EmsDevice> selectList(QueryDevice queryDevice) {
        return emsDeviceMapper.selectDeviceList(queryDevice);
    }

    @Override
    public List<EmsDevice> selectDetailList(QueryDevice queryDevice) {
        List<EmsDevice> list = emsDeviceMapper.selectDetailList(queryDevice);
        fillFieldBath(list);
        return list;
    }

    /**
     * 查询能源设备列表
     *
     * @param queryDevice 能源设备
     * @return 能源设备
     */
    @Override
    public List<EmsDevice> selectByAreaTree(QueryDevice queryDevice) {
        List<EmsDevice> list = emsDeviceMapper.selectByAreaTree(queryDevice);
        fillFieldBath(list);
        return list;
    }

    @Override
    public List<EmsDevice> selectByFlowRel(QueryDevice emsDevice) {
       return emsDeviceMapper.selectByFlowRel(emsDevice);
    }

    /**
     * 新增能源设备
     *
     * @param emsDevice 能源设备
     * @return 结果
     */
    @Override
    public int insertEmsDevice(EmsDevice emsDevice) {
        emsDevice.setCreateTime(DateUtils.getNowDate());
        String codePath = emsDeviceMapper.selectDevicePath(emsDevice.getAreaType(), emsDevice.getRefArea());
        emsDevice.setAreaAncestors(codePath);
        return emsDeviceMapper.insertEmsDevice(emsDevice);
    }

    /**
     * 修改能源设备
     *
     * @param emsDevice 能源设备
     * @return 结果
     */
    @Override
    public int updateEmsDevice(EmsDevice emsDevice) {
        emsDevice.setUpdateTime(DateUtils.getNowDate());
        String codePath = emsDeviceMapper.selectDevicePath(emsDevice.getAreaType(), emsDevice.getRefArea());
        emsDevice.setAreaAncestors(codePath);
        return emsDeviceMapper.updateEmsDevice(emsDevice);
    }

    /**
     * 批量删除能源设备
     *
     * @param ids 需要删除的能源设备主键
     * @return 结果
     */
    @Override
    public int deleteEmsDeviceByIds(Long[] ids) {
        return emsDeviceMapper.deleteEmsDeviceByIds(ids);
    }

    /**
     * 删除能源设备信息
     *
     * @param id 能源设备主键
     * @return 结果
     */
    @Override
    public int deleteEmsDeviceById(Long id) {
        return emsDeviceMapper.deleteEmsDeviceById(id);
    }

    @Override
    public List<Map<String, Object>> cntDeviceStatus(String areaCode) {
        return emsDeviceMapper.cntDeviceStatus(areaCode);
    }

    @Override
    public List<Map<String, Object>> calcDeviceOnlineSummary(String areaCode) {
        return emsDeviceMapper.calcDeviceOnlineSummary(areaCode);
    }

    /**
     * 批量填充字段
     *
     * @param list 能源设备列表
     */
    private void fillFieldBath(List<EmsDevice> list) {
        if (CollectionUtils.isNotEmpty(list)) {
            List<Area> areas = areaService.selectAreaList(new Area());
            Map<String, Area> areaMap = areas.stream()
                .collect(Collectors.toMap(Area::getAreaCode, Function.identity()));
            List<AreaBuilding> buildings = buildingService.selectAreaBuildingList(new AreaBuilding());
            Map<String, AreaBuilding> buildingMap = buildings.stream()
                .collect(Collectors.toMap(AreaBuilding::getBldgCode, Function.identity()));
            List<AreaBuildingZoning> zonings = zoningService.selectAreaBuildingZoningList(new AreaBuildingZoning());
            Map<String, AreaBuildingZoning> zoningMap = zonings.stream()
                .collect(Collectors.toMap(AreaBuildingZoning::getZoningCode, Function.identity()));

            for (EmsDevice device : list) {
                fillAreaName(device, areaMap, buildingMap, zoningMap);
            }
        }
    }

    private void fillAreaName(EmsDevice emsDevice, Map<String, Area> areaMap, Map<String, AreaBuilding> buildingMap,
        Map<String, AreaBuildingZoning> zoningMap) {
        if (StringUtils.equals(emsDevice.getAreaType(), AreaTier.Area.name())) {
            Area area = areaMap.get(emsDevice.getRefArea());

            if (null != area) {
                emsDevice.setRefAreaName(area.getAreaName());
                emsDevice.setAreaPath(null != area.getShortName() ? area.getShortName() : area.getAreaName());
            }
            else {
                emsDevice.setRefAreaName("未知");
                emsDevice.setAreaPath("未知/");
            }
        }
        else if (StringUtils.equals(emsDevice.getAreaType(), AreaTier.Building.name())) {
            AreaBuilding building = buildingMap.get(emsDevice.getRefArea());

            if (null != building) {
                emsDevice.setRefAreaName(building.getBldgName());
                emsDevice.setAreaPath(building.getAreaShortName() + "/" + building.getBldgName());
            }
            else {
                emsDevice.setRefAreaName("未知");
                emsDevice.setAreaPath("未知/");
            }
        }
        else if (StringUtils.equals(emsDevice.getAreaType(), AreaTier.Zoning.name())) {
            AreaBuildingZoning zoning = zoningMap.get(emsDevice.getRefArea());

            if (null != zoning) {
                emsDevice.setRefAreaName(zoning.getZoningName());

                AreaBuilding building = buildingMap.get(zoning.getBldgCode());

                if (null != building) {
                    emsDevice.setAreaPath(
                        building.getAreaShortName() + "/" + building.getBldgName() + "/" + zoning.getZoningName());
                }
                else {
                    emsDevice.setAreaPath("未知/");
                }
            }
            else {
                emsDevice.setRefAreaName("未知");
            }
        }
    }

    private void fillAreaName(EmsDevice emsDevice) {
        if (StringUtils.equals(emsDevice.getAreaType(), AreaTier.Area.name())) {
            Area area = areaService.selectAreaByCode(emsDevice.getRefArea());

            if (null != area) {
                emsDevice.setRefAreaName(area.getAreaName());
                emsDevice.setAreaPath(null != area.getShortName() ? area.getShortName() : area.getAreaName());
            }
            else {
                emsDevice.setRefAreaName("未知");
                emsDevice.setAreaPath("未知/");
            }
        }
        else if (StringUtils.equals(emsDevice.getAreaType(), AreaTier.Building.name())) {
            AreaBuilding building = buildingService.selectAreaBuildingByCode(emsDevice.getRefArea());

            if (null != building) {
                emsDevice.setRefAreaName(building.getBldgName());
                emsDevice.setAreaPath(building.getAreaShortName() + "/" + building.getBldgName());
            }
            else {
                emsDevice.setRefAreaName("未知");
                emsDevice.setAreaPath("未知/");
            }
        }
        else if (StringUtils.equals(emsDevice.getAreaType(), AreaTier.Zoning.name())) {
            AreaBuildingZoning zoning = zoningService.selectAreaBuildingZoningByCode(emsDevice.getRefArea());

            if (null != zoning) {
                emsDevice.setRefAreaName(zoning.getZoningName());

                AreaBuilding building = buildingService.selectAreaBuildingByCode(zoning.getBldgCode());

                if (null != building) {
                    emsDevice.setAreaPath(
                        building.getAreaShortName() + "/" + building.getBldgName() + "/" + zoning.getZoningName());
                }
                else {
                    emsDevice.setAreaPath("未知/");
                }
            }
            else {
                emsDevice.setRefAreaName("未知");
            }
        }
    }
}
