package thyyxxk.webserver.dao.his.executeItem;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import thyyxxk.webserver.entity.datamodify.YzActOrder;
import thyyxxk.webserver.entity.datamodify.ZyDetailCharge;
import thyyxxk.webserver.entity.executeItem.ZdChargeItem;
import thyyxxk.webserver.entity.executeItem.ZyLedgerFile;
import thyyxxk.webserver.entity.executeItem.ZyTmpReceiveData;
import thyyxxk.webserver.entity.inpatient.ZyActpatient;
import thyyxxk.webserver.entity.inpatient.dismiss.ZyInit;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Description: 存储过程转换
 * @Author:lihong
 * @Date: 2024/8/21
 */
@Mapper
public interface StoreProcedureConvertDao {

    @Select(" SELECT times_billed , rtrim(charge_type) charge_type,zk_ward,responce_type  " +
            "            from ${tableName} " +
            "            where inpatient_no = #{inpatientNo}" +
            "              and admiss_times = #{times} ")
    ZyActpatient selectZyActpatient(@Param("inpatientNo") String inpatientNo,@Param("times") Integer times, @Param("tableName") String tableName);
    @Select("  SELECT  account_date " +
            "        FROM zy_ledger_file " +
            "        WHERE inpatient_no = #{inpatientNo} " +
            "          AND admiss_times = #{times} " +
            "          AND ledger_sn = #{sn} ")
    Date selectAccountDate(@Param("inpatientNo") String inpatientNo,@Param("times") Integer times, @Param("sn") Integer sn);
    
    @Select(" SELECT inpatient_no,  " +
            "       admiss_times,  " +
            "       ledger_sn,  " +
            "       occ_time charge_date,  " +
            "       isnull(op_id, op_id_code) as op_id_code ,  " +
            "       charge_code,  " +
            "       infant_flag,  " +
            "       charge_status,  " +
            "       charge_fee,  " +
            "       self_flag,  " +
            "       separate_flag,  " +
            "       suppress_flag,  " +
            "       occ_time,  " +
            "       act_order_no,  " +
            "       dept_code,  " +
            "       ward_code,  " +
            "       order_count,  " +
            "       exec_unit,  " +
            "       'XXX' bill_item_code ,  " +
            "       1 p1,  " +
            "       1 p2,  " +
            "       1 p3,  " +
            "       1 p4,  " +
            "       1 p5,  " +
            "       op_date  " +
            " FROM yz_zy_patient_fee  " +
            " WHERE inpatient_no = #{inpatientNo}  " +
            "  AND admiss_times = #{times}  " +
            "  AND ((charge_status = '1' AND charge_code not in (select charge_code from zy_dept_charge)) or  " +
            "       charge_status = '5' or charge_status = '6')  " +
            "  and isnull(pay_self, '0') not in ('3', '2')  " +
            "  and charge_fee <> 0 ")
    List<ZyTmpReceiveData> selectZyTmpReceiveData(@Param("inpatientNo") String inpatientNo,@Param("times") Integer times);
    @Select(" select code, name, exec_unit, audit_code, charge_amount, effective_date, effective_amount, percentag1, percentag2, percentag3, percentag4, percentag5, self_flag, separate_flag, suppress_flag, py_code, d_code, bill_item_zy, bill_item_mz, charge_unit, percent_group_zy, percent_group_mz, in_out_flag, n_code, class_code, s_code, e_code, discription, del_flag, percent_group_1, charge_amount_lwg, yp_flag, bill_item_code, bill_item_out, percent_group, bill_item_yb, yb_type, self_flag_yb, xl_code, dl_code, co, yb_comment, yb_name, xnh_flag, add_date, hnsyb_code, hnsyb_name, xnh_code, xnh_name, xnh_ratio, xnh_quotamoney, xnh_status, instruction, cl_code, yb_flag_new, yb_comment_new, yb_bl_new, charge_amount_old, part_code, oper_id, national_code, national_name, national_status, match_oper, match_date, cancel_match_oper, cancel_match_date, new_name, oper_date, specification, manu_code, supplier, uploaded_flag, factory, limit_type, limit_flag, effective_time, effective_price, update_id, update_time, descriptions, exclude_content, connotation, approval_number, selfpay_prop, group_no, injury_match_status, aaz231, ake001, ake002, ake003, bke215, injury_match_oper, injury_match_datetime, cancel_injury_match_oper, cancel_injury_match_datetime, injury_upload_flag, aaz278, ba_class, zd_charge_ba_class from zd_charge_item where code in (${codes})")
    List<ZdChargeItem> selectZdChargeItem(@Param("codes")String codes);

    @Select("  SELECT room_rate1 bed_fee " +
            "        FROM zy_bed_mi " +
            "        WHERE inpatient_no = #{inpatientNo} " +
            "          AND admiss_times =#{times}")
    BigDecimal selectBedFee(@Param("inpatientNo") String inpatientNo,@Param("times") Integer times);

    @Select(" SELECT code FROM zy_special_bed ")
    List<String> selectZySpecialBedCode();

    @Select(" SELECT  isnull(max(detail_sn), 0) + 1 " +
            "        FROM ${tableName} " +
            "        WHERE inpatient_no = #{inpatientNo} " +
            "          AND admiss_times = #{times} " +
            "          AND ledger_sn = #{sn} ")

    Integer selectMaxDetailSn(@Param("inpatientNo") String inpatientNo,@Param("times") Integer times, @Param("sn") Integer sn,@Param("tableName") String tableName);

    @Select("<script>" +
            "select act_order_no, inpatient_no, admiss_times, order_code, order_time, frequ_code, enter_time, enter_oper, start_time, end_time, infant_flag, drug_flag, supply_code, performance_time, physician, confirm_time, signer, modifier, status_flag, status_time, drug_quan, instruction, drug_specification, mini_unit, drug_weight, drug_weight_unit, print_order_change, parent_no, self_buy, eat_card_flag, drug_volume, drug_vol_unit, dose, dose_unit, drug_occ, print_order, print_order_once, print_order_anew, print_infusion, exclu_back_time, exclu_act_order_no, order_name, discription, del_flag, pay_self, serial, group_no, ward_code, yb_self_flag, doctor_flag, dept_code, exec_unit, cl_code, zy_serial_no, executer1, executer2, reg_flag, msg_flag, exec_time, exec_id, ps_status, ps_oper, ps_time, exec_id2, act_order_no2, print_page, print_page_once, print_order_change_end, emergency_flag, req_receive_flag, inspect_stuff, charge_status, ps_result_time, kf_flag, modifier_oper, pass_flag, pass_id, pass_date, pass_comment, refer_physician, consult_physician  from ${tableName} where act_order_no in " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            " #{item}" +
            "</foreach>" +
            "</script>")
    List<YzActOrder> selectYzActOrderNo(@Param("list") List<BigDecimal> actOrderNoList,@Param("tableName") String tableName);
    @Select(" select rtrim(dept) dept from zd_dept_all ")
    List<String> selectZdDeptAll();
    @Update("  UPDATE zy_ledger_file  " +
            "                SET total_charge = total_charge + #{totalCharge},  " +
            "                    balance      = balance - #{totalChargeP},  " +
            "                    charge1      = charge1 + #{charge1},  " +
            "                    charge2      = charge2 + #{charge2},  " +
            "                    charge3      = charge3 + #{charge3},  " +
            "                    charge4      = charge4 + #{charge4},  " +
            "                    charge5      = charge5 + #{charge5},  " +
            "                    charge6      = charge6 + #{charge6},  " +
            "                    charge7      = charge7 + #{charge7},  " +
            "                    charge8      = charge8 + #{charge8},  " +
            "                    charge9      = charge9 + #{charge9},  " +
            "                    charge10= charge10 + #{charge10},  " +
            "                    charge11= charge11 + #{charge11},  " +
            "                    charge12= charge12 + #{charge12},  " +
            "                    charge13= charge13 + #{charge13},  " +
            "                    charge14= charge14 + #{charge14},  " +
            "                    charge15= charge15 + #{charge15},  " +
            "                    charge16= charge16 + #{charge16},  " +
            "                    charge17= charge17 + #{charge17},  " +
            "                    charge18= charge18 + #{charge18},  " +
            "                    charge19= charge19 + #{charge19},  " +
            "                    charge20= charge20 + #{charge20},  " +
            "                    charge21= charge21 + #{charge21},  " +
            "                    charge22= charge22 + #{charge22},  " +
            "                    charge23= charge23 + #{charge23},  " +
            "                    charge24= charge24 + #{charge24},  " +
            "                    charge25= charge25 + #{charge25},  " +
            "                    charge26= charge26 + #{charge26},  " +
            "                    charge27= charge27 + #{charge27},  " +
            "                    charge28= charge28 + #{charge28},  " +
            "                    charge29= charge29 + #{charge29},  " +
            "                    charge30= charge30 + #{charge30}  " +
            "                WHERE inpatient_no = #{inpatientNo}  " +
            "                  AND admiss_times = #{admissTimes}  " +
            "                  AND ledger_sn = #{ledgerSn} ")
    int updateZyLedgerFilechargeFee(ZyLedgerFile zyLedgerFile);
    @Update("  UPDATE ${tableName}  " +
            "                SET total_charge = total_charge + #{totalCharge},  " +
            "                    balance      = balance - #{totalChargeP},  " +
            "                    charge1      = charge1 + #{charge1},  " +
            "                    charge2      = charge2 + #{charge2},  " +
            "                    charge3      = charge3 + #{charge3},  " +
            "                    charge4      = charge4 + #{charge4},  " +
            "                    charge5      = charge5 + #{charge5},  " +
            "                    charge6      = charge6 + #{charge6},  " +
            "                    charge7      = charge7 + #{charge7},  " +
            "                    charge8      = charge8 + #{charge8},  " +
            "                    charge9      = charge9 + #{charge9},  " +
            "                    charge10= charge10 + #{charge10},  " +
            "                    charge11= charge11 + #{charge11},  " +
            "                    charge12= charge12 + #{charge12},  " +
            "                    charge13= charge13 + #{charge13},  " +
            "                    charge14= charge14 + #{charge14},  " +
            "                    charge15= charge15 + #{charge15},  " +
            "                    charge16= charge16 + #{charge16},  " +
            "                    charge17= charge17 + #{charge17},  " +
            "                    charge18= charge18 + #{charge18},  " +
            "                    charge19= charge19 + #{charge19},  " +
            "                    charge20= charge20 + #{charge20},  " +
            "                    charge21= charge21 + #{charge21},  " +
            "                    charge22= charge22 + #{charge22},  " +
            "                    charge23= charge23 + #{charge23},  " +
            "                    charge24= charge24 + #{charge24},  " +
            "                    charge25= charge25 + #{charge25},  " +
            "                    charge26= charge26 + #{charge26},  " +
            "                    charge27= charge27 + #{charge27},  " +
            "                    charge28= charge28 + #{charge28},  " +
            "                    charge29= charge29 + #{charge29},  " +
            "                    charge30= charge30 + #{charge30}  " +
            "                WHERE inpatient_no = #{inpatientNo}  " +
            "                  AND admiss_times = #{admissTimes}  ")
    int updateZyActpatient(ZyLedgerFile zyLedgerFile);
    /**
     * @description: 查询欠费下限
     * @author: lihong
     * @date: 2024/8/23 10:19
     * @param: patNo
     * @param: times
     * @return: java.math.BigDecimal
     **/
    @Select(" select act.balance + isnull(act.max_ledger, 0) + isnull(owe.owe_limit, 0)  " +
            "from zy_actpatient act,  " +
            "     zy_adtward owe  " +
            "where act.ward = owe.ward_code  " +
            "  and act.dept = owe.dept_code  " +
            "  and act.inpatient_no = #{patNo}  " +
            "  and act.admiss_times =#{times} ")
    BigDecimal selectoweLimit(@Param("patNo") String patNo, @Param("times") Integer times);

    @Update("  update zy_actpatient  " +
            "                                set intern_code = #{internCode}  " +
            "                                where inpatient_no = #{patNo}  " +
            "                                  and admiss_times = #{times}  " +
            "                                  ${condition} ")
    int updateInternCode(@Param("patNo") String patNo, @Param("times") Integer times,@Param("internCode") String internCode,@Param("condition")String condition);

    @Select("  SELECT west_drug_code," +
            "               chn_drug_code" +
            "        FROM zy_init ")
    ZyInit selectZyInit();
    @Select(" SELECT count(*)  " +
            "                  FROM zy_actpatient  " +
            "                  WHERE inpatient_no = #{inpatientNo}  " +
            "                    AND admiss_times = #{times} ")
    int selectCountZyActpatient(@Param("inpatientNo") String inpatientNo, @Param("times")Integer times);
    /**
     * @description: 查询药品接收费用
     * @author: lihong
     * @date: 2024/8/23 14:36
     * @param: patNo
     * @param: times
     * @param: sn
     * @param: westCode
     * @param: chnCode
     * @return: java.util.List<thyyxxk.webserver.entity.executeItem.ZyTmpReceiveData>
     **/
    @Select(" SELECT  " +
            "    inpatient_no,  " +
            "    admiss_times,  " +
            "    ${sn} ledger_sn,  " +
            "       order_date charge_date ,  " +
            "       drawer op_id_code ,  " +
            "       case  " +
            "           when charge_code > '00000' and charge_code < '30000' then #{westCode}  " +
            "           when charge_code > '30000' and charge_code < '40000' then #{chnCode}  " +
            "           else #{westCode} end charge_code ,  " +
            "       infant_flag,  " +
            "       '0' charge_status ,  " +
            "       retprice charge_fee ,  " +
            "       self_flag,  " +
            "       separate_flag,  " +
            "       suprice_flag suppress_flag ,  " +
            "       order_date occ_time,  " +
            "       act_order_no ,  " +
            "       dept_code,  " +
            "       ward_code,  " +
            "       amount,  " +
            "       'XXX' bill_item_code ,  " +
            "       1 p1,  " +
            "       1 p2,  " +
            "       1 p3,  " +
            "       1 p4,  " +
            "       1 p5,  " +
            "       serial,  " +
            "       group_no,  " +
            "       supply_code,  " +
            "       charge_code charge_item_code,  " +
            "       doctor_name doctor_code  " +
            "FROM yp_zy_patient  " +
            "WHERE inpatient_no = #{patNo}  " +
            "  AND admiss_times = #{times}  " +
            "  AND acct_sign = '0'  " +
            "  AND pay_self <> '2'")
    List<ZyTmpReceiveData> selectDrugZyTmpReceiveData(@Param("patNo") String patNo, @Param("times")Integer times,
                                                      @Param("sn")Integer sn, @Param("westCode")String westCode,
    /**
     * @description: 更新结账标识   acct_sign 0 未结账 1 已结账 2 住院接收
     * @author: lihong
     * @date: 2024/8/23 14:41
     * @param: patNo
     * @param: times
     * @param: sn
     * @param: westCode
     * @param: chnCode
     * @return: java.util.List<thyyxxk.webserver.entity.executeItem.ZyTmpReceiveData>
     **/                                                  @Param("chnCode")String chnCode);
    @Update(" UPDATE yp_zy_patient  " +
            "        SET acct_sign = '2'  " +
            "        where inpatient_no = #{patNo}  " +
            "          AND admiss_times = #{times}  " +
            "          AND acct_sign = '0'  " +
            "          and pay_self <> '2' ")
    void updateYpZyPatientAcctSign(@Param("patNo") String patNo, @Param("times") Integer times);
    @Select("<script>" +
            "select distinct rtrim(exec_unit) exec_unit ,rtrim(ward_code) ward_code ,rtrim(inpatient_no) inpatient_no,admiss_times,order_no  from zy_detail_charge where" +
            " inpatient_no = #{patNo}" +
            " and admiss_times = #{times}" +
            " and charge_fee &gt; 0 " +
            " and order_no in " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            " #{item}" +
            "</foreach>" +
            "</script>")
    List<ZyDetailCharge> selectZyDetailChargeExecUnitAndWardCode(@Param("list") List<BigDecimal> actOrderNos, @Param("patNo") String patNo, @Param("times") Integer times);
}
