package thyyxxk.webserver.service.search;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.search.SearchDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.dictionary.SearchDataParam;
import thyyxxk.webserver.utils.ResultVoUtil;

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

@Service
public class SearchService {
    private final SearchDao dao;

    @Autowired
    public SearchService(SearchDao dao) {
        this.dao = dao;
    }

    public ResultVo<Map<String, Object>> execute(SearchDataParam params) {
        IPage<CodeName> iPage = new Page<>(params.getPage(), 10);
        switch (params.getTarget()) {
            case "physician":
                iPage = deepinPhysician(params.getMethod(), params.getContent(), iPage);
                break;
            case "icd10":
                iPage = deepinIcd10Diag(params.getMethod(), params.getContent(), iPage);
                break;
            case "tcmDiag":
                iPage = deepinTcmDiag(params.getZyType(), params.getMethod(), params.getContent(), iPage);
                break;
            case "diag":
                iPage = deepinSiDiag(params.getMethod(), params.getContent(), iPage);
                break;
            case "surgery":
                iPage = deepinYbSurgery(params.getMethod(), params.getContent(), iPage);
                break;
            case "slwspcdiag":
                iPage = deepinSlwSpcDiag(params.getContent(), params.getPerstype(), iPage);
                break;
            case "injurydiag":
                iPage = deepinInjuryDiag(params.getMethod(), params.getContent(), iPage);
                break;
            case "SSCZ":
                iPage = deepinIcd9Cm3(params.getMethod(), params.getContent(), iPage);
                break;
            case "JY":
                iPage = deepinJcJy(params.getMethod(), params.getContent(), "jy_zd_item", iPage);
                break;
            case "JC":
                iPage = deepinJcJy(params.getMethod(), params.getContent(), "jc_zd_item", iPage);
                break;
            case "ZL":
                iPage = deepinZlItems(params.getMethod(), params.getContent(), iPage);
                break;
            case "dept":
                iPage = deepinDept(params.getMethod(), params.getContent(), iPage);
                break;
            case "hurtReason":
                iPage = deepinHurtReason(params.getMethod(), params.getContent(), iPage);
                break;
            case "pthDiag":
                iPage = deepinPthDiag(params.getMethod(), params.getContent(), iPage);
                break;
        }
        if (iPage.getTotal() > 0) {
            Map<String, Object> map = new HashMap<>();
            map.put("list", iPage.getRecords());
            map.put("totalSize", iPage.getTotal());
            return ResultVoUtil.success(map);
        }
        return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
    }

    private IPage<CodeName> deepinPhysician(String method, String content, IPage<CodeName> iPage) {
        content = '%' + content.toUpperCase() + '%';
        String column = filterCommonColumn(method);
        return dao.searchPhysician(iPage, column, content);
    }

    private IPage<CodeName> deepinDept(String method, String content, IPage<CodeName> iPage) {
        content = '%' + content.toUpperCase() + '%';
        String column = filterCommonColumn(method);
        return dao.searchDept(iPage, column, content);
    }

    private IPage<CodeName> deepinIcd9Cm3(String method, String content, IPage<CodeName> iPage) {
        content = '%' + content.toUpperCase() + '%';
        String column = filterCommonColumn(method);
        return dao.searchIcd9Cm3(iPage, column, content);
    }

    private IPage<CodeName> deepinJcJy(String method, String content, String table, IPage<CodeName> iPage) {
        content = '%' + content.toUpperCase() + '%';
        String column = filterCommonColumn(method);
        return dao.searchJcJyItem(iPage, column, content, table);
    }

    private IPage<CodeName> deepinZlItems(String method, String content, IPage<CodeName> iPage) {
        content = '%' + content.toUpperCase() + '%';
        String column = filterCommonColumn(method);
        return dao.searchZlItems(iPage, column, content);
    }

    private IPage<CodeName> deepinSiDiag(String method, String content, IPage<CodeName> iPage) {
        content = '%' + content.toUpperCase() + '%';
        String column;
        switch (method) {
            case "alpha":
                column = "yb_py_code";
                break;
            case "code":
                column = "yb_code";
                break;
            default:
                column = "yb_name";
                break;
        }
        return dao.searchSiDiagnose(iPage, column, content);
    }

    private IPage<CodeName> deepinYbSurgery(String method, String content, IPage<CodeName> iPage) {
        content = '%' + content.toUpperCase() + '%';
        String column = filterCommonColumn(method);
        return dao.searchYbSurgeries(iPage, column, content);
    }

    private IPage<CodeName> deepinIcd10Diag(String method, String content, IPage<CodeName> iPage) {
        content = '%' + content.toUpperCase() + '%';
        String column = filterCommonColumn(method);
        return dao.searchIcd10Diagnose(iPage, column, content);
    }

    private IPage<CodeName> deepinTcmDiag(String type, String method, String content, IPage<CodeName> iPage) {
        content = '%' + content.toUpperCase() + '%';
        String column;
        switch (method) {
            case "alpha":
                column = "py_code";
                break;
            case "code":
                column = "class_code";
                break;
            default:
                column = "sick_name";
                break;
        }
        String table = type.equals("B") ? "zy_jb_name_class" : "zy_zh_name_class";
        return dao.searchTcmDiag(iPage, table, column, content);
    }

    private IPage<CodeName> deepinSlwSpcDiag(String content, Integer perstype, IPage<CodeName> iPage) {
        content = "%" + content + "%";
        if (null == perstype) {
            perstype = 1;
        }
        String tableName = perstype == 1 ? "si_icd_employees_outpatient_chronic_idiopathic_disease"
                : "si_icd_residents_outpatient_chronic_idiopathic_disease";
        return dao.selectSlwSpcDiag(iPage, content, tableName);
    }

    private IPage<CodeName> deepinInjuryDiag(String method, String content, IPage<CodeName> iPage) {
        content = '%' + content.toUpperCase() + '%';
        String column = filterCommonColumn(method);
        return dao.searchInjurydiag(iPage, column, content);
    }

    private IPage<CodeName> deepinHurtReason(String method, String content, IPage<CodeName> iPage) {
        content = '%' + content.toUpperCase() + '%';
        String column = filterCommonColumn(method);
        return dao.searchHurtReason(iPage, column, content);
    }

    private IPage<CodeName> deepinPthDiag(String method, String content, IPage<CodeName> iPage) {
        content = '%' + content.toUpperCase() + '%';
        String column = filterCommonColumn(method);
        return dao.searchPthDiag(iPage, column, content);
    }

    private String filterCommonColumn(String method) {
        switch (method) {
            case "alpha":
                return "py_code";
            case "code":
                return "code";
            default:
                return "name";
        }
    }
}
