package thyyxxk.webserver.dao.his.inpatient;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import thyyxxk.webserver.entity.inpatient.patient.NotUploadedFee;

/**
 * @author dj
 */
@Mapper
public interface Routines {

    /**
     * 获取当前患者最大账页号
     *
     * @param inpatientNo 住院号
     * @param admissTimes 住院次数
     * @return 账页号
     */
    @Select("select max(ledger_sn) from zy_ledger_file where " +
            "inpatient_no=#{inpatientNo} and admiss_times=#{admissTimes}")
    Integer selectMaxLedgerSn(@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes);

    @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,yb_self_flag, " +
            "yb_code=(select max(national_code) from yp_zd_dict where code=charge_code_mx), " +
            "charge_name=(select top 1 rtrim(name) from yp_zd_dict where code=charge_code_mx) " +
            "from zy_detail_charge where inpatient_no=#{patNo} and admiss_times=#{times} and " +
            "ledger_sn=#{ledgerSn} and isnull(trans_flag_yb,0)=0 and isnull(infant_flag,0)=0 " +
            "and charge_date<=#{today} and isnull(serial,'') in (#{serial1},#{serial2})")
    IPage<NotUploadedFee> selectNotUploadedYpFees(IPage<NotUploadedFee> iPage,
                                                @Param("patNo") String patNo,
                                                @Param("times") int times,
                                                @Param("ledgerSn") int ledgerSn,
                                                @Param("today") String today,
                                                @Param("serial1") String serial1,
                                                @Param("serial2") String serial2);

    @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,yb_self_flag, " +
            "yb_code=(select isnull(national_code_27,national_code) from zd_charge_item where code=charge_code_mx), " +
            "charge_name=(select rtrim(name) from zd_charge_item where code=charge_code_mx) " +
            "from zy_detail_charge where inpatient_no=#{patNo} and admiss_times=#{times} and " +
            "ledger_sn=#{ledgerSn} and isnull(trans_flag_yb,0)=0 and isnull(infant_flag,0)=0 " +
            "and charge_date<=#{today} and isnull(serial,'') in (#{serial1},#{serial2})")
    IPage<NotUploadedFee> selectNotUploadedXmFees(IPage<NotUploadedFee> iPage,
                                                @Param("patNo") String patNo,
                                                @Param("times") int times,
                                                @Param("ledgerSn") int ledgerSn,
                                                @Param("today") String today,
                                                @Param("serial1") String serial1,
                                                @Param("serial2") String serial2);

    @Select("select detail_sn,charge_code_mx,charge_amount,charge_fee,charge_date,yb_self_flag, " +
            "yb_code=(select max(aaz231) from ${zdTable} where code=charge_code_mx), " +
            "charge_name=(select top 1 rtrim(name) from ${zdTable} where code=charge_code_mx) " +
            "from zy_detail_charge where inpatient_no=#{patNo} and admiss_times=#{times} and " +
            "ledger_sn=#{ledgerSn} and isnull(trans_flag_yb,0)=0 and isnull(infant_flag,0)=0 " +
            "and charge_date<=#{today} and isnull(serial,'') in (#{serial1},#{serial2})")
    IPage<NotUploadedFee> selectInjuryNotUploadedFees(IPage<NotUploadedFee> iPage,
                                                @Param("zdTable") String zdTable,
                                                @Param("patNo") String patNo,
                                                @Param("times") int times,
                                                @Param("ledgerSn") int ledgerSn,
                                                @Param("today") String today,
                                                @Param("serial1") String serial1,
                                                @Param("serial2") String serial2);

    @Select("select sum(charge_fee) from zy_detail_charge where inpatient_no=#{patNo} and admiss_times=#{times} and " +
            "ledger_sn=#{ledgerSn} and isnull(trans_flag_yb,0)=0 and charge_date<=#{today} and isnull(serial,'') in (#{serial1},#{serial2})")
    String selectSumChargeFee(@Param("patNo") String patNo,
                              @Param("times") int times,
                              @Param("ledgerSn") int ledgerSn,
                              @Param("today") String today,
                              @Param("serial1") String serial1,
                              @Param("serial2") String serial2);
}
