package com.yihu.jw.doctor.service;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.jw.doctor.dao.BaseDoctorDao;
import com.yihu.jw.doctor.dao.BaseDoctorSuggestDao;
import com.yihu.jw.doctor.dao.DoctorSpecialDiseaseDao;
import com.yihu.jw.entity.base.doctor.BaseDoctorDO;
import com.yihu.jw.entity.base.doctor.BaseDoctorSuggestDO;
import com.yihu.jw.entity.base.doctor.DoctorSpecialDiseaseDo;
import com.yihu.jw.entity.base.patient.BasePatientDO;
import com.yihu.jw.entity.hospital.consult.WlyyHospitalSysDictDO;
import com.yihu.jw.hospital.dict.WlyyHospitalSysDictDao;
import com.yihu.jw.patient.dao.BasePatientDao;
import com.yihu.jw.restmodel.web.MixEnvelop;
import com.yihu.jw.utils.hibernate.HibenateUtils;
import com.yihu.mysql.query.BaseJpaService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class BaseDoctorSuggestService  extends BaseJpaService<BaseDoctorSuggestDO, BaseDoctorSuggestDao> {
    @Autowired
    private BaseDoctorDao baseDoctorDao;
    @Autowired
    private HibenateUtils hibenateUtils;
    @Autowired
    private BasePatientDao basePatientDao;
    @Autowired
    private WlyyHospitalSysDictDao wlyyHospitalSysDictDao;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private BaseDoctorSuggestDao baseDoctorSuggestDao;
    @Value("${wechat.id}")
    private String wxId;
    @Value("${wechat.flag}")
    private boolean flag;

    public void saveDoctorSuggest(String suggestType,String  suggestContent,String  imgUrl,String doctor,String createUser,String relationCode,String relationType) throws  Exception{
        BaseDoctorSuggestDO baseDoctorSuggestDO=new BaseDoctorSuggestDO();
        baseDoctorSuggestDO.setCreateUser(createUser);
        baseDoctorSuggestDO.setSuggestType(suggestType);
        baseDoctorSuggestDO.setSuggestContent(suggestContent);
        baseDoctorSuggestDO.setImgUrl(imgUrl);
        baseDoctorSuggestDO.setRelationCode(relationCode);
        baseDoctorSuggestDO.setRelationType(relationType);
        baseDoctorSuggestDO.setDoctor(doctor);
        baseDoctorSuggestDO.setCreateTime(new Date());
        baseDoctorSuggestDO.setIsDel("1");
        BasePatientDO basePatientDO=basePatientDao.findById(baseDoctorSuggestDO.getCreateUser());
        if (basePatientDO!=null){
            baseDoctorSuggestDO.setCreateUserName(basePatientDO.getName());
        }
        BaseDoctorDO baseDoctorDO=baseDoctorDao.findById(baseDoctorSuggestDO.getDoctor());
        if (baseDoctorDO!=null){
            baseDoctorSuggestDO.setDoctorName(baseDoctorDO.getName());
        }
        List<WlyyHospitalSysDictDO> list =wlyyHospitalSysDictDao.findByDictName("doctorSuggestType");
        for (WlyyHospitalSysDictDO wlyyHospitalSysDictDO:list){
            if (baseDoctorSuggestDO.getSuggestType().equalsIgnoreCase(wlyyHospitalSysDictDO.getDictCode())){
                baseDoctorSuggestDO.setSuggestName(wlyyHospitalSysDictDO.getDictValue());
            }
        }
        baseDoctorSuggestDao.save(baseDoctorSuggestDO);
    }
    public MixEnvelop findDoctorSuggest(String doctor,String patient,String doctorName,String patientName,String startTime,String endTime,
                                        String suggestType,String relationType,String keyWord,Integer page,Integer pageSize){
        MixEnvelop mixEnvelop = new MixEnvelop();
        String sql = "select t.id as \"id\"," +
                "t.suggest_type as \"suggestType\"," +
                "t.suggest_name as \"suggestName\"," +
                "t.suggest_content as \"suggestContent\"," +
                "t.relation_code as \"relationCode\"," +
                "t.relation_type as \"relationType\"," ;
        if("xm_ykyy_wx".equals(wxId)){
            if (flag){
                sql+="date_format(t.create_time, '%Y-%m-%d %H:%i:%s')  as \"createTime\",";
            }else {
                sql+="  to_char(t.create_time,'yyyy-MM-dd HH24:mi:ss') as \"createTime\",";
            }
        }else{
            sql+="date_format(t.create_time, '%Y-%m-%d %H:%i:%s')  as \"createTime\",";
        }
        sql+= "t.create_user as \"createUser\"," +
                "t.create_user_name as \"createUserName\"," +
                "t.doctor as \"doctor\"," +
                "t.doctor_name as \"doctorName\"," +
                "t.is_del as \"isDel\"," +
                "t.img_url as \"imgUrl\"" +
                "from base_doctor_suggest t where t.is_del ='1'";
        if (StringUtils.isNoneBlank(doctor)){
            sql+=" and t.doctor='"+doctor+"'";
        }
        if (StringUtils.isNoneBlank(patient)){
            sql+=" and t.create_user='"+patient+"'";
        }
        if (StringUtils.isNoneBlank(doctorName)){
            sql+=" and t.doctor_name like '%"+doctorName+"%'";
        }
        if (StringUtils.isNoneBlank(patientName)){
            sql+=" and t.create_user_name like '%"+patientName+"%'";
        }if (StringUtils.isNoneBlank(suggestType)){
            sql+=" and t.suggest_type='"+suggestType+"'";
        }if (StringUtils.isNoneBlank(relationType)){
            sql+=" and t.relation_type='"+relationType+"'";
        }if (StringUtils.isNoneBlank(keyWord)){
            sql+=" and t.suggest_content like '%"+keyWord+"%'";
        }
        if (StringUtils.isNotBlank(startTime)){
            if("xm_ykyy_wx".equals(wxId)){
                if (flag){
                    sql+=" and t.create_time > '"+startTime+"'";
                }else {
                    sql+=" and t.create_time > to_date('" + startTime + "', 'yyyy-mm-dd hh24:mi:ss') ";
                }
            }else{
                sql+=" and t.create_time > '"+startTime+"'";
            }
        }
        if (StringUtils.isNotBlank(endTime)){
            if("xm_ykyy_wx".equals(wxId)){
                if (flag){
                    sql+="  and t.create_time<='" + endTime + "'";
                }else {
                    sql+="  and t.create_time<= to_date('" + endTime + "','yyyy-mm-dd hh24:mi:ss')";
                }
            }else{
                sql+="  and t.create_time<='" + endTime + "'";
            }
        }
        sql+=" order by t.create_time desc ";
        List<Map<String,Object>> list = hibenateUtils.createSQLQuery(sql,page,pageSize);
        String sqlcount = "SELECT COUNT(1) AS \"total\" FROM ("+sql+") q";
        Long count = 0L;
        List<Map<String,Object>> total = hibenateUtils.createSQLQuery(sqlcount);
        if(total!=null){
            //mysql 与 Oracle 聚合函数返回类型不一致，需要判断装换
            count = hibenateUtils.objTransformLong(total.get(0).get("total"));
        }
        mixEnvelop.setTotalCount(count.intValue());
        mixEnvelop.setDetailModelList(list);
        mixEnvelop.setPageSize(pageSize);
        mixEnvelop.setCurrPage(page);
        return mixEnvelop;
    }
    public BaseDoctorSuggestDO findById(String id){
        return baseDoctorSuggestDao.findOne(id);
    }
}
