package com.yihu.jw.utils;

import com.yihu.jw.entity.hospital.consult.WlyyHospitalSysDictDO;
import com.yihu.jw.entity.hospital.httplog.BaseOperateLogDO;
import com.yihu.jw.hospital.dict.WlyyHospitalSysDictDao;
import com.yihu.jw.util.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

/**
 * @ClassName: JDBCUtils
 * @Description: 实现连接池效果，初始化10个JDBC连接对象并放入池中，提供方法每次返回一个对象
 * @Author: xuezhouyi
 * @Version: V1.0
 **/
@Slf4j
@Service
public class JDBCSqlServer {
    @Autowired
    private WlyyHospitalSysDictDao wlyyHospitalSysDictDao;
    public  void insertZsSqlserver(BaseOperateLogDO baseOperateLogDO) {
        Connection connection=null;
        String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL数据库引擎
        String dbURL="jdbc:sqlserver://192.10.33.110:1433;DatabaseName=logdb";//数据源  ！！！注意若出现加载或者连接数据库失败一般是这里出现问题
        String dbName="nethoslogdba";
        String dbPwd="nethoslogdba^1";
        List<WlyyHospitalSysDictDO> configlist = wlyyHospitalSysDictDao.findByDictName("sqlServerConfig");
        for (WlyyHospitalSysDictDO wlyyHospitalSysDictDO:configlist){
            if ("isOpen".equalsIgnoreCase(wlyyHospitalSysDictDO.getDictCode())){
                if (!"1".equalsIgnoreCase(wlyyHospitalSysDictDO.getDictValue())){
                    System.out.println("关闭传入sqlserver");
                    return;
                }
            }
            System.out.println("开启传入sqlserver");
            if ("dbURL".equalsIgnoreCase(wlyyHospitalSysDictDO.getDictCode())){
                dbURL=wlyyHospitalSysDictDO.getDictValue();
            }
            if ("dbName".equalsIgnoreCase(wlyyHospitalSysDictDO.getDictCode())){
                dbName=wlyyHospitalSysDictDO.getDictValue();
            }
            if ("dbPwd".equalsIgnoreCase(wlyyHospitalSysDictDO.getDictCode())){
                dbPwd=wlyyHospitalSysDictDO.getDictValue();
            }
        }

        try{
            Class.forName(driverName);
            connection=DriverManager.getConnection(dbURL,dbName,dbPwd);
            System.out.println("连接数据库成功");
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("连接失败");
        }
        Statement stmt = null;
        try {
            stmt = connection.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String creatTime=DateUtil.dateToStr(baseOperateLogDO.getCreateTime(),"yyyy-MM-dd hh:mm:ss");
        //增删改操作
        String sql_1="insert into nethoslog values('"+baseOperateLogDO.getId()+"','"+baseOperateLogDO.getCode()+"','"+baseOperateLogDO.getName()+"','"+baseOperateLogDO.getOperator()+ "','"+baseOperateLogDO.getOperatorName()+"','"+baseOperateLogDO.getPatient()+"','"+baseOperateLogDO.getPatientName()+"','"+creatTime+ "','','','"+baseOperateLogDO.getIsDel()+"')";
        int count_1 = 0;
        try {
            count_1 = stmt.executeUpdate(sql_1);
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }//返回值表示增删改几条数据
        //处理结果
        if(count_1>0){
            System.out.println("更新成功!");
        }
        //关闭
        try {
            stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

