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 "diag":
                iPage = deepinSiDiag(params.getMethod(), params.getContent(), iPage);
                break;
            case "surgery":
                iPage = deepinSurgery(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;
        }
        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;
        switch (method) {
            case "alpha":
                column = "py_code";
                break;
            case "code":
                column = "code";
                break;
            default:
                column = "name";
                break;
        }
        return dao.searchPhysician(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";
        }
        return dao.searchSiDiagnose(iPage, column, content);
    }

    private IPage<CodeName> deepinSurgery(String method, String content, IPage<CodeName> iPage) {
        content = '%' + content.toUpperCase() + '%';
        String column;
        switch (method) {
            case "alpha":
                column = "py_code";
                break;
            case "code":
                column = "code";
                break;
            default:
                column = "name";
        }
        return dao.searchSurgeries(iPage, column, content);
    }

    private IPage<CodeName> deepinIcd10Diag(String method, String content, IPage<CodeName> iPage) {
        content = '%' + content.toUpperCase() + '%';
        String column;
        switch (method) {
            case "alpha":
                column = "py_code";
                break;
            case "code":
                column = "code";
                break;
            default:
                column = "name";
        }
        return dao.searchIcd10Diagnose(iPage, 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;
        switch (method) {
            case "alpha":
                column = "py_code";
                break;
            case "code":
                column = "code";
                break;
            default:
                column = "name";
                break;
        }
        return dao.searchInjurydiag(iPage, column, content);
    }
}
