package com.ruoyi.quartz.task;

import com.alibaba.fastjson.JSON;
import com.ruoyi.excel.wecom.domain.CorpInfo;
import com.ruoyi.excel.wecom.helper.HandleAllData;
import com.ruoyi.excel.wecom.helper.HandleAllDataV2;
import com.ruoyi.excel.wecom.mapper.CorpInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Component("wecomTask")
public class WeComTask {
    @Autowired
    private HandleAllData handleAllData;
    @Autowired
    private HandleAllDataV2 handleAllDataV2;
    @Autowired
    private CorpInfoMapper corpInfoMapper;
    public void initData() throws IOException {
        System.out.println("初始化项目数据 包括 部门 人员");
        handleAllData.initData();
    }

    public void getCustomerData() throws IOException {
        System.out.println("抓取企业微信客户数据");
        handleAllData.handleAllData();
    }

    public void createAllCustomerReport() throws IOException {
        System.out.println("计算所有流量看板数据");
        handleAllData.createAllReportData();
    }

    public void createAllDepartmentReportData() throws IOException {
        System.out.println("计算所有流量看板数据");
        handleAllData.createAllDepartmentReportData();
    }

    public void createAllDepartmentReportDataV2() throws IOException {
        System.out.println("计算所有流量看板数据V2");
        handleAllDataV2.createAllReportDataV2();
    }

    public void createCurDateCustomerReport() throws IOException {
        Date from = Date.from(LocalDate.now().atStartOfDay()
                .atZone(ZoneId.systemDefault()).toInstant());
        System.out.println("计算" + JSON.toJSONString(from) + "流量看板数据");
        List<CorpInfo> corpInfos = corpInfoMapper.selectCorpInfoList(new CorpInfo());
        corpInfos.forEach(item->{
            handleAllData.createReportData(item.getCorpId(), from);
        });
    }

    public void createCurDateDepartmentReportData() throws IOException {
        Date from = Date.from(LocalDate.now().atStartOfDay()
                .atZone(ZoneId.systemDefault()).toInstant());
        System.out.println("计算" + JSON.toJSONString(from) + "销售看板数据");
        List<CorpInfo> corpInfos = corpInfoMapper.selectCorpInfoList(new CorpInfo());
        corpInfos.forEach(item->{
            handleAllData.createDepartmentReportData(item.getCorpId(),Date.from(LocalDate.now().atStartOfDay()
                    .atZone(ZoneId.systemDefault()).toInstant()));
        });
    }

    public void createCurDateCustomerReportV2() throws IOException {
        Date from = Date.from(LocalDate.now().atStartOfDay()
                .atZone(ZoneId.systemDefault()).toInstant());
        System.out.println("计算" + JSON.toJSONString(from) + "流量看板数据");
        List<CorpInfo> corpInfos = corpInfoMapper.selectCorpInfoList(new CorpInfo());
        corpInfos.forEach(item->{
            handleAllDataV2.createReportDataV2(item.getCorpId(), from);
        });
    }

    //昨天的 流量v2数据
    public void createYestodayCustomerReportV2() throws IOException {
        Date from = Date.from(LocalDate.now().minusDays(1L).atStartOfDay()
                .atZone(ZoneId.systemDefault()).toInstant());
        System.out.println("计算V2" + JSON.toJSONString(from) + "流量看板数据");
        List<CorpInfo> corpInfos = corpInfoMapper.selectCorpInfoList(new CorpInfo());
        corpInfos.forEach(item->{
            handleAllDataV2.createReportDataV2(item.getCorpId(), from);
        });
    }

    //昨天的销售看板数据
    public void createYestodayDepartmentReportData() throws IOException {
        Date from = Date.from(LocalDate.now().minusDays(1L).atStartOfDay()
                .atZone(ZoneId.systemDefault()).toInstant());
        System.out.println("计算" + JSON.toJSONString(from) + "销售看板数据");
        List<CorpInfo> corpInfos = corpInfoMapper.selectCorpInfoList(new CorpInfo());
        corpInfos.forEach(item->{
            handleAllData.createDepartmentReportData(item.getCorpId(),Date.from(LocalDate.now().minusDays(1L).atStartOfDay()
                    .atZone(ZoneId.systemDefault()).toInstant()));
        });
    }


}
