package thyyxxk.webserver.dao.his.medicalinsurance;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import thyyxxk.webserver.entity.medicalinsurance.hospitalapproval.PatientFeeItem;

import java.util.List;

@Mapper
public interface HospitalApprovalDao {

    @Select("select rtrim(inpatient_no) as patNo,admiss_times as times,ledger_sn,ori_detail_sn, " +
            "detail_sn,charge_code_mx,charge_amount,charge_fee,charge_date, " +
            "charge_name=case when serial in ('', '00') then (select top 1 rtrim(name) from " +
            "zd_charge_item where code=charge_code_mx) " +
            "else (select top 1 rtrim(name) from yp_zd_dict where code=charge_code_mx) end, " +
            "applyStatus=(select t.apply_status from t_hospital_approval_apply t where t.pat_no=#{patNo} " +
            "and t.times=#{times} and t.ledger_sn=#{ledgerSn} and t.detail_sn=a.detail_sn) " +
            "from zy_detail_charge a where inpatient_no=#{patNo} and admiss_times=#{times} and " +
            "ledger_sn=#{ledgerSn} and isnull(trans_flag_yb,0)!=2 and isnull(infant_flag,0)=0 and " +
            "charge_amount>0 " )
    List<PatientFeeItem> getPatientFeeList(String patNo, Integer times, Integer ledgerSn);

    @Select("select * from t_hospital_approval_apply where pat_no=#{patNo} and " +
            "times=#{times} and ledger_sn=#{ledgerSn} and detail_sn=#{detailSn}")
    PatientFeeItem selectAccurateApply(String patNo, Integer times, Integer ledgerSn, Integer detailSn);

    @Select("select * from t_hospital_approval_apply where " +
            "pat_no=#{patNo} and times=#{times} and ledger_sn=#{ledgerSn} ")
    List<PatientFeeItem> selectPatientApplies(String patNo, Integer times, Integer ledgerSn);
    
    @Insert("insert into t_hospital_approval_apply (pat_no,times,ledger_sn,detail_sn,charge_code_mx, " +
            "charge_name,charge_amount,charge_fee,charge_date,staff_id,apply_datetime,apply_status) " +
            "values (#{patNo},#{times},#{ledgerSn},#{detailSn},#{chargeCodeMx},#{chargeName},#{chargeAmount}, " +
            "#{chargeFee},#{chargeDate},#{staffId},getdate(),0) ")
    void insertNewApply(PatientFeeItem item);

    @Select("select *,rtrim(b.name) as name,b.sex as gender,rtrim(b.admiss_dept) as wardName from " +
            "t_hospital_approval_apply a, zy_actpatient b where a.apply_datetime>=#{begntime} and " +
            "a.apply_datetime<=#{endtime} and a.pat_no=b.inpatient_no and a.times=b.admiss_times " +
            "and a.ledger_sn=b.times_billed")
    List<PatientFeeItem> selectAllApplies(String begntime, String endtime);

    @Update("update t_hospital_approval_apply set apply_status=#{applyStatus},apply_handler=#{applyHandler}, " +
            "handle_datetime=getdate(),handle_remark=#{handleRemark} where pat_no=#{patNo} and times=#{times} " +
            "and ledger_sn=#{ledgerSn} and detail_sn=#{detailSn}; " +
            "update zy_detail_charge set hosp_appr_flag=#{applyStatus},yb_self_flag=#{applyStatus} " +
            "where inpatient_no=#{patNo} and admiss_times=#{times} and ledger_sn=#{ledgerSn} and detail_sn=#{detailSn}")
    void handleApply(PatientFeeItem item);

    @Select("select trans_flag_yb from zy_detail_charge where inpatient_no=#{patNo} and " +
            "admiss_times=#{times} and ledger_sn=#{ledgerSn} and detail_sn=#{detailSn}")
    String selectFeeTransFlag(String patNo, Integer times, Integer ledgerSn, Integer detailSn);

    @Select("select med_type from t_si_pat_info where pat_no=#{patNo} and times=#{times} and ledger_sn=#{ledgerSn}")
    String selectMedType(String patNo, Integer times, Integer ledgerSn);
}
