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.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, " +
            "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, " +
            "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';")
    void receiveCharges(String patNo, int times);

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

    @Select("<script>" +
            "select " +
            "code=case when isnull(nullif(serial,''),'00')='00' then " +
            "(select isnull(isnull(national_code_27,national_code),code)  from zd_charge_item d where d.code=charge_code_mx) else " +
            "(select isnull(max(national_code),max(code)) from yp_zd_dict d where d.code=charge_code_mx) end, " +
            "name=case when isnull(nullif(serial,''),'00')='00' then " +
            "(select name from zd_charge_item d where d.code=charge_code_mx) else " +
            "(select max(name) from yp_zd_dict d where d.code=charge_code_mx) end, " +
            "specification=case when isnull(nullif(serial,''),'00')='00' then null else " +
            "(select max(specification) from yp_zd_dict d where d.code=charge_code_mx) end, " +
            "unit=case when isnull(nullif(serial,''),'00')='00' then " +
            "(select d.charge_unit from zd_charge_item d where d.code=charge_code_mx) else " +
            "(select name from yp_zd_unit e where e.code= " +
            "(select d.pack_unit from yp_zd_dict d where d.code=charge_code_mx and d.serial=a.serial)) end, " +
            "price=abs(round(charge_fee/case when charge_amount=0 then 1 else charge_amount end,2)), " +
            "quantity=a.charge_amount,cost=charge_fee,a.charge_date, " +
            "execDept=(select d.name from zd_unit_code d where d.code= " +
            "(case when isnull(exec_unit,'')='' then ward_code else exec_unit end)), " +
            "billItemCode=case when isnull(nullif(serial,''),'00')='00' then " +
            "(select d.bill_item_zy from zd_charge_item d where d.code=charge_code_mx) else " +
            "(select max(d.bill_item_zy) from yp_zd_dict d where d.code=charge_code_mx) end " +
            "from zy_detail_charge a where inpatient_no=#{patNo} and admiss_times=#{times} " +
            "and trans_flag_yb!=2 and charge_amount!=0 ${statement} " +
            "<if test=\"ledgerSn != null \">" +
            "and ledger_sn=#{ledgerSn} " +
            "</if>" +
            "</script>")
    List<ChargeItem> getChargeListWithChargeDate(PatOverview overview);
    
    @Select("select " +
            "code,name,specification,unit,price,billItemCode,execDept, " +
            "quantity=sum(quantity),cost=sum(cost) " +
            "from (select * from ( " +
            "select code=case when isnull(nullif(a.serial,''),'00')='00' then " +
            "(select isnull(isnull(national_code_27,national_code),code) national_code from zd_charge_item d where d.code=charge_code_mx) else " +
            "(select isnull(max(national_code),max(code)) from yp_zd_dict d where d.code=charge_code_mx) end, " +
            "name=case when isnull(nullif(a.serial,''),'00')='00' then " +
            "(select name as name from zd_charge_item d where d.code=charge_code_mx) else " +
            "(select max(name) from yp_zd_dict d where d.code=charge_code_mx) end, " +
            "specification=case when isnull(nullif(a.serial,''),'00')='00' then null else " +
            "(select max(specification) from yp_zd_dict d where d.code=charge_code_mx) end, " +
            "unit=case when isnull(nullif(a.serial,''),'00')='00' then " +
            "(select d.charge_unit from zd_charge_item d where d.code=charge_code_mx) else " +
            "(select name from yp_zd_unit e where e.code= " +
            "(select d.pack_unit from yp_zd_dict d where d.code=charge_code_mx and d.serial=a.serial)) end, " +
            "price=abs(round(charge_fee/case when charge_amount=0 then 1 else charge_amount end,2)), " +
            "quantity=(a.charge_amount),cost=(charge_fee), " +
            "execDept=(select d.name from zd_unit_code d where d.code= " +
            "(case when isnull(exec_unit,'')='' then ward_code else exec_unit end)), " +
            "billItemCode=case when isnull(nullif(a.serial,''),'00')='00' then " +
            "(select d.bill_item_zy from zd_charge_item d where d.code=charge_code_mx) else " +
            "(select max(d.bill_item_zy) from yp_zd_dict d where d.code=charge_code_mx) end " +
            "from zy_detail_charge a where inpatient_no=#{patNo} and admiss_times=#{times} " +
            "and trans_flag_yb!=2 and charge_amount!=0 and charge_date>=#{begndate} " +
            "and charge_date<=#{enddate}) temp) res group by " +
            "code, name, specification, unit, price, billItemCode, execDept ")
    List<ChargeItem> getChargeListWithoutChargeDate(PatOverview overview);

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

    @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 convert(varchar(10),start_time,21) 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 " +
            "(select order_code from yz_zd_order_item_confirm where item_name in (N'出院',N'死亡'))")
    String selectActOrderDisDate(@Param("patNo") String patNo, @Param("times") Integer times);
}
