package thyyxxk.webserver.dao.his.inpatient;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import thyyxxk.webserver.entity.inpatient.chargelist.BriefPatInfo;
import thyyxxk.webserver.entity.inpatient.chargelist.ChargeItem;
import thyyxxk.webserver.entity.inpatient.chargelist.PatOverview;

import java.util.Date;
import java.util.List;

@Mapper
public interface ChargeListDao {

    @Select("select rtrim(inpatient_no) as patNo,admiss_times as times,total_charge, " +
            "rtrim(name) as name, admiss_date as admdate,dis_date as disdate " +
            "from zy_inactpatient where inpatient_no=#{patNo} " +
            "union " +
            "select rtrim(inpatient_no) as patNo,admiss_times as times,total_charge, " +
            "rtrim(name) as name, admiss_date as admdate,dis_date as disdate " +
            "from zy_actpatient where inpatient_no=#{patNo}")
    List<PatOverview> selectPatOverviews(String patNo);

    @Select("select rtrim(inpatient_no) as patNo,admiss_times as times,rtrim(name) as name, " +
            "rtrim(a.bed_no) as bedNo,datediff(day,admiss_date,isnull(dis_date,getdate())) as days, " +
            "gender=(case when sex='1' then '男' when sex='2' then '女' else '未知' end), " +
            "rtrim(zk_ward) as dept,admiss_date as admdate,med_type as medtype, " +
            "admiss_date as begndate,dis_date as enddate, " +
            "balance=(select ((select isnull(sum(t.depo_amount),0) from zy_deposit_file t " +
            "where t.inpatient_no=a.inpatient_no and t.admiss_times=a.admiss_times and t.status in (1,2)) + " +
            "(select sum(e.fund_pay_sumamt) from zy_ledger_file e where e.inpatient_no=#{patNo} and e.admiss_times=#{times}) - " +
            "(select isnull(sum(t.charge_fee),0) from zy_detail_charge t where t.inpatient_no=a.inpatient_no " +
            "and t.admiss_times=a.admiss_times))),a.total_charge as totalCost " +
            "from ${table} a where a.inpatient_no=#{patNo} and a.admiss_times=#{times} ")
    BriefPatInfo selectBriefPatInfo(String patNo, int times, String table);

    @Select("execute zy_receive_drug #{patNo},#{times},#{patNo},'0';" +
            "execute zy_receive_one #{patNo},#{times},#{patNo},'0';" +
            "execute dbo.zy_cxtj_mxqd_new #{patNo},#{times}")
    List<ChargeItem> selectChargeList(String patNo, int times);

    @Select("execute dbo.zy_cxtj_mxqd_new #{patNo},#{times}")
    List<ChargeItem> selectChargeList2(String patNo, int times);

    @Select("select count(1) from zy_actpatient where inpatient_no=#{patNo} and admiss_times=#{times}")
    Integer isDischargedPatients(String patNo, int times);

    @Select("select count(1) from zy_actpatient where inpatient_no=#{patNo} and admiss_times=#{times}")
    int selectInhospCount(String patNo, int times);

    @Select("select ledger_sn from t_si_setlinfo where pat_no=#{patNo} and times=#{times} and revoked=0")
    List<Integer> selectSiLedgers(String patNo, int times);

    @Select("select ledger_sn from zy_ledger_file where inpatient_no=#{patNo} and admiss_times=#{times} and ledger_sn>0")
    List<Integer> selectAllLedgers(String patNo, int times);

    @Select("select start_time from yz_act_order where inpatient_no=#{patNo} and admiss_times=#{times} " +
            "and status_flag > '1' and isnull(group_no, '00')='00' and order_code in ('06026','06053','05973')")
    Date selectActOrderDisDate(@Param("patNo") String patNo, @Param("times") Integer times);
}
