package com.iformall.service.kw.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.domain.dto.KwMeterDataDto;
import com.iformall.domain.po.base.BaseEntity;
import com.iformall.domain.po.KwMeterData;
import com.iformall.domain.vo.KwMeterDataVo;
import com.iformall.enums.EnumMeterStatus;
import com.iformall.mapper.KwMeterDataMapper;
import com.iformall.service.kw.KwMeterDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class KwMeterDataServiceImpl implements KwMeterDataService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    KwMeterDataMapper kwMeterDataMapper;

    @Override
    public PageInfo<KwMeterDataVo> listVoAsPage(KwMeterDataDto kwMeterDataDto, Integer pageIndex, Integer pageSize) {
        if (kwMeterDataDto.getSortColumn() == null)
            kwMeterDataDto.setSortColumns(BaseEntity.SortField.ReportTime_DESC);
        kwMeterDataDto.setStatus(EnumMeterStatus.VALID.getCode());
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> kwMeterDataMapper.findVoList(kwMeterDataDto));
    }

    @Override
    public List<KwMeterDataVo> findVoList(KwMeterDataDto kwMeterDataDto) {
        if (kwMeterDataDto.getSortColumn() == null && kwMeterDataDto.getSortColumns() == null)
            kwMeterDataDto.setSortColumns(BaseEntity.SortField.ReportTime_ASC);
        kwMeterDataDto.setStatus(EnumMeterStatus.VALID.getCode());
        return kwMeterDataMapper.findVoList(kwMeterDataDto);
    }

    @Override
    public PageInfo<KwMeterData> listAsPage(KwMeterData kwMeterData, Integer pageIndex, Integer pageSize) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> kwMeterDataMapper.findList(kwMeterData));
    }

    @Override
    public List<KwMeterData> findList(KwMeterData kwMeterData) {
        return kwMeterDataMapper.findList(kwMeterData);
    }

    @Override
    public int save(List<KwMeterData> recordList) {
        //return kwMeterDataMapper.insertList(recordList);
        return 0;
    }

    @Override
    public List<KwMeterData> findLastRecordList(KwMeterData kwMeterData) {
        return kwMeterDataMapper.findLastRecordList(kwMeterData);
    }

    @Override
    public KwMeterData findLastRecord(Long meterId) {
        KwMeterData kwMeterData = new KwMeterData();
        kwMeterData.setMeterId(meterId);
        List<KwMeterData> list = kwMeterDataMapper.findLastRecordList(kwMeterData);
        if (list.size() > 0)
            return list.get(0);
        else return new KwMeterData();
    }


    @Override
    public KwMeterDataVo getPower(KwMeterDataDto dto)
    {
        KwMeterDataVo usedPower = new KwMeterDataVo();
        usedPower.setMeterId(dto.getMeterId());
        usedPower.setTenantId(dto.getTenantId());
        usedPower.setReportTime(new Date().toString());
        if (dto.getStartTime() != null && dto.getEndTime() != null) {
            KwMeterDataDto param = new KwMeterDataDto();
            param.setTenantId(dto.getTenantId());
            param.setMeterId(dto.getMeterId());
            param.setReportType(dto.getReportType());
            param.setStartTime(dto.getStartTime());
            param.setEndTime(dto.getEndTime());
            KwMeterDataVo usedPowerRes = kwMeterDataMapper.getPower(param);
            if (usedPowerRes != null) {
                usedPower.setTotalPower(usedPowerRes.getTotalPower());
                usedPower.setLastTotalPower(usedPowerRes.getLastTotalPower());
                usedPower.setRecordStartTime(usedPowerRes.getRecordStartTime());
                usedPower.setRecordEndTime(usedPowerRes.getRecordEndTime());
                usedPower.setUsedPower(usedPowerRes.getUsedPower());
            }
        } else {

            if (dto.getStartTime() != null) {
                KwMeterDataDto param = new KwMeterDataDto();
                param.setTenantId(dto.getTenantId());
                param.setMeterId(dto.getMeterId());
                param.setReportType(dto.getReportType());
                param.setStartTime(dto.getStartTime());
                KwMeterDataVo startPower = kwMeterDataMapper.getPower(param);

                if (startPower != null) {
                    usedPower.setRecordStartTime(startPower.getRecordStartTime());
                    usedPower.setLastTotalPower(startPower.getTotalPower());
                }
            }
            if (dto.getEndTime() != null) {
                KwMeterDataDto param = new KwMeterDataDto();
                param.setTenantId(dto.getTenantId());
                param.setMeterId(dto.getMeterId());
                param.setReportType(dto.getReportType());
                param.setEndTime(dto.getEndTime());
                KwMeterDataVo endPower = kwMeterDataMapper.getPower(param);

                if (endPower != null) {
                    usedPower.setRecordEndTime(endPower.getRecordEndTime());
                    usedPower.setTotalPower(endPower.getTotalPower());
                }
            }
            if (usedPower.getLastTotalPower() == null)
                usedPower.setLastTotalPower(dto.getStartNumber());
            if (usedPower.getTotalPower() == null)
                usedPower.setTotalPower(dto.getEndNumber());

            if (usedPower.getLastTotalPower() != null && usedPower.getTotalPower() != null)
                usedPower.setUsedPower(usedPower.getTotalPower() - usedPower.getLastTotalPower());
        }
        return usedPower;
    }
}
