package com.hw.ems.record.service.impl;


import com.hw.common.core.text.Convert;
import com.hw.common.core.utils.StringUtils;
import com.hw.ems.base.domain.BaseMonitorInfo;
import com.hw.ems.base.service.IBaseMonitorInfoService;
import com.hw.ems.record.domain.RecordWaterInstant;
import com.hw.ems.record.mapper.RecordWaterInstantMapper;
import com.hw.ems.record.service.IRecordWaterInstantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 水实时数据Service业务层处理
 *
 * @author YinQ
 * @date 2023-04-08
 */
@Service
public class RecordWaterInstantServiceImpl implements IRecordWaterInstantService {
    @Autowired
    private RecordWaterInstantMapper recordWaterInstantMapper;

    @Autowired
    private IBaseMonitorInfoService baseMonitorInfoService;

    /**
     * 查询水实时数据
     *
     * @param objid 水实时数据主键
     * @return 水实时数据
     */
    @Override
    //@DS("#header.poolName")
    public RecordWaterInstant selectRecordWaterInstantByObjid(Long objid) {
        return recordWaterInstantMapper.selectRecordWaterInstantByObjid(objid);
    }

    /**
     * 查询水实时数据列表
     *
     * @param recordWaterInstant 水实时数据
     * @return 水实时数据
     */
    @Override
    //@DS("#header.poolName")
//    @DataScope(deptAlias = "null")
    public List<RecordWaterInstant> selectRecordWaterInstantList(RecordWaterInstant recordWaterInstant) {
        return recordWaterInstantMapper.selectRecordWaterInstantList(recordWaterInstant);
    }

    /**
     * 新增水实时数据
     *
     * @param recordWaterInstant 水实时数据
     * @return 结果
     */
    @Override
    //@DS("#header.poolName")
    public int insertRecordWaterInstant(RecordWaterInstant recordWaterInstant) {
        recordWaterInstant.setRecordTime(new Date());
        recordWaterInstant.setCollectType(0L);
        return recordWaterInstantMapper.insertRecordWaterInstant(recordWaterInstant);
    }

    /**
     * 修改水实时数据
     *
     * @param recordWaterInstant 水实时数据
     * @return 结果
     */
    @Override
    //@DS("#header.poolName")
    public int updateRecordWaterInstant(RecordWaterInstant recordWaterInstant) {
        recordWaterInstant.setRecordTime(new Date());
        return recordWaterInstantMapper.updateRecordWaterInstant(recordWaterInstant);
    }

    /**
     * 批量删除水实时数据
     *
     * @param objids 需要删除的水实时数据主键
     * @return 结果
     */
    @Override
    //@DS("#header.poolName")
    public int deleteRecordWaterInstantByObjids(String objids) {
        return recordWaterInstantMapper.deleteRecordWaterInstantByObjids(Convert.toStrArray(objids));
    }

    /**
     * 删除水实时数据信息
     *
     * @param objid 水实时数据主键
     * @return 结果
     */
    @Override
    //@DS("#header.poolName")
    public int deleteRecordWaterInstantByObjid(Long objid) {
        return recordWaterInstantMapper.deleteRecordWaterInstantByObjid(objid);
    }

    /**
     * 实时数据
     *
     * @param recordWaterInstant
     * @return
     */
    @Override
    //@DS("#header.poolName")
    public List<RecordWaterInstant> instantList(RecordWaterInstant recordWaterInstant) {
        if (StringUtils.isEmpty(recordWaterInstant.getMonitorSubset())) {
            return recordWaterInstantMapper.instantList(new RecordWaterInstant());
        }
        List<RecordWaterInstant> waterInstants = new ArrayList<>();
        BaseMonitorInfo monitorInfo = new BaseMonitorInfo();
        monitorInfo.setParentId(Long.valueOf(recordWaterInstant.getMonitorSubset()));
        monitorInfo.setParams(null);
        recordWaterInstant.setParams(null);
        List<BaseMonitorInfo> baseMonitorInfos = baseMonitorInfoService.selectBaseMonitorInfoList(monitorInfo);
        for (BaseMonitorInfo baseMonitorInfo : baseMonitorInfos) {
            RecordWaterInstant waterInstant = new RecordWaterInstant();
            waterInstant.setMonitorId(baseMonitorInfo.getMonitorId());
            waterInstant.setMonitorName(baseMonitorInfo.getMonitorName());
            RecordWaterInstant waterInstant1 = new RecordWaterInstant();
            waterInstant1.setMonitorSubset(baseMonitorInfo.getMonitorId());
            List<RecordWaterInstant> recordWaterInstants = recordWaterInstantMapper.instantList(waterInstant1);
            if (recordWaterInstants.size() > 0) {
                BigDecimal waterFlow = recordWaterInstants.stream().map(RecordWaterInstant::getWaterFlow).reduce(BigDecimal.ZERO, BigDecimal::add);
                Date collectTime = recordWaterInstants.stream().map(RecordWaterInstant::getCollectTime).max(Date::compareTo).orElse(null);
                waterInstant.setWaterFlow(waterFlow);
                waterInstant.setCollectTime(collectTime);
            }
            waterInstants.add(waterInstant);
        }
        return waterInstants;
    }

    /**
     * 获取最新实时电数据
     *
     * @param recordWaterInstant
     * @return
     */
    @Override
    //@DS("#header.poolName")
    public List<RecordWaterInstant> selectRealTimeParentList(RecordWaterInstant recordWaterInstant) {
        List<RecordWaterInstant> waterInstantList = new ArrayList<>();
        if (StringUtils.isEmpty(recordWaterInstant.getMonitorSubset())) {
            return recordWaterInstantMapper.instantList(new RecordWaterInstant());
        }
        RecordWaterInstant waterInstant = new RecordWaterInstant();
        BaseMonitorInfo monitorInfo = new BaseMonitorInfo();
        monitorInfo.setMonitorId(recordWaterInstant.getMonitorSubset());
        recordWaterInstant.setParams(null);
        List<BaseMonitorInfo> baseMonitorInfos = baseMonitorInfoService.selectBaseMonitorInfoList(monitorInfo);
        if (baseMonitorInfos.size() > 0) {
            List<RecordWaterInstant> waterInstantListOld = recordWaterInstantMapper.instantList(recordWaterInstant);
            waterInstant.setMonitorId(baseMonitorInfos.get(0).getMonitorId());
            waterInstant.setMonitorName(baseMonitorInfos.get(0).getMonitorName());
            if (waterInstantListOld.size() > 0) {
                BigDecimal waterFlow = waterInstantListOld.stream().map(RecordWaterInstant::getWaterFlow).reduce(BigDecimal.ZERO, BigDecimal::add);
                Date collectTime = waterInstantListOld.stream().map(RecordWaterInstant::getCollectTime).max(Date::compareTo).orElse(null);
                waterInstant.setWaterFlow(waterFlow);
                waterInstant.setCollectTime(collectTime);
            }
        }
        waterInstantList.add(waterInstant);
        return waterInstantList;
    }

    @Override
    //@DS("#header.poolName")
    public RecordWaterInstant tablewareDetailsInfo(RecordWaterInstant waterInstant) {
        if (StringUtils.isEmpty(waterInstant.getMonitorId())) {
            return null;
        }
        List<RecordWaterInstant> recordWaterInstants = recordWaterInstantMapper.instantList(waterInstant);
        if (recordWaterInstants.size() == 0) {
            return null;
        }
        return recordWaterInstants.get(0);
    }

}
