package com.docus.server.archive.converter;

import com.docus.core.util.Func;
import com.docus.server.archive.entity.TBasic;
import com.docus.server.archive.utils.PingYinUtil;
import com.docus.server.fsy.entity.JswzhPatientInfoView;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author YongBin Wen
 * @date 2024/3/27 14:52
 */
public class PatientInfoConverter {
    private final Map<String, String> userNameMap;
    private final Map<String, String> deptCodeNameMap;

    public PatientInfoConverter(Map<String, String> userNameMap, Map<String, String> deptCodeNameMap) {
        this.deptCodeNameMap = deptCodeNameMap;
        this.userNameMap = userNameMap;
    }

    public TBasic convertArchiveBasicInfo(JswzhPatientInfoView view) {
        if (view == null) {
            return null;
        }
        Objects.requireNonNull(userNameMap);
        Objects.requireNonNull(deptCodeNameMap);
        Map<String, Integer> ageMap = parseAge(view.getNL());

        TBasic tBasic = new TBasic();
        tBasic.setJzh(view.getJZH());
        tBasic.setInpatientNo(view.getZY());
        tBasic.setAdmissTimes(view.getZYCS());
        tBasic.setName(view.getXM());
        tBasic.setNameSpell(getPinyinFirstSpell(view.getXM()));
        tBasic.setSex(sex(view.getXB()));
        tBasic.setSexName(sexName(view.getXB()));
        tBasic.setAdmissDate(view.getRYRQ());
        tBasic.setAdmissDept(view.getRYKSBQ());
        tBasic.setAdmissDeptName(view.getRYKSMC());
        tBasic.setBedNo(view.getCWDM());
        tBasic.setAttending(view.getDQYS());
        tBasic.setAttendingName(userNameMap.get(view.getDQYS()));
        tBasic.setAge(ageMap.get("age"));
        tBasic.setAgeMonth(ageMap.get("ageMonth"));
        tBasic.setAgeDay(ageMap.get("ageDay"));
        tBasic.setIdCard(view.getSFZH());
        tBasic.setTelphone(view.getLXDH());
        tBasic.setDisDate(view.getCYRQ());
        tBasic.setDisDept(view.getDQBQ());
        tBasic.setDisDeptName(view.getDQKS());
        tBasic.setTotalCost(view.getZJINE());
        tBasic.setIsDead(view.getSFSW());
        tBasic.setJzCardNo(view.getJZKH());
        tBasic.setFileSource(1);
        return tBasic;
    }

    public static String getPinyinFirstSpell(String str) {
        try {
            return PingYinUtil.getFirstSpell(str);
        }catch (Exception ex){
            return null;
        }
    }

    /**
     * age
     * ageMonth
     * ageDay
     */
    public static Map<String, Integer> parseAge(String nl) {
        int age = 0;
        int ageMonth = 0;
        int ageDay = 0;
        if (Func.isNotBlank(nl)) {
            String[] split = nl.split("[岁月天]");
            int length = split.length;
            // 岁月天都有
            if (length == 3) {
                age = Integer.parseInt(split[0]);
                ageMonth = Integer.parseInt(split[1]);
                ageDay = Integer.parseInt(split[2]);
            }

            if (length == 2) {
                // 情况一：岁月 情况二：月天
                if (nl.contains("岁")) {
                    age = Integer.parseInt(split[0]);
                    ageMonth = Integer.parseInt(split[1]);
                } else {
                    ageMonth = Integer.parseInt(split[0]);
                    ageDay = Integer.parseInt(split[1]);
                }
            }
            if (length == 1) {
                // 包含哪个是哪个
                if (nl.contains("岁")) {
                    age = Integer.parseInt(split[0]);
                } else if (nl.contains("月")) {
                    ageMonth = Integer.parseInt(split[0]);
                } else if (nl.contains("天")) {
                    ageDay = Integer.parseInt(split[0]);
                }
            }
        }
        HashMap<String, Integer> map = new HashMap<>();
        map.put("age", age);
        map.put("ageMonth", ageMonth);
        map.put("ageDay", ageDay);
        return map;
    }

    public static void main(String[] args) {
        System.out.println(parseAge("1天"));
        System.out.println(parseAge("2月1天"));
        System.out.println(parseAge("9岁8月"));
        System.out.println(parseAge("5岁4月3天"));
    }

    public static String sex(String sexCode) {
        return "M".equalsIgnoreCase(sexCode) ? "1" : "2";
    }

    public static String sexName(String sexCode) {
        return "M".equalsIgnoreCase(sexCode) ? "男" : "女";
    }
}
