package com.ruoyi.ems.service.impl;

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

import com.ruoyi.ems.mapper.EmsObjAttrValueMapper;
import com.ruoyi.ems.service.IEmsObjAttrService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.ems.domain.Area;
import com.ruoyi.ems.domain.EmsDevice;
import com.ruoyi.ems.domain.EmsDeviceModel;
import com.ruoyi.ems.mapper.EmsDeviceMapper;
import com.ruoyi.ems.model.QueryDevice;
import com.ruoyi.ems.service.IAreaService;
import com.ruoyi.ems.service.IEmsDeviceService;
import com.ruoyi.ems.util.AreaUtils;

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

    @Autowired
    private EmsObjAttrValueMapper attrValueMapper;

    @Autowired
    private IAreaService areaService;

    /**
     * 查询能源设备
     *
     * @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) {
        return emsDeviceMapper.insertEmsDevice(emsDevice);
    }

    @Override
    public int insertBatch(List<EmsDevice> list) {
        return emsDeviceMapper.insertBatch(list);
    }

    /**
     * 修改能源设备
     *
     * @param emsDevice 能源设备
     * @return 结果
     */
    @Override
    public int updateEmsDevice(EmsDevice emsDevice) {
        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 int deleteDevice(String modelCode, String deviceCode) {
        return  emsDeviceMapper.deleteEmsDevice(modelCode,deviceCode);
    }

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

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

    @Override
    public List<EmsDeviceModel> selectSubSystemDeviceState(EmsDeviceModel params) {
        return emsDeviceMapper.selectSubSystemDeviceState(params);
    }

    /**
     * 批量填充字段
     *
     * @param list 能源设备列表
     */
    private void fillFieldBath(List<EmsDevice> list) {
        if (CollectionUtils.isNotEmpty(list)) {
            List<Area> areas = areaService.selectArea(new Area());
            Map<String, Area> areaMap = areas.stream()
                .collect(Collectors.toMap(Area::getAreaCode, Function.identity()));

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

    private void fillAreaName(EmsDevice emsDevice) {
        List<Area> areas = areaService.selectArea(new Area());
        Map<String, Area> areaMap = areas.stream().collect(Collectors.toMap(Area::getAreaCode, Function.identity()));
        fillAreaName(emsDevice, areaMap);
    }

    private void fillAreaName(EmsDevice emsDevice, Map<String, Area> areaMap) {
        String areaCode = emsDevice.getLocationRef();
        String areaNamePath = AreaUtils.getAreaNamePath(areaCode, areaMap);

        Area area = areaMap.get(areaCode);
        emsDevice.setLocationRefName(null != area ? area.getAreaName() : "未知");
        emsDevice.setAreaPath(areaNamePath);
    }
}
