<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.ss.apiBill.mapper.ApiBillMapper">

    <resultMap type="ApiBillVO" id="ApiBillResult" autoMapping="true"/>

    <sql id="selectApiBillVo">
        select
            sab.bill_id,
            sab.bill_no,
            sab.user_id,
            sab.access_id,
            sab.api_id,
            sab.amount,
            sab.status,
            sab.expire_time,
            sab.pay_id,
            sab.api_type,
            sab.api_num,
            sab.api_name,
            sab.api_description,
            sab.api_price,
            sab.create_time
        from ss_api_bill sab
    </sql>

    <sql id="searchCondition">
        <if test="query.billId != null "> and sab.bill_id = #{query.billId}</if>
        <if test="query.billNo != null  and query.billNo != ''"> and sab.bill_no like concat('%', #{query.billNo}, '%')</if>
        <if test="query.userId != null "> and sab.user_id = #{query.userId}</if>
        <if test="query.accessId != null "> and sab.access_id = #{query.accessId}</if>
        <if test="query.apiId != null "> and sab.api_id = #{query.apiId}</if>
        <if test="query.status != null  and query.status != ''"> and sab.status = #{query.status}</if>
        <if test="query.payId != null "> and sab.pay_id = #{query.payId}</if>
        <if test="query.apiType != null  and query.apiType != ''"> and sab.api_type = #{query.apiType}</if>
        <if test="query.apiName != null  and query.apiName != ''"> and sab.api_name like concat('%', #{query.apiName}, '%')</if>
        <if test="query.apiDescription != null  and query.apiDescription != ''"> and sab.api_description like concat('%', #{query.apiDescription}, '%')</if>
    </sql>

    <select id="selectApiBillList" parameterType="ApiBillQuery" resultMap="ApiBillResult">
        <include refid="selectApiBillVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectApiBillByBillId" parameterType="Long" resultMap="ApiBillResult">
        <include refid="selectApiBillVo"/>
        where sab.bill_id = #{billId}
    </select>

    <insert id="insertApiBill" parameterType="ApiBill" useGeneratedKeys="true" keyProperty="billId">
        insert into ss_api_bill
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="billNo != null and billNo != ''">bill_no,</if>
            <if test="userId != null">user_id,</if>
            <if test="accessId != null">access_id,</if>
            <if test="apiId != null">api_id,</if>
            <if test="amount != null">amount,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="expireTime != null">expire_time,</if>
            <if test="payId != null">pay_id,</if>
            <if test="apiType != null and apiType != ''">api_type,</if>
            <if test="apiNum != null">api_num,</if>
            <if test="apiName != null and apiName != ''">api_name,</if>
            <if test="apiDescription != null and apiDescription != ''">api_description,</if>
            <if test="apiPrice != null">api_price,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="billNo != null and billNo != ''">#{billNo},</if>
            <if test="userId != null">#{userId},</if>
            <if test="accessId != null">#{accessId},</if>
            <if test="apiId != null">#{apiId},</if>
            <if test="amount != null">#{amount},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="expireTime != null">#{expireTime},</if>
            <if test="payId != null">#{payId},</if>
            <if test="apiType != null and apiType != ''">#{apiType},</if>
            <if test="apiNum != null">#{apiNum},</if>
            <if test="apiName != null and apiName != ''">#{apiName},</if>
            <if test="apiDescription != null and apiDescription != ''">#{apiDescription},</if>
            <if test="apiPrice != null">#{apiPrice},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateApiBill" parameterType="ApiBill">
        update ss_api_bill
        <trim prefix="SET" suffixOverrides=",">
            <if test="data.billNo != null and data.billNo != ''">bill_no = #{data.billNo},</if>
            <if test="data.userId != null">user_id = #{data.userId},</if>
            <if test="data.accessId != null">access_id = #{data.accessId},</if>
            <if test="data.apiId != null">api_id = #{data.apiId},</if>
            <if test="data.amount != null">amount = #{data.amount},</if>
            <if test="data.status != null and data.status != ''">status = #{data.status},</if>
            <if test="data.expireTime != null">expire_time = #{data.expireTime},</if>
            <if test="data.payId != null">pay_id = #{data.payId},</if>
            <if test="data.apiType != null and data.apiType != ''">api_type = #{data.apiType},</if>
            <if test="data.apiNum != null">api_num = #{data.apiNum},</if>
            <if test="data.apiName != null and data.apiName != ''">api_name = #{data.apiName},</if>
            <if test="data.apiDescription != null and data.apiDescription != ''">api_description = #{data.apiDescription},</if>
            <if test="data.apiPrice != null">api_price = #{data.apiPrice},</if>
            <if test="data.createTime != null">create_time = #{data.createTime},</if>
        </trim>
        where bill_id = #{data.billId}
    </update>

    <delete id="deleteApiBillByBillId" parameterType="Long">
        delete from ss_api_bill where bill_id = #{billId}
    </delete>

    <delete id="deleteApiBillByBillIds" parameterType="String">
        delete from ss_api_bill where bill_id in
        <foreach item="billId" collection="array" open="(" separator="," close=")">
            #{billId}
        </foreach>
    </delete>
</mapper>
