package thyyxxk.webserver.service.dictionary;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.dao.his.dictionary.HisWjwMatchDao;
import thyyxxk.webserver.entity.dictionary.HisWjwMatchEntity;
import thyyxxk.webserver.utils.StringUtil;
import thyyxxk.webserver.utils.TokenUtil;

import java.util.*;

@Slf4j
@Service
public class HisWjwMatchService {
    private final HisWjwMatchDao dao;

    @Autowired
    public HisWjwMatchService(HisWjwMatchDao dao) {
        this.dao = dao;
    }

    public Map<String, List<HisWjwMatchEntity>> selectMatchableDataByLabel(String label) {
        String histable, wjwtable;
        int type;
        switch (label) {
            case "department":
                histable = "zd_unit_code";
                wjwtable = "t_wjw_dept";
                type = 1;
                break;
            case "anaesthesia":
                histable = "zd_anaesthesia";
                wjwtable = "t_wjw_anaesthesia";
                type = 1;
                break;
            case "diagnose":
                histable = "zd_icd_code_new";
                wjwtable = "t_si_dl_dss_dns";
                type = 2;
                break;
            case "surgery":
                histable = "zd_icd9_cm3";
                wjwtable = "t_si_dl_oprtn";
                type = 3;
                break;
            case "si_diag":
                histable = "zd_icd_code_new";
                wjwtable = "zd_icd_code_new";
                type = 5;
                break;
            case "si_surgery":
                histable = "zd_icd9_cm3";
                wjwtable = "zd_icd9_cm3";
                type = 5;
                break;
            case "surgery_chargeCode":
            default:
                histable = "zd_icd9_cm3";
                wjwtable = "zd_charge_item";
                type = 4;
                break;
        }
        List<HisWjwMatchEntity> hisList;
        List<HisWjwMatchEntity> wjwList;
        if (type == 1) {
            hisList = dao.selectHisData(histable);
            wjwList = dao.selectWjwData(wjwtable);
        } else if (type == 5) {
            hisList = dao.selectYbData(histable);
            wjwList = dao.selectYbData(wjwtable);
        } else if (type == 4) {
            hisList = dao.selectSurgeries();
            wjwList = dao.selectChargeCodes();
            for (HisWjwMatchEntity entity : hisList) {
                if (null != entity.getWjwCode()) {
                    entity.setChargeCodeString(entity.getWjwCode());
                    entity.setChargeCode(entity.getWjwCode().split("\\^"));
                    entity.setWjwCode(null);
                }
                if (null != entity.getWjwName()) {
                    entity.setChargeNameString(entity.getWjwName());
                    entity.setChargeName(entity.getWjwName().split("\\^"));
                    entity.setWjwName(null);
                }
            }
        } else {
            hisList = dao.selectNationTempData(histable);
            wjwList = new ArrayList<>();
        }
        Map<String, List<HisWjwMatchEntity>> result = new HashMap<>();
        result.put("hisList", hisList);
        result.put("wjwList", wjwList);
        return result;
    }

    public List<HisWjwMatchEntity> fetchSimilarities(String label, String name, String key) {
        String table,codeColumn,nameColumn;
        if (label.equals("diagnose")) {
            table = "t_si_dl_dss_dns";
            codeColumn = "diagnosis_code";
            nameColumn = "diagnosis_name";
        } else {
            table = "t_si_dl_oprtn";
            codeColumn = "surgical_operation_code";
            nameColumn = "operation_operation_name";
        }
        List<HisWjwMatchEntity> list = dao.selectMedInsData(table, codeColumn, nameColumn, key);
        list.forEach(item -> {
            item.setSimilarity(StringUtil.getSimilarDegree(name, item.getName()));
        });
        list.sort(Comparator.comparingDouble(HisWjwMatchEntity::getSimilarity).reversed());
        return list;
    }

    public String executeMatchAction(HisWjwMatchEntity entity) {
        if (entity.getLabel().equals("surgery_chargeCode")) {
            if (null != entity.getChargeCode()) {
                StringBuilder codeBuilder = new StringBuilder();
                StringBuilder nameBuilder = new StringBuilder();
                for (String code : entity.getChargeCode()) {
                    codeBuilder.append(code).append('^');
                }
                if (codeBuilder.length() > 1) {
                    entity.setWjwCode(codeBuilder.substring(0, codeBuilder.length() - 1));
                }
                for (String name : entity.getChargeName()) {
                    nameBuilder.append(name).append('^');
                }
                if (nameBuilder.length() > 1) {
                    entity.setWjwName(nameBuilder.substring(0, nameBuilder.length() - 1));
                }
            }
            dao.updateDateAndOper(entity.getCode(), TokenUtil.getInstance().getTokenUserId());
        }
        dao.executeMatchAction(entity);
        log.info("【操作员：{}】匹配卫健委/国临字典：{}", TokenUtil.getInstance().getTokenUserId(), JSONObject.toJSONString(entity));
        return StringUtil.isBlank(entity.getWjwCode()) ? "撤销匹配成功" : "匹配成功";
    }

}
