package cn.com.countrygarden.bi.main.service.wcx;

import cn.com.countrygarden.bi.main.constant.wcx.ApplicationContextRegister;
import cn.com.countrygarden.bi.main.dao.wcx.WcxWeeklyStatisticsMapper;
import cn.com.countrygarden.bi.main.dao.wcx.WcxWeeklyStatisticsRegionMapper;
import cn.com.countrygarden.bi.main.dao.wcx.WcxWeeklyStatisticsRegionProjectMapper;
import cn.com.countrygarden.bi.main.model.wcx.WcxWeeklyStatistics;
import cn.com.countrygarden.bi.main.model.wcx.WcxWeeklyStatisticsRegion;
import cn.com.countrygarden.bi.main.model.wcx.WcxWeeklyStatisticsRegionProject;
import cn.com.countrygarden.bi.main.util.erp.StringUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.io.IOException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Slf4j
@Configuration
@EnableScheduling
public class WcxSchedu<c> {

    String zhouUrl="https://api.weixin.qq.com/datacube/getweanalysisappidweeklyvisittrend?access_token=";

    String yueUrl="https://api.weixin.qq.com/datacube/getweanalysisappidmonthlyvisittrend?access_token=";

    String riUrl="https://api.weixin.qq.com/datacube/getweanalysisappiddailyvisittrend?access_token=";

    WcxService wcxService= ApplicationContextRegister.getApplicationContext().getBean(WcxService.class);

    WcxWeeklyStatisticsMapper wcxWeeklyStatisticsMapper=ApplicationContextRegister.getApplicationContext().getBean(WcxWeeklyStatisticsMapper.class);

    WcxWeeklyStatisticsRegionMapper weeklyStatisticsRegionMapper=ApplicationContextRegister.getApplicationContext().getBean(WcxWeeklyStatisticsRegionMapper.class);

    WcxWeeklyStatisticsRegionProjectMapper weeklyStatisticsRegionProjectMapper=ApplicationContextRegister.getApplicationContext().getBean(WcxWeeklyStatisticsRegionProjectMapper.class);

   // @Scheduled(cron = "1 10 0 * * ?")
   @Scheduled(cron = "2 10 0 1 1 ?")
    public void tongjiri() throws IOException {
        log.info("微创新报表定时器执行开始---------------------------");
       System.out.println("微创新报表定时器执行开始---------------------------");

       Map<String,Object> wxSessionMap = wcxService.getWxAccessToken();
       if(null == wxSessionMap){
           log.error("调用返回值为空");
       }
       if(wxSessionMap.containsKey("errcode")){
           log.error("未知异常errcode");
       }
       String accessToken = (String)wxSessionMap.get("access_token");
       Calendar cal = Calendar.getInstance();
       int yeas = cal.get(Calendar.YEAR);
       yeas=yeas-1;
       Integer userNumsum=0;
       Integer newUserNumsum=0;
       WcxWeeklyStatistics weeklyStatistics=new WcxWeeklyStatistics();
       weeklyStatistics.setWekindex(3);

       weeklyStatistics.setDateRange(String.valueOf(yeas));

       weeklyStatistics.setWeek(String.valueOf(yeas));


       Map<String, Map<String, String>> getYearWeekMapyue=getMonthWeekMap(yeas);
       for (int i = 1; i <=getYearWeekMapyue.size() ; i++) {
           Map<String, String> map=getYearWeekMapyue.get(String.valueOf(i));
           String[] startyue=map.get("monthStart").split("/");
           String[] endyue=map.get("monthEnd").split("/");

           String startyueStr=startyue[0]+startyue[1]+startyue[2];
           String endyueStr=endyue[0]+endyue[1]+endyue[2];

           String urlzh=yueUrl+accessToken;
           String zhoushuju=wcxService.postUr(urlzh,startyueStr,endyueStr);

           JSONObject object = JSONObject.parseObject(zhoushuju);
           String jsoerr=object.getString("errcode");
           if(StringUtil.isNotBlank(jsoerr)){
               continue;
           }
           JSONArray jsonlist=object.getJSONArray("list");

           if(jsonlist!=null){
               if(jsonlist.size()!=0){
                   for (int j = 0; j < jsonlist.size(); j++) {
                       JSONObject jObject3=jsonlist.getJSONObject(j);
                       userNumsum+=Integer.valueOf(jObject3.getString("visit_uv"));
                       newUserNumsum+=Integer.valueOf(jObject3.getString("visit_uv_new"));
                   }

               }
           }
       }

       weeklyStatistics.setInnovationNum(0);
       weeklyStatistics.setInnovationNumChain("0");
       weeklyStatistics.setUserNum(userNumsum);
       weeklyStatistics.setUserNumChain("0");
       weeklyStatistics.setNewUserNum(newUserNumsum);
       weeklyStatistics.setNewUserChain("0");


       wcxWeeklyStatisticsMapper.insert(weeklyStatistics);
       //weeklyStatisticsRegionMapper.insert(weeklyStatisticsRegion);
       //weeklyStatisticsRegionProjectMapper.insert(weeklyStatisticsRegionProject);

       List<WcxWeeklyStatistics> innovationNumDateTongji= wcxWeeklyStatisticsMapper.innovationNumDateTongji();
       for (int c = 0; c <innovationNumDateTongji.size() ; c++) {
           WcxWeeklyStatistics weics=new WcxWeeklyStatistics();
           weics.setWeek(innovationNumDateTongji.get(c).getWeek());
           weics.setInnovationNum(innovationNumDateTongji.get(c).getInnovationNum());
           if(c==0){
               weics.setInnovationNumChain("0");
           }else {
               //(Double.valueOf(innovationNumDateTongji1.get(c).getInnovationNum())-Double.valueOf(innovationNumDateTongji1.get(c-1).getInnovationNum()))/Double.valueOf(innovationNumDateTongji1.get(c-1).getInnovationNum())*100
               DecimalFormat df = new DecimalFormat("#.00");
               Double sumdo=(Double.valueOf(innovationNumDateTongji.get(c).getInnovationNum())-Double.valueOf(innovationNumDateTongji.get(c-1).getInnovationNum()))/Double.valueOf(innovationNumDateTongji.get(c-1).getInnovationNum())*100;
               weics.setInnovationNumChain(df.format(sumdo));
           }

           wcxWeeklyStatisticsMapper.updateWeek(weics);
       }


       List<WcxWeeklyStatisticsRegion> innovationNumRiTongjiRegion=weeklyStatisticsRegionMapper.innovationNumRiTongjiRegion();

       for (int i = 0; i <innovationNumRiTongjiRegion.size() ; i++) {
           WcxWeeklyStatisticsRegion weeklyStion=new WcxWeeklyStatisticsRegion();
           weeklyStion.setWekindex(3);
           weeklyStion.setWeek(String.valueOf(yeas));
           weeklyStion.setDateRange(String.valueOf(yeas));
           weeklyStion.setInnovationNum(innovationNumRiTongjiRegion.get(i).getInnovationNum());
           weeklyStion.setInnovationNumChain(innovationNumRiTongjiRegion.get(i).getInnovationNumChain());
           weeklyStion.setUserNum(0);
           weeklyStion.setUserNumChain("0");
           weeklyStion.setNewUserNum(0);
           weeklyStion.setNewUserChain("0");
           weeklyStion.setOrgCode(innovationNumRiTongjiRegion.get(i).getOrgCode());
           weeklyStion.setOrgName(innovationNumRiTongjiRegion.get(i).getOrgName());
           weeklyStion.setInnovationNum(innovationNumRiTongjiRegion.get(i).getInnovationNum());
           weeklyStion.setInnovationNumChain(innovationNumRiTongjiRegion.get(i).getInnovationNumChain());
           // weeklyStatisticsRegionMapper.updateWeekRegion(weeklyStion);
           weeklyStatisticsRegionMapper.insert(weeklyStion);
       }



       List<WcxWeeklyStatisticsRegionProject> innovationNumRiTongjiRegionProject=weeklyStatisticsRegionProjectMapper.innovationNumRiTongjiRegionProject();

       for (int i = 0; i <innovationNumRiTongjiRegionProject.size() ; i++) {
           WcxWeeklyStatisticsRegionProject weeklyStRegiProj=new WcxWeeklyStatisticsRegionProject();
           weeklyStRegiProj.setWekindex(3);
           weeklyStRegiProj.setInnovationNum(innovationNumRiTongjiRegionProject.get(i).getInnovationNum());
           weeklyStRegiProj.setInnovationNumChain(innovationNumRiTongjiRegionProject.get(i).getInnovationNumChain());
           weeklyStRegiProj.setUserNum(0);
           weeklyStRegiProj.setUserNumChain("0");
           weeklyStRegiProj.setNewUserNum(0);
           weeklyStRegiProj.setNewUserChain("0");
           weeklyStRegiProj.setWeek(String.valueOf(yeas));
           weeklyStRegiProj.setDateRange(String.valueOf(yeas));
           weeklyStRegiProj.setWeek(innovationNumRiTongjiRegionProject.get(i).getWeek());
           weeklyStRegiProj.setOrgCode(innovationNumRiTongjiRegionProject.get(i).getOrgCode());
           weeklyStRegiProj.setOrgName(innovationNumRiTongjiRegionProject.get(i).getOrgName());
           weeklyStRegiProj.setCommCode(innovationNumRiTongjiRegionProject.get(i).getCommCode());
           weeklyStRegiProj.setCommName(innovationNumRiTongjiRegionProject.get(i).getCommName());
           weeklyStRegiProj.setInnovationNum(innovationNumRiTongjiRegionProject.get(i).getInnovationNum());
           weeklyStRegiProj.setInnovationNumChain(innovationNumRiTongjiRegionProject.get(i).getInnovationNumChain());
           //weeklyStatisticsRegionProjectMapper.updateWeekRegionProject(weeklyStRegiProj);
           weeklyStatisticsRegionProjectMapper.insert(weeklyStRegiProj);
       }


       List<WcxWeeklyStatistics> StatisticsNumZhouTongji= wcxWeeklyStatisticsMapper.StatisticsNumZhouTongji();

       for (int c = 0; c <StatisticsNumZhouTongji.size() ; c++) {
           WcxWeeklyStatistics weics=new WcxWeeklyStatistics();
           weics.setWeek(StatisticsNumZhouTongji.get(c).getWeek());
           if(c==0){
               weics.setUserNumChain("0");
               weics.setNewUserChain("0");
           }else {
               DecimalFormat df = new DecimalFormat("#.00");
               Double sumdoun=(Double.valueOf(StatisticsNumZhouTongji.get(c).getUserNum())-Double.valueOf(StatisticsNumZhouTongji.get(c-1).getUserNum()))/Double.valueOf(StatisticsNumZhouTongji.get(c-1).getUserNum())*100;

               Double sumdonun=(Double.valueOf(StatisticsNumZhouTongji.get(c).getNewUserNum())-Double.valueOf(StatisticsNumZhouTongji.get(c-1).getNewUserNum()))/Double.valueOf(StatisticsNumZhouTongji.get(c-1).getNewUserNum())*100;

               weics.setUserNumChain(df.format(sumdoun));
               weics.setNewUserChain(df.format(sumdonun));
           }

           wcxWeeklyStatisticsMapper.updateWeeChain(weics);
       }
       /* Calendar cal= Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        Date time=cal.getTime();

        String startyueStr=new SimpleDateFormat("yyyyMMdd").format(time);
        String endyueStr=new SimpleDateFormat("yyyyMMdd").format(time);
        String urlzh=riUrl+accessToken;
        String zhoushuju=wcxService.postUr(urlzh,startyueStr,endyueStr);


        String startyueStr22=new SimpleDateFormat("yyyy-MM-dd").format(time);
        JSONObject object = JSONObject.parseObject(zhoushuju);
        String jsoerr=object.getString("errcode");
        if(StringUtil.isNotBlank(jsoerr)){
            log.error("未知异常errcode");
        }
        JSONArray jsonlist=object.getJSONArray("list");

        WcxWeeklyStatistics weeklyStatistics=new WcxWeeklyStatistics();
        WcxWeeklyStatisticsRegion weeklyStatisticsRegion=new WcxWeeklyStatisticsRegion();
        WcxWeeklyStatisticsRegionProject weeklyStatisticsRegionProject=new WcxWeeklyStatisticsRegionProject();
        weeklyStatistics.setWekindex(3);
        weeklyStatisticsRegion.setWekindex(3);
        weeklyStatisticsRegionProject.setWekindex(3);

        weeklyStatistics.setDateRange(startyueStr22);
        weeklyStatisticsRegion.setDateRange(startyueStr22);
        weeklyStatisticsRegionProject.setDateRange(startyueStr22);

        weeklyStatistics.setWeek(startyueStr22);
        weeklyStatisticsRegion.setWeek(startyueStr22);
        weeklyStatisticsRegionProject.setWeek(startyueStr22);
        if(jsonlist!=null){
            if(jsonlist.size()!=0){
                for (int j = 0; j < jsonlist.size(); j++) {
                    JSONObject jObject3=jsonlist.getJSONObject(j);
                    weeklyStatistics.setInnovationNum(0);
                    weeklyStatistics.setInnovationNumChain("0");
                    weeklyStatistics.setUserNum(Integer.valueOf(jObject3.getString("visit_uv")));
                    weeklyStatistics.setUserNumChain("0");
                    weeklyStatistics.setNewUserNum(Integer.valueOf(jObject3.getString("visit_uv_new")));
                    weeklyStatistics.setNewUserChain("0");


                    weeklyStatisticsRegion.setInnovationNum(0);
                    weeklyStatisticsRegion.setInnovationNumChain("0");
                    weeklyStatisticsRegion.setUserNum(Integer.valueOf(jObject3.getString("visit_uv")));
                    weeklyStatisticsRegion.setUserNumChain("0");
                    weeklyStatisticsRegion.setNewUserNum(Integer.valueOf(jObject3.getString("visit_uv_new")));
                    weeklyStatisticsRegion.setNewUserChain("0");

                    weeklyStatisticsRegionProject.setInnovationNum(0);
                    weeklyStatisticsRegionProject.setInnovationNumChain("0");
                    weeklyStatisticsRegionProject.setUserNum(Integer.valueOf(jObject3.getString("visit_uv")));
                    weeklyStatisticsRegionProject.setUserNumChain("0");
                    weeklyStatisticsRegionProject.setNewUserNum(Integer.valueOf(jObject3.getString("visit_uv_new")));
                    weeklyStatisticsRegionProject.setNewUserChain("0");
                }
            }else {
                weeklyStatistics.setInnovationNum(0);
                weeklyStatistics.setInnovationNumChain("0");
                weeklyStatistics.setUserNum(0);
                weeklyStatistics.setUserNumChain("0");
                weeklyStatistics.setNewUserNum(0);
                weeklyStatistics.setNewUserChain("0");

                weeklyStatisticsRegion.setInnovationNum(0);
                weeklyStatisticsRegion.setInnovationNumChain("0");
                weeklyStatisticsRegion.setUserNum(0);
                weeklyStatisticsRegion.setUserNumChain("0");
                weeklyStatisticsRegion.setNewUserNum(0);
                weeklyStatisticsRegion.setNewUserChain("0");

                weeklyStatisticsRegionProject.setInnovationNum(0);
                weeklyStatisticsRegionProject.setInnovationNumChain("0");
                weeklyStatisticsRegionProject.setUserNum(0);
                weeklyStatisticsRegionProject.setUserNumChain("0");
                weeklyStatisticsRegionProject.setNewUserNum(0);
                weeklyStatisticsRegionProject.setNewUserChain("0");
            }
            wcxWeeklyStatisticsMapper.insert(weeklyStatistics);
            weeklyStatisticsRegionMapper.insert(weeklyStatisticsRegion);
            weeklyStatisticsRegionProjectMapper.insert(weeklyStatisticsRegionProject);
        }

    List<WcxWeeklyStatistics> innovationNumDateTongji= wcxWeeklyStatisticsMapper.innovationNumDateTongji();


        for (int c = 0; c <innovationNumDateTongji.size(); c++) {
        WcxWeeklyStatistics weics=new WcxWeeklyStatistics();
        weics.setWeek(innovationNumDateTongji.get(c).getWeek());
        weics.setInnovationNum(innovationNumDateTongji.get(c).getInnovationNum());
        weics.setInnovationNumChain(innovationNumDateTongji.get(c).getInnovationNumChain());
        wcxWeeklyStatisticsMapper.updateWeek(weics);
    }


    List<WcxWeeklyStatisticsRegion> innovationNumRiTongjiRegion=weeklyStatisticsRegionMapper.innovationNumRiTongjiRegion();

        for (int i = 0; i <innovationNumRiTongjiRegion.size();i++) {
        WcxWeeklyStatisticsRegion weeklyStion=new WcxWeeklyStatisticsRegion();
        weeklyStion.setWeek(innovationNumRiTongjiRegion.get(i).getWeek());
        weeklyStion.setOrgCode(innovationNumRiTongjiRegion.get(i).getOrgCode());
        weeklyStion.setOrgName(innovationNumRiTongjiRegion.get(i).getOrgName());
        weeklyStion.setInnovationNum(innovationNumRiTongjiRegion.get(i).getInnovationNum());
        weeklyStion.setInnovationNumChain(innovationNumRiTongjiRegion.get(i).getInnovationNumChain());
        weeklyStatisticsRegionMapper.updateWeekRegion(weeklyStion);
    }



    List<WcxWeeklyStatisticsRegionProject> innovationNumRiTongjiRegionProject=weeklyStatisticsRegionProjectMapper.innovationNumRiTongjiRegionProject();

        for (int i = 0; i <innovationNumRiTongjiRegionProject.size();i++) {
        WcxWeeklyStatisticsRegionProject weeklyStRegiProj=new WcxWeeklyStatisticsRegionProject();
        weeklyStRegiProj.setWeek(innovationNumRiTongjiRegionProject.get(i).getWeek());
        weeklyStRegiProj.setOrgCode(innovationNumRiTongjiRegionProject.get(i).getOrgCode());
        weeklyStRegiProj.setOrgName(innovationNumRiTongjiRegionProject.get(i).getOrgName());
        weeklyStRegiProj.setCommCode(innovationNumRiTongjiRegionProject.get(i).getCommCode());
        weeklyStRegiProj.setCommName(innovationNumRiTongjiRegionProject.get(i).getCommName());
        weeklyStRegiProj.setInnovationNum(innovationNumRiTongjiRegionProject.get(i).getInnovationNum());
        weeklyStRegiProj.setInnovationNumChain(innovationNumRiTongjiRegionProject.get(i).getInnovationNumChain());
        weeklyStatisticsRegionProjectMapper.updateWeekRegionProject(weeklyStRegiProj);
    }


    List<WcxWeeklyStatistics> StatisticsNumZhouTongji= wcxWeeklyStatisticsMapper.StatisticsNumZhouTongji(3);

        for (int c = 0; c <StatisticsNumZhouTongji.size();c++) {
        WcxWeeklyStatistics weics=new WcxWeeklyStatistics();
        weics.setWeek(StatisticsNumZhouTongji.get(c).getWeek());
        weics.setUserNumChain(StatisticsNumZhouTongji.get(c).getUserNumChain());
        weics.setNewUserChain(StatisticsNumZhouTongji.get(c).getNewUserChain());
        wcxWeeklyStatisticsMapper.updateWeeChain(weics);
    }*/

        log.info("微创新报表定时器执行完毕---------------------");
       System.out.println("微创新报表定时器执行完毕---------------------------");

   }

    /**
     * 获取某年所有月的时间跨度
     * @param year
     * @return
     */
    public static Map<String, Map<String, String>> getMonthWeekMap(int year) {
        Map<String, Map<String, String>> monthWeekMap = new HashMap<String, Map<String, String>>();
        for (int i = 1; i <=12 ; i++) {
            Map<String, String> map=new HashMap<>();
            String getMonthStart=getMonthStart(year,i);
            String getMonthEnd=getMonthEnd(year,i);
            map.put("monthStart",getMonthStart);
            map.put("monthEnd",getMonthEnd);
            monthWeekMap.put(i+"",map);
        }
        return monthWeekMap;
    }

    /**
     * 获取某年某月开始日期
     * @return String
     * **/
    public static String getMonthStart(int year,int month){
        Calendar cal=Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month-1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy/MM/dd").format(time);
    }

    /**
     * 获取某年某月最后一天
     * @return String
     * **/
    public static String getMonthEnd(int year,int month){
        Calendar cal=Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month-1);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy/MM/dd").format(time);
    }

    @Scheduled(cron = "1 1 5 1 * ?")
    public void tongjiyue() throws IOException {
        log.info("微创新报表定时器执行开始---------------------------");
        System.out.println("微创新报表定时器执行开始---------------------------");

        Map<String,Object> wxSessionMap = wcxService.getWxAccessToken();
        if(null == wxSessionMap){
            log.error("调用返回值为空");
        }
        if(wxSessionMap.containsKey("errcode")){
            log.error("未知异常errcode");
        }
        String accessToken = (String)wxSessionMap.get("access_token");

        LocalDate today = LocalDate.now();
        today = today.minusMonths(1);
        DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy/MM");
        String qianri=formatters.format(today)+"/01";

        String year=formatters.format(today).substring(0,4);
        String month=formatters.format(today).substring(6,7);


        Calendar cal=Calendar.getInstance();
        cal.set(Calendar.YEAR, Integer.valueOf(year));
        cal.set(Calendar.MONTH, Integer.valueOf(month)-1);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date time=cal.getTime();
        String houri=new SimpleDateFormat("yyyy/MM/dd").format(time);
        String[] startyue=qianri.split("/");
        String[] endyue=houri.split("/");

        String startyueStr=startyue[0]+startyue[1]+startyue[2];
        String endyueStr=endyue[0]+endyue[1]+endyue[2];

        String urlzh=yueUrl+accessToken;
        String zhoushuju=wcxService.postUr(urlzh,startyueStr,endyueStr);

        JSONObject object = JSONObject.parseObject(zhoushuju);
        String jsoerr=object.getString("errcode");
        if(StringUtil.isNotBlank(jsoerr)){
            log.error("未知异常errcode");
        }
        JSONArray jsonlist=object.getJSONArray("list");


        WcxWeeklyStatistics weeklyStatistics=new WcxWeeklyStatistics();
        WcxWeeklyStatisticsRegion weeklyStatisticsRegion=new WcxWeeklyStatisticsRegion();
        WcxWeeklyStatisticsRegionProject weeklyStatisticsRegionProject=new WcxWeeklyStatisticsRegionProject();
        weeklyStatistics.setWekindex(2);
        weeklyStatisticsRegion.setWekindex(2);
        weeklyStatisticsRegionProject.setWekindex(2);
        weeklyStatistics.setDateRange(qianri+"-"+houri);
        weeklyStatisticsRegion.setDateRange(qianri+"-"+houri);
        weeklyStatisticsRegionProject.setDateRange(qianri+"-"+houri);
        if(Integer.valueOf(month)<10&&Integer.valueOf(month)>0){
            weeklyStatistics.setWeek(year+"年第"+"0"+Integer.valueOf(month)+"月");
            weeklyStatisticsRegion.setWeek(year+"年第"+"0"+Integer.valueOf(month)+"月");
            weeklyStatisticsRegionProject.setWeek(year+"年第"+"0"+Integer.valueOf(month)+"月");
        }else {
            weeklyStatistics.setWeek(year+"年第"+Integer.valueOf(month)+"月");
            weeklyStatisticsRegion.setWeek(year+"年第"+Integer.valueOf(month)+"月");
            weeklyStatisticsRegionProject.setWeek(year+"年第"+Integer.valueOf(month)+"月");
        }

        if(jsonlist!=null){
            if(jsonlist.size()!=0){
                for (int j = 0; j < jsonlist.size(); j++) {
                    JSONObject jObject3=jsonlist.getJSONObject(j);
                    weeklyStatistics.setInnovationNum(0);
                    weeklyStatistics.setInnovationNumChain("0");
                    weeklyStatistics.setUserNum(Integer.valueOf(jObject3.getString("visit_uv")));
                    weeklyStatistics.setUserNumChain("0");
                    weeklyStatistics.setNewUserNum(Integer.valueOf(jObject3.getString("visit_uv_new")));
                    weeklyStatistics.setNewUserChain("0");

                    weeklyStatisticsRegion.setInnovationNum(0);
                    weeklyStatisticsRegion.setInnovationNumChain("0");
                    weeklyStatisticsRegion.setUserNum(0);
                    weeklyStatisticsRegion.setUserNumChain("0");
                    weeklyStatisticsRegion.setNewUserNum(0);
                    weeklyStatisticsRegion.setNewUserChain("0");


                    weeklyStatisticsRegionProject.setInnovationNum(0);
                    weeklyStatisticsRegionProject.setInnovationNumChain("0");
                    weeklyStatisticsRegionProject.setUserNum(0);
                    weeklyStatisticsRegionProject.setUserNumChain("0");
                    weeklyStatisticsRegionProject.setNewUserNum(0);
                    weeklyStatisticsRegionProject.setNewUserChain("0");
                }
            }else {
                weeklyStatistics.setInnovationNum(0);
                weeklyStatistics.setInnovationNumChain("0");
                weeklyStatistics.setUserNum(0);
                weeklyStatistics.setUserNumChain("0");
                weeklyStatistics.setNewUserNum(0);
                weeklyStatistics.setNewUserChain("0");

                weeklyStatisticsRegion.setInnovationNum(0);
                weeklyStatisticsRegion.setInnovationNumChain("0");
                weeklyStatisticsRegion.setUserNum(0);
                weeklyStatisticsRegion.setUserNumChain("0");
                weeklyStatisticsRegion.setNewUserNum(0);
                weeklyStatisticsRegion.setNewUserChain("0");

                weeklyStatisticsRegionProject.setInnovationNum(0);
                weeklyStatisticsRegionProject.setInnovationNumChain("0");
                weeklyStatisticsRegionProject.setUserNum(0);
                weeklyStatisticsRegionProject.setUserNumChain("0");
                weeklyStatisticsRegionProject.setNewUserNum(0);
                weeklyStatisticsRegionProject.setNewUserChain("0");
            }
            wcxWeeklyStatisticsMapper.insert(weeklyStatistics);
            weeklyStatisticsRegionMapper.insert(weeklyStatisticsRegion);
            weeklyStatisticsRegionProjectMapper.insert(weeklyStatisticsRegionProject);
        }


    List<WcxWeeklyStatistics> innovationNumYueTongji= wcxWeeklyStatisticsMapper.innovationNumYueTongji();
        for (int c = 0; c <innovationNumYueTongji.size() ; c++) {
        WcxWeeklyStatistics weics=new WcxWeeklyStatistics();
        weics.setWeek(innovationNumYueTongji.get(c).getWeek());
        weics.setInnovationNum(innovationNumYueTongji.get(c).getInnovationNum());
        weics.setInnovationNumChain(innovationNumYueTongji.get(c).getInnovationNumChain());
        wcxWeeklyStatisticsMapper.updateWeek(weics);
    }

    List<WcxWeeklyStatisticsRegion> innovationNumYueTongjiRegion=weeklyStatisticsRegionMapper.innovationNumYueTongjiRegion();

        for (int i = 0; i <innovationNumYueTongjiRegion.size() ; i++) {
        WcxWeeklyStatisticsRegion weeklyStion=new WcxWeeklyStatisticsRegion();
        weeklyStion.setWeek(innovationNumYueTongjiRegion.get(i).getWeek());
        weeklyStion.setOrgCode(innovationNumYueTongjiRegion.get(i).getOrgCode());
        weeklyStion.setOrgName(innovationNumYueTongjiRegion.get(i).getOrgName());
        weeklyStion.setInnovationNum(innovationNumYueTongjiRegion.get(i).getInnovationNum());
        weeklyStion.setInnovationNumChain(innovationNumYueTongjiRegion.get(i).getInnovationNumChain());
        weeklyStatisticsRegionMapper.updateWeekRegion(weeklyStion);
    }


    List<WcxWeeklyStatisticsRegionProject> innovationNumYueTongjiRegionProject=weeklyStatisticsRegionProjectMapper.innovationNumYueTongjiRegionProject();

        for (int i = 0; i <innovationNumYueTongjiRegionProject.size() ; i++) {
        WcxWeeklyStatisticsRegionProject weeklyStRegiProj=new WcxWeeklyStatisticsRegionProject();
        weeklyStRegiProj.setWeek(innovationNumYueTongjiRegionProject.get(i).getWeek());
        weeklyStRegiProj.setOrgCode(innovationNumYueTongjiRegionProject.get(i).getOrgCode());
        weeklyStRegiProj.setOrgName(innovationNumYueTongjiRegionProject.get(i).getOrgName());
        weeklyStRegiProj.setCommCode(innovationNumYueTongjiRegionProject.get(i).getCommCode());
        weeklyStRegiProj.setCommName(innovationNumYueTongjiRegionProject.get(i).getCommName());
        weeklyStRegiProj.setInnovationNum(innovationNumYueTongjiRegionProject.get(i).getInnovationNum());
        weeklyStRegiProj.setInnovationNumChain(innovationNumYueTongjiRegionProject.get(i).getInnovationNumChain());
        weeklyStatisticsRegionProjectMapper.updateWeekRegionProject(weeklyStRegiProj);
    }



    List<WcxWeeklyStatistics> StatisticsNumZhouTongji= wcxWeeklyStatisticsMapper.StatisticsNumZhouTongjican(2);

        for (int c = 0; c <StatisticsNumZhouTongji.size() ; c++) {
        WcxWeeklyStatistics weics=new WcxWeeklyStatistics();
        weics.setWeek(StatisticsNumZhouTongji.get(c).getWeek());
        weics.setUserNumChain(StatisticsNumZhouTongji.get(c).getUserNumChain());
        weics.setNewUserChain(StatisticsNumZhouTongji.get(c).getNewUserChain());
        wcxWeeklyStatisticsMapper.updateWeeChain(weics);
    }

        log.info("微创新报表定时器执行完毕---------------------");
        System.out.println("微创新报表定时器执行完毕---------------------------");

    }

    @Scheduled(cron = "1 1 3 ? * 2")
    public void tongjizhou() throws IOException, ParseException {
        log.info("微创新报表定时器执行开始---------------------------");
        System.out.println("微创新报表定时器执行开始---------------------------");

        Calendar calendar = Calendar.getInstance();
        Integer year = calendar.get(Calendar.YEAR);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd");


        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
        int offset1 = 1 - dayOfWeek;
        int offset2 = 7 - dayOfWeek;
        calendar1.add(Calendar.DATE, offset1 - 7);
        calendar2.add(Calendar.DATE, offset2 - 7);
        String lastBeginDate = sdf.format(calendar1.getTime());
        String lastEndDate = sdf.format(calendar2.getTime());
        System.out.println(lastBeginDate + "," + lastEndDate);

        String lastBeginDate2 = sdf2.format(calendar1.getTime());
        String lastEndDate2 = sdf2.format(calendar2.getTime());

        String[] startyuenian=lastBeginDate2.split("/");



        Map<String,Object> wxSessionMap = wcxService.getWxAccessToken();
        if(null == wxSessionMap){
            log.error("调用返回值为空");
        }
        if(wxSessionMap.containsKey("errcode")){
            log.error("未知异常errcode");
        }
        String accessToken = (String)wxSessionMap.get("access_token");

        Map<String, Map<String, String>> getYearWeekMapyue=getYearWeekMap(year);
        for (int k = 1; k <=getYearWeekMapyue.size() ; k++) {
            Map<String, String> map=getYearWeekMapyue.get(String.valueOf(k));
            if(map.get("startTime").equals(lastBeginDate2)){
                Integer zhou=k;

                String urlzh=zhouUrl+accessToken;
                String zhoushuju=wcxService.postUr(urlzh,lastBeginDate,lastEndDate);

                JSONObject object = JSONObject.parseObject(zhoushuju);
                String jsoerr=object.getString("errcode");
                if(StringUtil.isNotBlank(jsoerr)){
                    log.error("未知异常errcode");
                }


                JSONArray jsonlist=object.getJSONArray("list");
                WcxWeeklyStatistics weeklyStatistics=new WcxWeeklyStatistics();
                WcxWeeklyStatisticsRegion weeklyStatisticsRegion=new WcxWeeklyStatisticsRegion();
                WcxWeeklyStatisticsRegionProject weeklyStatisticsRegionProject=new WcxWeeklyStatisticsRegionProject();
                weeklyStatistics.setWekindex(1);
                weeklyStatisticsRegion.setWekindex(1);
                weeklyStatisticsRegionProject.setWekindex(1);
                weeklyStatistics.setDateRange(lastBeginDate2+"-"+lastEndDate2);
                weeklyStatisticsRegion.setDateRange(lastBeginDate2+"-"+lastEndDate2);
                weeklyStatisticsRegionProject.setDateRange(lastBeginDate2+"-"+lastEndDate2);



                if(zhou<10&&zhou>0){
                    weeklyStatistics.setWeek(startyuenian[0]+"年第"+"0"+zhou+"周");
                    weeklyStatisticsRegion.setWeek(startyuenian[0]+"年第"+"0"+zhou+"周");
                    weeklyStatisticsRegionProject.setWeek(startyuenian[0]+"年第"+"0"+zhou+"周");
                }else {
                    weeklyStatistics.setWeek(startyuenian[0]+"年第"+zhou+"周");
                    weeklyStatisticsRegion.setWeek(startyuenian[0]+"年第"+zhou+"周");
                    weeklyStatisticsRegionProject.setWeek(startyuenian[0]+"年第"+zhou+"周");
                }

                if(jsonlist!=null){
                    if(jsonlist.size()!=0){
                        for (int j = 0; j < jsonlist.size(); j++) {
                            JSONObject jObject3=jsonlist.getJSONObject(j);
                            weeklyStatistics.setInnovationNum(0);
                            weeklyStatistics.setInnovationNumChain("0");
                            weeklyStatistics.setUserNum(Integer.valueOf(jObject3.getString("visit_uv")));
                            weeklyStatistics.setUserNumChain("0");
                            weeklyStatistics.setNewUserNum(Integer.valueOf(jObject3.getString("visit_uv_new")));
                            weeklyStatistics.setNewUserChain("0");

                            weeklyStatisticsRegion.setInnovationNum(0);
                            weeklyStatisticsRegion.setInnovationNumChain("0");
                            weeklyStatisticsRegion.setUserNum(0);
                            weeklyStatisticsRegion.setUserNumChain("0");
                            weeklyStatisticsRegion.setNewUserNum(0);
                            weeklyStatisticsRegion.setNewUserChain("0");


                            weeklyStatisticsRegionProject.setInnovationNum(0);
                            weeklyStatisticsRegionProject.setInnovationNumChain("0");
                            weeklyStatisticsRegionProject.setUserNum(0);
                            weeklyStatisticsRegionProject.setUserNumChain("0");
                            weeklyStatisticsRegionProject.setNewUserNum(0);
                            weeklyStatisticsRegionProject.setNewUserChain("0");
                        }
                    }else {
                        weeklyStatistics.setInnovationNum(0);
                        weeklyStatistics.setInnovationNumChain("0");
                        weeklyStatistics.setUserNum(0);
                        weeklyStatistics.setUserNumChain("0");
                        weeklyStatistics.setNewUserNum(0);
                        weeklyStatistics.setNewUserChain("0");

                        weeklyStatisticsRegion.setInnovationNum(0);
                        weeklyStatisticsRegion.setInnovationNumChain("0");
                        weeklyStatisticsRegion.setUserNum(0);
                        weeklyStatisticsRegion.setUserNumChain("0");
                        weeklyStatisticsRegion.setNewUserNum(0);
                        weeklyStatisticsRegion.setNewUserChain("0");

                        weeklyStatisticsRegionProject.setInnovationNum(0);
                        weeklyStatisticsRegionProject.setInnovationNumChain("0");
                        weeklyStatisticsRegionProject.setUserNum(0);
                        weeklyStatisticsRegionProject.setUserNumChain("0");
                        weeklyStatisticsRegionProject.setNewUserNum(0);
                        weeklyStatisticsRegionProject.setNewUserChain("0");
                    }
                    wcxWeeklyStatisticsMapper.insert(weeklyStatistics);
                    weeklyStatisticsRegionMapper.insert(weeklyStatisticsRegion);
                    weeklyStatisticsRegionProjectMapper.insert(weeklyStatisticsRegionProject);
                }


                List<WcxWeeklyStatistics> innovationNumZhouTongji= wcxWeeklyStatisticsMapper.innovationNumZhouTongji();
                for (int c = 0; c <innovationNumZhouTongji.size() ; c++) {
                    WcxWeeklyStatistics weics=new WcxWeeklyStatistics();
                    weics.setWeek(innovationNumZhouTongji.get(c).getWeek());
                    weics.setInnovationNum(innovationNumZhouTongji.get(c).getInnovationNum());
                    weics.setInnovationNumChain(innovationNumZhouTongji.get(c).getInnovationNumChain());
                    wcxWeeklyStatisticsMapper.updateWeek(weics);
                }

                List<WcxWeeklyStatisticsRegion> innovationNumZhouTongjiRegion=weeklyStatisticsRegionMapper.innovationNumZhouTongjiRegion();

                for (int i = 0; i <innovationNumZhouTongjiRegion.size() ; i++) {
                    WcxWeeklyStatisticsRegion weeklyStion=new WcxWeeklyStatisticsRegion();
                    weeklyStion.setWeek(innovationNumZhouTongjiRegion.get(i).getWeek());
                    weeklyStion.setOrgCode(innovationNumZhouTongjiRegion.get(i).getOrgCode());
                    weeklyStion.setOrgName(innovationNumZhouTongjiRegion.get(i).getOrgName());
                    weeklyStion.setInnovationNum(innovationNumZhouTongjiRegion.get(i).getInnovationNum());
                    weeklyStion.setInnovationNumChain(innovationNumZhouTongjiRegion.get(i).getInnovationNumChain());
                    weeklyStatisticsRegionMapper.updateWeekRegion(weeklyStion);
                }

                List<WcxWeeklyStatisticsRegionProject> innovationNumZhouTongjiRegionProject=weeklyStatisticsRegionProjectMapper.innovationNumZhouTongjiRegionProject();

                for (int i = 0; i <innovationNumZhouTongjiRegionProject.size() ; i++) {
                    WcxWeeklyStatisticsRegionProject weeklyStRegiProj=new WcxWeeklyStatisticsRegionProject();
                    weeklyStRegiProj.setWeek(innovationNumZhouTongjiRegionProject.get(i).getWeek());
                    weeklyStRegiProj.setOrgCode(innovationNumZhouTongjiRegionProject.get(i).getOrgCode());
                    weeklyStRegiProj.setOrgName(innovationNumZhouTongjiRegionProject.get(i).getOrgName());
                    weeklyStRegiProj.setCommCode(innovationNumZhouTongjiRegionProject.get(i).getCommCode());
                    weeklyStRegiProj.setCommName(innovationNumZhouTongjiRegionProject.get(i).getCommName());
                    weeklyStRegiProj.setInnovationNum(innovationNumZhouTongjiRegionProject.get(i).getInnovationNum());
                    weeklyStRegiProj.setInnovationNumChain(innovationNumZhouTongjiRegionProject.get(i).getInnovationNumChain());
                    weeklyStatisticsRegionProjectMapper.updateWeekRegionProject(weeklyStRegiProj);
                }


                List<WcxWeeklyStatistics> StatisticsNumZhouTongji= wcxWeeklyStatisticsMapper.StatisticsNumZhouTongjican(1);

                for (int c = 0; c <StatisticsNumZhouTongji.size() ; c++) {
                    WcxWeeklyStatistics weics=new WcxWeeklyStatistics();
                    weics.setWeek(StatisticsNumZhouTongji.get(c).getWeek());
                    weics.setUserNumChain(StatisticsNumZhouTongji.get(c).getUserNumChain());
                    weics.setNewUserChain(StatisticsNumZhouTongji.get(c).getNewUserChain());
                    wcxWeeklyStatisticsMapper.updateWeeChain(weics);
                }
            }
        }
        log.info("微创新报表定时器执行结束---------------------------");
        System.out.println("微创新报表定时器执行结束---------------------------");
    }

    public static Map<String, Map<String, String>> getYearWeekMap(int year) {
        int weeks = getYearWeekConut(year);
        Map<String, Map<String, String>> yearWeekMap = new HashMap<String, Map<String, String>>();
        for (int i =1;i<=weeks;i++){
            Map<String, String> timeMap = getWeekRangeMap(year,i);
            yearWeekMap.put(i+"",timeMap);
        }
        return yearWeekMap;
    }

    //获取某年有多少周
    public static int getYearWeekConut(int year) {
        int week = 52;
        try {
            /*Calendar calendar = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            calendar.setFirstDayOfWeek(Calendar.MONDAY);//设置星期一为一周开始的第一天
            calendar.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
            week = calendar.get(Calendar.WEEK_OF_YEAR);*/
            Map<String, String> timeMap = getWeekRangeMap(year, 53);
            if (!org.springframework.util.CollectionUtils.isEmpty(timeMap)) {
                String startTime = timeMap.get("startTime");
                if (startTime.substring(0, 4).equals(year + "")) { //判断年度是否相符，如果相符说明有53个周。
                    week = 53;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return week;
    }

    public static Map<String, String> getWeekRangeMap(int year, int week) {
        Map<String, String> timeMap = new HashMap<String, String>();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);//设置星期一为一周开始的第一天
        calendar.setMinimalDaysInFirstWeek(4);//可以不用设置
        int weekYear = calendar.get(Calendar.YEAR);//获得当前的年
        calendar.setWeekDate(weekYear, week, 2);//获得指定年的第几周的开始日期
        Date time = calendar.getTime();
        String startTime = new SimpleDateFormat("yyyy/MM/dd").format(time);
        timeMap.put("startTime", startTime);
        calendar.setWeekDate(weekYear, week, 1);//获得指定年的第几周的结束日期
        time = calendar.getTime();
        String endTime = new SimpleDateFormat("yyyy/MM/dd").format(time);
        timeMap.put("endTime", endTime);
        return timeMap;
    }

}
