package thyyxxk.webserver.dao.his.medicalinsurance;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.medicalinsurance.digitalreceipt.*;
import thyyxxk.webserver.entity.medicalinsurance.digitalreceipt.request.RxRevokeRequest;

import java.util.List;

@Mapper
public interface DigitalReceiptDao {

    @Select("select visit_date as prscTime, " +
            "hospRxno=rtrim(patient_id)+'_'+cast(times as varchar)+'_1' " +
            "from mz_visit_table where patient_id=#{patientId} and times=#{times}")
    RxPreCheck selectRxPreCheck(String patientId, int times);

    @Select("select fixmedinsCode='H43010500370',fixmedinsName='长沙泰和医院', " +
            "mdtrt_id,med_type,iptOtpNo=pat_no,psn_no,psn_name as patnName,psn_cert_type, " +
            "certno,age as patnAge,gend, " +
            "spDiseFlag=case when med_type='14' then '1' else '0' end, " +
            "prscDeptName=(select d.name from zd_unit_code d where d.code=b.visit_dept_code), " +
            "prscDeptCode=rtrim(b.visit_dept_code), " +
            "drCode=(select d.yb_code from a_employee_mi d where d.code=b.doctor_code), " +
            "prscDrName=(select rtrim(d.name) from a_employee_mi d where d.code=b.doctor_code), " +
            "drProfttlCodg=(select rtrim(d.emp_tit_code) from a_employee_mi d where d.code=b.doctor_code), " +
            "drDeptCode=(select rtrim(d.dept_code) from a_employee_mi d where d.code=b.doctor_code), " +
            "caty=(select d.si_caty from zd_unit_code d where d.code=b.visit_dept_code), " +
            "mdtrtTime=b.visit_date,maindiagCode=b.icd_code_new,maindiagName=b.icd_text_new " +
            "from t_si_pat_info a,mz_visit_table b " +
            "where a.pat_no=#{patientId} and a.times=#{times} and a.ledger_sn=0 " +
            "and a.pat_no=b.patient_id and a.times=b.times")
    RxMdtrtInfo selectRxMdtrtInfo(String patientId, int times);

    @Select("select diag_code as code,diag_name as name from t_si_mz_diag " +
            "where pat_no=#{patientId} and times=#{times} and diag_srt_no=1")
    CodeName selectMaindiag(String patientId, int times);

    @Select("select " +
            "medListCodg=case when medi_item_type=0 then " +
            "    (select d.national_code from zd_charge_item d where d.code=a.his_item_code) " +
            "else (select max(d.national_code) from yp_zd_dict d where d.code=a.his_item_code) end, " +
            "fixmedinsHilistId=a.his_item_code,rxItemTypeCode=med_chrgitm_type,drugGenname=a.his_item_name, " +
            "drugDosform=case when medi_item_type=0 then '' else " +
            "(select d.name from yp_zd_dosage d where d.code= " +
            "(select max(e.dosage) from yp_zd_dict e where e.code=a.his_item_code)) end, " +
            "drugSpec=case when medi_item_type=0 then '' else " +
            "(select max(d.specification) from yp_zd_dict d where d.code=a.his_item_code) end, " +
            "sinDosunt=a.drug_unit, " +
            "medcWayCodg=case when medi_item_type=0 then '' else 1 end, " +
            "medcWayDscr=case when medi_item_type=0 then '' else N'口服' end, " +
            "drugCnt=quantity,medcBegntime=a.fee_date,medcEndtime=a.fee_date+3,medcDays=3, " +
            "sinDoscnt=1,usedFrquCodg=12,usedFrquName='BID',hospApprFlag='1' " +
            "from t_mt_receipt a where a.patient_id=#{patientId} and a.times=#{times}")
    List<RxDrugDetail> selectRxDrugDetailList(String patientId, int times);

    @Select("select " +
            "diag_type,diag_srt_no,diag_code,diag_name,diag_dept, " +
            "maindiagFlag=case when diag_srt_no=1 then '1' else '0' end, " +
            "diagDrNo=(select d.yb_code from a_employee_mi d where d.code=a.dise_dor_no), " +
            "diagDrName=dise_dor_name,diag_time from t_si_mz_diag a " +
            "where a.pat_no=#{patientId} and a.times=#{times} order by diag_srt_no")
    List<RxDiseInfo> selectRxDiseInfo(String patientId, int times);

    @Select("select *,original_rx_file as rxFile from t_rx_precheck ")
    List<RxUpload> getRxForAuditing();

    @Select("select pharDeptName=rtrim(b.name),pharDeptCode=rtrim(a.dept_code), " +
            "pharCode=a.yb_code,pharName=rtrim(a.name) from a_employee_mi a,zd_unit_code b " +
            "where a.code=#{code} and b.code=a.dept_code")
    RxUpload selectPharInfo(String code);

    @Update("update t_rx_precheck set phar_dept_name=#{info.pharDeptName}," +
            "phar_dept_code=#{info.pharDeptCode},phar_code=#{info.pharCode}," +
            "phar_name=#{info.pharName},phar_chk_time=getdate() " +
            "where patient_id=#{patientId} and times=#{times}")
    void updatePharInfo(String patientId, int times, RxUpload info);

    @Select("select fixmedinsCode='H43010500370',fixmedinsName='长沙泰和医院'," +
            "hi_rxno, rx_trace_code, mdtrt_id, patn_name, psn_cert_type, " +
            "certno, dr_code, prsc_dr_name, phar_dept_code, phar_dept_name, " +
            "phar_code, phar_name, phar_chk_time from t_rx_precheck " +
            "where patient_id=#{patientId} and times=#{times}")
    RxUpload selectRxUploadForSign(String patientId, int times);

    @Select("select original_rx_file from t_rx_precheck " +
            "where patient_id=#{patientId} and times=#{times}")
    String selectOriginRxFile(String patientId, int times);

    @Select("select *,fixmedinsCode='H43010500370',fixmedinsName='长沙泰和医院', " +
            "original_rx_file as rxFile " +
            "from t_rx_precheck where patient_id=#{patientId} and times=#{times}")
    RxUpload selectFullRxUploadInfo(String patientId, int times);
    
    @Select("select " +
            "a.hi_rxno,fixmedinsCode='H43010500370', " +
            "drCode=b.yb_code,undoDrName=rtrim(b.name), " +
            "undoDrCertType='01',undoDrCertno=rtrim(b.social_no), " +
            "undoTime=getdate(),undoRea=N'处方有修改' " +
            "from t_rx_precheck a, a_employee_mi b " +
            "where a.patient_id=#{patientId} and a.times=#{times} " +
            "and b.code=#{staffId}")
    RxRevoke selectRxRevoke(RxRevokeRequest request);

    @Select("select fixmedinsCode='H43010500370',hi_rxno, " +
            "mdtrt_id,patn_name as psnName,psn_cert_type,certno " +
            "from t_rx_precheck where patient_id=#{patientId} and times=#{times}")
    RxInfoQuery selectRxInfoQuery(String patientId, int times);
}
