package thyyxxk.webserver.dao.his.medicalinsurance;

import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.medicalinsurance.inpatient.settleapply.response.SiSettleApply;
import thyyxxk.webserver.entity.inpatient.dismiss.ZyDisYbSrgry;

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

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

    @Select("select isnull(total_charge,0) from zy_ledger_file where inpatient_no=#{patNo} and admiss_times=#{times} and ledger_sn=#{ledgerSn}")
    int selectTotalCharge(String patNo, int times, int ledgerSn);

    @Select("select mdtrt_id from t_si_pat_info with(nolock) where pat_no=#{patNo} and times=#{times} and ledger_sn=#{ledger}")
    String selectMdtrtId(String patNo, int times, int ledger);

    @Select("select gend from t_si_pat_info with(nolock) where pat_no=#{patNo} and times=#{times} and ledger_sn=#{ledger}")
    String selectYbGender(String patNo, int times, int ledger);

    @Select("select start_time from ${table} with(nolock) 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'死亡')) " +
            "and status_flag <> '6'  ")
    Date selectActOrderDisDate(String table, String patNo, Integer times);

    @Select("select start_time from ${table} with(nolock) 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'死亡')) " +
            "and status_flag <> '6'  ")
    List<Date> selectActOrderDisDate2(String table, String patNo, Integer times);

    @Select("select top 1 status,handle_comment,type,settle_datetime,acct_used_flag,death_flag,sort_no from t_si_settle_apply " +
            "where pat_no=#{patNo} and times=#{times} and ledger_sn=#{ledger} and type=#{type} order by sort_no desc")
    SiSettleApply selectSettleApply(String patNo, int times, int ledger, int type);

    @Insert("insert into t_si_settle_apply " +
            "(pat_no,times,ledger_sn,input_staff,input_comment,input_datetime,status,type," +
            "settle_datetime,settled,acct_used_flag,death_flag,sort_no,handle_staff," +
            "handle_comment,handle_datetime) values " +
            "(#{patNo},#{times},#{ledgerSn},#{inputStaff},#{inputComment},getdate()," +
            "#{status},#{type},#{settleDatetime},0,#{acctUsedFlag},#{deathFlag},#{sortNo}," +
            "#{handleStaff},#{handleComment},#{handleDatetime})")
    void insertSettleApply(SiSettleApply apply);

    @Select("select a.*,c.sex as gender,rtrim(c.name) as name,b.med_type, " +
            "handleStaffName=(select rtrim(d.name) from a_employee_mi d where d.code=a.handle_staff)," +
            "wardName=(select rtrim(name) from zd_unit_code with(nolock) where code=c.ward) " +
            "from t_si_settle_apply a with(nolock), t_si_pat_info b with(nolock), zy_actpatient c with(nolock) " +
            "where a.settled=0 and a.pat_no=b.pat_no and a.times=b.times and a.ledger_sn=b.ledger_sn and a.pat_no=c.inpatient_no " +
            "and sort_no=(select max(sort_no) from t_si_settle_apply c where c.pat_no=a.pat_no and c.times=a.times and c.ledger_sn=a.ledger_sn and c.type=a.type)")
    List<SiSettleApply> selectUnhandledApplies();

    @Select("select a.pat_no,a.times,a.ledger_sn,a.input_staff,a.input_comment,a.input_datetime,a.status,a.handle_staff,a.acct_used_flag, " +
            "a.handle_comment,a.handle_datetime,a.type,a.settle_datetime,b.med_type, b.insuplc_admdvs,b.insutype,b.mdtrt_id,a.sort_no, " +
            "detailTotalCharge=(select isnull(sum(charge_fee),0) from zy_detail_charge c with(nolock) where c.inpatient_no=a.pat_no " +
            "and c.admiss_times=a.times and c.ledger_sn=a.ledger_sn), " +
            "ledgerTotalCharge=(select isnull(total_charge,0) from zy_ledger_file c with(nolock) where c.inpatient_no=a.pat_no " +
            "and c.admiss_times=a.times and c.ledger_sn=a.ledger_sn), " +
            "medinsTotalCharge=(select isnull(sum(charge_fee),0) from zy_detail_charge c with(nolock) where c.inpatient_no=a.pat_no " +
            "and c.admiss_times=a.times and c.ledger_sn=a.ledger_sn and trans_flag_yb=1), " +
            "fundpay=(select isnull(nullif(charge_yb,0), isnull(fund_pay_sumamt,0)) from zy_ledger_file c with(nolock) where c.inpatient_no=a.pat_no " +
            "and c.admiss_times=a.times and c.ledger_sn=a.ledger_sn),death_flag " +
            "from t_si_settle_apply a with(nolock), t_si_pat_info b with(nolock) where a.pat_no=#{patNo} and a.times=#{times} and " +
            "a.ledger_sn=#{ledgerSn} and a.type=#{type} and a.sort_no=#{sortNo} and a.pat_no=b.pat_no and a.times=b.times and a.ledger_sn=b.ledger_sn")
    SiSettleApply selectCurrentApply(SiSettleApply apply);

    @Select("select detailTotalCharge=(select isnull(sum(charge_fee),0) from zy_detail_charge c with(nolock) where " +
            "c.inpatient_no=#{patNo} and c.admiss_times=#{times} and c.ledger_sn=#{ledgerSn}), " +
            "ledgerTotalCharge=(select isnull(total_charge,0) from zy_ledger_file c with(nolock) where " +
            "c.inpatient_no=#{patNo} and c.admiss_times=#{times} and c.ledger_sn=#{ledgerSn})")
    SiSettleApply selectTotalChargeAgain(SiSettleApply apply);

    @Select("select zyh,zycs,ssxh,ssbm,ssmc,ssrq,ssys,sszs1,sszs2,mzys from batj_ba4 where zyh=#{patNo} and zycs=#{times} order by ssxh")
    List<ZyDisYbSrgry> selectSurgeries(String patNo, int times);

    @Update("update t_si_settle_apply set status=#{status},handle_staff=#{handleStaff}, " +
            "handle_comment=#{handleComment},handle_datetime=getdate() where pat_no=#{patNo} " +
            "and times=#{times} and ledger_sn=#{ledgerSn} and type=#{type} and sort_no=#{sortNo}")
    void handleApply(SiSettleApply apply);

    @Select("select * from t_si_settle_apply where pat_no=#{patNo} and times=#{times} and " +
            "ledger_sn=#{ledgerSn} and type=#{type} order by sort_no")
    List<SiSettleApply> selectApplyHistories(SiSettleApply apply);

    @Select("select a.*,c.sex as gender,rtrim(c.name) as name,b.med_type, " +
            "wardName=(select rtrim(name) from zd_unit_code with(nolock) where code=c.ward)  " +
            "from t_si_settle_apply a with(nolock), t_si_pat_info b with(nolock), zy_actpatient c with(nolock) " +
            "where a.settled=1 and a.pat_no=b.pat_no and a.times=b.times and a.ledger_sn=b.ledger_sn and a.pat_no=c.inpatient_no " +
            "and a.settle_datetime>=#{start} and a.settle_datetime<=#{end}")
    List<SiSettleApply> selectSettledApplies(String start, String end);

    @Select("select rtrim(code) from a_employee_mi where " +
            "dept_code=#{groupId} and isnull(del_flag,'0')!='1'")
    Set<String> selectUserCodeByGroupId(String groupId);
}
