package com.lantone.qc.kernel;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.lantone.qc.kernel.model.ImportDiag;
import com.lantone.qc.pub.util.FastJsonUtils;
import com.lantone.qc.pub.util.PropertiesUtil;

import java.sql.*;
import java.util.*;

/**
 * @ClassName : DataTest
 * @Description :
 * @Author : 楼辉荣
 * @Date: 2020-04-01 20:06
 */
public class DataTest {
    public static void main(String[] args ) {

    }

    public static List<Map<String, Object>> loadHomePage(String hospitalId, String tableName, String patientId) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        List<Map<String, Object>> resultList = null;
        try {
            conn = MysqlJdbcUtil.getConnection(hospitalId);
            stmt = conn.createStatement();

            DatabaseMetaData dmd = conn.getMetaData();
            ResultSet dmdRs = dmd.getColumns(null, null, tableName, null);

            Map<String, String> colMap = new LinkedHashMap<>();
            while(dmdRs.next()){
                colMap.put(dmdRs.getString(4), dmdRs.getString("REMARKS"));
            }
            dmdRs.close();

            rs = stmt.executeQuery("select * from " + tableName + " where brzyid = '" + patientId + "'");
            resultList = resultSetToJson(rs, colMap);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                    rs = null;
                }
                if (stmt != null) {
                    stmt.close();
                    stmt = null;
                }
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return resultList;
    }

    public static List<Map<String, String>> loadHomePageDiagnose(String hospitalId, String tableName, String zdlbdm, String patientId) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        List<Map<String, String>> resultList = new ArrayList<>();
        try {
            conn = MysqlJdbcUtil.getConnection(hospitalId);
            stmt = conn.createStatement();
            if (zdlbdm.equals("1")) {
                zdlbdm = "'门诊诊断'";
            } else if (zdlbdm.equals("2")) {
                zdlbdm = "'主要诊断','其他诊断'";
            } else if (zdlbdm.equals("3")) {
                zdlbdm = "'损伤中毒诊断'";
            } else if (zdlbdm.equals("4")) {
                zdlbdm = "'病理诊断'";
            }
            rs = stmt.executeQuery("select zdjbmc, icdm from " + tableName + " where zdlbdm in (" + zdlbdm + ") and basyid = '" + patientId + "' order by zdlbdm desc");
            while (rs.next()) {
                Map<String, String> map = new LinkedHashMap<>();
                map.put("诊断名称", rs.getString(1));
                map.put("诊断编码", rs.getString(2));
                resultList.add(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                    rs = null;
                }
                if (stmt != null) {
                    stmt.close();
                    stmt = null;
                }
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return resultList;
    }

    public static List<Map<String, String>> loadHomePageOperation(String hospitalId, String tableNameOperation, String tableNameUser, String patientId) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        List<Map<String, String>> resultList = new ArrayList<>();
        try {
            conn = MysqlJdbcUtil.getConnection(hospitalId);
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT r.BASYID, r.brssxh, r.BRSSRQ, r.BRSSMC, SSDMID, r.QKDJDM, r.YHDJDM, SSYSID, \n" +
                    "U1.YHRYMC, r.YZHSID, u3.yhrymc, r.MZYSID, u2.yhrymc, r.MZFFMC  FROM " + tableNameOperation + " r\n" +
                    "left join " + tableNameUser + " u1 on r.SSYSID = u1.YHRYDM\n" +
                    "left join " + tableNameUser + " u2 on r.mzysid = u2.YHRYDM\n" +
                    "left join " + tableNameUser + " u3 on r.yzhsid = u3.YHRYDM\n" +
                    "where r.BASYID = '" + patientId + "' " +
                    "order by r.BASYID, r.brssxh");
            while (rs.next()) {
                Map<String, String> map = new LinkedHashMap<>();
                map.put("手术日期", rs.getString(3));
                map.put("手术名称", rs.getString(4));
                map.put("手术编码", rs.getString(5));
                map.put("切口等级", rs.getString(6));
                map.put("愈合等级", rs.getString(7));
                map.put("术者", rs.getString(9));
                map.put("一助", rs.getString(11));
                map.put("麻醉医师", rs.getString(13));
                map.put("麻醉方式", rs.getString(14));
                resultList.add(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                    rs = null;
                }
                if (stmt != null) {
                    stmt.close();
                    stmt = null;
                }
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return resultList;
    }

    public static List<Map<String, Object>> resultSetToJson(ResultSet rs, Map<String, String> map) throws SQLException, JSONException {
        // 获取列数
        ResultSetMetaData metaData = (ResultSetMetaData) rs.getMetaData();
        int columnCount = metaData.getColumnCount();
        List<Map<String, Object>> resultList = new ArrayList<>();
        while (rs.next()) {
            // 遍历ResultSet中的每条数据
            Map<String, Object> result = new LinkedHashMap<>();
            // 遍历每一列
            for (int i = 1; i <= columnCount; i++) {
                String columnName = metaData.getColumnLabel(i);
                String value = rs.getString(columnName) == null ? "" : rs.getString(columnName);
                result.put(map.get(columnName), value);
            }
            resultList.add(result);
        }
        return resultList;
    }
}