<?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.system.mapper.UsageRecordMapper">
    
    <resultMap type="UsageRecord" id="UsageRecordResult">
        <result property="recordId"    column="record_id"    />

        <result property="businessId"    column="business_id"    />
        <result property="policyId"    column="policy_id"    />
        <result property="interestsId"    column="interests_id"    />

        <result property="recordYear"    column="record_year"    />
        <result property="recordType"    column="record_type"    />
        <result property="recordName"    column="record_name"    />

        <result property="recordLevel"    column="record_level"    />

        <result property="orderNum"    column="order_num"    />
        <result property="useNum"    column="use_num"    />
        <result property="creditId"    column="credit_id"    />

        <result property="creditName"    column="credit_name"    />

        <result property="creditPhone"    column="credit_phone"    />
        <result property="surplusNum"    column="surplus_num"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectUsageRecordVo">
        select record_id,business_id,policy_id,interests_id, record_year, record_type, record_name,record_level, order_num, use_num, credit_id,credit_name,credit_phone, surplus_num, create_by, create_time, update_by, update_time, remark from usage_record
    </sql>

    <select id="selectUsageRecordList" parameterType="UsageRecord" resultMap="UsageRecordResult">
        <include refid="selectUsageRecordVo"/>
        <where>
            <if test="creditId != null  and creditId != ''"> and credit_id = #{creditId}</if>
            <if test="creditPhone != null  and creditPhone != ''"> and credit_phone = #{creditPhone}</if>
            <if test="businessId != null  and businessId != ''"> and business_id = #{businessId}</if>
            <if test="creditName != null  and creditName != ''"> and credit_name like concat( #{creditName}, '%')</if>
            <if test="recordYear != null  and recordYear != ''"> and record_year = #{recordYear }</if>
            <if test="recordLevel != null  and recordLevel != ''"> and record_level = #{recordLevel}</if>
            <if test="time != null "> and create_time like concat( #{time}, '%')</if>
        </where>
    </select>
    
    <select id="selectUsageRecordByRecordId" parameterType="Long" resultMap="UsageRecordResult">
        <include refid="selectUsageRecordVo"/>
        where record_id = #{recordId}
    </select>

    <select id="selectUsageRecordListByRecordId" resultMap="UsageRecordResult">
        <include refid="selectUsageRecordVo"/>
        where credit_id = #{creditId}
        order by create_time DESC
    </select>

    <select id="selectUsageRecordByRecordNum" resultType="java.lang.Integer">
        select count(1) from usage_record where interests_id = #{interestsId} and record_year = #{year} and credit_phone = #{creditPhone}
    </select>

    <insert id="insertUsageRecord" parameterType="UsageRecord" useGeneratedKeys="true" keyProperty="recordId">
        insert into usage_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="businessId != null and businessId != ''">business_id,</if>
            <if test="policyId != null and policyId != ''">policy_id,</if>
            <if test="interestsId != null and interestsId != ''">interests_id,</if>

            <if test="recordYear != null and recordYear != ''">record_year,</if>
            <if test="recordType != null and recordType != ''">record_type,</if>
            <if test="recordName != null">record_name,</if>

            <if test="recordLevel != null">record_level,</if>

            <if test="orderNum != null">order_num,</if>
            <if test="useNum != null">use_num,</if>
            <if test="creditId != null">credit_id,</if>
            <if test="creditName != null">credit_name,</if>
            <if test="creditPhone != null">credit_phone,</if>
            <if test="surplusNum != null">surplus_num,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            create_time
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">

            <if test="businessId != null and businessId != ''">#{businessId},</if>
            <if test="policyId != null and policyId != ''">#{policyId},</if>
            <if test="interestsId != null and interestsId != ''">#{interestsId},</if>

            <if test="recordYear != null and recordYear != ''">#{recordYear},</if>
            <if test="recordType != null and recordType != ''">#{recordType},</if>
            <if test="recordName != null">#{recordName},</if>

            <if test="recordLevel != null">#{recordLevel},</if>

            <if test="orderNum != null">#{orderNum},</if>
            <if test="useNum != null">#{useNum},</if>
            <if test="creditId != null">#{creditId},</if>
            <if test="creditName != null">#{creditName},</if>
            <if test="creditPhone != null">#{creditPhone},</if>
            <if test="surplusNum != null">#{surplusNum},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            sysdate()
         </trim>
    </insert>

    <update id="updateUsageRecord" parameterType="UsageRecord">
        update usage_record
        <trim prefix="SET" suffixOverrides=",">

            <if test="businessId != null and businessId != ''">business_id = #{businessId},</if>
            <if test="policyId != null and policyId != ''">policy_id = #{policyId},</if>
            <if test="interestsId != null and interestsId != ''">interests_id = #{interestsId},</if>

            <if test="recordYear != null and recordYear != ''">record_year = #{recordYear},</if>
            <if test="recordType != null and recordType != ''">record_type = #{recordType},</if>
            <if test="recordName != null">record_name = #{recordName},</if>

            <if test="recordLevel != null">record_level = #{recordLevel},</if>

            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="useNum != null">use_num = #{useNum},</if>
            <if test="creditId != null">credit_id = #{creditId},</if>

            <if test="creditName != null">credit_name = #{creditName},</if>

            <if test="creditPhone != null">credit_phone = #{creditPhone},</if>
            <if test="surplusNum != null">surplus_num = #{surplusNum},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where record_id = #{recordId}
    </update>

    <delete id="deleteUsageRecordByRecordId" parameterType="Long">
        delete from usage_record where record_id = #{recordId}
    </delete>

    <delete id="deleteUsageRecordByRecordIds" parameterType="String">
        delete from usage_record where record_id in 
        <foreach item="recordId" collection="array" open="(" separator="," close=")">
            #{recordId}
        </foreach>
    </delete>
</mapper>