package com.ruoyi.ems.service.impl;

import com.ruoyi.ems.domain.ElecLoadIndex;
import com.ruoyi.ems.mapper.ElecLoadIndex15minMapper;
import com.ruoyi.ems.model.QueryMeter;
import com.ruoyi.ems.service.IElecLoadIndexService;
import com.ruoyi.ems.util.PowerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 电力负荷设施指标Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-09
 */
@Service
public class ElecLoadIndexServiceImpl implements IElecLoadIndexService {
    @Autowired
    private ElecLoadIndex15minMapper index15minMapper;

    @Override
    public ElecLoadIndex selectNewIndex(String areaCode, String objType, String objCode) {
        return index15minMapper.selectNewIndex(areaCode, objType, objCode);
    }

    /**
     * 查询电力负荷设施指标列表
     *
     * @param queryMeter 查询参数
     * @return 电力负荷设施指标
     */
    @Override
    public List<ElecLoadIndex> selectMin15IndexList(QueryMeter queryMeter) {
        List<ElecLoadIndex> indexList = index15minMapper.selectIndexList(queryMeter);

        for (ElecLoadIndex index : indexList) {
            index.setS(PowerUtils.getSPower(index.getP(), index.getQ()));
            index.setPf(PowerUtils.getPF(index.getP(), index.getS()));
        }

        return indexList;
    }

//    @Override
//    public List<ElecLoadIndex> selectMin15IndexDetailList(QueryMeter queryMeter) {
//        List<ElecLoadIndex> indexList = index15minMapper.selectIndexList(queryMeter);
//
//        for (ElecLoadIndex index : indexList) {
//            index.setS(PowerUtils.getSPower(index.getP(), index.getQ()));
//            index.setPf(PowerUtils.getPF(index.getP(), index.getS()));
//        }
//
//        return indexList;
//    }

    @Override
    public ElecLoadIndex selectMin15IndexMaxLoad(QueryMeter queryMeter) {
        ElecLoadIndex index = index15minMapper.selectIndexMaxLoad(queryMeter);

        if (null != index) {
            index.setS(PowerUtils.getSPower(index.getP(), index.getQ()));
            index.setPf(PowerUtils.getPF(index.getP(), index.getS()));
        }

        return index;
    }

    @Override
    public List<ElecLoadIndex> selectDayIndexMaxLoad(QueryMeter queryMeter) {
        List<ElecLoadIndex> indexList = index15minMapper.selectDayIndexMaxLoad(queryMeter);

        for (ElecLoadIndex index : indexList) {
            index.setS(PowerUtils.getSPower(index.getP(), index.getQ()));
            index.setPf(PowerUtils.getPF(index.getP(), index.getS()));
        }

        return indexList;
    }

    /**
     * 新增电力负荷设施指标
     *
     * @param loadIndex 电力负荷设施指标
     * @return 结果
     */
    @Override
    public int insertMin15Index(ElecLoadIndex loadIndex) {
        return index15minMapper.insertIndex(loadIndex);
    }
}
