package com.yihu.jw.base.service.dict;

import com.yihu.jw.base.dao.saas.SaasDao;
import com.yihu.jw.dict.dao.*;
import com.yihu.jw.doctor.dao.BaseDoctorHospitalDao;
import com.yihu.jw.entity.base.dict.*;
import com.yihu.jw.entity.base.doctor.BaseDoctorHospitalDO;
import com.yihu.jw.entity.base.org.BaseOrgDO;
import com.yihu.jw.entity.hospital.consult.WlyyHospitalSysDictDO;
import com.yihu.jw.hospital.dict.WlyyHospitalSysDictDao;
import com.yihu.jw.org.dao.BaseOrgDao;
import com.yihu.jw.restmodel.web.MixEnvelop;
import com.yihu.jw.rm.base.BaseRequestMapping;
import com.yihu.jw.utils.StringUtil;
import com.yihu.jw.utils.hibernate.HibenateUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.xerces.dom.PSVIAttrNSImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TemplateManageService {
    @Autowired
    private BaseDrugDictDao baseDrugDictDao;
    @Autowired
    private BaseTemplateDao baseTemplateDao;
    @Autowired
    private BaseTemplateDictDao baseTemplateDictDao;
    @Autowired
    private BaseTemplateOrgDao baseTemplateOrgDao;
    @Autowired
    private HibenateUtils hibenateUtils;
    @Autowired
    private BaseDrugUseDictDao baseDrugUseDictDao;
    @Autowired
    private BaseFrequencyDictDao baseFrequencyDictDao;
    @Autowired
    private BaseDictIcd10Dao baseDictIcd10Dao;
    @Autowired
    private WlyyHospitalSysDictDao wlyyHospitalSysDictDao;
    @Autowired
    private BaseOrgDao baseOrgDao;
    @Autowired
    private BaseFollowUpDictDao baseFollowUpDictDao;
    @Autowired
    private BaseDoctorHospitalDao baseDoctorHospitalDao;


    public MixEnvelop findTempalte(String templateType,String code,String name,String diseaseName,String diseaseId, Integer page ,Integer pageSize){
        String sql =" select t.id as \"id\" ," +
                " t.name as \"name\"," +
                " t.code as \"code\"," +
                " t.children_count as \"childrenCount\"," +
                " t.update_user as \"updateUser\"," +
                " t.update_user_name as \"updateUserName\"," +
                " t.update_time as \"updateTime\"," +
                " t.disease_name as \"diseaseName\"," +
                " t.disease_id as \"diseaseId\"," +
                " t.template_type as \"templateType\"," +
                " t.is_del as \"isDel\" " +
                " from base_template t " +
                " where t.is_del='1' ";
        String sqlCount=" select count(1) as \"count\" from base_template t where t.is_del='1'";
        if (StringUtils.isNotBlank(templateType)){
            sql+= " and t.template_type='"+templateType+"'";
            sqlCount+= " and t.template_type='"+templateType+"'";
        }
        if (StringUtils.isNotBlank(code)){
            sql+= " and t.code like '%"+code+"%'";
            sqlCount+= " and t.code like '%"+code+"%'";
        }
        if (StringUtils.isNotBlank(diseaseName)){
            sql+= " and t.disease_name like '%"+diseaseName+"%'";
            sqlCount+= " and t.disease_name like '%"+diseaseName+"%'";
        }
        if (StringUtils.isNotBlank(diseaseId)){
            sql+= " and t.disease_id like '%"+diseaseId+"%'";
            sqlCount+= " and t.disease_id like '%"+diseaseId+"%'";
        }
        if (StringUtils.isNotBlank(name)){
            sql+= " and t.name like '%"+name+"%'";
            sqlCount+= " and t.name like '%"+name+"%'";
        }
        sql+=" order by t.update_time desc";
        Integer count=0;
        List<Map<String,Object>> countList=hibenateUtils.createSQLQuery(sqlCount);
        if (countList!=null&&countList.size()>0){
            count = Integer.parseInt(countList.get(0).get("count").toString());
        }
        List<Map<String,Object>> list=hibenateUtils.createSQLQuery(sql,page,pageSize);
        for (Map<String,Object> map:list){
            List<BaseTemplateOrgDO> orgTemplate=baseTemplateOrgDao.findByTemplateId(map.get("id").toString());
            List<String> orgNames= new ArrayList<>();
            int i=0;
            for (BaseTemplateOrgDO baseTemplateOrgDO :orgTemplate){
                orgNames.add(baseTemplateOrgDO.getOrgName());
            }
            map.put("orgNames",orgNames.toString());
        }
        MixEnvelop mixEnvelop = new MixEnvelop();
        mixEnvelop.setDetailModelList(list);
        mixEnvelop.setPageSize(pageSize);
        mixEnvelop.setCurrPage(page);
        mixEnvelop.setTotalCount(count);
        return mixEnvelop;
    }
   public void addOrRemoveChildrenDict(String templateId,String dictIds,String isDel){
        String[] dictArr=dictIds.split(",");
        for (int i=0;i<dictArr.length;i++){
            String dictId=dictArr[i];
            BaseTemplateDictDO baseTemplateDictDO = baseTemplateDictDao.findByDictIdAndTemplateId(dictId,templateId);
            if ("1".equalsIgnoreCase(isDel)){
                if (baseTemplateDictDO!=null){
                    baseTemplateDictDO.setIsDel("1");
                }else {
                    baseTemplateDictDO = new BaseTemplateDictDO();
                    baseTemplateDictDO.setTemplateId(templateId);
                    baseTemplateDictDO.setDictId(dictId);
                    baseTemplateDictDO.setCreateTime(new Date());
                    baseTemplateDictDO.setIsDel("1");
                }

            }else {
                if (baseTemplateDictDO!=null){
                    baseTemplateDictDO.setIsDel("0");
                }else {
                    return;
                }
            }
            baseTemplateDictDao.save(baseTemplateDictDO);
            BaseTemplateDO baseTemplateDO = baseTemplateDao.findOne(templateId);
            Integer chuildrenCount =  baseTemplateDictDao.getCountByTemplateId(templateId);
            if (baseTemplateDO !=null ){
                baseTemplateDO.setChildrenCount(chuildrenCount.toString());
            }
            baseTemplateDao.save(baseTemplateDO);
        }

   }
   public void saveOrUpdateTemplate(String templateId,String code,String name,String user,String userName,String templateType,String diseaseId,String diseaseName){
       BaseTemplateDO baseTemplateDO = baseTemplateDao.findOne(templateId);
       String localCode="MD"+new Date().getTime();
       System.out.println("templatType"+templateType);
       if (baseTemplateDO!=null){
           baseTemplateDO.setCode(localCode);
           baseTemplateDO.setName(name);
           baseTemplateDO.setUpdateTime(new Date());
           baseTemplateDO.setUpdateUser(user);
           baseTemplateDO.setUpdateUserName(userName);
           baseTemplateDO.setDiseaseId(diseaseId);
           baseTemplateDO.setDiseaseName(diseaseName);
           baseTemplateDO.setTemplateType(templateType);
       }else {
           baseTemplateDO = new BaseTemplateDO();
           baseTemplateDO.setCode(localCode);
           baseTemplateDO.setName(name);
           baseTemplateDO.setCreateTime(new Date());
           baseTemplateDO.setCreateUser(user);
           baseTemplateDO.setCreateUserName(userName);
           baseTemplateDO.setTemplateType(templateType);
           baseTemplateDO.setDiseaseId(diseaseId);
           baseTemplateDO.setDiseaseName(diseaseName);
           baseTemplateDO.setIsDel("1");
       }
       baseTemplateDao.save(baseTemplateDO);
   }

   public MixEnvelop findTemlateChildList(String templateId,String name,String code,Integer page,Integer pageSize){
        BaseTemplateDO baseTemplateDictDO = baseTemplateDao.findOne(templateId);
        PageRequest pageRequest = new PageRequest(page-1, pageSize);
        List<Map<String,Object>> returnList= new ArrayList<>();
        MixEnvelop mixEnvelop = new MixEnvelop();
        mixEnvelop.setPageSize(pageSize);
        mixEnvelop.setCurrPage(page);
        if (baseTemplateDictDO!=null){
            if ("frequencyDict".equalsIgnoreCase(baseTemplateDictDO.getTemplateType())){
                List<BaseFrequencyDictDO> list= new ArrayList<>();
                if (StringUtils.isNoneBlank(name)){
                    list= baseFrequencyDictDao.findByNameDel(name,code,pageRequest);
                }else {
                    list= baseFrequencyDictDao.findByDel(pageRequest);
                }
                Integer count = baseFrequencyDictDao.getconuntByDel();
                for (BaseFrequencyDictDO baseFrequencyDictDO:list){
                    Map<String,Object> map = new HashMap<>();
                    map.put("templateDetail",baseTemplateDictDO);
                    map.put("id",baseFrequencyDictDO.getId());
                    map.put("code",baseFrequencyDictDO.getFreq_code());
                    map.put("name",baseFrequencyDictDO.getFreq_name());
                    map.put("py_code",baseFrequencyDictDO.getFreq_sp());
                    map.put("daily_times",baseFrequencyDictDO.getDaily_times());
                    map.put("weekly_times",baseFrequencyDictDO.getWeekly_times());
                    BaseTemplateDictDO baseTemplateDictDO1= baseTemplateDictDao.findByDictIdAndTemplateId(baseFrequencyDictDO.getId(),templateId);
                    if (baseTemplateDictDO1!=null){
                        map.put("isAdd",1);
                    }else {
                        map.put("isAdd",0);
                    }
                    returnList.add(map);
                }
                mixEnvelop.setDetailModelList(returnList);
                mixEnvelop.setTotalCount(count);
                return mixEnvelop;
            }else if ("drugUseDict".equalsIgnoreCase(baseTemplateDictDO.getTemplateType())){
                List<BaseDrugUseDictDO> list= new ArrayList<>();
                if (StringUtils.isNoneBlank(name)){
                    list= baseDrugUseDictDao.findByNameDel(name,code,pageRequest);
                }else {
                    list= baseDrugUseDictDao.findByDel(pageRequest);
                }
                Integer count = baseDrugUseDictDao.getconuntByDel();
                for (BaseDrugUseDictDO baseDrugUseDictDO:list){
                    Map<String,Object> map = new HashMap<>();
                    map.put("id",baseDrugUseDictDO.getId());
                    map.put("templateDetail",baseTemplateDictDO);
                    map.put("code",baseDrugUseDictDO.getSupply_code());
                    map.put("name",baseDrugUseDictDO.getSupply_name());
                    map.put("py_code",baseDrugUseDictDO.getPy_code());
                    BaseTemplateDictDO baseTemplateDictDO1= baseTemplateDictDao.findByDictIdAndTemplateId(baseDrugUseDictDO.getId(),templateId);
                    if (baseTemplateDictDO1!=null){
                        map.put("isAdd",1);
                    }else {
                        map.put("isAdd",0);
                    }
                    returnList.add(map);
                }
                mixEnvelop.setDetailModelList(returnList);
                mixEnvelop.setTotalCount(count);
                return mixEnvelop;
            }else if ("drugListDict".equalsIgnoreCase(baseTemplateDictDO.getTemplateType())){
                List<BaseDrugDictDO> list= new ArrayList<>();
                if (StringUtils.isNoneBlank(name)){
                    list= baseDrugDictDao.findByNameDel(name,code,pageRequest);
                }else {
                    list= baseDrugDictDao.findByDel(pageRequest);
                }
                Integer count = baseDrugDictDao.getconuntByDel();
                for (BaseDrugDictDO baseDrugDictDO:list){
                    Map<String,Object> map = new HashMap<>();
                    map.put("id",baseDrugDictDO.getId());
                    map.put("templateDetail",baseTemplateDictDO);
                    map.put("code",baseDrugDictDO.getCharge_code());
                    map.put("name",baseDrugDictDO.getDrugname());
                    map.put("dosage",baseDrugDictDO.getDosage());
                    map.put("specification",baseDrugDictDO.getSpecification());
                    map.put("stock_amount",baseDrugDictDO.getStock_amount());
                    map.put("manu_name",baseDrugDictDO.getManu_name());
                    BaseTemplateDictDO baseTemplateDictDO1= baseTemplateDictDao.findByDictIdAndTemplateId(baseDrugDictDO.getId(),templateId);
                    if (baseTemplateDictDO1!=null){
                        map.put("isAdd",1);
                    }else {
                        map.put("isAdd",0);
                    }
                    returnList.add(map);
                }
                mixEnvelop.setDetailModelList(returnList);
                mixEnvelop.setTotalCount(count);
                return mixEnvelop;
            }else if ("icd10ListDict".equalsIgnoreCase(baseTemplateDictDO.getTemplateType())){
                List<DictIcd10DO> list= new ArrayList<>();
                if (StringUtils.isNoneBlank(name)){
                    list= baseDictIcd10Dao.findByNameDel(name,code,pageRequest);
                }else {
                    list= baseDictIcd10Dao.findByDel(pageRequest);
                }
                Integer count = baseDrugDictDao.getconuntByDel();
                for (DictIcd10DO dictIcd10DO:list){
                    Map<String,Object> map = new HashMap<>();
                    map.put("templateDetail",baseTemplateDictDO);
                    map.put("id",dictIcd10DO.getId());
                    map.put("code",dictIcd10DO.getCode());
                    map.put("name",dictIcd10DO.getName());
                    map.put("disea_reason",dictIcd10DO.getDisea_reason());
                    map.put("death_reason",dictIcd10DO.getDeath_reason());
                    map.put("py_code",dictIcd10DO.getPy_code());
                    map.put("d_code",dictIcd10DO.getD_code());
                    map.put("flag",dictIcd10DO.getFlag());
                    BaseTemplateDictDO baseTemplateDictDO1= baseTemplateDictDao.findByDictIdAndTemplateId(dictIcd10DO.getId().toString(),templateId);
                    if (baseTemplateDictDO1!=null){
                        map.put("isAdd",1);
                    }else {
                        map.put("isAdd",0);
                    }
                    returnList.add(map);
                }
                mixEnvelop.setDetailModelList(returnList);
                mixEnvelop.setTotalCount(count);
                return mixEnvelop;
            }else if ("followUpDict".equalsIgnoreCase(baseTemplateDictDO.getTemplateType())){
                List<BaseFollowUpDictDO> list= new ArrayList<>();
                if (StringUtils.isNoneBlank(name)){
                    list= baseFollowUpDictDao.findByNameDel(name,code,pageRequest);
                }else {
                    list= baseFollowUpDictDao.findByDel(pageRequest);
                }
                Integer count = baseDrugDictDao.getconuntByDel();
                for (BaseFollowUpDictDO followUpDictDO:list){
                    Map<String,Object> map = new HashMap<>();
                    map.put("templateDetail",baseTemplateDictDO);
                    map.put("id",followUpDictDO.getId());
                    map.put("code",followUpDictDO.getCode());
                    map.put("name",followUpDictDO.getName());
                    map.put("project_content",followUpDictDO.getProjectContent());
                    map.put("status",followUpDictDO.getStatus());
                    map.put("is_del",followUpDictDO.getIsDel());
                    BaseTemplateDictDO baseTemplateDictDO1= baseTemplateDictDao.findByDictIdAndTemplateId(followUpDictDO.getId().toString(),templateId);
                    if (baseTemplateDictDO1!=null){
                        map.put("isAdd",1);
                    }else {
                        map.put("isAdd",0);
                    }
                    returnList.add(map);
                }
                mixEnvelop.setDetailModelList(returnList);
                mixEnvelop.setTotalCount(count);
                return mixEnvelop;

            } else {
                return null;
            }
        }  else {
            return null;
        }
   }

   public  BaseTemplateDO findOneTemplateById(String id){
        return baseTemplateDao.findOne(id);
   }
    public void saveUserTemplateRelation(String orgCode,String templateIds){
        String[] templateArray = templateIds.split(",");
        BaseOrgDO baseOrgDO= baseOrgDao.findByCode(orgCode);
        String orgName="";
        if (baseOrgDO!=null){
            orgName=baseOrgDO.getName();
        }
        List<BaseTemplateOrgDO> list= baseTemplateOrgDao.findByTemplateType("followUpDict",orgCode);
        baseTemplateOrgDao.delete(list);
        for (int i=0;i<templateArray.length;i++){
            String templateId=templateArray[i];
            BaseTemplateDO baseTemplateDO = baseTemplateDao.findOne(templateId);
            if (baseTemplateDO!=null){
                String templateType = baseTemplateDO.getTemplateType();
                if (!"followUpDict".equalsIgnoreCase(templateType)){
                    BaseTemplateOrgDO baseTemplateOrgDO=baseTemplateOrgDao.findByOrgCodeAndtemplateType(orgCode,templateType);
                    if (baseTemplateOrgDO!=null){
                        if (baseTemplateOrgDO.getTemplateId().equalsIgnoreCase(templateId)){
                            baseTemplateOrgDO.setIsDel("1");
                            baseTemplateOrgDao.save(baseTemplateOrgDO);
                        }else {
                            baseTemplateOrgDO.setIsDel("0");
                            BaseTemplateOrgDO baseTemplateOrgNew=new BaseTemplateOrgDO();
                            baseTemplateOrgNew.setOrgCode(orgCode);
                            baseTemplateOrgNew.setOrgName(orgName);
                            baseTemplateOrgNew.setIsDel("1");
                            baseTemplateOrgNew.setTemplateId(templateId);
                            baseTemplateOrgNew.setTemplateType(templateType);
                            baseTemplateOrgDao.save(baseTemplateOrgDO);
                            baseTemplateOrgDao.save(baseTemplateOrgNew);
                        }
                    }else {
                        baseTemplateOrgDO = new BaseTemplateOrgDO();
                        baseTemplateOrgDO.setOrgCode(orgCode);
                        baseTemplateOrgDO.setOrgName(orgName);
                        baseTemplateOrgDO.setIsDel("1");
                        baseTemplateOrgDO.setTemplateId(templateId);
                        baseTemplateOrgDO.setTemplateType(templateType);
                    }
                    baseTemplateOrgDao.save(baseTemplateOrgDO);
                }else {
                    BaseTemplateOrgDO baseTemplateOrgDO = new BaseTemplateOrgDO();
                    baseTemplateOrgDO.setOrgCode(orgCode);
                    baseTemplateOrgDO.setOrgName(orgName);
                    baseTemplateOrgDO.setIsDel("1");
                    baseTemplateOrgDO.setTemplateId(templateId);
                    baseTemplateOrgDO.setTemplateType(templateType);
                    baseTemplateOrgDao.save(baseTemplateOrgDO);
                }


            }
        }

    }
    public Map<String,Object> findUseTemplate(String orgCode){
        List<BaseTemplateOrgDO> list = baseTemplateOrgDao.findByOrgCode(orgCode);
        List<BaseTemplateOrgDO> followUpDictList= baseTemplateOrgDao.findByTemplateType("followUpDict",orgCode);
        List<WlyyHospitalSysDictDO> wlyyHospitalSysDictDOS = wlyyHospitalSysDictDao.findByDictName("dictManageList");
        Map map = new HashMap();
        for (WlyyHospitalSysDictDO wlyyHospitalSysDictDO:wlyyHospitalSysDictDOS){
            if ("followUpDict".equalsIgnoreCase(wlyyHospitalSysDictDO.getDictCode())){
                List<Map<String,Object>> followUp= new ArrayList<>();
               for (BaseTemplateOrgDO baseTemplateOrgDO:followUpDictList){
                   Map baseTemplateOrgDOMap=new HashMap();
                   baseTemplateOrgDOMap.put("followupDict",baseTemplateOrgDO.getTemplateId());
                   followUp.add(baseTemplateOrgDOMap);
               }
                map.put(wlyyHospitalSysDictDO.getDictCode(),followUp);
            }else {
                for (BaseTemplateOrgDO baseTemplateOrgDO:list){
                    if (wlyyHospitalSysDictDO.getDictCode().equalsIgnoreCase(baseTemplateOrgDO.getTemplateType())){
                        map.put(wlyyHospitalSysDictDO.getDictCode(),baseTemplateOrgDO);
                    }
                }
            }

        }
        return  map;
    }



    public MixEnvelop findTemlateAddChildList(String templateId,String name,String code,Integer page,Integer pageSize){
        BaseTemplateDO baseTemplateDictDO = baseTemplateDao.findOne(templateId);
        MixEnvelop mixEnvelop = new MixEnvelop();
        mixEnvelop.setPageSize(pageSize);
        mixEnvelop.setCurrPage(page);
        if (baseTemplateDictDO!=null){
            if ("frequencyDict".equalsIgnoreCase(baseTemplateDictDO.getTemplateType())){
                String sql="select t.id as \"id\"," +
                        " t.freq_code as \"freq_code\"," +
                        " t.freq_name as \"freq_name\"," +
                        " t.freq_sp as \"freq_sp\"," +
                        " t.daily_times as \"daily_times\"," +
                        " t.weekly_times as \"weekly_times\"," +
                        " t.hosp_code as \"hosp_code\"," +
                        " t.is_del as \"is_del\"," +
                        " t.is_op_ip as \"is_op_ip\"," +
                        " t.status as \"status\"," +
                        " t.create_time as \"create_time\"" +
                        "from base_template_dict b left join  base_frequency_dict t on b.dict_id=t.id" +
                        " where t.is_del='1' and b.is_del='1' and t.status ='1' ";
                String sqlCount =" select count(1) as \"count\" from base_template_dict b left join  base_frequency_dict t on b.dict_id=t.id" +
                        "  where t.is_del='1' and b.is_del='1' and t.status ='1' ";
                if (StringUtils.isNoneBlank(code)){
                    sql+=" and t.freq_code like '%"+code+"%'";
                    sqlCount+=" and t.freq_code like '%"+code+"%'";
                }
                if (StringUtils.isNoneBlank(name)){
                    sql+=" and t.freq_name like '%"+name+"%'";
                    sqlCount+=" and t.freq_name like '%"+name+"%'";
                }
                sql+=" and b.template_id = '"+baseTemplateDictDO.getId()+"'";
                sqlCount+=" and b.template_id = '"+baseTemplateDictDO.getId()+"'";
                List<Map<String,Object>> list=hibenateUtils.createSQLQuery(sql,page,pageSize);
                for (Map<String,Object> map:list){
                    if (map.get("status")!=null&&"1".equalsIgnoreCase(map.get("status").toString())){
                        map.put("statusName","生效中");
                    }else {
                        map.put("statusName","已失效");
                    }
                }
                List<Map<String,Object>> countList=hibenateUtils.createSQLQuery(sqlCount);
                Integer count=0;
                if (countList!=null&&countList.size()>0) {
                    count = Integer.valueOf(countList.get(0).get("count").toString());
                }
                mixEnvelop.setDetailModelList(list);
                mixEnvelop.setTotalCount(count);
                return mixEnvelop;
            }else if ("drugUseDict".equalsIgnoreCase(baseTemplateDictDO.getTemplateType())){
                String sql="select t.id as \"id\"," +
                        " t.supply_code as \"supply_code\"," +
                        " t.supply_name as \"supply_name\"," +
                        " t.supply_occ as \"supply_occ\"," +
                        " t.del_flag as \"del_flag\"," +
                        " t.sort_code as \"sort_code\"," +
                        " t.print_name as \"print_name\"," +
                        " t.py_code as \"py_code\"," +
                        " t.d_code as \"d_code\"," +
                        " t.yb_name as \"yb_name\"," +
                        " t.yb_code as \"yb_code\"," +
                        " t.status as \"status\"," +
                        " t.create_time as \"create_time\" " +
                        "from  base_template_dict b left join base_drug_use_dict t on b.dict_id=t.id" +
                        " where t.del_flag='1' and b.is_del='1' and t.status ='1' ";
                String sqlCount =" select count(1) as \"count\" from  base_template_dict b left join base_drug_use_dict t on b.dict_id=t.id" +
                        " where t.del_flag='1' and b.is_del='1' and t.status ='1' ";
                if (StringUtils.isNoneBlank(code)){
                    sql+=" and t.supply_code like '%"+code+"%'";
                    sqlCount+=" and t.supply_code like '%"+code+"%'";
                }
                if (StringUtils.isNoneBlank(name)){
                    sql+=" and t.supply_name like '%"+name+"%'";
                    sqlCount+=" and t.supply_name like '%"+name+"%'";
                }
                sql+=" and b.template_id = '"+baseTemplateDictDO.getId()+"'";
                sqlCount+=" and b.template_id = '"+baseTemplateDictDO.getId()+"'";
                List<Map<String,Object>> list=hibenateUtils.createSQLQuery(sql,page,pageSize);
                for (Map<String,Object> map:list){
                    if (map.get("status")!=null&&"1".equalsIgnoreCase(map.get("status").toString())){
                        map.put("statusName","生效中");
                    }else {
                        map.put("statusName","已失效");
                    }
                }
                List<Map<String,Object>> countList=hibenateUtils.createSQLQuery(sqlCount);
                Integer count=0;
                if (countList!=null&&countList.size()>0) {
                    count = Integer.valueOf(countList.get(0).get("count").toString());
                }
                mixEnvelop.setDetailModelList(list);
                mixEnvelop.setTotalCount(count);
                return mixEnvelop;
            }else if ("drugListDict".equalsIgnoreCase(baseTemplateDictDO.getTemplateType())){
                String sql = "select t.id as \"id\"," +
                        " t.charge_code as \"charge_code\"," +
                        " t.drugname as \"drugname\"," +
                        " t.specification as \"specification\"," +
                        " t.py_code as \"py_code\"," +
                        " t.supply_code as \"supply_code\"," +
                        " t.supply_name as \"supply_name\"," +
                        " t.retprice as \"retprice\"," +
                        " t.manu_name as \"manu_name\"," +
                        " t.weigh_unit as \"weigh_unit\"," +
                        " t.mini_unit_name as \"mini_unit_name\"," +
                        " t.pack_unit_name as \"pack_unit_name\"," +
                        " t.weigh_unit_name as \"weigh_unit_name\"," +
                        " t.vol_unit_name as \"vol_unit_name\"," +
                        " t.win_no as \"win_no\"," +
                        " t.pack_retprice as \"pack_retprice\"," +
                        " t.mini_unit as \"mini_unit\"," +
                        " t.pack_size as \"pack_size\"," +
                        " t.action as \"action\"," +
                        " t.serial as \"serial\"," +
                        " t.group_no as \"group_no\"," +
                        " t.stock_amount as \"stock_amount\"," +
                        " t.visible_flag as \"visible_flag\"," +
                        " t.drug_flag as \"drug_flag\"," +
                        " t.create_time as \"create_time\"," +
                        " t.update_time as \"update_time\"," +
                        " t.status as \"status\"," +
                        " t.mini_stock_amount as \"mini_stock_amount\"," +
                        " t.dosage as \"dosage\"," +
                        " t.max_stock_amount as \"max_stock_amount\"," +
                        " t.pack_unit as \"pack_unit\"," +
                        " t.is_del as \"is_del\"" +
                        " from base_template_dict b left join base_drug_dict t on b.dict_id = t.id " +
                        " where t.is_del='1' and b.is_del='1' and t.status ='1' ";
                String sqlCount = "select count(1) as \"count\" from base_template_dict b left join base_drug_dict t on b.dict_id = t.id " +
                        " where t.is_del='1' and b.is_del='1' and t.status ='1' ";
                if (StringUtils.isNoneBlank(code)){
                    sql+=" and t.charge_code like '%"+code+"%'";
                    sqlCount+=" and t.charge_code like '%"+code+"%'";
                }
                if (StringUtils.isNoneBlank(name)){
                    sql+=" and t.drugname like '%"+name+"%'";
                    sqlCount+=" and t.drugname like '%"+name+"%'";
                }
                sql+=" and b.template_id = '"+baseTemplateDictDO.getId()+"'";
                sqlCount+=" and b.template_id = '"+baseTemplateDictDO.getId()+"'";
                List<Map<String,Object>> list=hibenateUtils.createSQLQuery(sql,page,pageSize);
                for (Map<String,Object> map:list){
                    if (map.get("status")!=null&&"1".equalsIgnoreCase(map.get("status").toString())){
                        map.put("statusName","生效中");
                    }else {
                        map.put("statusName","已失效");
                    }
                }
                List<Map<String,Object>> countList=hibenateUtils.createSQLQuery(sqlCount);
                Integer count=0;
                if (countList!=null&&countList.size()>0) {
                    count = Integer.valueOf(countList.get(0).get("count").toString());
                }
                mixEnvelop.setDetailModelList(list);
                mixEnvelop.setTotalCount(count);
                return mixEnvelop;
            }else if ("icd10ListDict".equalsIgnoreCase(baseTemplateDictDO.getTemplateType())){
                String sql="select t.id as \"id\"," +
                        " t.code as \"code\"," +
                        " t.name as \"name\"," +
                        " t.description as \"description\"," +
                        " t.create_time as \"create_time\"," +
                        " t.disea_reason as \"disea_reason\"," +
                        " t.death_reason as \"death_reason\"," +
                        " t.py_code as \"py_code\"," +
                        " t.d_code as \"d_code\"," +
                        " t.flag as \"flag\"," +
                        " t.is_del as \"is_del\"," +
                        " t.status as \"status\" " +
                        " from base_template_dict b left join dict_icd10 t on b.dict_id=t.id" +
                        " where t.is_del='1' and b.is_del='1' and t.status ='1' ";
                String sqlCount =" select count(1) as \"count\" from base_template_dict b left join dict_icd10 t on b.dict_id=t.id" +
                        " where t.is_del='1' and b.is_del='1' and t.status ='1' ";
                if (StringUtils.isNoneBlank(code)){
                    sql+=" and t.code like '%"+code+"%'";
                    sqlCount+=" and t.code like '%"+code+"%'";
                }
                if (StringUtils.isNoneBlank(name)){
                    sql+=" and t.name like '%"+name+"%'";
                    sqlCount+=" and t.name like '%"+name+"%'";
                }
                sql+=" and b.template_id = '"+baseTemplateDictDO.getId()+"'";
                sqlCount+=" and b.template_id = '"+baseTemplateDictDO.getId()+"'";
                List<Map<String,Object>> list=hibenateUtils.createSQLQuery(sql,page,pageSize);
                for (Map<String,Object> map:list){
                    if (map.get("status")!=null&&"1".equalsIgnoreCase(map.get("status").toString())){
                        map.put("statusName","生效中");
                    }else {
                        map.put("statusName","已失效");
                    }
                }
                List<Map<String,Object>> countList=hibenateUtils.createSQLQuery(sqlCount);
                Integer count=0;
                if (countList!=null&&countList.size()>0) {
                    count = Integer.valueOf(countList.get(0).get("count").toString());
                }
                mixEnvelop.setDetailModelList(list);
                mixEnvelop.setTotalCount(count);
                return mixEnvelop;
            }else if ("followUpDict".equalsIgnoreCase(baseTemplateDictDO.getTemplateType())){
                String sql="select t.id as \"id\"," +
                        " t.code as \"code\"," +
                        " t.name as \"name\"," +
                        " t.is_required as \"isRequired\"," +
                        " t.project_content as \"projectContent\"," +
                        " t.status as \"status\"," +
                        " t.is_del as \"isDel\"," +
                        " t.create_time as \"createTime\"" +
                        " from base_template_dict b left join base_follow_up_dict t on b.dict_id=t.id" +
                        " where t.is_del='1' and b.is_del='1' and t.status ='1' ";
                String sqlCount =" select count(1) as \"count\" from base_template_dict b left join base_follow_up_dict t on b.dict_id=t.id" +
                        " where t.is_del='1' and b.is_del='1' and t.status ='1' ";
                if (StringUtils.isNoneBlank(code)){
                    sql+=" and t.code like '%"+code+"%'";
                    sqlCount+=" and t.code like '%"+code+"%'";
                }
                if (StringUtils.isNoneBlank(name)){
                    sql+=" and t.name like '%"+name+"%'";
                    sqlCount+=" and t.name like '%"+name+"%'";
                }
                sql+=" and b.template_id = '"+baseTemplateDictDO.getId()+"'";
                sqlCount+=" and b.template_id = '"+baseTemplateDictDO.getId()+"'";
                List<Map<String,Object>> list=hibenateUtils.createSQLQuery(sql,page,pageSize);
                for (Map<String,Object> map:list){
                    if (map.get("status")!=null&&"1".equalsIgnoreCase(map.get("status").toString())){
                        map.put("statusName","生效中");
                    }else {
                        map.put("statusName","已失效");
                    }
                    if (map.get("isRequired")!=null&&"1".equalsIgnoreCase(map.get("isRequired").toString())){
                        map.put("requiredName","必填");
                    }else {
                        map.put("requiredName","非必填");
                    }
                }
                List<Map<String,Object>> countList=hibenateUtils.createSQLQuery(sqlCount);
                Integer count=0;
                if (countList!=null&&countList.size()>0) {
                    count = Integer.valueOf(countList.get(0).get("count").toString());
                }
                mixEnvelop.setDetailModelList(list);
                mixEnvelop.setTotalCount(count);
                return mixEnvelop;
            }else {
                return null;
            }
        }else {
            return null;
        }
    }


    public MixEnvelop findTemlateNotAddChildList(String templateId,String name,String code,Integer page,Integer pageSize){
        BaseTemplateDO baseTemplateDictDO = baseTemplateDao.findOne(templateId);
        MixEnvelop mixEnvelop = new MixEnvelop();
        mixEnvelop.setPageSize(pageSize);
        mixEnvelop.setCurrPage(page);
        if (baseTemplateDictDO!=null){
            if ("frequencyDict".equalsIgnoreCase(baseTemplateDictDO.getTemplateType())){
                String sql="select DISTINCT t.id as \"id\"," +
                        " t.freq_code as \"freq_code\"," +
                        " t.freq_name as \"freq_name\"," +
                        " t.freq_sp as \"freq_sp\"," +
                        " t.daily_times as \"daily_times\"," +
                        " t.weekly_times as \"weekly_times\"," +
                        " t.hosp_code as \"hosp_code\"," +
                        " t.is_del as \"is_del\"," +
                        " t.is_op_ip as \"is_op_ip\"," +
                        " t.status as \"status\"," +
                        " t.create_time as \"create_time\"" +
                        " from  base_frequency_dict t where t.id not in ( SELECT DISTINCT g.id from base_frequency_dict g left join base_template_dict " +
                        "m on g.id = m.dict_id " +
                        "where m.template_id ='"+templateId+"' and m.is_del ='1') and t.is_del='1' and t.status='1' ";
                String sqlCount =" select count(1) as \"count\" from  base_frequency_dict t where t.id not in ( SELECT DISTINCT g.id from base_frequency_dict g left join base_template_dict " +
                        "m on g.id = m.dict_id " +
                        "where m.template_id ='"+templateId+"' and m.is_del ='1') and t.is_del='1' and t.status='1' ";
                if (StringUtils.isNoneBlank(code)){
                    sql+=" and t.freq_code like '%"+code+"%'";
                    sqlCount+=" and t.freq_code like '%"+code+"%'";
                }
                if (StringUtils.isNoneBlank(name)){
                    sql+=" and t.freq_name like '%"+name+"%'";
                    sqlCount+=" and t.freq_name like '%"+name+"%'";
                }
                List<Map<String,Object>> list=hibenateUtils.createSQLQuery(sql,page,pageSize);
                for (Map<String,Object> map:list){
                    if (map.get("status")!=null&&"1".equalsIgnoreCase(map.get("status").toString())){
                        map.put("statusName","生效中");
                    }else {
                        map.put("statusName","已失效");
                    }
                }
                List<Map<String,Object>> countList=hibenateUtils.createSQLQuery(sqlCount);
                Integer count=0;
                if (countList!=null&&countList.size()>0) {
                    count = Integer.valueOf(countList.get(0).get("count").toString());
                }
                mixEnvelop.setDetailModelList(list);
                mixEnvelop.setTotalCount(count);
                return mixEnvelop;
            }else if ("drugUseDict".equalsIgnoreCase(baseTemplateDictDO.getTemplateType())){
                String sql="select DISTINCT t.id as \"id\"," +
                        " t.supply_code as \"supply_code\"," +
                        " t.supply_name as \"supply_name\"," +
                        " t.supply_occ as \"supply_occ\"," +
                        " t.del_flag as \"del_flag\"," +
                        " t.sort_code as \"sort_code\"," +
                        " t.print_name as \"print_name\"," +
                        " t.py_code as \"py_code\"," +
                        " t.d_code as \"d_code\"," +
                        " t.yb_name as \"yb_name\"," +
                        " t.yb_code as \"yb_code\"," +
                        " t.status as \"status\"," +
                        " t.create_time as \"create_time\" " +
                        " from  base_drug_use_dict t where t.id not in ( SELECT DISTINCT g.id from base_drug_use_dict g left join base_template_dict " +
                        "m on g.id = m.dict_id " +
                        "where m.template_id ='"+templateId+"' and m.is_del ='1') and t.del_flag='1' and t.status='1'";
                String sqlCount =" select count(1) as \"count\" from  base_drug_use_dict t where t.id not in ( SELECT DISTINCT g.id from base_drug_use_dict g left join base_template_dict " +
                        "m on g.id = m.dict_id " +
                        "where m.template_id ='"+templateId+"' and m.is_del ='1') and t.del_flag='1' and t.status='1'";
                if (StringUtils.isNoneBlank(code)){
                    sql+=" and t.supply_code like '%"+code+"%'";
                    sqlCount+=" and t.supply_code like '%"+code+"%'";
                }
                if (StringUtils.isNoneBlank(name)){
                    sql+=" and t.supply_name like '%"+name+"%'";
                    sqlCount+=" and t.supply_name like '%"+name+"%'";
                }
                List<Map<String,Object>> list=hibenateUtils.createSQLQuery(sql,page,pageSize);
                for (Map<String,Object> map:list){
                    if (map.get("status")!=null&&"1".equalsIgnoreCase(map.get("status").toString())){
                        map.put("statusName","生效中");
                    }else {
                        map.put("statusName","已失效");
                    }
                }
                List<Map<String,Object>> countList=hibenateUtils.createSQLQuery(sqlCount);
                Integer count=0;
                if (countList!=null&&countList.size()>0) {
                    count = Integer.valueOf(countList.get(0).get("count").toString());
                }
                mixEnvelop.setDetailModelList(list);
                mixEnvelop.setTotalCount(count);
                return mixEnvelop;
            }else if ("drugListDict".equalsIgnoreCase(baseTemplateDictDO.getTemplateType())){
                String sql = "select DISTINCT t.id as \"id\"," +
                        " t.charge_code as \"charge_code\"," +
                        " t.drugname as \"drugname\"," +
                        " t.specification as \"specification\"," +
                        " t.py_code as \"py_code\"," +
                        " t.supply_code as \"supply_code\"," +
                        " t.supply_name as \"supply_name\"," +
                        " t.retprice as \"retprice\"," +
                        " t.manu_name as \"manu_name\"," +
                        " t.weigh_unit as \"weigh_unit\"," +
                        " t.mini_unit_name as \"mini_unit_name\"," +
                        " t.pack_unit_name as \"pack_unit_name\"," +
                        " t.weigh_unit_name as \"weigh_unit_name\"," +
                        " t.vol_unit_name as \"vol_unit_name\"," +
                        " t.win_no as \"win_no\"," +
                        " t.pack_retprice as \"pack_retprice\"," +
                        " t.mini_unit as \"mini_unit\"," +
                        " t.pack_size as \"pack_size\"," +
                        " t.action as \"action\"," +
                        " t.serial as \"serial\"," +
                        " t.group_no as \"group_no\"," +
                        " t.stock_amount as \"stock_amount\"," +
                        " t.visible_flag as \"visible_flag\"," +
                        " t.drug_flag as \"drug_flag\"," +
                        " t.create_time as \"create_time\"," +
                        " t.update_time as \"update_time\"," +
                        " t.status as \"status\"," +
                        " t.mini_stock_amount as \"mini_stock_amount\"," +
                        " t.dosage as \"dosage\"," +
                        " t.max_stock_amount as \"max_stock_amount\"," +
                        " t.pack_unit as \"pack_unit\"," +
                        " t.is_del as \"is_del\"" +
                        " from  base_drug_dict t where t.id not in ( SELECT DISTINCT g.id from base_drug_dict g left join base_template_dict " +
                        " m on g.id = m.dict_id " +
                        "where m.template_id ='"+templateId+"' and m.is_del ='1') and t.is_del='1' and t.status='1'";
                String sqlCount = "select count(1) as \"count\"  from  base_drug_dict t where t.id not in ( SELECT DISTINCT g.id from base_drug_dict g left join base_template_dict " +
                        "m on g.id = m.dict_id " +
                        "where m.template_id ='"+templateId+"' and m.is_del ='1') and t.is_del='1' and t.status='1'";
                if (StringUtils.isNoneBlank(code)){
                    sql+=" and t.charge_code like '%"+code+"%'";
                    sqlCount+=" and t.charge_code like '%"+code+"%'";
                }
                if (StringUtils.isNoneBlank(name)){
                    sql+=" and t.drugname like '%"+name+"%'";
                    sqlCount+=" and t.drugname like '%"+name+"%'";
                }
                List<Map<String,Object>> list=hibenateUtils.createSQLQuery(sql,page,pageSize);
                for (Map<String,Object> map:list){
                    if (map.get("status")!=null&&"1".equalsIgnoreCase(map.get("status").toString())){
                        map.put("statusName","生效中");
                    }else {
                        map.put("statusName","已失效");
                    }
                }
                List<Map<String,Object>> countList=hibenateUtils.createSQLQuery(sqlCount);
                Integer count=0;
                if (countList!=null&&countList.size()>0) {
                    count = Integer.valueOf(countList.get(0).get("count").toString());
                }
                mixEnvelop.setDetailModelList(list);
                mixEnvelop.setTotalCount(count);
                return mixEnvelop;
            }else if ("icd10ListDict".equalsIgnoreCase(baseTemplateDictDO.getTemplateType())){
                String sql="select DISTINCT t.id as \"id\"," +
                        " t.code as \"code\"," +
                        " t.name as \"name\"," +
                        " t.description as \"description\"," +
                        " t.create_time as \"create_time\"," +
                        " t.disea_reason as \"disea_reason\"," +
                        " t.death_reason as \"death_reason\"," +
                        " t.py_code as \"py_code\"," +
                        " t.d_code as \"d_code\"," +
                        " t.flag as \"flag\"," +
                        " t.is_del as \"is_del\"," +
                        " t.status as \"status\" " +
                        " from  dict_icd10 t where t.id not in ( SELECT DISTINCT g.id from dict_icd10 g left join base_template_dict " +
                        "m on g.id = m.dict_id " +
                        "where m.template_id ='"+templateId+"' and m.is_del ='1') and t.is_del='1' and t.status='1'";
                String sqlCount =" select count(1) as \"count\" from  dict_icd10 t where t.id not in ( SELECT DISTINCT g.id from dict_icd10 g left join base_template_dict " +
                        "m on g.id = m.dict_id " +
                        "where m.template_id ='"+templateId+"' and m.is_del ='1') and t.is_del='1' and t.status='1'";
                if (StringUtils.isNoneBlank(code)){
                    sql+=" and t.code like '%"+code+"%'";
                    sqlCount+=" and t.code like '%"+code+"%'";
                }
                if (StringUtils.isNoneBlank(name)){
                    sql+=" and t.name like '%"+name+"%'";
                    sqlCount+=" and t.name like '%"+name+"%'";
                }
                List<Map<String,Object>> list=hibenateUtils.createSQLQuery(sql,page,pageSize);
                for (Map<String,Object> map:list){
                    if (map.get("status")!=null&&"1".equalsIgnoreCase(map.get("status").toString())){
                        map.put("statusName","生效中");
                    }else {
                        map.put("statusName","已失效");
                    }
                }
                List<Map<String,Object>> countList=hibenateUtils.createSQLQuery(sqlCount);
                Integer count=0;
                if (countList!=null&&countList.size()>0) {
                    count = Integer.valueOf(countList.get(0).get("count").toString());
                }
                mixEnvelop.setDetailModelList(list);
                mixEnvelop.setTotalCount(count);
                return mixEnvelop;
            }else if ("followUpDict".equalsIgnoreCase(baseTemplateDictDO.getTemplateType())){
                String sql="select DISTINCT t.id as \"id\"," +
                        " t.code as \"code\"," +
                        " t.name as \"name\"," +
                        " t.is_required as \"isRequired\"," +
                        " t.project_content as \"projectContent\"," +
                        " t.status as \"status\"," +
                        " t.is_del as \"isDel\"," +
                        " t.create_time as \"createTime\"" +
                        " from  base_follow_up_dict t where t.id not in ( SELECT DISTINCT g.id from base_follow_up_dict g left join base_template_dict " +
                        "m on g.id = m.dict_id " +
                        "where m.template_id ='"+templateId+"' and m.is_del ='1') and t.is_del='1' and t.status='1'";
                String sqlCount =" select count(1) as \"count\" from  base_follow_up_dict t where t.id not in ( SELECT DISTINCT g.id from base_follow_up_dict g left join base_template_dict " +
                        "m on g.id = m.dict_id " +
                        "where m.template_id ='"+templateId+"' and m.is_del ='1') and t.is_del='1' and t.status='1'";
                if (StringUtils.isNoneBlank(code)){
                    sql+=" and t.code like '%"+code+"%'";
                    sqlCount+=" and t.code like '%"+code+"%'";
                }
                if (StringUtils.isNoneBlank(name)){
                    sql+=" and t.name like '%"+name+"%'";
                    sqlCount+=" and t.name like '%"+name+"%'";
                }
                List<Map<String,Object>> list=hibenateUtils.createSQLQuery(sql,page,pageSize);
                for (Map<String,Object> map:list){
                    if (map.get("status")!=null&&"1".equalsIgnoreCase(map.get("status").toString())){
                        map.put("statusName","生效中");
                    }else {
                        map.put("statusName","已失效");
                    }
                    if (map.get("isRequired")!=null&&"1".equalsIgnoreCase(map.get("isRequired").toString())){
                        map.put("requiredName","必填");
                    }else {
                        map.put("requiredName","非必填");
                    }
                }
                List<Map<String,Object>> countList=hibenateUtils.createSQLQuery(sqlCount);
                Integer count=0;
                if (countList!=null&&countList.size()>0) {
                    count = Integer.valueOf(countList.get(0).get("count").toString());
                }
                mixEnvelop.setDetailModelList(list);
                mixEnvelop.setTotalCount(count);
                return mixEnvelop;
            }else {
                return null;
            }
        }else {
            return null;
        }
    }
    public List<Map<String,Object>> findTemplateByDisease(String doctor,String diseaseId){
        List<BaseDoctorHospitalDO> baseDoctorHospitalDO = baseDoctorHospitalDao.findByDoctorCode(doctor);
        String orgCode="";
        if (baseDoctorHospitalDO!=null&&baseDoctorHospitalDO.size()>0){
            orgCode=baseDoctorHospitalDO.get(0).getOrgCode();
        }
        String sql =" select DISTINCT t.id as \"id\" ," +
                " t.name as \"name\"," +
                " t.code as \"code\"," +
                " t.children_count as \"children_count\"," +
                " t.template_type as \"template_type\"," +
                " t.disease_id as \"disease_id\"," +
                " t.disease_name as \"disease_name\"," +
                " t.is_del as \"is_del\"," +
                " t.update_time as \"updateTime\" " +
                " from base_template_org m left join base_template t " +
                " on m.template_id=t.id " +
                " left join base_template_dict d on t.id = d.template_id " +
                " left join base_follow_up_dict f on f.id=d.dict_id " +
                " where t.is_del='1' and m.is_del='1' and d.is_del='1' and f.is_del='1'  and t.template_type='followUpDict' ";
        if (StringUtils.isNoneBlank(orgCode)){
            sql+=" and m.org_code='"+orgCode+"'";
        }
        if (StringUtils.isNoneBlank(diseaseId)){
            sql+=" and t.disease_id='"+diseaseId+"'";
        }
        System.out.println(sql);
        List<Map<String,Object>> list = hibenateUtils.createSQLQuery(sql);
        for (Map<String,Object> map:list){
            String sqlChild=" select  f.id as \"id\" ," +
                    " f.name as \"name\"," +
                    " f.code as \"code\"," +
                    " f.project_content as \"project_content\"," +
                    " f.status as \"status\"," +
                    " f.is_del as \"is_del\" " +
                    " from base_template t " +
                    " left join base_template_dict d on t.id = d.template_id " +
                    " left join base_follow_up_dict f on f.id=d.dict_id " +
                    " where t.is_del='1'  and d.is_del='1' and f.is_del='1'  and t.template_type='followUpDict' ";
            sqlChild+=" and t.id='"+map.get("id").toString()+"'";
            System.out.println("=="+sqlChild);
            map.put("childList",hibenateUtils.createSQLQuery(sqlChild));
            /*if (map.get("disease_id")!=null){
                String[] diseaseIds=map.get("disease_id").toString().split(",");
                for (int i=0;i<diseaseIds.length;i++){
                    if (diseaseIds[i].equalsIgnoreCase(diseaseId)){
                        resultList.add(map);
                    }
                }
            }*/
        }
        return list;
    }
}
