package com.ruoyi.ems.service.impl;

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.common.AreaTier;
import com.ruoyi.ems.domain.vo.QueryDevice;
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 org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 能源设备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;
    }

    /**
     * 查询能源设备列表
     *
     * @param emsDevice 能源设备
     * @return 能源设备
     */
    @Override
    public List<EmsDevice> selectEmsDeviceList(EmsDevice emsDevice) {
        List<EmsDevice> list = emsDeviceMapper.selectEmsDeviceList(emsDevice);

        if (CollectionUtils.isNotEmpty(list)) {
            for (EmsDevice device : list) {
                fillAreaName(device);
            }
        }

        return list;
    }

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

        if (CollectionUtils.isNotEmpty(list)) {
            for (EmsDevice device : list) {
                fillAreaName(device);
            }
        }

        return list;
    }

    /**
     * 新增能源设备
     *
     * @param emsDevice 能源设备
     * @return 结果
     */
    @Override
    public int insertEmsDevice(EmsDevice emsDevice) {
        emsDevice.setCreateTime(DateUtils.getNowDate());
        return emsDeviceMapper.insertEmsDevice(emsDevice);
    }

    /**
     * 修改能源设备
     *
     * @param emsDevice 能源设备
     * @return 结果
     */
    @Override
    public int updateEmsDevice(EmsDevice emsDevice) {
        emsDevice.setUpdateTime(DateUtils.getNowDate());
        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);
    }

    private void fillAreaName(EmsDevice emsDevice) {
        if (StringUtils.equals(emsDevice.getAreaType(), AreaTier.Area.name())) {
            Area area = areaService.selectAreaByCode(emsDevice.getRefArea());
            emsDevice.setRefAreaName(area != null ? area.getAreaName() : "未知");
        }
        else if (StringUtils.equals(emsDevice.getAreaType(), AreaTier.Building.name())) {
            AreaBuilding building = buildingService.selectAreaBuildingByCode(emsDevice.getRefArea());
            emsDevice.setRefAreaName(building != null ? building.getBldgName() : "未知");
        }
        else if (StringUtils.equals(emsDevice.getAreaType(), AreaTier.Zoning.name())) {
            AreaBuildingZoning zoning = zoningService.selectAreaBuildingZoningByCode(emsDevice.getRefArea());
            emsDevice.setRefAreaName(zoning != null ? zoning.getZoningName() : "未知");
        }
    }
}
