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

import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.makedbzfees.InpatientInfo;
import thyyxxk.webserver.entity.makedbzfees.ZyDetailCharge;

import java.util.List;

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

    @Select("select rtrim(mz_no) from a_patient_mi with(nolock) where inpatient_no=#{inpatientNo}")
    String getPatientId(@Param("inpatientNo") String inpatientNo);

    @Select("select rtrim(inpatient_no) from a_patient_mi with(nolock) where mz_no=#{patientId}")
    String getInpatientNo(@Param("patientId") String patientId);

    @Select("select rtrim(inpatient_no) inpatientNo, " +
            "admiss_times," +
            "rtrim(admiss_ward) wardCode, " +
            "rtrim(small_dept) deptCode, " +
            "rtrim(isnull(operation, '')) as operation, " +
            "rtrim(refer_physician) referPhysician, " +
            "rtrim(consult_physician) consultPhysician, " +
            "ledgerSn=(select max(ledger_sn) from zy_ledger_file with(nolock) " +
            "where inpatient_no=a.inpatient_no and admiss_times=a.admiss_times), " +
            "maxDetailSn=(select max(detail_sn) from zy_detail_charge with(nolock) " +
            "where inpatient_no=a.inpatient_no and admiss_times=a.admiss_times) " +
            "from zy_actpatient a with(nolock) where inpatient_no=#{inpatientNo}")
    InpatientInfo getInpatientInfo(@Param("inpatientNo") String inpatientNo);

    @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_amount,charge_fee,ward_code,dept_code,order_no,ope_flag, " +
            "gen_time,charge_code_mx,serial,doctor_code,trans_flag_yb,refer_physician,consult_physician,exec_unit) VALUES " +
            "<foreach collection='list' item='fee' separator=',' open='' close=''>" +
            "(#{info.inpatientNo}, #{info.admissTimes}, #{info.ledgerSn}, #{fee.detailSn}, getDate(), #{info.staffId}, " +
            "#{fee.chargeCode}, 0, 2, #{fee.chargeAmount}, #{fee.chargeFee}, #{info.wardCode}, #{info.deptCode}, 1, " +
            "#{info.operation},getDate(), #{fee.chargeCodeMx},#{fee.serial},#{fee.doctorCode},0,#{info.referPhysician}," +
            "#{info.consultPhysician},#{fee.execDept})" +
            "</foreach>" +
            "</script>")
    void insertZyDetailChargeForSingleDisease(@Param("info") InpatientInfo info, @Param("list")List<ZyDetailCharge> zyFees);

    @Select("select rtrim(specification) from yp_zd_dict with(nolock) where code=#{code} and serial=#{serial}")
    String selectSpecification(@Param("code") String code, @Param("serial") String serial);

    @Select("select rtrim(supply_name) from mz_zd_supply_type with(nolock) where supply_code=#{code}")
    String selectSupplyName(@Param("code") String code);

    @Select("select rtrim(charge_unit) from zd_charge_item with(nolock) where code=#{code}")
    String selectXmChargeUnit(@Param("code") String code);

    @Select("select rtrim(name) from yp_zd_unit with(nolock) where code=#{code}")
    String selectDrugUnit(@Param("code") String code);

}
