package com.yihu.jw.hospital.httplog.service;

import com.yihu.jw.doctor.dao.BaseDoctorDao;
import com.yihu.jw.entity.base.doctor.BaseDoctorDO;
import com.yihu.jw.entity.base.patient.BasePatientDO;
import com.yihu.jw.entity.hospital.httplog.BaseOperateLogDO;
import com.yihu.jw.entity.hospital.httplog.WlyyHttpLogDO;
import com.yihu.jw.entity.hospital.mapping.DoctorMappingDO;
import com.yihu.jw.entity.hospital.mapping.PatientMappingDO;
import com.yihu.jw.hospital.httplog.dao.BaseOperateLogDao;
import com.yihu.jw.hospital.httplog.dao.WlyyHttpLogDao;
import com.yihu.jw.hospital.mapping.dao.DoctorMappingDao;
import com.yihu.jw.hospital.mapping.dao.PatientMappingDao;
import com.yihu.jw.patient.dao.BasePatientDao;
import com.yihu.jw.restmodel.web.MixEnvelop;
import com.yihu.jw.utils.JDBCSqlServer;
import com.yihu.jw.utils.hibernate.HibenateUtils;
import com.yihu.mysql.query.BaseJpaService;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
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 BaseOperateLogService extends BaseJpaService<BaseOperateLogDO, BaseOperateLogDao> {
    @Autowired
    private BaseOperateLogDao baseOperateLogDao;
    @Autowired
    private BasePatientDao basePatientDao;
    @Autowired
    private BaseDoctorDao baseDoctorDao;
    @Autowired
    private JDBCSqlServer jdbcSqlServer;
    @Autowired
    private HibenateUtils hibenateUtils;
    @Autowired
    private PatientMappingDao patientMappingDao;
    @Autowired
    private DoctorMappingDao doctorMappingDao;
    @Value("${wechat.id}")
    private String wxId;
    @Value("${wechat.flag}")
    private boolean flag;
    public void saveOperateLog(String operator,String patient,String code,String name,String relationType,String relationCode){
        BaseOperateLogDO baseOperateLogDO=new BaseOperateLogDO();
        BaseDoctorDO baseDoctorDO=baseDoctorDao.findById(operator);
        baseOperateLogDO.setOperator(operator);
        baseOperateLogDO.setPatient(patient);
        baseOperateLogDO.setCode(code);
        baseOperateLogDO.setName(name);
        baseOperateLogDO.setRelationType(relationType);
        baseOperateLogDO.setCreateTime(new Date());
        baseOperateLogDO.setIsDel("1");
        if (baseDoctorDO!=null){
            baseOperateLogDO.setOperatorName(baseDoctorDO.getName());
        }
        BasePatientDO basePatientDO=basePatientDao.findById(patient);
        if (basePatientDO!=null){
            baseOperateLogDO.setPatientName(basePatientDO.getName());
        }
        baseOperateLogDO=baseOperateLogDao.save(baseOperateLogDO);
        try {
            /*PatientMappingDO patientMappingDO=patientMappingDao.findByPatient(baseOperateLogDO.getPatient());
            if (patientMappingDO!=null){
                baseOperateLogDO.setPatient(patientMappingDO.getMappingCode());
            }
            DoctorMappingDO doctorMappingDO = doctorMappingDao.findByDoctor(baseOperateLogDO.getOperator());
            if (doctorMappingDO!=null){
                baseOperateLogDO.setOperator(doctorMappingDO.getMappingCode());
            }*/
            jdbcSqlServer.insertZsSqlserver(baseOperateLogDO);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public MixEnvelop  findOperateLogList(String code,String name,String patientName,String doctorName,String startTime,String endTime,Integer page,Integer pageSize){
        MixEnvelop mixEnvelop = new MixEnvelop();
        String sql ="select t.id as \"id\"," +
                " t.code as \"code\"," +
                " t.name as \"name\"," +
                " t.operator as \"operator\"," +
                " t.operator_name as \"operator_name\"," +
                " t.patient as \"patient\"," +
                " t.patient_name as \"patient_name\"," ;
        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.relation_type as \"relation_type\"," +
                " t.relation_code as \"relation_code\"" +
                " from base_operate_log t where t.is_del='1'";
        if (StringUtils.isNoneBlank(code)){
            sql+=" and t.code='"+code+"'";
        }
        if (StringUtils.isNoneBlank(name)){
            sql+=" and t.name like '%"+name+"%'";
        }
        if (StringUtils.isNoneBlank(patientName)){
            sql+=" and t.patient_name like '%"+patientName+"%'";
        }
        if (StringUtils.isNoneBlank(doctorName)){
            sql+=" and t.operator_name like '%"+doctorName+"%'";
        }
        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;
    }

}
