package com.ruoyi.ems.service.impl;

import cn.hutool.core.thread.ThreadUtil;
import com.ruoyi.common.core.utils.DateTimeUtil;
import com.ruoyi.ems.domain.ElecMeterH;
import com.ruoyi.ems.domain.MeterDevice;
import com.ruoyi.ems.enums.MeterObjType;
import com.ruoyi.ems.mapper.MeterDeviceMapper;
import com.ruoyi.ems.model.QueryDevice;
import com.ruoyi.ems.service.IMeterDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * 计量设备Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-08
 */
@Service
@Slf4j
public class MeterDeviceServiceImpl implements IMeterDeviceService {
    @Autowired
    private MeterDeviceMapper meterDeviceMapper;

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

    @Override
    public MeterDevice selectMeterDeviceByCode(String deviceCode) {
        return meterDeviceMapper.selectMeterDeviceByCode(deviceCode);
    }

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

    @Override
    public List<MeterDevice> selectMeterDevByTag(String tagCode, QueryDevice queryDevice) {
        return meterDeviceMapper.selectMeterDevByTag(tagCode, queryDevice);
    }

    @Override
    public List<MeterDevice> selectByModel(String deviceModel) {
        return meterDeviceMapper.selectByModel(deviceModel);
    }

    @Override
    public List<MeterDevice> selectMeterDeviceByObj(String areaCode, Integer meterCls, Integer colMode,
        MeterObjType objType, String objCode) {
        return meterDeviceMapper.selectMeterDeviceByObj(areaCode, meterCls, colMode, objType.getType(), objCode);
    }

    /**
     * 新增计量设备
     *
     * @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);
    }

    @Override
    public Map<String, Integer> cntMeterDevice(String areaCode) {
        Future<?> totalDevice = ThreadUtil.execAsync(() -> meterDeviceMapper.countMeterDeviceByObj(new MeterDevice() {{
            setAreaCode(areaCode);
        }}));
        Future<?> onlineDevice = ThreadUtil.execAsync(
            () -> meterDeviceMapper.countOnlineMeterDeviceByObj(new ElecMeterH() {
                {
                    setStartRecTime(DateTimeUtil.currentDateTime(DateTimeUtil.DateFormatter.yyyy_MM_dd));
                    setAreaCode(areaCode);
                }
            }));

        Map<String, Integer> result = new HashMap<>();
        try {
            result.put("totalDevice", (Integer) totalDevice.get());
            result.put("onlineDevice", (Integer) onlineDevice.get());
        }
        catch (Exception e) {
            log.error("cntMeterDevice error {}", e.getMessage());
        }
        return result;
    }
}
