package thyyxxk.webserver.service.zhuyuanyisheng;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.dao.his.zhuyuanyisheng.PatientInfoQueryDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.datamodify.GetDropdownBox;
import thyyxxk.webserver.entity.inpatient.patient.Patient;
import thyyxxk.webserver.entity.zhuyuanyisheng.patInfoquery.PatInfoQueryParam;
import thyyxxk.webserver.utils.ListUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;

import java.io.PushbackInputStream;
import java.util.List;

@Service
@Slf4j
public class PatientInfoQueryService {

    private final PatientInfoQueryDao dao;

    @Autowired
    public PatientInfoQueryService(PatientInfoQueryDao dao) {
        this.dao = dao;
    }

    public ResultVo<List<GetDropdownBox>> getHospWardAndDept() {
        return ResultVoUtil.success(dao.getHospWardAndDept());
    }

    public ResultVo<List<GetDropdownBox>> diagnosis(String code) {
        return ResultVoUtil.success(dao.selectDiagnsis(StringUtil.isContainChinese(code)));
    }

    /**
     * @return 获取手术
     */
    public ResultVo<List<GetDropdownBox>> getSurgery(String code) {
        return ResultVoUtil.success(dao.selectSurgery(StringUtil.isContainChinese(code)));
    }

    public ResultVo<List<GetDropdownBox>> employeeList(String code) {
        return ResultVoUtil.success(dao.employeeList(StringUtil.isContainChinese(code)));
    }


    public ResultVo<List<Patient>> getPatientInfo(PatInfoQueryParam param) {
        log.info("查询患者信息：{}", JSON.toJSONString(param));
        QueryWrapper<?> qw = new QueryWrapper<>();

        if (StringUtil.notBlank(param.getPatNo())) {
            qw.eq("a.inpatient_no", param.getPatNo());
        }
        if (StringUtil.notBlank(param.getName())) {
            qw.like("a.name", param.getName());
        }
        if (ListUtil.notBlank(param.getAdmissionTime())) {
            qw.ge("a.admiss_date", param.getAdmissionTime().get(0));
            qw.le("a.admiss_date", param.getAdmissionTime().get(1));
        }
        if (ListUtil.notBlank(param.getDischargeTime())) {
            qw.ge("a.dis_date", param.getDischargeTime().get(0));
            qw.le("a.dis_date", param.getDischargeTime().get(1));
        }
        if (StringUtil.notBlank(param.getDept())) {
            qw.eq("isnull(a.small_dept, a.zk_dept)", param.getDept());
        }
        if (StringUtil.notBlank(param.getWard())) {
            qw.eq("a.ward", param.getWard());
        }
        if (StringUtil.notBlank(param.getDiagnosticCode())) {
            if (param.getIsPrimaryDiagnosis()) {
                qw.gt(String.format("(select count(1) from t_case_frontsheet_disdiag  where a.inpatient_no = t_case_frontsheet_disdiag.bah and a.admiss_times = t_case_frontsheet_disdiag.times " +
                        " and code = '%s' and no = 1 )", param.getDiagnosticCode()), 0);
            } else {
                qw.gt(String.format("(select count(1) from t_case_frontsheet_disdiag where a.inpatient_no = t_case_frontsheet_disdiag.bah and a.admiss_times = t_case_frontsheet_disdiag.times " +
                        " and code = '%s')", param.getDiagnosticCode()), 0);
            }
        }
        if (StringUtil.notBlank(param.getEmpCode())) {
            qw.eq("a.refer_physician", param.getEmpCode());
        }
        if (StringUtil.notBlank(param.getDisDept())) {
            qw.eq("a.dis_dept", param.getDisDept());
        }

        if (param.getEmrAudit().equals(1)) {
            qw.gt("(select count(1) from emr_audit_detail emr where pat_no = a.inpatient_no and emr.times = a.admiss_times)", 0);
        }
        if (param.getEmrAudit().equals(2)) {
            qw.eq("(select count(1) from emr_audit_detail emr where pat_no = a.inpatient_no and emr.times = a.admiss_times)", 0);
        }

        List<Patient> list = dao.getPatInfo(qw, param.getLeaveHospital() ? "zy_inactpatient" : "zy_actpatient");
        return ResultVoUtil.success(list);
    }


}
