<?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"/>

    <sql id="selectAgreementVo">
        select
            bag.id,
            bag.area_id,
            bag.title,
            bag.brief,
            bag.content,
            bag.content_type,
            bag.agreement_type,
            bag.duration,
            bag.create_time,
            bag.update_time,
            su.nick_name as user_name,
            ba.name as area_name,
            ba.user_id as store_id,
            su.agent_id as agent_id
        from <include refid="searchTables"/>
    </sql>

    <sql id="searchTables">
        bst_agreement bag
            left join bst_area ba on bag.area_id = ba.id
            left join sys_user su on ba.user_id = su.user_id
    </sql>

    <sql id="searchCondition">
        <if test="query.storeId != null "> and ba.user_id = #{query.storeId}</if>
        <if test="query.areaId != null "> and bag.area_id = #{query.areaId}</if>
        <if test="query.title != null  and query.title != ''"> and bag.title like concat('%', #{query.title}, '%')</if>
        <if test="query.brief != null  and query.brief != ''"> and bag.brief like concat('%', #{query.brief}, '%')</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>
        <if test="query.contentType != null  and query.contentType != ''"> and bag.content_type = #{query.contentType}</if>
        <if test="query.userName != null  and query.userName != ''"> and su.nick_name like concat('%', #{query.userName}, '%')</if>
        <if test="query.duration != null "> and bag.duration = #{query.duration}</if>
        <if test="query.ids != null and query.ids.size() > 0">
            and bag.id in
            <foreach collection="query.ids" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        ${@com.ruoyi.framework.util.DataScopeUtil@dataScope(
            null,
            "ba.user_id,su.agent_id",
            null,
            null,
            query.scope
        )}
        ${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>


    <select id="selectDistinct" resultType="java.lang.Integer">
        select count(1) from <include refid="searchTables"/>
        <where>
            <include refid="searchCondition"/>
            and bag.id != #{query.id}
        </where>
    </select>

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

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

    <sql id="updateColumns">
        <if test="data.areaId != null">area_id = #{data.areaId},</if>
        <if test="data.title != null and data.title != ''">title = #{data.title},</if>
        <if test="data.brief != null">brief = #{data.brief},</if>
        <if test="data.content != null">content = #{data.content},</if>
        <if test="data.agreementType != null">agreement_type = #{data.agreementType},</if>
        <if test="data.contentType != null">content_type = #{data.contentType},</if>
        <if test="data.duration != null">duration = #{data.duration},</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>
