package org.dromara.ems.base.service.impl;

import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import cn.hutool.core.util.ObjectUtil;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.ems.base.domain.EmsBaseMonitorInfo;
import org.dromara.ems.base.domain.EmsBaseMonitorThreshold;
import org.dromara.ems.base.mapper.EmsBaseMonitorInfoMapper;
import org.dromara.ems.base.mapper.EmsBaseMonitorThresholdMapper;
import org.dromara.ems.base.service.IEmsBaseMonitorThresholdService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 计量设备阈值维护Service业务层处理
 *
 * @author Yinq
 * @date 2024-07-09
 */
@Service
@RequiredArgsConstructor
public class EmsBaseMonitorThresholdServiceImpl implements IEmsBaseMonitorThresholdService {
    
    private final EmsBaseMonitorThresholdMapper emsBaseMonitorThresholdMapper;

    
    private final EmsBaseMonitorInfoMapper emsBaseMonitorInfoMapper;

    /**
     * 查询计量设备阈值维护
     *
     * @param objId 计量设备阈值维护主键
     * @return 计量设备阈值维护
     */
    @Override
    public EmsBaseMonitorThreshold selectEmsBaseMonitorThresholdByObjId(Long objId) {
        return emsBaseMonitorThresholdMapper.selectEmsBaseMonitorThresholdByObjId(objId);
    }

    /**
     * 查询计量设备阈值维护列表
     *
     * @param emsBaseMonitorThreshold 计量设备阈值维护
     * @return 计量设备阈值维护
     */
    @Override
    public List<EmsBaseMonitorThreshold> selectEmsBaseMonitorThresholdList(EmsBaseMonitorThreshold emsBaseMonitorThreshold) {
        List<EmsBaseMonitorThreshold> monitorThresholds = emsBaseMonitorThresholdMapper.selectEmsBaseMonitorThresholdList(emsBaseMonitorThreshold);

        if (monitorThresholds.size() == 0 && ObjectUtil.isNotEmpty(emsBaseMonitorThreshold.getMonitorCode())) {
            EmsBaseMonitorInfo monitorInfo = new EmsBaseMonitorInfo();
            monitorInfo.setMonitorCode(emsBaseMonitorThreshold.getMonitorCode());
            List<EmsBaseMonitorInfo> monitorInfoList = emsBaseMonitorInfoMapper.selectEmsBaseMonitorInfoList(monitorInfo);
            if (monitorInfoList.size() > 0 && monitorInfoList.get(0).getIsAmmeter().equals("1")) {
                EmsBaseMonitorInfo emsBaseMonitorInfo = monitorInfoList.get(0);
                emsBaseMonitorThreshold.setMonitorType(emsBaseMonitorInfo.getMonitorType());
                emsBaseMonitorThreshold.setMonitorName(emsBaseMonitorInfo.getMonitorName());
                this.insertEmsBaseMonitorThreshold(emsBaseMonitorThreshold);
                monitorThresholds.add(emsBaseMonitorThreshold);
            }
        }
        return monitorThresholds;
    }

    /**
     * 新增计量设备阈值维护
     *
     * @param emsBaseMonitorThreshold 计量设备阈值维护
     * @return 结果
     */
    @Override
    public int insertEmsBaseMonitorThreshold(EmsBaseMonitorThreshold emsBaseMonitorThreshold) {
        emsBaseMonitorThreshold.setCreateTime(DateUtils.getNowDate());
        return emsBaseMonitorThresholdMapper.insertEmsBaseMonitorThreshold(emsBaseMonitorThreshold);
    }

    /**
     * 修改计量设备阈值维护
     *
     * @param emsBaseMonitorThreshold 计量设备阈值维护
     * @return 结果
     */
    @Override
    public int updateEmsBaseMonitorThreshold(EmsBaseMonitorThreshold emsBaseMonitorThreshold) {
        emsBaseMonitorThreshold.setUpdateTime(DateUtils.getNowDate());
        return emsBaseMonitorThresholdMapper.updateEmsBaseMonitorThreshold(emsBaseMonitorThreshold);
    }

    /**
     * 批量删除计量设备阈值维护
     *
     * @param objIds 需要删除的计量设备阈值维护主键
     * @return 结果
     */
    @Override
    public int deleteEmsBaseMonitorThresholdByObjIds(Long[] objIds) {
        return emsBaseMonitorThresholdMapper.deleteEmsBaseMonitorThresholdByObjIds(objIds);
    }

    /**
     * 删除计量设备阈值维护信息
     *
     * @param objId 计量设备阈值维护主键
     * @return 结果
     */
    @Override
    public int deleteEmsBaseMonitorThresholdByObjId(Long objId) {
        return emsBaseMonitorThresholdMapper.deleteEmsBaseMonitorThresholdByObjId(objId);
    }
}
