package com.ruoyi.ems.base.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ems.base.domain.EmsBaseMonitorInfo;
import com.ruoyi.ems.base.mapper.EmsBaseMonitorInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ems.base.mapper.EmsBaseMonitorThresholdMapper;
import com.ruoyi.ems.base.domain.EmsBaseMonitorThreshold;
import com.ruoyi.ems.base.service.IEmsBaseMonitorThresholdService;

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

    @Autowired
    private 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 && StringUtils.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.setCreateBy(SecurityUtils.getUsername());
                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);
    }
}
