<?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.bst.agreement.mapper.AgreementMapper">

    <resultMap type="AgreementVO" id="AgreementResult" autoMapping="true">
        <result property="id"    column="id"    />
        <result property="appId"    column="app_id"    />
        <result property="title"    column="title"    />
        <result property="content"    column="content"    />
        <result property="agreementType"    column="agreement_type"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectAgreementVo">
        select
            bag.id,
            bag.app_id,
            bag.title,
            bag.content,
            bag.agreement_type,
            bag.create_time,
            bag.update_time,
            ba.name as app_name
        from bst_agreement bag
        left join bst_app ba on bag.app_id = ba.id
    </sql>

    <sql id="searchCondition">
        <if test="query.appId != null "> and bag.app_id = #{query.appId}</if>
        <if test="query.title != null  and query.title != ''"> and bag.title like concat('%', #{query.title}, '%')</if>
        <if test="query.content != null  and query.content != ''"> and bag.content like concat('%', #{query.content}, '%')</if>
        <if test="query.agreementType != null  and query.agreementType != ''"> and bag.agreement_type = #{query.agreementType}</if>
        ${@com.ruoyi.framework.util.DataScopeUtil@create(query.scope)
        .appAlias("bag.app_id")
        .build()
        }
        ${query.params.dataScope}
    </sql>

    <select id="selectAgreementList" parameterType="AgreementQuery" resultMap="AgreementResult">
        <include refid="selectAgreementVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectAgreementById" parameterType="Long" resultMap="AgreementResult">
        <include refid="selectAgreementVo"/>
        where bag.id = #{id}
    </select>

    <insert id="insertAgreement" parameterType="Agreement" useGeneratedKeys="true" keyProperty="id">
        insert into bst_agreement
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="appId != null">app_id,</if>
            <if test="title != null and title != ''">title,</if>
            <if test="content != null">content,</if>
            <if test="agreementType != null and agreementType != ''">agreement_type,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="appId != null">#{appId},</if>
            <if test="title != null and title != ''">#{title},</if>
            <if test="content != null">#{content},</if>
            <if test="agreementType != null and agreementType != ''">#{agreementType},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <insert id="batchInsert" parameterType="Agreement" useGeneratedKeys="true" keyProperty="id">
        insert into bst_agreement
        <trim prefix="(" suffix=")" suffixOverrides=",">
            app_id,
            title,
            content,
            agreement_type,
            create_time,
            update_time,
        </trim>
        values
        <foreach collection="list" item="i" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="i.appId != null ">#{i.appId},</if>
                <if test="i.appId == null ">default,</if>
                <if test="i.title != null  and i.title != ''">#{i.title},</if>
                <if test="i.title == null  or i.title == ''">default,</if>
                <if test="i.content != null ">#{i.content},</if>
                <if test="i.content == null ">default,</if>
                <if test="i.agreementType != null  and i.agreementType != ''">#{i.agreementType},</if>
                <if test="i.agreementType == null  or i.agreementType == ''">default,</if>
                <if test="i.createTime != null ">#{i.createTime},</if>
                <if test="i.createTime == null ">default,</if>
                <if test="i.updateTime != null ">#{i.updateTime},</if>
                <if test="i.updateTime == null ">default,</if>
            </trim>
        </foreach>
    </insert>

    <update id="batchUpdate">
        update bst_agreement
        <trim prefix="SET" suffixOverrides=",">
            <foreach open="app_id = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.appId != null ">
                        WHEN #{item.id} THEN #{item.appId}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `app_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="title = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.title != null  and item.title != ''">
                        WHEN #{item.id} THEN #{item.title}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `title`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="content = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.content != null ">
                        WHEN #{item.id} THEN #{item.content}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `content`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="agreement_type = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.agreementType != null  and item.agreementType != ''">
                        WHEN #{item.id} THEN #{item.agreementType}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `agreement_type`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="create_time = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.createTime != null ">
                        WHEN #{item.id} THEN #{item.createTime}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `create_time`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="update_time = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.updateTime != null ">
                        WHEN #{item.id} THEN #{item.updateTime}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `update_time`
                    </otherwise>
                </choose>
            </foreach>
        </trim>
        where id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item.id}
        </foreach>
    </update>

    <update id="updateAgreement" parameterType="Agreement">
        update bst_agreement ba
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where ba.id = #{data.id}
    </update>

    <sql id="updateColumns">
        <if test="data.appId != null">ba.app_id = #{data.appId},</if>
        <if test="data.title != null and data.title != ''">title = #{data.title},</if>
        <if test="data.content != null">content = #{data.content},</if>
        <if test="data.agreementType != null and data.agreementType != ''">agreement_type = #{data.agreementType},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.updateTime != null">update_time = #{data.updateTime},</if>
    </sql>

    <delete id="deleteAgreementById" parameterType="Long">
        delete from bst_agreement where id = #{id}
    </delete>

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