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

import com.yihu.jw.entity.hospital.httplog.YlzHttpLogDO;
import com.yihu.jw.hospital.httplog.dao.YlzHttpLogDao;
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 YlzHttpLogService extends BaseJpaService<YlzHttpLogDO, YlzHttpLogDao> {
    @Autowired
    private YlzHttpLogDao wlyyHttpLogDao;
    @Value("${wechat.id}")
    private String wxId;
    @Autowired
    private HibenateUtils hibenateUtils;


    @Value("${wechat.flag}")
    private boolean flag;
    //保存http日志接口
    public  void  saveHttpLog(String code,String name,String patient,String doctor,String request,String response,String status ){
        YlzHttpLogDO wlyyHttpLogDO = new YlzHttpLogDO();
        wlyyHttpLogDO.setCode(code);
        wlyyHttpLogDO.setName(name);
        wlyyHttpLogDO.setPatient(patient);
        wlyyHttpLogDO.setDoctor(doctor);
        wlyyHttpLogDO.setRequest(request);
        wlyyHttpLogDO.setResponse(response);
        wlyyHttpLogDO.setStatus(status);
        wlyyHttpLogDO.setCreateTime(new Date());
        wlyyHttpLogDao.save(wlyyHttpLogDO);
    }
    public MixEnvelop findLog(String startTime, String endTime, String faceName, String patient, String doctor, Integer page, Integer pageSize){
        MixEnvelop mixEnvelop = new MixEnvelop();
        String sql = "select t.id as \"id\"," +
                "t.code as \"code\"," +
                "t.name as \"name\"," +
                "t.patient as \"patient\"," +
                "t.doctor as \"doctor\"," +
                "t.request as \"request\"," +
                "t.response as \"response\"," +
                "t.status as \"status\",";
        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+=" a.name as \"patientName\"," +
                "b.name as \"doctorName\"" +
                " from ylz_http_log t left join base_patient a on t.patient = a.id " +
                " left join base_doctor b on t.doctor = b.id where 1=1 ";
        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 + "'";
            }
        }
        if (StringUtils.isNotBlank(faceName)){
            sql+=" and t.name like '%"+faceName+"%'";
        }
        if (StringUtils.isNotBlank(patient)){
            sql+=" and a.name like '%"+patient+"%'";
        }
        if (StringUtils.isNotBlank(doctor)){
            sql+=" and b.name like '%"+doctor+"%'";
        }
        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 MixEnvelop findWlyyHttpLogInBase(String startTime, String endTime, String code,String faceName, String patient, String doctor,String responeKeyWord,String requestKeyWord, Integer page, Integer pageSize){
        MixEnvelop mixEnvelop = new MixEnvelop();
        String sql = "select t.id as \"id\"," +
                "t.code as \"code\"," +
                "t.name as \"name\"," +
                "t.patient as \"patient\"," +
                "t.doctor as \"doctor\"," +
                "t.request as \"request\"," +
                "t.response as \"response\"," +
                "t.status as \"status\",";
        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+=" a.name as \"patientName\"," +
                "b.name as \"doctorName\"" +
                " from ylz_http_log t left join base_patient a on t.patient = a.id " +
                " left join base_doctor b on t.doctor = b.id where 1=1 ";
        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 + "'";
            }
        }
        if (StringUtils.isNotBlank(code)){
            sql+=" and t.code = '"+code+"'";
        }
        if (StringUtils.isNotBlank(faceName)){
            sql+=" and (t.name like '%"+faceName+"%' or t.code = '"+faceName+"')";
        }
        if (StringUtils.isNotBlank(patient)){
            sql+=" and (a.name like '%"+patient+"%' or t.patient = '"+patient+"')";
        }
        if (StringUtils.isNotBlank(doctor)){
            sql+=" and (b.name like '%"+doctor+"%'or t.doctor = '"+doctor+"')";
        }
        if (StringUtils.isNotBlank(requestKeyWord)){
            sql+=" and t.request like '%"+requestKeyWord+"%'";
        }
        if (StringUtils.isNotBlank(responeKeyWord)){
            sql+=" and t.response like '%"+responeKeyWord+"%'";
        }
        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 MixEnvelop findLoginLog(String startTime, String endTime, String userId,String userName, String openId, String userAgent,String loginType,String opreateStart, String opreateEnd,  Integer page, Integer pageSize) {
        MixEnvelop mixEnvelop = new MixEnvelop();
        String sql = "select t.id as \"id\"," +
                "t.user_id as \"userId\"," +
                "p.name as \"patientName\"," +
                "d.name as \"doctorName\"," +
                "t.user_agent as \"userAgent\"," +
                "t.login_type as \"loginType\"," +
                "t.openid as \"openid\"," ;
        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\",";
        }
        if("xm_ykyy_wx".equals(wxId)){
            if (flag){
                sql+="date_format(t.operate_time, '%Y-%m-%d %H:%i:%s')  as \"operateTime\",";
            }else {
                sql+="  to_char(t.operate_time,'yyyy-MM-dd HH24:mi:ss') as \"operateTime\",";
            }
        }else{
            sql+="date_format(t.operate_time, '%Y-%m-%d %H:%i:%s')  as \"operateTime\",";
        }
        sql+=" a.name as \"patientName\"," +
                "b.name as \"doctorName\"" +
                " from ylz_http_log t left join base_patient p on t.user_id = p.id " +
                " left join base_doctor d on t.user_id = d.id where 1=1 ";
        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 + "'";
            }
        }
        if (StringUtils.isNotBlank(userId)){
            sql+=" and t.user_id = '"+userId+"'";
        }
        if (StringUtils.isNoneBlank(userName)){
            if ("2".equalsIgnoreCase(loginType)){
                sql+=" and d.name like '%"+userName+"%'";
            }else {
                sql+=" and p.name like '%"+userName+"%'";
            }
        }
        if (StringUtils.isNotBlank(loginType)){
            sql+=" and t.login_type ='"+loginType+"'";
        }
        if (StringUtils.isNotBlank(openId)){
            sql+=" and t.openId like '%"+openId+"%'";
        }
        if (StringUtils.isNotBlank(userAgent)){
            sql+=" and t.user_agent like '%"+userAgent+"%'";
        }
        if (StringUtils.isNotBlank(opreateStart)){
            if("xm_ykyy_wx".equals(wxId)){
                if (flag){
                    sql+=" and t.operate_time > '"+opreateStart+"'";
                }else {
                    sql+=" and t.operate_time > to_date('" + opreateStart + "', 'yyyy-mm-dd hh24:mi:ss') ";
                }
            }else{
                sql+=" and t.operate_time > '"+opreateStart+"'";
            }
        }
        if (StringUtils.isNotBlank(opreateEnd)){
            if("xm_ykyy_wx".equals(wxId)){
                if (flag){
                    sql+="  and t.operate_time<'" + opreateEnd + "'";
                }else {
                    sql+="  and t.operate_time< to_date('" + opreateEnd + "','yyyy-mm-dd hh24:mi:ss')";
                }
            }else{
                sql+="  and t.operate_time<'" + opreateEnd + "'";
            }
        }
        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;
    }
}
