package com.yihu.jw.es.service.yqfk;


import com.alibaba.fastjson.JSONObject;
import com.yihu.jw.entity.IntegerIdentityEntity;
import com.yihu.jw.util.date.DateUtil;
import com.yihu.jw.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * 卫建局疫情防控
 *
 * Created by wangzhinan on 2020/08/21
 */
@Service
public class YqfkService {

    private Logger logger = LoggerFactory.getLogger(YqfkService.class);


    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 疫情防控头部总数统计
     * @param startTime
     * @param endTime
     * @return
     */
    public JSONObject yqfkStatisticsHead(String startTime,String endTime){
        JSONObject object = new JSONObject();
        //现有确诊
        String nowConfirmedSql = "SELECT\n" +
                "\tCOUNT(1) AS confirmedTotal\n" +
                "FROM\n" +
                "\tMID_CDC_CASE_INFO\n" +
                "WHERE\n" +
                "\tdim_cond IS NOT NULL\n" +
                "AND dim_cond NOT IN ('6', '7')\n" +
                "AND dim_input_case = '99'\n" +
                "AND DIM_CASE_TYPE = '1'\n" +
                "and LOCAL_CASE_FLAG ='是'";
        logger.info("nowConfirmedSql"+nowConfirmedSql);
        Map<String,Object> confirmedMap = jdbcTemplate.queryForMap(nowConfirmedSql);
        Integer newConfirmedTotal = 0;
        if (confirmedMap!=null){
            newConfirmedTotal=Integer.parseInt(confirmedMap.get("confirmedTotal").toString());
        }
        //新增本土
        String newHomeSql = "select COUNT(1) as newHomeTotal from MID_CDC_CASE_INFO where dim_cond is not null AND dim_input_case = '99' AND dim_cond not in('6','7') AND DIM_CASE_TYPE = '1' and LOCAL_CASE_FLAG ='是' and diag_time >='"+ DateUtil.getStringDateShort()+" 00:00:00"+"'"+" and diag_time >='"+ DateUtil.getStringDateShort()+" 23:59:59"+"'";
        logger.info("newHomeSql"+newHomeSql);
        Map<String,Object> newHomeMap = jdbcTemplate.queryForMap(newHomeSql);
        Integer newHomeTotal = 0;
        if (newHomeMap!=null){
            newHomeTotal=Integer.parseInt(newHomeMap.get("newHomeTotal").toString());
        }
        //新增无症状
        String newAsymptomaticSql = "select COUNT(1) as newAsymptomaticTotal from MID_CDC_CASE_INFO where dim_cond is not null AND dim_cond in('0') AND dim_input_case = '99' AND DIM_CASE_TYPE = '1' and LOCAL_CASE_FLAG ='是' and diag_time >='"+ DateUtil.getStringDateShort()+" 00:00:00"+"'"+" and diag_time >='"+ DateUtil.getStringDateShort()+" 23:59:59"+"'";
        logger.info("newAsymptomaticSql"+newAsymptomaticSql);
        Map<String,Object> newAsymptomaticeMap = jdbcTemplate.queryForMap(newAsymptomaticSql);
        Integer newAsymptomaticTotal = 0;
        if (newAsymptomaticeMap!=null){
            newAsymptomaticTotal=Integer.parseInt(newAsymptomaticeMap.get("newAsymptomaticTotal").toString());
        }
        //累计确诊
        String confirmedTotalSql = "select COUNT(1) as confirmedTotal from MID_CDC_CASE_INFO where  DIM_CASE_TYPE = '1' and LOCAL_CASE_FLAG ='是' AND dim_input_case = '99' AND dim_cond is not null ";
        logger.info("confirmedTotalSql"+confirmedTotalSql);
        Map<String,Object> confirmedTotalMap = jdbcTemplate.queryForMap(confirmedTotalSql);
        Integer confirmedTotalTotal = 0;
        if (confirmedTotalMap!=null){
            confirmedTotalTotal=Integer.parseInt(confirmedTotalMap.get("confirmedTotal").toString());
        }
        //累计治愈
        String cureTotalSql = "select COUNT(1) as cureTotal from MID_CDC_CASE_INFO where dim_cond is not null AND DIM_CASE_TYPE = '1' and LOCAL_CASE_FLAG ='是' AND dim_input_case = '99' AND dim_cond in('6') ";
        logger.info("cureTotalSql"+cureTotalSql);
        Map<String,Object> cureTotalMap = jdbcTemplate.queryForMap(cureTotalSql);
        Integer cureTotalTotal = 0;
        if (cureTotalMap!=null){
            cureTotalTotal=Integer.parseInt(cureTotalMap.get("cureTotal").toString());
        }
        //累计死亡
        String deathTotalSql = "select COUNT(1) as deathTotal from MID_CDC_CASE_INFO where dim_cond is not null AND DIM_CASE_TYPE = '1' and LOCAL_CASE_FLAG ='是' AND dim_input_case = '99' AND dim_cond in('7') ";
        logger.info("deathTotalSql"+deathTotalSql);
        Map<String,Object> deathTotalMap = jdbcTemplate.queryForMap(deathTotalSql);
        Integer deathTotalTotal = 0;
        if (deathTotalMap!=null){
            deathTotalTotal=Integer.parseInt(deathTotalMap.get("deathTotal").toString());
        }
        object.put("confirmed",newConfirmedTotal);//现有确诊
        object.put("newHome",newHomeTotal);//新增本土
        object.put("newAsymptomatic",newAsymptomaticTotal);//新增无症状；
        object.put("confirmedTotal",confirmedTotalTotal);//累计确诊
        object.put("cureTotal",cureTotalTotal);//累计治愈
        object.put("deathTotal",deathTotalTotal);//累计死亡
        return object;
    }


    /**
     * 疫情防控底部-密接次接数据统计
     * @param patientId
     * @param endTime
     * @return
     */
    public JSONObject yqfkStatisticsTail(String patientId,String endTime){
        JSONObject object = new JSONObject();
        String patientSql = "";
        if (StringUtils.isNoneBlank(patientId)){
            String[] str= patientId.split(",");
            String patientList = "";
            for (int i=0;i<str.length;i++){
                patientList+="'"+str[i]+"',";
            }
            if (StringUtils.isNoneBlank(patientList)){
                patientList=patientList.substring(0,patientList.length()-1);
            }
            patientSql=" AND PATIENT_ID IN ("+patientList+")   ";
        }else {
            patientSql = "  AND PATIENT_ID IN (SELECT md.PATIENT_ID FROM MID_CDC_CASE_INFO md where  md.DIM_CASE_TYPE IN ('1','3'))   ";
        }
        JSONObject MjObject = new JSONObject();//在厦密接
        //密接已隔离
        String mjGlSql = "select count(1) as \"glTotal\" from MID_CDC_CCC_INFO where  dim_track_stat=1 AND dim_mg_mode=1"+patientSql;

        logger.info("mjGlSql"+mjGlSql);
        Map<String,Object> mjglMap= jdbcTemplate.queryForMap(mjGlSql);
        Integer mjglTotal = 0;
        if (mjglMap!=null){
            mjglTotal = Integer.parseInt(mjglMap.get("glTotal").toString());
        }
        //县区列表
        String countySqlList = "select DIM_COUNTY as \"code\",DIM_COUNTY_NAME as \"area\" from DIM_COUNTY_DICT where DIM_AREA = '350200'";
        List<Map<String,Object>> countySqlMapList = jdbcTemplate.queryForList(countySqlList);
        logger.info("countySqlList"+countySqlList);
        //密接已隔离列表
        String mjGlSqlList = "select dim_county,COUNT(1) as total  from MID_CDC_CCC_INFO  where  dim_track_stat=1 and dim_county is not NULL AND dim_mg_mode=1 "+patientSql+"  GROUP BY dim_county ORDER BY COUNT(1) DESC   ";
        List<Map<String,Object>> mjGlSqlMapList = jdbcTemplate.queryForList(mjGlSqlList);
        //密接待落实列表
        String mjDlsSqlList = "select dim_county,COUNT(1) as total  from MID_CDC_CCC_INFO  where  dim_imp_stat NOT IN('1') and dim_imp_stat is not NULL AND dim_mg_mode=1 "+patientSql+"  GROUP BY dim_county ORDER BY COUNT(1) DESC   ";
        List<Map<String,Object>> mjDlsSqlMapList = jdbcTemplate.queryForList(mjDlsSqlList);

        List<Map<String,Object>> mjList = countySqlMapList;
        for (Map<String,Object> map:mjList){
            String code = map.get("code").toString();
            for (Map<String,Object> map1:mjGlSqlMapList){
                String dimCounty = map1.get("dim_county").toString();
                if (code.equalsIgnoreCase(dimCounty)){
                    map.put("ygl",map1.get("total"));
                }
            }
        }
        for (Map<String,Object> map:mjList){
            String code = map.get("code").toString();
            for (Map<String,Object> map1:mjDlsSqlMapList){
                String dimCounty = map1.get("dim_county").toString();
                if (code.equalsIgnoreCase(dimCounty)){
                    map.put("dls",map1.get("total"));
                }
            }
        }
        for (Map<String,Object> map:mjList){
            Integer total = 0;
            if (map.get("ygl")!=null){
                Integer ygl = Integer.parseInt(map.get("ygl").toString());
                if (map.get("dls")!=null){
                    Integer dls = Integer.parseInt(map.get("dls").toString());
                    total = ygl+dls;
                }else {
                    map.put("dls",0);
                    total = ygl;
                }
            }else {
                map.put("ygl",0);
                if (map.get("dls")!=null){
                    Integer dls = Integer.parseInt(map.get("dls").toString());
                    total = dls;
                }else {
                    map.put("dls",0);
                }
            }
            map.put("total",total);
        }
        //密接待落实
        String mjDlsSql = "select count(1) as \"dlsTotal\" from MID_CDC_CCC_INFO where dim_imp_stat NOT IN('1') and dim_imp_stat is not NULL AND dim_mg_mode=1 "+patientSql;
        logger.info("mjDlsSql"+mjDlsSql);
        Map<String,Object> mjdlsMap= jdbcTemplate.queryForMap(mjDlsSql);
        Integer mjDlsTotal = 0;
        if (mjdlsMap!=null){
            mjDlsTotal = Integer.parseInt(mjdlsMap.get("dlsTotal").toString());
        }
        /*   MjObject.put("zyTotal","");//正转运*/
        MjObject.put("ygl",mjglTotal);//已隔离
        MjObject.put("dls",mjDlsTotal);//待落实
        MjObject.put("total",mjglTotal+mjDlsTotal);//总计
        MjObject.put("list",mjList);//列表
        //次密接已隔离
        String cmglSql = "select count(1) as \"glTotal\" from MID_CDC_CCC_SED_INFO where dim_track_stat=1 AND dim_mg_mode=1 "+patientSql;;
        logger.info("cmglSql"+cmglSql);
        Map<String,Object> cmglMap= jdbcTemplate.queryForMap(cmglSql);
        Integer cmglTotal = 0;
        if (cmglMap!=null){
            cmglTotal = Integer.parseInt(cmglMap.get("glTotal").toString());
        }
        //次密接待落实
        String cmDlsSql = "select count(1) as \"dlsTotal\" from MID_CDC_CCC_SED_INFO where dim_imp_stat NOT IN('1') and dim_imp_stat is not NULL AND dim_mg_mode=1 "+patientSql;;
        logger.info("cmDlsSql"+cmDlsSql);
        Map<String,Object> cmdlsMap= jdbcTemplate.queryForMap(cmDlsSql);
        Integer cmDlsTotal = 0;
        if (cmdlsMap!=null){
            cmDlsTotal = Integer.parseInt(cmdlsMap.get("dlsTotal").toString());
        }
        //县区列表
        String cmCountySqlList = "select DIM_COUNTY as \"code\",DIM_COUNTY_NAME as \"area\" from DIM_COUNTY_DICT where DIM_AREA = '350200'";
        List<Map<String,Object>> cmCountySqlMapList = jdbcTemplate.queryForList(cmCountySqlList);
        logger.info("cmCountySqlMapList"+cmCountySqlMapList);
        //次密接已隔离列表
        String cmGlSqlList = "select dim_county,COUNT(1) as total  from MID_CDC_CCC_SED_INFO  where  dim_track_stat=1 and dim_county is not NULL AND dim_mg_mode=1 "+patientSql+"  GROUP BY dim_county ORDER BY COUNT(1) DESC   ";
        List<Map<String,Object>> cmGlSqlMapList = jdbcTemplate.queryForList(cmGlSqlList);
        //次密接待落实列表
        String cmDlsSqlList = "select dim_county,COUNT(1) as total  from MID_CDC_CCC_SED_INFO  where  dim_imp_stat NOT IN('1') and dim_imp_stat is not NULL AND dim_mg_mode=1 "+patientSql+"  GROUP BY dim_county ORDER BY COUNT(1) DESC   ";
        List<Map<String,Object>> cmDlsSqlMapList = jdbcTemplate.queryForList(cmDlsSqlList);
        List<Map<String,Object>> cmList = cmCountySqlMapList;
        logger.info("cmList"+cmList);
        for (Map<String,Object> map:cmList){
            String code = map.get("code").toString();
            for (Map<String,Object> map1:cmGlSqlMapList){
                String dimCounty = map1.get("dim_county").toString();
                if (code.equalsIgnoreCase(dimCounty)){
                    map.put("ygl",map1.get("total"));
                }
            }
        }
        for (Map<String,Object> map:cmList){
            String code = map.get("code").toString();
            for (Map<String,Object> map1:cmDlsSqlMapList){
                String dimCounty = map1.get("dim_county").toString();
                if (code.equalsIgnoreCase(dimCounty)){
                    map.put("dls",map1.get("total"));
                }
            }
        }
        for (Map<String,Object> map:cmList){
            Integer total = 0;
            if (map.get("ygl")!=null){
                Integer ygl = Integer.parseInt(map.get("ygl").toString());
                if (map.get("dls")!=null){
                    Integer dls = Integer.parseInt(map.get("dls").toString());
                    total = ygl+dls;
                }else {
                    map.put("dls",0);
                    total = ygl;
                }
            }else {
                map.put("ygl",0);
                if (map.get("dls")!=null){
                    Integer dls = Integer.parseInt(map.get("dls").toString());
                    total = dls;
                }else {
                    map.put("dls",0);
                }
            }
            map.put("total",total);
        }
        JSONObject CmObject = new JSONObject();//在厦次密
      /*  CmObject.put("zyTotal","");//正转运*/
        CmObject.put("ygl",cmglTotal);//已隔离
        CmObject.put("dls",cmDlsTotal);//待落实
        CmObject.put("total",cmglTotal+cmDlsTotal);//总计
        CmObject.put("list",cmList);//列表
        //一般密接已隔离
        String ybglSql = "select count(1) as \"glTotal\" from MID_KEYNOTE_CROWD_INFO where dim_track_stat=1 AND dim_mg_mode=1 "+patientSql;
        logger.info("ybglSql"+ybglSql);
        Map<String,Object> ybglMap= jdbcTemplate.queryForMap(ybglSql);
        Integer ybglTotal = 0;
        if (ybglMap!=null){
            ybglTotal = Integer.parseInt(ybglMap.get("glTotal").toString());
        }
        //一般密接待落实
        String ybDlsSql = "select count(1) as \"dlsTotal\" from MID_KEYNOTE_CROWD_INFO where dim_imp_stat NOT IN('1') and dim_imp_stat is not NULL AND dim_mg_mode=1 "+patientSql;;
        logger.info("ybDlsSql"+ybDlsSql);
        Map<String,Object> ybdlsMap= jdbcTemplate.queryForMap(ybDlsSql);
        Integer ybDlsTotal = 0;
        if (ybdlsMap!=null){
            ybDlsTotal = Integer.parseInt(ybdlsMap.get("dlsTotal").toString());
        }
        //县区列表
        String ybCountySqlList = "select DIM_COUNTY as \"code\",DIM_COUNTY_NAME as \"area\" from DIM_COUNTY_DICT where DIM_AREA = '350200'";
        List<Map<String,Object>> ybCountySqlMapList = jdbcTemplate.queryForList(ybCountySqlList);
        logger.info("ybCountySqlMapList"+ybCountySqlList);
        //次密接已隔离列表
        String ybGlSqlList = "select dim_county,COUNT(1) as total  from MID_KEYNOTE_CROWD_INFO  where  dim_track_stat=1 and dim_county is not NULL AND dim_mg_mode=1 "+patientSql+"  GROUP BY dim_county ORDER BY COUNT(1) DESC   ";
        List<Map<String,Object>> ybGlSqlMapList = jdbcTemplate.queryForList(ybGlSqlList);
        //次密接待落实列表
        String ybDlsSqlList = "select dim_county,COUNT(1) as total  from MID_KEYNOTE_CROWD_INFO  where  dim_imp_stat NOT IN('1') and dim_imp_stat is not NULL AND dim_mg_mode=1 "+patientSql+"  GROUP BY dim_county ORDER BY COUNT(1) DESC   ";
        List<Map<String,Object>> ybDlsSqlMapList = jdbcTemplate.queryForList(ybDlsSqlList);

        List<Map<String,Object>> ybList = ybCountySqlMapList;
        logger.info("ybList="+ybList);
        for (Map<String,Object> map:ybList){
            String code = map.get("code").toString();
            for (Map<String,Object> map1:ybGlSqlMapList){
                String dimCounty = map1.get("dim_county").toString();
                if (code.equalsIgnoreCase(dimCounty)){
                    map.put("ygl",map1.get("total"));
                }
            }
        }
        for (Map<String,Object> map:ybList){
            String code = map.get("code").toString();
            for (Map<String,Object> map1:ybDlsSqlMapList){
                String dimCounty = map1.get("dim_county").toString();
                if (code.equalsIgnoreCase(dimCounty)){
                    map.put("dls",map1.get("total"));
                }
            }
        }
        for (Map<String,Object> map:ybList){
            Integer total = 0;
            if (map.get("ygl")!=null){
                Integer ygl = Integer.parseInt(map.get("ygl").toString());
                if (map.get("dls")!=null){
                    Integer dls = Integer.parseInt(map.get("dls").toString());
                    total = ygl+dls;
                }else {
                    map.put("dls",0);
                    total = ygl;
                }
            }else {
                map.put("ygl",0);
                if (map.get("dls")!=null){
                    Integer dls = Integer.parseInt(map.get("dls").toString());
                    total = dls;
                }else {
                    map.put("dls",0);
                }
            }
            map.put("total",total);
        }
        JSONObject ybObject = new JSONObject();//在厦一般接触者
/*        ybObject.put("zyTotal","");//正转运*/
        ybObject.put("ygl",ybglTotal);//已隔离
        ybObject.put("dls",ybDlsTotal);//待落实
        ybObject.put("total",ybDlsTotal+ybglTotal);//总计
        ybObject.put("list",ybList);
        object.put("zxmj",MjObject);
        object.put("zxcm", CmObject);
        object.put("zxybjcz",ybObject);
        return object;
    }


    /**
     * 确诊人数详情
     * @return
     * @param flag 1、本地 2、外地协查，3、境外输入，4全部,5、本地阳性、6、境外阳性
     */
    public  List<Map<String, Object>> confirmedList(String name,String flag){
        String localSql = "";
        if (StringUtils.isNoneBlank(flag)&&flag!=null){
            if (flag.equalsIgnoreCase("1")){//本地
                localSql=" and  md.LOCAL_CASE_FLAG = '是' AND md.dim_input_case = '99' and md.DIM_CASE_TYPE = '1'  ";
            }else if (flag.equalsIgnoreCase("2")){//外地协查
                localSql=" and  md.LOCAL_CASE_FLAG = '否'  and md.DIM_CASE_TYPE = '1'  ";
            }else if (flag.equalsIgnoreCase("3")){//境外输入
                localSql=" and  md.LOCAL_CASE_FLAG = '是'  and md.DIM_CASE_TYPE = '1' AND md.dim_input_case != '99' ";
            }else if (flag.equalsIgnoreCase("4")){//全部
                localSql=" and md.DIM_CASE_TYPE IN ('1','3') ";
            }else if (flag.equalsIgnoreCase("5")){//本地阳性
                localSql=" and md.DIM_CASE_TYPE IN ('3') and  md.LOCAL_CASE_FLAG = '是' AND md.dim_input_case = '99' ";
            }else if (flag.equalsIgnoreCase("6")){//境外阳性
                localSql=" and md.DIM_CASE_TYPE IN ('3') and  md.LOCAL_CASE_FLAG = '是' AND md.dim_input_case != '99' ";
            }

        }
        String confirmedListSql = "SELECT\n" +
                "\tmd.PATIENT_ID AS \"patientId\",\n" +
                "\tmd.PATIENT_NAME AS \"patientName\",\n" +
                "\tmd.PATIENT_SEX AS \"patientSex\",\n" +
                "\tmd.PATIENT_AGE AS \"patientAge\",\n" +
                "\tmd.DIAG_TIME AS \"diagTime\",\n" +
                "\tmd.DIM_COND AS \"dimCond\",\n" +
                "\tp.DIM_PROV_NAME AS \"dimProvName\",\n" +
                "\ta.DIM_AREA_NAME AS \"dimAreaName\",\n" +
                "\tmj.total as \"mjTotal\",\n" +
                "\tcm.total as \"cmjTotal\",\n" +
                "\tyb.total as \"ybmjTotal\"\n" +
                "FROM\n" +
                "\tMID_CDC_CASE_INFO md\n" +
                "LEFT JOIN DIM_PROV_DICT p ON p.DIM_PROV = md.DIM_CS_PROV\n" +
                "LEFT JOIN DIM_AREA_DICT a ON a.DIM_AREA = md.DIM_CS_AREA\n" +
                "LEFT JOIN MID_CDC_CCC_INFO_COUNT mj ON mj.patientId = md.PATIENT_ID\n" +
                "LEFT JOIN MID_CDC_CCC_SED_INFO_COUNT cm ON cm.patientId = md.PATIENT_ID\n" +
                "LEFT JOIN MID_KEYNOTE_CROWD_INFO_COUNT yb ON yb.patientId = md.PATIENT_ID" +
                " WHERE\n" +
                "   1=1 "+localSql;
        if (name!=null&&name!=""){
            confirmedListSql =confirmedListSql+" and md.PATIENT_NAME LIKE '%"+name+"%'";
        }
        logger.info("confirmedListSql"+confirmedListSql);
        List<Map<String, Object>> confirmedList = jdbcTemplate.queryForList(confirmedListSql);
      /*  if (confirmedList!=null&&confirmedList.size()!=0){
            for (Map<String,Object> map:confirmedList){
                String patientId = map.get("patientId").toString();
                //密接人数计算
                String mjTotalSql = "select count(1) as total from MID_CDC_CCC_INFO where PATIENT_ID='"+patientId+"'";
                logger.info("mjTotalSql"+mjTotalSql);
                List<Map<String, Object>> mjList = jdbcTemplate.queryForList(mjTotalSql);
                Integer mjTotal = 0;//密接人数
                if (mjList!=null&&mjList.size()!=0){
                    mjTotal = Integer.parseInt(mjList.get(0).get("total").toString());
                }
                map.put("mjTotal",mjTotal);

                //次密接人数计算
                String cmjTotalSql = "select count(1) as total from MID_CDC_CCC_SED_INFO where PATIENT_ID='"+patientId+"'";
                logger.info("cmjTotalSql"+cmjTotalSql);
                List<Map<String, Object>> cmjList = jdbcTemplate.queryForList(cmjTotalSql);
                Integer cmjTotal = 0;//密接人数
                if (cmjList!=null&&cmjList.size()!=0){
                    cmjTotal = Integer.parseInt(cmjList.get(0).get("total").toString());
                }
                map.put("cmjTotal",cmjTotal);


                //一般接触人数计算
                String ybmjTotalSql = "select count(1) as total from MID_KEYNOTE_CROWD_INFO where PATIENT_ID='"+patientId+"'";
                logger.info("ybmjTotalSql"+ybmjTotalSql);
                List<Map<String, Object>> ybmjList = jdbcTemplate.queryForList(ybmjTotalSql);
                Integer ybmjTotal = 0;//密接人数
                if (ybmjList!=null&&ybmjList.size()!=0){
                    ybmjTotal = Integer.parseInt(ybmjList.get(0).get("total").toString());
                }
                map.put("ybmjTotal",ybmjTotal);
            }
        }*/
        return confirmedList;
    }






}
