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.medicalinsurance.digitalreceipt.*;
import thyyxxk.webserver.entity.medicalinsurance.digitalreceipt.request.IndexInquiry;
import thyyxxk.webserver.entity.medicalinsurance.digitalreceipt.request.RxRevokeRequest;
import thyyxxk.webserver.entity.medicalinsurance.outpatient.MzPatientInfo;

import java.util.List;

@Mapper
public interface DigitalReceiptDao {

    @Select("select a.hosp_rxno,b.patn_name,b.prsc_dr_name, " +
            "a.prsc_time,state=isnull(c.rx_state,'NEW_RECEIPT') " +
            "from t_rx_receiptinfo a,t_rx_mdtrtinfo b " +
            "left join t_rx_precheck c on c.hosp_rxno=b.hosp_rxno " +
            "where a.hosp_rxno=b.hosp_rxno and a.prsc_time>=#{begintime} " +
            "and a.prsc_time<=#{endtime} order by a.prsc_time desc ")
    List<RxIndex> getRxIndex(IndexInquiry inquiry);

    @Select("select original_rx_file from t_rx_precheck where hosp_rxno=#{hospRxno}")
    String getRxFile(String hospRxno);

    @Select("select a.*,b.certno as mdtrtCertNo,b.insuplc_admdvs as insuPlcNo " +
            "from t_rx_receiptinfo a left join t_si_pat_info b on " +
            "b.pat_no=isnull(a.zy_no,a.pat_no) " +
            "and b.times=isnull(a.zy_times,a.times) " +
            "where hosp_rxno=#{hospRxno}")
    RxPreCheck getRxReceiptinfo(String hospRxno);

    @Select("select fixmedinsCode='H32132200561',fixmedinsName='沭阳铭和医院',a.*, " +
            "c.mdtrt_id,c.med_type,c.psn_no,c.psn_cert_type,c.psn_no,c.gend,c.certno " +
            "from t_rx_mdtrtinfo a, t_rx_receiptinfo b left join " +
            "t_si_pat_info c on " +
            "c.pat_no=isnull(b.zy_no,b.pat_no) " +
            "and c.times=isnull(b.zy_times,b.times) " +
            "where a.hosp_rxno=#{hospRxno} and a.hosp_rxno=b.hosp_rxno")
    RxMdtrtInfo getRxMdtrtinfo(String hospRxno);

    @Select("select * from t_rx_druginfo where hosp_rxno=#{hospRxno}")
    List<RxDrugDetail> getRxDruginfo(String hospRxno);

    @Select("select * from t_rx_diseinfo where hosp_rxno=#{hospRxno}")
    List<RxDiseInfo> getRxDiseinfo(String hospRxno);

    @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=#{pharDeptName}," +
            "phar_dept_code=#{pharDeptCode},phar_code=#{pharCode}," +
            "phar_name=#{pharName},phar_chk_time=getdate() " +
            "where hosp_rxno=#{hospRxno}")
    void updatePharInfo(RxUpload info);

    @Select("select fixmedinsCode='H32132200561',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 hosp_rxno=#{hospRxno}")
    RxUpload selectRxUploadForSign(String hospRxno);

    @Select("select original_rx_file from t_rx_precheck where hosp_rxno=#{hospRxno} ")
    String selectOriginRxFile(String hospRxno);

    @Select("select *,fixmedinsCode='H32132200561',fixmedinsName='沭阳铭和医院', " +
            "original_rx_file as rxFile from t_rx_precheck where hosp_rxno=#{hospRxno}")
    RxUpload selectFullRxUploadInfo(String hospRxno);
    
    @Select("select hi_rxno,fixmedinsCode='H32132200561', " +
            "dr_code,undoDrName=prsc_dr_name,undoDrCertType='01'," +
            "undoDrCertno=(select rtrim(max(d.social_no)) from a_employee_mi d where " +
            "d.yb_code=dr_code and isnull(d.del_flag,0)!='1'), " +
            "undoTime=getdate(),undoRea=N'处方有修改' " +
            "from t_rx_precheck where hosp_rxno=#{hospRxno}")
    RxRevoke selectRxRevoke(RxRevokeRequest request);

    @Select("select fixmedinsCode='H32132200561',hi_rxno, " +
            "mdtrt_id,patn_name as psnName,psn_cert_type,certno " +
            "from t_rx_precheck where hosp_rxno=#{hospRxno} ")
    RxInfoQuery selectRxInfoQuery(String hospRxno);

    @Select("select pat_no,times from t_rx_receiptinfo where hosp_rxno=#{hospRxno}")
    MzPatientInfo getBriefPatInfo(String hospRxno);
}
