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

import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.inpatient.dismiss.SnAndResponce;
import thyyxxk.webserver.entity.inpatient.dismiss.ZyInit;
import thyyxxk.webserver.entity.inpatient.dismiss.ZyTmpReceiveData;

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

@Mapper
public interface ReceiveDrugDao {
    @Select("select west_drug_code,chn_drug_code,inNo=#{inNo},times=#{times} from zy_init")
    ZyInit getZyInit(String inNo, int times);

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

    @Select("select sn=times_billed,resType=responce_type from ${tableName} " +
            "where inpatient_no=#{vNo} and admiss_times=#{times}")
    SnAndResponce getSnAndResponce(String vNo, int times, String tableName);

    @Select("select account_date from zy_ledger_file where " +
            "inpatient_no=#{vNo} and admiss_times=#{times} and ledger_sn=#{sn}")
    Date getAccountDate(String vNo, int times, int sn);

    @Select("select isnull(max(detail_sn),0)+1 from zy_detail_charge where " +
            "inpatient_no=#{vNo} and admiss_times=#{times} and ledger_sn=#{sn}")
    int getMaxDetailSn(String vNo, int times, int sn);

    @Select("select chargeDate=order_date,opIdCode=drawer,patNo=#{inNo},times=#{times}," +
            "chargeCode=case when charge_code>'00000' and charge_code<'30000' " +
            "then #{westDrugCode} when charge_code>'30000' and charge_code<'40000' " +
            "then #{chnDrugCode} else #{westDrugCode} end," +
            "a.infant_flag,chargeStatus=#{chgStatus},retprice as chargeFee,self_flag," +
            "separate_flag,suprice_flag,order_date as occTime, " +
            "actOrderNo=cast(cast(b.act_order_no as decimal) as varchar), " +
            "b.dept_code,b.ward_code,amount as orderCount," +
            "execUnit=isnull(b.exec_unit,c.exec_unit), " +
            "ybSelfFlag=isnull(b.yb_self_flag,c.yb_self_flag)," +
            "doctorCode=isnull(b.physician,c.physician)," +
            "referPhysician=isnull(b.refer_physician,c.refer_physician), " +
            "consultPhysician=isnull(b.consult_physician,c.consult_physician), " +
            "billItemCode='xxx',p1=1,p2=1,p3=1,p4=1,p5=1, " +
            "a.serial,a.group_no,a.supply_code,charge_code as chargeItemCode " +
            "from yp_zy_patient a, yz_act_order b left join yz_erase_order c on " +
            "b.act_order_no=c.act_order_no " +
            "where a.inpatient_no=#{inNo} and a.admiss_times=#{times} " +
            "and a.acct_sign='0' and a.pay_self!='2' " +
            "and a.act_order_no=b.act_order_no")
    List<ZyTmpReceiveData> getTmpReceiveData(ZyInit zyInit);

    @Update("update yp_zy_patient set acct_sign='2' " +
            "where inpatient_no=#{inNo} and admiss_times=#{times} " +
            "and acct_sign='0' and pay_self!='2'")
    void updateYpZyPatient(String inNo, int times);

    @Insert({"<script>" +
            "insert into zy_detail_charge(inpatient_no,admiss_times,ledger_sn,detail_sn, " +
            "charge_date,op_id_code,charge_code,infant_flag,charge_status,charge_fee," +
            "self_flag,separate_flag,suppress_flag,ward_code,dept_code,order_no,ope_flag," +
            "exec_unit,charge_amount,charge_code_mx,serial,doctor_code,refer_physician," +
            "consult_physician,yb_self_flag) values " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{item.vNo},#{item.times},#{item.ledgerSn},#{item.detailSn}, " +
            "#{item.chargeDate},#{item.opIdCode},#{item.chargeCode},#{item.infantFlag}, " +
            "#{item.chargeStatus},#{item.chargeFee},#{item.selfFlag},#{item.separateFlag}, " +
            "#{item.suppressFlag},#{item.wardCode},#{item.deptCode},#{item.actOrderNo},'0', " +
            "#{item.execUnit},#{item.orderCount},#{item.chargeItemCode},#{item.serial}, " +
            "#{item.doctorCode},#{item.referPhysician},#{item.consultPhysician}, " +
            "#{item.ybSelfFlag})" +
            "</foreach>" +
            "</script>"})
    void insertZyChargeFee(List<ZyTmpReceiveData> list);

    @Select("select a.balance+isnull(a.max_ledger,0)+isnull(b.owe_limit,0) " +
            "from zy_actpatient a,zy_adtward b where a.ward=b.ward_code and " +
            "a.dept=b.dept_code and a.inpatient_no=#{inNo} and a.admiss_times=#{times}")
    int getOweLimit(String inNo, int times);

    @Update("update zy_actpatient set intern_code='9' " +
            "where inpatient_no=#{inNo} and admiss_times=#{times} and " +
            "isnull(intern_code,'0')!='1'")
    void updateInterCode9(String inNo, int times);

    @Update("update zy_actpatient set intern_code='0' " +
            "where inpatient_no=#{inNo} and admiss_times=#{times} and " +
            "isnull(intern_code,'0')='9'")
    void updateInterCode0(String inNo, int times);
}
