package thyyxxk.webserver.dao.his.executeItem;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import thyyxxk.webserver.entity.executeItem.YzZyPatientFee;

import java.util.List;

/**
 * (YzZyPatientFee)表数据库访问层
 *
 * @author lihong
 * @since 2024-01-08 16:23:09
 */
@Mapper
public interface YzZyPatientFeeDao extends BaseMapper<YzZyPatientFee> {
    /**
     * @description: 修改收费状态 状态改为 无效 生成药品或诊疗 最后执行
     * @author: lihong
     * @date: 2024/1/8 17:29
     * @param: wardCode
     * @return: int
     **/
    @Update(" update yz_zy_patient_fee " +
            " set charge_status ='2' " +
            " from yz_act_order " +
            " where yz_zy_patient_fee.dept_code = #{wardCode} " +
            "  and yz_zy_patient_fee.ward_code = #{wardCode} " +
            "  and yz_zy_patient_fee.occ_time > yz_act_order.end_time " +
            "  and yz_zy_patient_fee.charge_status <> '0' " +
            "  and yz_act_order.frequ_code <> 'ONCE' " +
            "  and (yz_zy_patient_fee.act_order_no = yz_act_order.act_order_no or " +
            "       - yz_zy_patient_fee.act_order_no = yz_act_order.act_order_no) " +
            "  and (yz_zy_patient_fee.order_count > 0 or isnull(yz_act_order.drug_quan, 1) < 0)")
    int updateChargeStatus(String wardCode);
    /**
     * @description: 将金额改为负,状态改为 未接收 生成药品或诊疗 最后执行
     * @author: lihong
     * @date: 2024/1/8 17:28
     * @param: wardCode
     * @return: int
     **/
    @Update(" update yz_zy_patient_fee " +
            " set order_count   =0 - order_count, " +
            "    charge_status ='1' " +
            " from yz_act_order " +
            " where yz_zy_patient_fee.dept_code = #{wardCode} " +
            "  and yz_zy_patient_fee.ward_code = #{wardCode} " +
            "  and yz_zy_patient_fee.occ_time > yz_act_order.end_time " +
            "  and yz_zy_patient_fee.order_count > 0 " +
            "  and (yz_zy_patient_fee.charge_status = '0' or yz_zy_patient_fee.pay_self >= '2') " +
            "  and yz_act_order.frequ_code <> 'ONCE' " +
            "  and (yz_zy_patient_fee.act_order_no = yz_act_order.act_order_no or " +
            "       - yz_zy_patient_fee.act_order_no = yz_act_order.act_order_no) " +
            "  and (yz_zy_patient_fee.order_count > 0 or isnull(yz_act_order.drug_quan, 1) < 0)")
    int updateChargeStatusAndOrderCount(String wardCode);
    /**
     * @description: 更新状态为接收状态
     * @author: lihong
     * @date: 2024/8/22 11:12
     * @param: inpatientNo
     * @param: times
     * @return: int
     **/
    @Update(" UPDATE yz_zy_patient_fee  " +
            "        SET charge_status = '0'  " +
            "        where inpatient_no = #{inpatientNo}  " +
            "          AND admiss_times = #{times}  " +
            "          AND ((charge_status = '1') or  " +
            "               charge_status = '5' or charge_status = '6')  " +
            "          and isnull(pay_self, '0') not in ('3', '2')  " +
            "          and charge_fee <> 0 ")
    int updateChargeStatusReceived(@Param("inpatientNo") String inpatientNo,@Param("times") Integer times);

    @Insert("<script> " +
            " insert into yz_zy_patient_fee (inpatient_no, admiss_times, ledger_sn, detail_sn, charge_date, op_id_code, charge_code, infant_flag, charge_status, charge_fee, occ_time, act_order_no, dept_code, ward_code, order_count, exec_unit, zy_serial_no, op_flag, op_id, op_date, yb_self_flag ) values " +
            "<foreach collection='list' item='item'  separator=','>"+
            "( #{item.inpatientNo},#{item.admissTimes},#{item.ledgerSn},#{item.detailSn},#{item.chargeDate,jdbcType=TIMESTAMP},#{item.opIdCode},#{item.chargeCode},#{item.infantFlag},#{item.chargeStatus},#{item.chargeFee,jdbcType=DECIMAL},#{item.occTime,jdbcType=TIMESTAMP},${item.actOrderNoStr},#{item.deptCode},#{item.wardCode},#{item.orderCount,jdbcType=DECIMAL},#{item.execUnit},#{item.zySerialNo},#{item.opFlag},#{item.opId},#{item.opDate,jdbcType=TIMESTAMP},#{item.ybSelfFlag} )"+
            "</foreach>" +
            "</script>")
    int batchInsertData(List<YzZyPatientFee> list);

}

