package com.zcxk.charge.web.service;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxk.charge.api.dto.*;
import com.zcxk.charge.api.vo.*;
import com.zcxk.charge.core.dao.DayReportMapper;
import com.zcxk.charge.core.entity.DayReportEntity;
import com.zcxk.core.common.util.BeanCopyUtils;
import com.zcxk.core.oauth2.pojo.LoginUser;
import com.zcxk.core.oauth2.util.UserUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 日报Service接口
 *
 * @author: WYY
 * @date 2020-12-03 16:20
 */
@Service
public class DayReportService extends AbstractReportService<DayReportMapper, DayReportEntity>{

    @Resource
    private DayReportMapper dayReportMapper;

    /**
     * 查列表
     */
    public List<DayReportEntity> findList(DayReportEntity dayReportEntity) {
        return dayReportMapper.findList(dayReportEntity);
    }

    public Page<ReportVo> findPage(IPage<ReportDto> page, Long[] ids, Integer year, Integer month, Integer day) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        Page<ReportVo> reportPage = new Page<>();
        //查询场景下的所有设备信息
        List<DeviceVo> devices = findDevices(ids);
        if (CollectionUtils.isEmpty(devices)) {
            return reportPage;
        }
        //根据设备ID查询报表测点信息
        DayReportEntity dayReportEntity = new DayReportEntity();
        dayReportEntity.setYear(year);
        dayReportEntity.setMonth(month);
        dayReportEntity.setDay(day);
        dayReportEntity.setTenantId(currentUser.getTenantId());
        dayReportEntity.setParentSceneIds(ids);
        dayReportEntity.setDeviceIds(devices);
        // 查询到分页的行数
        reportPage = dayReportMapper.findPage(page, dayReportEntity);
        //查询标题
        ReportDto reportDto = new ReportDto();
        reportDto.setYear(year);
        reportDto.setMonth(month);
        reportDto.setDay(day);
        reportDto.setDeviceIds(devices);
        reportDto.setParentSceneIds(ids);
        formatReport(reportDto, reportPage, devices);
        return reportPage;
    }

    @Override
    public void calcUsage(ReportDto item, List<ReportVo> reportDtos) {
        if (item == null || reportDtos.size() == 0) {
            return;
        }
        // 固定参数项
        int index = -1;
        for (int i = 0; i < reportDtos.size(); i++) {
            if (reportDtos.get(i).getHour().equals(item.getHour())) {
                index = i;
            }
        }
        if (index == -1) {
            return;
        }
        calcReportUsage(index, reportDtos, item);
    }


    /**
     * 查询报表数据
     * 根据自定义报表参数，是否查找自定义值
     * @param page
     * @param queryDto
     * @return
     */
    public Page<ReportVo> findPageReport(IPage<ReportDto> page, ReportQueryDto queryDto) {
        return findPageReport(page, queryDto, false);
    }

    public Page<ReportVo> findDevicePressPageReport(IPage<ReportDto> page,  ReportQueryDto queryDto) {
        return findPageReport(page, queryDto, true);
    }

    @Override
    public List<ReportVo> findReport(ReportDto reportDto) {
        return dayReportMapper.findReport(reportDto);
    }

    @Override
    public List<ReportAttributeVo> findAttributeList(ReportDto item) {
        return dayReportMapper.findAttributeList(item);
    }

    @Override
    public String formatCollectDate(ReportVo item){
        return item.getYear() + "-" + String.format("%02d", item.getMonth()) + "-" + String.format("%02d", item.getDay()) + " " + String.format("%02d", item.getHour()) + ":00";
    }

    @Override
    public Page<ReportVo> findPageReport(IPage<ReportDto> page, ReportPageQueryDto reportPageQueryDto) {
        return dayReportMapper.findPageReprot(page, reportPageQueryDto);
    }


}
