package thyyxxk.webserver.dao.his.inpatient;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.inpatient.inpatientcost.ZyDetailChargeTable;
import thyyxxk.webserver.entity.inpatient.ZyActpatient;
import thyyxxk.webserver.service.inpatient.HospitalizationCostsService;

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

@Mapper
public interface HospitalizationCostsDao extends BaseMapper<ZyDetailChargeTable> {

    @Select("select count(1) from  yp_zd_dict where code = #{code} and serial = #{serial} and drug_kind = '0013'")
    Integer getDrugKing(String code, String serial);

    @Select("select b.class           as class_code,\n" +
            "       a.drug_flag       as drug_class,\n" +
            "       a.mini_unit,\n" +
            "       c.refundable_fees as refundable_fees\n" +
            "from yz_act_order a,\n" +
            "     yz_supply_type b,\n" +
            "     yp_zd_unit c\n" +
            "where a.supply_code = b.supply_code\n" +
            "  and c.code = a.mini_unit\n" +
            "  and a.act_order_no = #{order}\n" +
            "union all\n" +
            "select b.class           as class_code,\n" +
            "       a.drug_flag       as drug_class,\n" +
            "       a.mini_unit,\n" +
            "       c.refundable_fees as refundable_fees\n" +
            "from yz_erase_order a,\n" +
            "     yz_supply_type b,\n" +
            "     yp_zd_unit c\n" +
            "where a.supply_code = b.supply_code\n" +
            "  and c.code = a.mini_unit\n" +
            "  and a.act_order_no = #{order}")
    HospitalizationCostsService.OrderInfo selectDrugClassByOrderNo(BigDecimal order);

    /**
     * 插入数据
     *
     * @param list 插入退费的数据 生成一条一样的数据 只是 费用和数量不一致 以及录入人 和 录入时间 ori_detail_sn 保存退费的原流水号
     */
    @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, retprice, charge_amount, charge_fee, self_flag, separate_flag," +
            " suppress_flag, ward_code, dept_code, order_no, ope_flag, exec_unit, gen_time, " +
            "pay_percent, zj_flag, zfl, charge_code_mx, serial, zy_serial_no, doctor_code, " +
            "yb_self_flag, orig_charge_fee, confirm_id, trans_flag_yb, ss_code, gs_flag, " +
            "refer_physician, consult_physician, ori_detail_sn)   " +
            "values " +
            "<foreach collection='list' item='item' index='index' separator=','>" +
            "(#{item.inpatientNo},#{item.admissTimes},#{item.ledgerSn},#{item.detailSn}," +
            "#{item.chargeDate},#{item.opIdCode},#{item.chargeCode},#{item.infantFlag}," +
            "#{item.chargeStatus},cast(#{item.retprice,jdbcType=DECIMAL} as decimal(14,5))," +
            "cast(#{item.chargeAmount,jdbcType=DECIMAL} as decimal(14,5))," +
            "cast(#{item.chargeFee,jdbcType=DECIMAL} as decimal(14,5))," +
            "#{item.selfFlag},#{item.separateFlag},#{item.suppressFlag},#{item.wardCode},#{item.deptCode},#{item.orderNo}," +
            "#{item.opeFlag},#{item.execUnit},#{item.genTime}," +
            "cast(#{item.payPercent,jdbcType=DECIMAL} as decimal(14,5)),#{item.zjFlag},cast(#{item.zfl,jdbcType=DECIMAL} as decimal(14,5)),#{item.chargeCodeMx},#{item.serial},#{item.zySerialNo}," +
            "#{item.doctorCode}," +
            "#{item.ybSelfFlag},cast(#{item.origChargeFee,jdbcType=DECIMAL} as decimal(14,5)),#{item.confirmId},0," +
            "#{item.ssCode},#{item.gsFlag}," +
            "#{item.referPhysician},#{item.consultPhysician},#{item.oriDetailSn})" +
            "</foreach>" +
            "</script>")
    void insertRefundData(@Param("list") List<ZyDetailChargeTable> list);


    @Insert("<script>" +
            "insert into yz_yp_zy_order (inpatient_no, admiss_times, name, bed_no, dept_code, ward_code, charge_code, amount, " +
            "                            retprice, charge_date, infant_flag, drawer, always_flag, status_flag, drug_class,  " +
            "                            occ_time, act_order_no, serial, group_no, doctor_name, page_no_ty,ledger_sn,detail_sn,serail,page_class) " +
            "values " +
            "<foreach collection='list' item='item' index='index' separator=','>" +
            "(#{patNo}, #{times}, #{name}, #{bedNo}, #{wardCode},  #{wardCode}, #{item.chargeCodeMx}, #{item.chargeAmount,jdbcType=REAL}, " +
            "       (select pack_retprice from yp_zd_dict where code = #{item.chargeCodeMx} and serial = #{item.serial}), getdate(), #{infantFlag}, #{userCode}, 'a', '1', " +
            "       #{item.drugClass},  getdate(), ${index}, #{item.serial}, #{groupNo}, " +
            "       #{item.doctorCode}, '-1',#{ledgerSn},#{item.oriDetailSn},0,'HT')" +
            "</foreach>" +
            "</script>")
    void detailsOfDrugReturnForm(@Param("list") List<ZyDetailChargeTable> list,
                                 @Param("patNo") String patNo,
                                 @Param("times") Integer times,
                                 @Param("name") String name,
                                 @Param("bedNo") String bedNo,
                                 @Param("wardCode") String wardCode,
                                 @Param("infantFlag") Integer infantFlag,
                                 @Param("groupNo") String groupNo,
                                 @Param("userCode") String userCode,
                                 @Param("ledgerSn") Integer ledgerSn);


    @Insert("<script>" +
            "insert into zy_drug ( inpatient_no , admiss_times , ledger_sn , detail_sn , charge_date , op_id_code ," +
            " charge_code , infant_flag , " +
            "charge_fee , self_flag , separate_flag , supress_flag , ward_code , dept_code , ope_flag ," +
            " exec_unit , retprice , amount , zy_serial_no , " +
            "serial , group_no , occ_cate ) values " +
            "<foreach collection='list' item='item' index='index' separator=','>" +
            "( #{yz.inpatientNo} , #{yz.admissTimes} , #{ledgerSn} , #{item.detailSn} , DATEADD(second, ${index}, GETDATE()) , #{opIdCode} , " +
            "#{item.chargeCodeMx} ,  #{infantFlag} , " +
            // 金额
            "cast(#{item.chargeFee,jdbcType=DECIMAL} as decimal(14,5))," +
            " '0' , '0' , null , #{yz.ward} , #{yz.dept} , '0' , #{item.execUnit} ," +
            // 单价
            "  cast(#{item.chargeFee,jdbcType=DECIMAL} / #{item.chargeAmount,jdbcType=DECIMAL} as decimal(14,5)) , " +
            // 数量
            "cast(#{item.chargeAmount,jdbcType=DECIMAL} as decimal(14,5)) " +
            ", #{yz.zySerialNo} , #{item.serial} , '73' ,'1' )" +
            "</foreach>" +
            "</script>")
    void shenQingYaoPing(@Param("yz") ZyActpatient yz,
                         @Param("list") List<ZyDetailChargeTable> list,
                         @Param("infantFlag") int infantFlag,
                         @Param("ledgerSn") Integer ledgerSn,
                         @Param("opIdCode") String opIdCode);


    @Update("<script>" +
            "<foreach collection='list' item='item' separator=';'>" +
            "update zy_detail_charge " +
            "set ori_detail_sn = #{item.oriDetailSn} " +
            "where inpatient_no = #{item.inpatientNo} " +
            "  and admiss_times = #{item.admissTimes} " +
            "  and ledger_sn = #{item.ledgerSn} " +
            "  and detail_sn = #{item.detailSn}" +
            "</foreach> " +
            "</script>")
    void updateOriFlag(@Param("list") List<ZyDetailChargeTable> list);
}
