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.inpatient.FeeDtle;
import thyyxxk.webserver.entity.medicalinsurance.inpatient.SiChargeTemp;
import thyyxxk.webserver.entity.medicalinsurance.outpatient.MzPatientInfo;

import java.util.List;

@Mapper
public interface EmergencyRescueDao {

    @Select("select count(1) from t_mt_receipt where patient_id=#{patNo} and times=#{times}")
    int checkGeneratedFees(MzPatientInfo mzptnt);

    @Select("select code=b.si_caty,name=b.si_caty_name from mz_visit_table a,zd_unit_code b " +
            "where patient_id=#{patNo} and times=#{times} and b.code=a.visit_dept_code")
    CodeName getSiCaty(String patNo, int times);
    
    @Select("select item_no as feedetlSn,order_no as drordNo,quantity as cnt, " +
            "input_date as feeOcurTime,his_item_code as medinsListCodg, " +
            "charge_fee as detItemFeeSumamt,pric=charge_fee/quantity, " +
            "tcmdrugUsedWay=(case when medi_item_type=2 then '1' else '' end), " +
            "hosp_appr_flag,bilgDrName=input_man, " +
            "mdtrtId=(select mdtrt_id from t_si_pat_info b where b.pat_no=patient_id and b.times=a.times), " +
            "psnNo=(select psn_no from t_si_pat_info b where b.pat_no=patient_id and b.times=a.times), " +
            "medType=(select med_type from t_si_pat_info b where b.pat_no=patient_id and b.times=a.times), " +
            "medListCodg=case when a.medi_item_type=0 then  " +
            "(select isnull(national_code_27, national_code) from zd_charge_item where code=his_item_code) else  " +
            "(select max(national_code) from yp_zd_dict where code=his_item_code) end, " +
            "bilgDrCodg=(select rtrim(yb_code) from a_employee_mi where code=input_staff) " +
            "from t_mt_receipt a where patient_id=#{patNo} and times=#{times} and yb_trans_flag=0")
    List<FeeDtle> getFeeList(String patNo, int times);

    @Update("<script>" +
            "update t_mt_receipt set yb_trans_flag=1 where patient_id=#{patNo} and " +
            "times=#{times} and item_no in " +
            "<foreach collection='tempList' item='fee' separator=',' open='(' close=')'>" +
            "#{fee.feedetlSn}</foreach>" +
            "</script>")
    void updateTransFlag(String patNo, Integer times, List<SiChargeTemp> tempList);

    @Select("select rtrim(social_no) from mz_patient_mi where patient_id=#{patNo}")
    String getMzSocialNo(String patNo);
}
