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.YearReportMapper;
import com.zcxk.charge.core.entity.YearReportEntity;
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 YearReportService extends AbstractReportService<YearReportMapper, YearReportEntity> {

    @Resource
    private YearReportMapper yearReportMapper;

    /**
     * 查列表
     */
    public List<YearReportEntity> findList(YearReportEntity yearReportEntity) {
        return yearReportMapper.findList(yearReportEntity);
    }

    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;
        }
        YearReportEntity yearReportEntity = new YearReportEntity();
        yearReportEntity.setYear(year);
        yearReportEntity.setTenantId(currentUser.getTenantId());
        yearReportEntity.setParentSceneIds(ids);
        yearReportEntity.setDeviceIds(devices);
        // 查询到分页的行数
        reportPage = yearReportMapper.findPage(page, yearReportEntity);
        // 查询标题
        ReportDto reportDto = new ReportDto();
        reportDto.setYear(year);
        reportDto.setDeviceIds(devices);
        reportDto.setParentSceneIds(ids);
        formatReport(reportDto, reportPage, devices);
        return reportPage;
    }

    /**
     * 查询报表数据
     * 根据自定义报表参数，是否查找自定义值
     * @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 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).getMonth().equals(item.getMonth())){
                index = i;
            }
        }
        if (index == -1) {
            return;
        }
        calcReportUsage(index, reportDtos, item);
    }

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

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

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

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

}
