package com.ruoyi.ems.service.impl;

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.MeterDevice;
import com.ruoyi.ems.domain.common.AreaTier;
import com.ruoyi.ems.domain.common.MeterObjSubType;
import com.ruoyi.ems.domain.common.MeterObjType;
import com.ruoyi.ems.mapper.MeterDeviceMapper;
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;
import com.ruoyi.ems.service.IMeterDeviceService;
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-08-08
 */
@Service
public class MeterDeviceServiceImpl implements IMeterDeviceService {
    @Autowired
    private MeterDeviceMapper meterDeviceMapper;

    @Autowired
    private IAreaService areaService;

    @Autowired
    private IAreaBuildingService buildingService;

    @Autowired
    private IAreaBuildingZoningService zoningService;

    @Autowired
    private IEmsDeviceService deviceService;

    @Autowired
    private IEmsFacsService facsService;

    /**
     * 查询计量设备
     *
     * @param id 计量设备主键
     * @return 计量设备
     */
    @Override
    public MeterDevice selectMeterDeviceById(Long id) {
        MeterDevice meterDevice = meterDeviceMapper.selectMeterDeviceById(id);
        fillObjName(meterDevice);
        return meterDevice;
    }

    @Override
    public MeterDevice selectMeterDeviceByCode(String areaCode, String deviceCode) {
        MeterDevice meterDevice = meterDeviceMapper.selectMeterDeviceByCode(areaCode, deviceCode);
        fillObjName(meterDevice);
        return meterDevice;
    }

    /**
     * 查询计量设备列表
     *
     * @param meterDevice 计量设备
     * @return 计量设备
     */
    @Override
    public List<MeterDevice> selectMeterDeviceList(MeterDevice meterDevice) {
        List<MeterDevice> meterDevices = meterDeviceMapper.selectMeterDeviceList(meterDevice);

        for (MeterDevice device : meterDevices) {
            fillObjName(device);
        }

        return meterDevices;
    }

    /**
     * 新增计量设备
     *
     * @param meterDevice 计量设备
     * @return 结果
     */
    @Override
    public int insertMeterDevice(MeterDevice meterDevice) {
        return meterDeviceMapper.insertMeterDevice(meterDevice);
    }

    /**
     * 修改计量设备
     *
     * @param meterDevice 计量设备
     * @return 结果
     */
    @Override
    public int updateMeterDevice(MeterDevice meterDevice) {
        return meterDeviceMapper.updateMeterDevice(meterDevice);
    }

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

    /**
     * 删除计量设备信息
     *
     * @param id 计量设备主键
     * @return 结果
     */
    @Override
    public int deleteMeterDeviceById(Long id) {
        return meterDeviceMapper.deleteMeterDeviceById(id);
    }

    private void fillObjName(MeterDevice meterDevice) {
        // 计量区块
        if (MeterObjType.METER_ABZ.getType() == meterDevice.getObjType()) {
            if (MeterObjSubType.METER_AREA.getType() == meterDevice.getObjSubType()) {
                Area area = areaService.selectAreaByCode(meterDevice.getObjCode());
                meterDevice.setObjName(area != null ? area.getShortName() : "未知");
            }
            else if (MeterObjSubType.METER_BUILDING.getType() == meterDevice.getObjSubType()) {
                AreaBuilding building = buildingService.selectAreaBuildingByCode(meterDevice.getObjCode());
                meterDevice.setObjName(
                    building != null ? building.getAreaShortName() + "/" + building.getBldgName() : "未知");
            }
            else if (MeterObjSubType.METER_ZONING.getType() == meterDevice.getObjSubType()) {
                AreaBuildingZoning zoning = zoningService.selectAreaBuildingZoningByCode(meterDevice.getObjCode());

                if (null != zoning) {
                    AreaBuilding building = buildingService.selectAreaBuildingByCode(zoning.getBldgCode());
                    meterDevice.setObjName(building != null ?
                        building.getAreaShortName() + "/" + building.getBldgName() + "/" + zoning.getZoningName() :
                        "未知");
                }
                else {
                    meterDevice.setObjName("未知");
                }
            }
        }
        // 计量设备设施
        else if (MeterObjType.METER_FD.getType() == meterDevice.getObjType()) {
            if (MeterObjSubType.METER_FACS.getType() == meterDevice.getObjSubType()) {
                EmsFacs facs = facsService.selectEmsFacsByCode(meterDevice.getObjCode());

                if (facs != null) {
                    Area area = areaService.selectAreaByCode(facs.getRefArea());
                    meterDevice.setObjName(
                        area != null ? area.getShortName() + "/" + facs.getFacsName() : facs.getFacsName());
                }
                else {
                    meterDevice.setObjName("未知");
                }
            }
            else if (MeterObjSubType.METER_DEVICE.getType() == meterDevice.getObjSubType()) {
                EmsDevice device = deviceService.selectByCode(meterDevice.getObjCode());
                if (device != null) {
                    meterDevice.setObjName(device.getAreaPath() + "/" + device.getDeviceName());
                }
                else {
                    meterDevice.setObjName("未知");
                }
            }
        }
    }
}
