package thyyxxk.webserver.dao.his.inpatient.casefrontsheet.tcmsheet;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.casefrontsheet.*;
import thyyxxk.webserver.entity.casefrontsheet.request.AdvanceSearch;
import thyyxxk.webserver.entity.casefrontsheet.request.SheetOverview;
import thyyxxk.webserver.entity.casefrontsheet.state.AuditState;
import thyyxxk.webserver.entity.casefrontsheet.tcmsheet.TcmFrontSheetDisdiag;
import thyyxxk.webserver.entity.casefrontsheet.tcmsheet.TcmFrontSheetMain;
import thyyxxk.webserver.entity.casefrontsheet.tcmsheet.TcmFrontSheetSurgery;
import thyyxxk.webserver.entity.covid.Region;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.dictionary.HisWjwMatchEntity;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface TcmFrontSheetMainDao extends BaseMapper<TcmFrontSheetMain> {
    @Select("select admiss_date from ${table} with(nolock) " +
            "where inpatient_no=#{patNo} and admiss_times=#{times}")
    Date getAdmDate(String table, String patNo, int times);

    @Select("select healthCardNo=rtrim(health_card_no),patNo=rtrim(inpatient_no),name=rtrim(name), " +
            "gender=rtrim(sex),country=rtrim(country),nation=rtrim(nation_code),phone=rtrim(home_tel), " +
            "certNo=rtrim(social_no),occupation=rtrim(occupation_code),marriage=rtrim(marry_code), " +
            "unitName=rtrim(employer_name),unitPlace=rtrim(employer_street),unitPhone=rtrim(employer_tel), " +
            "unitZipCode=rtrim(employer_zipcode),conName=rtrim(relation_name),conRel=rtrim(relation_code), " +
            "conPhone=(rtrim(relation_tel)) from a_patient_mi with(nolock) where inpatient_no=#{patNo}")
    TcmFrontSheetMain getAPatientMi(String patNo);

    @Select("select birth_date from a_patient_mi where inpatient_no=#{patNo}")
    Date getBirthday(String patNo);

    @Select("select times=#{times}," +
            "rtrim(small_dept) as admissDept,rtrim(admiss_ward) as admissWard," +
            "rtrim(zk_ward) as transDept,rtrim(zk_ward) as dismissDept,ward as dismissWard, " +
            "admissDeptName=(select rtrim(name) from zd_unit_code with(nolock) where code=small_dept), " +
            "admissWardName=(select rtrim(name) from zd_unit_code with(nolock) where code=admiss_ward), " +
            "transDeptName=(select rtrim(name) from zd_unit_code with(nolock) where code=zk_ward)," +
            "dismissDeptName=(select rtrim(name) from zd_unit_code with(nolock) where code=zk_ward)," +
            "dismissWardName=(select rtrim(name) from zd_unit_code with(nolock) where code=ward)," +
            "totalCost=total_charge,autopsy=rtrim(autopsy_mark), " +
            "selfPay=(select sum(last_balance) from zy_ledger_file with(nolock) " +
            "where inpatient_no=#{patNo} and admiss_times=#{times} and ledger_sn>0)," +
            "mzycy=rtrim(diagnose_conform1),ryycy=rtrim(diagnose_conform2)," +
            "sqysh=rtrim(diagnose_conform3),lcybl=rtrim(diagnose_conform4)," +
            "fsybl=rtrim(diagnose_conform5),pthDiagCode=rtrim(pathology_diag)," +
            "pthDiagName=rtrim(pathology_diag_comment) " +
            "from ${table} with(nolock) where inpatient_no=#{patNo} and admiss_times=#{times}")
    TcmFrontSheetMain getZyActPatient(String table, String patNo, int times);

    @Select("select payWay=rtrim(pay_method),infAge=rtrim(age_days),nbWeight1=rtrim(new_born_weight)," +
            "nbAdmWeight=rtrim(new_born_admiss_weight),zyAdmWay=rtrim(rytj)," +
            "conAddr=rtrim(lxdz),conName=rtrim(lxdz2),hpZipCode=rtrim(hkyb) " +
            "from batj_ba1 with(nolock) where bah=#{patNo} and zycs=#{times}")
    TcmFrontSheetMain getBatjBa1(String patNo, int times);

    @Select("select pthNo=rtrim(a.blzd_qph),allergy=rtrim(a.gm_flag),allergyMed=rtrim(a.gmwy)," +
            "bloodType=rtrim(a.xx),rh=rtrim(a.xx_rh),kzy=rtrim(isnull(kzr, b.dept_director))," +
            "zyfzyDoc=rtrim(isnull(zrys, b.dept_director)),mainDoc=rtrim(isnull(zzys, b.consult_physician))," +
            "admDoc=rtrim(isnull(zyys, b.refer_physician)),dutyNur=rtrim(a.job_nurse),jxDoc=rtrim(a.jxys)," +
            "sxDoc=rtrim(a.sxys),coder=rtrim(a.bmy),sheetQuality=rtrim(a.bazl),qltyCtrlDoc=rtrim(a.zkys)," +
            "kzyName=(select rtrim(name) from a_employee_mi with(nolock) where code=(isnull(kzr, b.dept_director)))," +
            "zyfzyDocName=(select rtrim(name) from a_employee_mi with(nolock) where code=(isnull(zrys, b.dept_director)))," +
            "mainDocName=(select rtrim(name) from a_employee_mi with(nolock) where code=(isnull(zzys, b.consult_physician)))," +
            "admDocName=(select rtrim(name) from a_employee_mi with(nolock) where code=isnull(zyys, b.refer_physician))," +
            "dutyNurName=(select rtrim(name) from a_employee_mi with(nolock) where code=a.job_nurse)," +
            "jxDocName=(select rtrim(name) from a_employee_mi with(nolock) where code=jxys)," +
            "sxDocName=(select rtrim(name) from a_employee_mi with(nolock) where code=sxys)," +
            "coderName=(select rtrim(name) from a_employee_mi with(nolock) where code=bmy)," +
            "qltyCtrlDocName=(select rtrim(name) from a_employee_mi with(nolock) where code=zkys)," +
            "qltyCtrlNurse=rtrim(a.zkhs),zyDismissWay=rtrim(a.lyfs),dismissDestination=rtrim(a.jsjg)," +
            "qltyCtrlNurseName=(select rtrim(name) from a_employee_mi with(nolock) where code=zkhs)," +
            "admissAgain=rtrim(a.zzy_flag),admissAgainPurpose=rtrim(a.zzy_md)," +
            "comaDaysBeforeAdmiss=rtrim(a.ryq_hmsj_day),comaHoursBeforeAdmiss=rtrim(a.ryq_hmsj_hour)," +
            "comaMinutesBeforeAdmiss=rtrim(a.ryq_hmsj_min),comaDaysAfterAdmiss=rtrim(a.ryh_hmsj_day)," +
            "comaHoursAfterAdmiss=rtrim(a.ryh_hmsj_hour),comaMinutesAfterAdmiss=rtrim(a.ryh_hmsj_min)," +
            "rescueTimes=rtrim(a.qjcs),rescueSuccessTimes=rtrim(a.cgcs)," +
            "hurtReasonName=rtrim(a.hurt_reason_name),hurtReasonCode=rtrim(a.hurt_reason_code) " +
            "from batj_ba2 a with(nolock), ${table} b with(nolock) where " +
            "b.inpatient_no=a.bah and a.bah=#{patNo} and a.zycs=#{times}")
    TcmFrontSheetMain getBatjBa2(String table, String patNo, int times);

    @Select("select barq from batj_ba2 with(nolock ) where bah=#{patNo} and zycs=#{times}")
    Date getZkrq(String patNo, int times);

    @Select("select c.tcm_sheet_column as code,sum(a.charge_fee) as name " +
            "from zy_detail_charge a,zd_charge_item b,zd_charge_ba_class c " +
            "where inpatient_no=#{patNo} and admiss_times=#{times} " +
            "and a.charge_code_mx=b.code " +
            "and b.zd_charge_ba_class=c.code " +
            "group by c.tcm_sheet_column " +
            "union " +
            "select case when charge_code='BILL01' then 'westMedicineFee' when charge_code='BILL28' " +
            "then 'zcyFee' else 'herbalFee' end sheet_column, " +
            "SUM(charge_fee) as name " +
            "from zy_detail_charge " +
            "where inpatient_no=#{patNo} and admiss_times=#{times} and charge_code in ('BILL01','BILL02','BILL28') " +
            "group by charge_code")
    List<CodeName> selectPatCharges(String patNo, int times);

    @Select("select sortNo=rtrim(dis_diag_no), tcmName=rtrim(dis_diag_comment), " +
            "tcmCode=rtrim(dis_diag), tcmAdmStatus=rtrim(admiss_status) " +
            "from ba_first_page1 with(nolock) " +
            "where inpatient_no=#{patNo} and admiss_times=#{times}")
    List<TcmFrontSheetDisdiag> getDisdiags(String patNo, int times);

    @Select("select sortNo=rtrim(ssxh),name=rtrim(ssmc),date=ssrq,level=rtrim(ssjb), " +
            "code=rtrim(ssbm),operCode=rtrim(ssys),anestorCode=rtrim(mzys), " +
            "operName=(select rtrim(name) from a_employee_mi with(nolock) where code=ssys)," +
            "assistantCode1=rtrim(sszs1),assistantCode2=rtrim(sszs2), " +
            "assistantName1=(select rtrim(name) from a_employee_mi with(nolock) where code=sszs1)," +
            "assistantName2=(select rtrim(name) from a_employee_mi with(nolock) where code=sszs2)," +
            "cut=rtrim(qkjb),heal=rtrim(yhqk),anaesthesia=rtrim(mzff)," +
            "anestorName=(select rtrim(name) from a_employee_mi with(nolock) where code=mzys) " +
            "from batj_ba4 with(nolock) where bah=#{patNo} and zycs=#{times} order by ssxh")
    List<TcmFrontSheetSurgery> getSurgeries(String patNo, int times);

    @Select("select * from tcm_front_sheet_disdiag where sheet_id=#{sheetId} ")
    List<TcmFrontSheetDisdiag> selectSignedSheetDiags(String sheetId);

    @Select("select * from tcm_front_sheet_surgery where sheet_id=#{sheetId} ")
    List<TcmFrontSheetSurgery> selectSignedSheetSurgeries(String sheetId);
}
