package com.joju.datamanager.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.db.handler.BeanListHandler;
import com.joju.datamanager.model.medicalprescription.MedicalPrescription;
import com.joju.datamanager.model.viewmodel.PatientList;
import com.joju.datamanager.service.HisViewSearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.sql.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 手术管理服务接口实体类
 * @author: Mr.zs
 * @create: 2024-03-15 17:45
 **/
@Service
@Slf4j
public class HisViewSearchServiceImpl implements HisViewSearchService {

    //查看手术视图 HIS_SSXXCX

    public List<Map<String, Object>> getSurgicalView(String hzxm, String blh) {
        log.info("拼接数据");
//        String URL = "jdbc:sqlserver:thin:@//168.168.0.10:1433/THIS4";
        String URL = "jdbc:sqlserver://168.168.0.10:1433;databaseName=THIS4";
        String USER = "joju";
        String PASSWORD = "Joju@123";
        List<Map<String, Object>> list = null;
        // 1.加载驱动程序
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            // 2.获得数据库链接
            log.info("开始链接数据库");
            Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
            log.info("打印链接状态：" + conn);
            // 3.通过数据库的连接操作数据库，实现增删改查（使用Statement类）
//            String inHosnum = "811134";
            String sql = "select * from HIS_SSXXCX  where hzxm ='" + hzxm + "'  and blh = '" + blh + "' ";
            log.info("打印sql" + sql);
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            log.info("返回结果是：" + rs);
            // 4.处理数据库的返回结果(使用ResultSet类)
            while (rs.next()) {
                log.info("开始处理数据");
                Map<String, Object> map = new HashMap<String, Object>();
//                log.info("打印itemType" + rs.getString("ItemType"));
                map.put("blh", rs.getString("blh"));
                map.put("hzxm", rs.getString("hzxm"));
                map.put("ssmc", rs.getString("ssmc"));
                map.put("sqrq", rs.getString("sqrq"));
                map.put("aprq", rs.getString("aprq"));
                map.put("kssj", rs.getString("kssj"));
                map.put("jssj", rs.getString("jssj"));
                map.put("zt", rs.getString("zt"));
                list.add(map);
            }
            log.info("list is " + list);
            // 关闭资源【多谢指正】
            rs.close();
            statement.close();
            conn.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    //查看历史就诊视图 v_patient_list
    public List<PatientList> getHistoricalVisits(String patient_name, String idcard) {
        log.info("拼接数据");
//        String URL = "jdbc:sqlserver:thin:@//168.168.0.10:1433/THIS4";
        String URL = "jdbc:sqlserver://168.168.0.10:1433;databaseName=THIS4";
        String USER = "joju";
        String PASSWORD = "Joju@123";
        List<PatientList> patientLists = null;
        // 1.加载驱动程序
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            // 2.获得数据库链接
            log.info("开始链接数据库");
            Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
            log.info("打印链接状态：" + conn);
            // 3.通过数据库的连接操作数据库，实现增删改查（使用Statement类）
//            String inHosnum = "811134";
            String sql = "select * from v_patient_list  where patient_name ='" + patient_name + "'  and idcard = '" + idcard + "' " + "order by register_date DESC";
            log.info("打印sql" + sql);
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            log.info("返回结果是：" + rs);

            BeanListHandler<PatientList> patientListBeanListHandler = new BeanListHandler<>(PatientList.class);

            patientLists = patientListBeanListHandler.handle(rs);

            log.info("list is " + patientLists);
            // 关闭资源【多谢指正】
            rs.close();
            statement.close();
            conn.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return patientLists;
    }

    @Override
    public List<MedicalPrescription> getMedicalPrescription() {
        log.info("拼接数据");
//        String URL = "jdbc:sqlserver:thin:@//168.168.0.10:1433/THIS4";
        String URL = "jdbc:sqlserver://168.168.0.10:1433;databaseName=THIS4";
        String USER = "joju";
        String PASSWORD = "Joju@123";
        List<MedicalPrescription> medicalPrescriptions = null;
        // 1.加载驱动程序
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            // 2.获得数据库链接
            log.info("开始链接数据库");
            Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
            log.info("打印链接状态：" + conn);
            // 3.通过数据库的连接操作数据库，实现增删改查（使用Statement类）
            String today = DateUtil.today();
            String todayString = today + "00:00:00";
            DateTime tomorrow = DateUtil.tomorrow();
            String s = DateUtil.formatDate(tomorrow);
            String tomorrowString = s + "00:00:00";
            //       SELECT * FROM HIS_WJFHZCFXX WHERE lrrq BETWEEN '2024-04-02 00:00:00' AND '2024-04-03 00:00:00 '
            String sql = " SELECT * FROM HIS_WJFHZCFXX WHERE lrrq BETWEEN '" + todayString + "' AND '" + tomorrowString + "'";
            log.info("打印sql" + sql);
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            log.info("返回结果是：" + rs);

            BeanListHandler<MedicalPrescription> medicalPrescriptionBeanListHandler = new BeanListHandler<>(MedicalPrescription.class);
            medicalPrescriptions = medicalPrescriptionBeanListHandler.handle(rs);

            log.info("list is " + medicalPrescriptions);
            // 关闭资源【多谢指正】
            rs.close();
            statement.close();
            conn.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return medicalPrescriptions;
    }


}
