package thyyxxk.webserver.dao.his.inpatient.charge;

import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.inpatient.charge.forceinout.SimplePatient;

import java.util.List;

@Mapper
public interface ForceInAndOutDao {

    @Select("select count(1) from zy_actpatient where inpatient_no=#{patNo}")
    int getActpatientCount(String patNo);

    @Select("select setl_id from t_si_setlinfo where pat_no=#{patNo} " +
            "and times=#{times} and ledger_sn=#{ledgerSn} and revoked=0")
    String getSetlId(SimplePatient patient);

    @Select("select rtrim(inpatient_no) as patNo,admiss_times as times,rtrim(name) as name," +
            "admiss_date as admDate,med_type,dis_date,rtrim(bed_status) as bedStatus," +
            "rtrim(ward) as ward,rtrim(zk_ward) as dept,rtrim(bed_no) as bedNo " +
            "from zy_inactpatient where inpatient_no like #{input} or name like #{input} " +
            "order by inpatient_no,dis_date desc")
    List<SimplePatient> getZyInactpatient(String input);

    @Select("select rtrim(inpatient_no) as patNo,admiss_times as times,rtrim(name) as name," +
            "admiss_date as admDate,med_type,dis_date,rtrim(bed_status) as bedStatus," +
            "rtrim(ward) as ward,rtrim(zk_ward) as dept,rtrim(bed_no) as bedNo " +
            "from zy_actpatient where inpatient_no like #{input} or name like #{input} ")
    List<SimplePatient> getZyActpatient(String input);

    @Insert("insert into zy_work_log(inpatient_no,admiss_times,ledger_sn," +
            "op_id,op_date,log_type,dept_code,ward_code,name) values " +
            "(#{patNo},#{times},0,#{staffId},#{staffOpTime},#{staffOpType}, " +
            "#{dept},#{ward},#{staffName}) ")
    void insertZyWorkLog(SimplePatient patient);

    @Select("select isnull(max(ledger_sn),1) from zy_ledger_file " +
            "where inpatient_no=#{patNo} and admiss_times=#{times}")
    int getMaxLedgerSn(SimplePatient patient);

    // 床位状态：1空闲，2占用
    @Select("select count(1) from zy_bed_mi where dept_code=#{ward} " +
            "and ward_code=#{ward} and bed_no=#{bedNo} and bed_status='1' ")
    int certainBedFree(SimplePatient patient);

    @Select("select max(rtrim(bed_no)) from zy_bed_mi where dept_code=#{ward} " +
            "and ward_code=#{ward} and bed_status='1'")
    String getMaxFreeBedNo(String ward);

    @Update("update zy_inactpatient set bed_no=#{bedNo},dis_ward=null,dis_dept=null," +
            "bed_status='9' where inpatient_no=#{patNo} and admiss_times=#{times}")
    void changeBedNoAndBedStatus(SimplePatient patient);

    @Insert("insert into zy_actpatient select * from zy_inactpatient " +
            "where inpatient_no=#{patNo} and admiss_times=#{times} ")
    void transferToZyActpatient(SimplePatient patient);

    @Insert("insert into zy_actpatient select * from zy_inactpatient " +
            "where inpatient_no>=#{babyNoBegin} and inpatient_no<=#{babyNoEnd} and admiss_times=#{times}")
    void transferToZyActpatientBaby(SimplePatient patient);

    @Update("update zy_bed_mi set bed_status='2',inpatient_no=#{patNo},admiss_times=#{times} " +
            "where ward_code=#{ward} and dept_code=#{ward} and bed_no=#{bedNo}")
    void lockZyBedMi(SimplePatient patient);

    @Update("update zy_adt set dis_date=null,orig_ward=null,orig_dept=null,orig_bed=null " +
            "where inpatient_no=#{patNo} and admiss_times=#{times} and trans_times=0")
    void clearZyAdt(SimplePatient patient);

    @Delete("delete from zy_inactpatient where inpatient_no=#{patNo} and admiss_times=#{times}")
    void deleteZyInactpatient(SimplePatient patient);

    @Delete("delete from zy_inactpatient where inpatient_no>=#{babyNoBegin} " +
            "and inpatient_no<=#{babyNoEnd} and admiss_times=#{times}")
    void deleteZyInactpatientBaby(SimplePatient patient);

    @Insert("insert into yz_act_order select * from yz_inact_order " +
            "where inpatient_no=#{patNo} and admiss_times=#{times}")
    void transferToYzActOrder(SimplePatient patient);

    @Insert("insert into yz_act_order select *from yz_inact_order " +
            "where inpatient_no>=#{babyNoBegin} and inpatient_no<=#{babyNoEnd} and admiss_times=#{times}")
    void transferToYzActOrderBaby(SimplePatient patient);

    @Delete("delete from yz_inact_order where inpatient_no=#{patNo} and admiss_times=#{times}")
    void deleteYzInactOrder(SimplePatient patient);

    @Delete("delete from yz_inact_order where inpatient_no>=#{babyNoBegin} " +
            "and inpatient_no<=#{babyNoEnd} and admiss_times=#{times}")
    void deleteYzInactOrderBaby(SimplePatient patient);

    @Select("select count(1) from yz_act_order where inpatient_no=#{patNo} and admiss_times=#{times} " +
            "and status_flag>'2' 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'死亡'))")
    int getDisOrderCount(SimplePatient patient);

    @Select("select sum(total_charge) from zy_ledger_file where " +
            "inpatient_no=#{patNo} and admiss_times=#{times} and account_date is null")
    Double getUnsettleCost(SimplePatient patient);

    @Update("update zy_actpatient set dis_date=#{disDate},dis_ward=ward," +
            "dis_dept=dept,job_nurse='00000',bed_status='5' " +
            "where inpatient_no=#{patNo} and admiss_times=#{times}")
    void updateZyActpatient(SimplePatient patient);

    @Update("update zy_actpatient set dis_date=#{disDate},dis_ward=ward," +
            "dis_dept=dept,job_nurse='00000',bed_status='5' " +
            "where inpatient_no>=#{babyNoBegin} and inpatient_no<=#{babyNoEnd} and admiss_times=#{times}")
    void updateZyActpatientBaby(SimplePatient patient);

    @Insert("insert into zy_inactpatient select * from zy_actpatient " +
            "where inpatient_no=#{patNo} and admiss_times=#{times}")
    void transferToZyInactpatient(SimplePatient patient);

    @Insert("insert into zy_inactpatient select * from zy_actpatient " +
            "where inpatient_no>=#{babyNoBegin} and inpatient_no<=#{babyNoEnd} and admiss_times=#{times}")
    void transferToZyInactpatientBaby(SimplePatient patient);

    @Insert("insert into yz_inact_order select * from yz_act_order " +
            "where inpatient_no=#{patNo} and admiss_times=#{times}")
    void transferToYzInactOrder(SimplePatient patient);

    @Insert("insert into yz_inact_order select *from yz_act_order " +
            "where inpatient_no>=#{babyNoBegin} and inpatient_no<=#{babyNoEnd} and admiss_times=#{times}")
    void transferToYzInactOrderBaby(SimplePatient patient);

    @Delete("delete from yz_act_order where inpatient_no=#{patNo} and admiss_times=#{times}")
    void deleteYzActOrder(SimplePatient patient);

    @Delete("delete from yz_act_order where inpatient_no>=#{babyNoBegin} " +
            "and inpatient_no<=#{babyNoEnd} and admiss_times=#{times}")
    void deleteYzActOrderBaby(SimplePatient patient);

    @Update("update zy_adt set dis_date=#{disDate},orig_ward=#{dept},orig_dept=#{ward},orig_bed=#{bedNo} " +
            "where inpatient_no=#{patNo} and admiss_times=#{times} and trans_times=0")
    void updateZyAdt(SimplePatient patient);

    @Update("update zy_bed_mi set bed_status='1',admiss_times=null,inpatient_no=null " +
            "where inpatient_no=#{patNo} and admiss_times=#{times}")
    void freeZyBedMi(SimplePatient patient);

    @Delete("delete from zy_actpatient where inpatient_no=#{patNo}")
    void deleteZyActpatient(SimplePatient patient);

    @Delete("delete from zy_actpatient where inpatient_no>=#{babyNoBegin} " +
            "and inpatient_no<=#{babyNoEnd} and admiss_times=#{times}")
    void deleteZyActpatientBaby(SimplePatient patient);

    /**
     *  0=未结算  1=出院结算  3=中间结算  4=已出纳
     */
    @Select("select rtrim(isnull(settle_type,'0')) from zy_ledger_file where inpatient_no=#{patNo} " +
            "and admiss_times=#{times} and ledger_sn=#{ledgerSn}")
    String getSettleType(SimplePatient patient);

    @Update("update zy_ledger_file set account_date=null,ward_code=null,dept_code=null," +
            "balance=deposit-total_charge,settle=0,settle_type='0' " +
            "where inpatient_no=#{patNo} and admiss_times=#{times} and ledger_sn=#{ledgerSn}")
    void clearZyLedgerFile(SimplePatient patient);

    @Update("update zy_detail_charge set charge_status='2' where inpatient_no=#{patNo} " +
            "and admiss_times=#{times} and ledger_sn=#{ledgerSn} and charge_status='3'")
    void updateZyDetailCharge(SimplePatient patient);

    @Delete("delete from zy_receipt where inpatient_no=#{patNo} " +
            "and admiss_times=#{times} and ledger_sn=#{ledgerSn}")
    void deleteZyReceipt(SimplePatient patient);

    @Delete("delete zy_ledger_file_yb where inpatient_no=#{patNo} " +
            "and admiss_times=#{times} and ledger_sn=#{ledgerSn}")
    void deleteZyLedgerFileYb(SimplePatient patient);

    @Delete("delete zy_owe_file where inpatient_no=#{patNo} and admiss_times=#{times} " +
            "and ledger_sn=#{ledgerSn} and owe_type!='0'")
    void deleteZyOweFile(SimplePatient patient);

}
