package com.yihu.wlyy.statistics.etl.mycache;

import com.yihu.wlyy.statistics.dao.SignPatientLabelInfoDao;
import com.yihu.wlyy.statistics.etl.extract.DBExtract;
import com.yihu.wlyy.statistics.etl.model.CacheModel;
import com.yihu.wlyy.statistics.job.business.Constant;
import com.yihu.wlyy.statistics.model.label.SignPatientLabelInfo;
import com.yihu.wlyy.statistics.model.signfamily.SignFamily;
import com.yihu.wlyy.statistics.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016.10.19.
 */
@Component
public class CachePool {
    private static List<String> keys=new ArrayList<>();

    /**
     * 到达量的缓存  key 是时间yyyy-MM-dd
     */
    private static Map<String,CacheModel> arriveSignFamilyExpenseStatus1Map=new HashMap<String,CacheModel>();

    /**
     * 分组标签的缓存 key 是患者code
     */
    private static Map<String,String> patientGroup=new HashMap<String,String>();
    /**
     * 健康标签的缓存 key 是患者code
     */
    private static Map<String,String> healthGroup=new HashMap<String,String>();
    /**
     * 疾病标签的缓存 key 是患者code
     */
    private static Map<String,List<String>> diseaseGroup=new HashMap<String,List<String>>();
    /**
     * 某天的签约的已扣费的到达量
     * @param date
     * @return
     * @throws Exception
     */
    public synchronized CacheModel getSignFamilysWithExpenseStatus1ByDate(String date) throws Exception{
        CacheModel model= arriveSignFamilyExpenseStatus1Map.get(date);
        if(model==null){
            model=new CacheModel();
            String sql=" select id,code,idcard,hospital,admin_team_code,expenses_status,patient,server_type from wlyy_sign_family a " +
                    " where " +
                    " status in(1,2) " +
                    " and a.type =2  " +
                    " and expenses_status=1 " +
                    " and a.expenses_time< '"+date+ Constant.quota_date_last+"' " +
                    " and a.sign_year ='"+Constant.getNowYearByDateYeaterday(date)+"'  " ;
            String sqlCount="select count(id) from wlyy_sign_family a " +
                    " where  " +
                    " status in(1,2) " +
                    " and a.type =2 " +
                    " and expenses_status=1 " +
                    " and a.expenses_time< '"+date+ Constant.quota_date_last+"'  " +
                    " and a.sign_year ='"+Constant.getNowYearByDateYeaterday(date)+"'  " ;
            //抽取數據 分页抽取
            List<SignFamily> signFamiliesTemp= SpringUtil.getBean(DBExtract.class).extractByPage(SignFamily.class,sql,sqlCount,true);
            model.setSql(sql);
            model.setSignFamilies(signFamiliesTemp);
            arriveSignFamilyExpenseStatus1Map.put(date,model);
            return model;
        }else{
            return model;
        }
    }
    /**
     * 查询某人的分组标签
     * @param patientCode
     * @return
     * @throws Exception
     */
    public String getPatientGroup(String patientCode){
        String code=null;
        try{
            if(patientGroup.size()==0){
                initPatientGroup();
            }
            code= patientGroup.get(patientCode);
            if(StringUtils.isEmpty(code)){
                code="0";
            }
        }catch (Exception e){
            e.printStackTrace();
            return code;
        }
        return code;
    }

    private synchronized void initPatientGroup() throws  Exception{
        if(patientGroup.size()==0) {
            String sql="select patient,label from wlyy_sign_patient_label_info where label_type='1' and status=1";
            String countSql="select count(id) from wlyy_sign_patient_label_info where label_type='1' and status=1";
            List<SignPatientLabelInfo> signPatientLabelInfoList = SpringUtil.getBean(DBExtract.class).extractByPage(SignPatientLabelInfo.class,sql,countSql,true);
            for (SignPatientLabelInfo signPatientLabelInfo : signPatientLabelInfoList) {
                patientGroup.put(signPatientLabelInfo.getPatient(), signPatientLabelInfo.getLabel());
            }
        }
    }

    /**
     * 查询某人的健康标签
     * @param patientCode
     * @return
     * @throws Exception
     */
    public  String getHealthGroup(String patientCode){
        String code;
        try{
            if(healthGroup.size()==0){
                initHealthGroup();
            }
             code= healthGroup.get(patientCode);
        }catch (Exception e){
            e.printStackTrace();
            return "0";
        }
        if(StringUtils.isEmpty(code)){
            code="0";
        }
        return code;
    }

    private synchronized void initHealthGroup()  throws  Exception{
        if(healthGroup.size()==0){
            String sql="select patient,label from wlyy_sign_patient_label_info where label_type='2' and status=1";
            String countSql="select count(id) from wlyy_sign_patient_label_info where label_type='2' and status=1";
            List<SignPatientLabelInfo> signPatientLabelInfoList = SpringUtil.getBean(DBExtract.class).extractByPage(SignPatientLabelInfo.class,sql,countSql,true);
            for(SignPatientLabelInfo signPatientLabelInfo:signPatientLabelInfoList){
                healthGroup.put(signPatientLabelInfo.getPatient(),signPatientLabelInfo.getLabel());
            }
        }
    }

    /**
     * 查询某人的疾病标签
     * @param patientCode
     * @return
     * @throws Exception
     */
    public  List<String> getDiseaseGroup(String patientCode) {
        try{
            if(diseaseGroup.size()==0){
                initDiseaseGroup();
            }
            List<String> code= diseaseGroup.get(patientCode);
            return code;
        }catch (Exception e){
            return null;
        }
    }

    private synchronized void initDiseaseGroup()  throws  Exception{
        if(diseaseGroup.size()==0){
            String sql="select patient,label from wlyy_sign_patient_label_info where label_type='3' and status=1";
            String countSql="select count(id) from wlyy_sign_patient_label_info where label_type='3' and status=1";
            List<SignPatientLabelInfo> signPatientLabelInfoList = SpringUtil.getBean(DBExtract.class).extractByPage(SignPatientLabelInfo.class,sql,countSql,true);
            for(SignPatientLabelInfo signPatientLabelInfo:signPatientLabelInfoList){
                if(diseaseGroup.containsKey(signPatientLabelInfo.getPatient())){
                    List<String> code= diseaseGroup.get(signPatientLabelInfo.getPatient());
                    code.add(signPatientLabelInfo.getLabel());
                    diseaseGroup.put(signPatientLabelInfo.getPatient(),code);
                }else{
                    List<String> code=new ArrayList<>();
                    code.add(signPatientLabelInfo.getLabel());
                    diseaseGroup.put(signPatientLabelInfo.getPatient(),code);
                }
            }
        }
    }

    public static void cleanAllCache() {
        arriveSignFamilyExpenseStatus1Map.clear();
        patientGroup.clear();
        diseaseGroup.clear();
        healthGroup.clear();
    }

    public static Map<String, CacheModel> getArriveSignFamilyExpenseStatus1Map() {
        return arriveSignFamilyExpenseStatus1Map;
    }

    public static void addKeys(List<String> keyTemp) {
        keys.clear();
        keys.addAll(keyTemp);
    }
    public static List<String> getKeys() {
        return keys;
    }
    public static Map<String, String> getPatientGroup() {
        return patientGroup;
    }

    public static Map<String, String> getHealthGroup() {
        return healthGroup;
    }

    public static Map<String, List<String>> getDiseaseGroup() {
        return diseaseGroup;
    }
}
