<?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="storeId"    column="store_id"    />
        <result property="areaId"    column="area_id"    />
        <result property="title"    column="title"    />
        <result property="brief"    column="brief"    />
        <result property="content"    column="content"    />
        <result property="agreementType"    column="agreement_type"    />
        <result property="duration"    column="duration"    />
        <result property="userName"    column="user_name"    />
        <result property="areaName"    column="area_name"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

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

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

    <sql id="searchCondition">
        <if test="query.storeId != null "> and bag.store_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.duration != null "> and bag.duration = #{query.duration}</if>
        ${@com.ruoyi.framework.util.DataScopeUtil@dataScope(
         null,
         "bag.store_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="com.ruoyi.bst.agreement.domain.AgreementVO">
        <include refid="selectAgreementVo"/>
        where bag.area_id = #{areaId} and bag.agreement_type = #{agreementType} and (bag.id != #{id} or #{id} is null)
    </select>

    <!--查询最新的协议信息-->
    <select id="selectLatest" resultType="com.ruoyi.bst.agreement.domain.AgreementVO">
        <include refid="selectAgreementVo"></include>
        where bag.area_id = #{areaId} and bag.agreement_type = #{agreementType}
        order by create_time desc limit 1
    </select>

    <insert id="insertAgreement" parameterType="Agreement" useGeneratedKeys="true" keyProperty="id">
        insert into bst_agreement
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="storeId != null">store_id,</if>
            <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="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="storeId != null">#{storeId},</if>
            <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="duration != null">#{duration},</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=",">
            store_id,
            area_id,
            title,
            brief,
            content,
            agreement_type,
            duration,
            create_time,
            update_time,
        </trim>
        values
        <foreach collection="list" item="i" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="i.storeId != null ">#{i.storeId},</if>
                <if test="i.storeId == null ">default,</if>
                <if test="i.areaId != null ">#{i.areaId},</if>
                <if test="i.areaId == 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.brief != null ">#{i.brief},</if>
                <if test="i.brief == null ">default,</if>
                <if test="i.content != null ">#{i.content},</if>
                <if test="i.content == null ">default,</if>
                <if test="i.agreementType != null ">#{i.agreementType},</if>
                <if test="i.agreementType == null ">default,</if>
                <if test="i.duration != null ">#{i.duration},</if>
                <if test="i.duration == null ">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="store_id = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.storeId != null ">
                        WHEN #{item.id} THEN #{item.storeId}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `store_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="area_id = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.areaId != null ">
                        WHEN #{item.id} THEN #{item.areaId}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `area_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="brief = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.brief != null ">
                        WHEN #{item.id} THEN #{item.brief}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `brief`
                    </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 ">
                        WHEN #{item.id} THEN #{item.agreementType}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `agreement_type`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="duration = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.duration != null ">
                        WHEN #{item.id} THEN #{item.duration}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `duration`
                    </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 bag
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where bag.id = #{data.id}
    </update>

    <sql id="updateColumns">
        <if test="data.storeId != null">store_id = #{data.storeId},</if>
        <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.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>
