package com.usoftchina.uas.office.qywx.service;

import com.usoftchina.uas.office.service.AbstractService;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author yingp
 * @date 2020/2/16
 */
@Service
public class UasAnalysisService extends AbstractService {
    public List<Map<String,Object>> getAnalysisByWeek(int instanceid) {
        List<Map<String,Object>> reList = new ArrayList<>();
        try {
            List<Map<String, Object>> masterList = jdbcTemplate.queryForList("select * from master");
            //获取推送主要信息 开始时间 结束时间 部门
            List<Map<String, Object>> depList = jdbcTemplate.queryForList("select DEPART_, STARTDATE_, ENDDATE_,WEEK_ FROM UASANALYSIS_WEEK_PUSH WHERE INSTANCEID_ = ?  order by DEPART_ ASC",instanceid);
            for(Map<String,Object> dep : depList){//循环获取各部门信息
                Map<String, Object> res = new HashMap<String,Object>();
                List<Map<String, Object>> durationList = new ArrayList<>();
                List<Map<String, Object>> dailyCount = new ArrayList<>();
                Object depart = dep.get("DEPART_");
                Object startTime = dep.get("STARTDATE_");
                Object endTime = dep.get("ENDDATE_");
                Object week_ = dep.get("WEEK_");
                res.put("DEPART",depart);//部门
                int userCount = jdbcTemplate.queryForObject("select count(1) from UASANALYSIS_WEEK_PUSHMAP where INSTANCEID_ = ? ",Integer.class, instanceid);
                res.put("USERCOUNT",userCount);//部门使用人数
                try{
                    durationList = jdbcTemplate.queryForList( "SELECT * FROM ( " +
                            " SELECT EM_CODE EMCODE,EM_NAME EMNAME,WEEK.DURATION_ DURATION " +
                            " FROM UASANALYSIS_WEEK_PUSHMAP PM " +
                            " LEFT JOIN UASANALYSIS_WEEK WEEK ON WEEK.EMCODE_ = PM.EMCODE_ AND WEEK.WEEK_ = ? " +
                            " LEFT JOIN EMPLOYEE ON EM_CODE = WEEK.EMCODE_ " +
                            " WHERE PM.INSTANCEID_ = ?  order by WEEK.DURATION_ DESC,EM_NAME ASC)",week_,instanceid);
                } catch (EmptyResultDataAccessException e) {

                }
                try{
                    dailyCount = jdbcTemplate.queryForList( "SELECT DATE_TIME,EMCOUNT FROM ( " +
                            "SELECT DT, " +
                            " (CASE WHEN SUBSTR(DATE_TIME,6,1)='0' THEN SUBSTR(DATE_TIME,7,4) ELSE SUBSTR(DATE_TIME,6,5) END ) as date_time,  " +
                            "    SUM(CASE WHEN nvl(DURATION_,0) = 0 THEN 0 ELSE 1 END) as emcount " +
                            " from (  select level as dt,to_char(to_date('"+startTime+"','yyyy-mm-dd')+level-1,'yyyy-mm-dd') as date_time " +
                            "         from DUAL connect by level <=to_date('"+endTime+"','yyyy-mm-dd')-to_date('"+startTime+"','yyyy-mm-dd')+1 " +
                            "     ) left join " +
                            "     (SELECT ABYDAY.DURATION_ DURATION_, ABYDAY.TIME_ TIME_ " +
                            "            FROM UASANALYSIS_WEEK_PUSHMAP PM " +
                            "            LEFT JOIN UASANALYSIS_DAY ABYDAY ON PM.EMCODE_ = ABYDAY.EMCODE_" +
                            "            LEFT JOIN EMPLOYEE ON EM_CODE = ABYDAY.EMCODE_" +
                            "        WHERE PM.INSTANCEID_ = ? "+
                            "     ) tab on date_time = tab.TIME_ " +
                            "     GROUP BY DATE_TIME,DT " +
                            " ) ORDER BY DT ASC",instanceid);
                } catch (EmptyResultDataAccessException e) {

                }
                res.put("DURATIONLIST",durationList);
                res.put("DAILYCOUNT",dailyCount);
                reList.add(res);
            }
        } catch (EmptyResultDataAccessException e) {
            return reList;
        }
        return reList;
    }

    public Map<String,Object> getAnalysisByPerson(int instanceid,String emCode) {
        Map<String, Object> res = new HashMap<String,Object>();
        List<Map<String,Object>> modelList = new ArrayList<>();
        res.put("EMCODE",emCode);
        try {
            Map<String, Object> pushMap = jdbcTemplate.queryForMap("SELECT * FROM (" +
                    "SELECT WEEK_, " +
                    "REPLACE((CASE WHEN SUBSTR(STARTDATE_,6,1)='0' THEN SUBSTR(STARTDATE_,7,4) ELSE SUBSTR(STARTDATE_,6,5) END),'-','.')||'-'||REPLACE((CASE WHEN SUBSTR(ENDDATE_,6,1)='0' THEN SUBSTR(ENDDATE_,7,4) ELSE SUBSTR(ENDDATE_,6,5) END),'-','.') AS RANGETIME " +
                    "FROM UASANALYSIS_WEEK_PUSH WHERE INSTANCEID_ = ? )", instanceid);
            if (pushMap != null) {
                Object week_ = pushMap.get("WEEK_");
                Map<String, Object> wMap = jdbcTemplate.queryForMap("SELECT * FROM (SELECT EM_CODE EMCODE,EM_NAME EMNAME, " +
                        "NVL(DURATION_,0) DURATION,NVL(ADD_,0) ADDCOUNT,NVL(COMMIT_,0) COMMITCOUNT,NVL(AUDIT_,0) AUDITCOUNT " +
                        "FROM UASANALYSIS_WEEK WEEK LEFT JOIN EMPLOYEE ON EM_CODE = EMCODE_ " +
                        "WHERE EMCODE_ = ? AND WEEK_ = ?)", emCode, week_);
                if (wMap != null) {
                    res.put("EMNAME", wMap.get("EMNAME"));
                    res.put("RANGETIME", pushMap.get("RANGETIME"));
                    res.put("DURATION", wMap.get("DURATION"));
                    res.put("ADDCOUNT", wMap.get("ADDCOUNT"));
                    res.put("COMMITCOUNT", wMap.get("COMMITCOUNT"));
                    res.put("AUDITCOUNT", wMap.get("AUDITCOUNT"));
                }
                modelList = jdbcTemplate.queryForList("SELECT * FROM (SELECT MOUDLE_ MODELNAME,DURATION_ DURATION " +
                        "FROM UASANALYSISDET_WEEK WHERE EMCODE_ = ? AND WEEK_ = ? ORDER BY DURATION_ DESC)", emCode, week_);
            }
        } catch (EmptyResultDataAccessException e) {
            return res;
        }
        res.put("MODELLIST",modelList);
        return res;
    }

    public List<Map<String,Object>> getPushMan(String type){
        List<Map<String,Object>> leaderList = new ArrayList<Map<String,Object>>();
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);  //美国是以周日为每周的第一天 现把周一设成第一天
        calendar.setTime(new Date());
        String week_ = calendar.get(Calendar.YEAR)+(calendar.get(Calendar.WEEK_OF_YEAR)<10?"0":"")+String.valueOf(calendar.get(Calendar.WEEK_OF_YEAR));
        System.out.println("当前周次为:"+week_);
        try{
            leaderList = jdbcTemplate.queryForList("SELECT INSTANCEID_,EMCODE_,DEPART_ FROM UASANALYSIS_WEEK_PUSH LEFT JOIN EMPLOYEE ON EM_CODE = EMCODE_ " +
                    " WHERE ISPUSH_ = 0 AND EM_CLASS <> '离职' AND EM_QYWX IS NOT NULL AND PUSHTYPE_ = ? AND WEEK_ = ? order by INSTANCEID_ ASC ",type,week_);
        } catch (EmptyResultDataAccessException e) {
            return leaderList;
        }
        return leaderList;
    }
}
