<?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.store.mapper.StoreMapper">

    <resultMap type="StoreVO" id="StoreResult" autoMapping="true">
        <result property="storeId"    column="store_id"    />
        <result property="userId"    column="user_id"    />
        <result property="storeName"    column="store_name"    />
        <result property="userName"    column="user_name"    />
        <result property="phone"    column="phone"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="province"    column="province"    />
        <result property="city"    column="city"    />
        <result property="county"    column="county"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="address"    column="address"    />
        <result property="picture"    column="picture"    />
        <result property="createTime"    column="create_time"    />
        <result property="deleted"    column="deleted"    />
    </resultMap>

    <sql id="selectStoreVO">
        select
            bs.store_id,
            bs.user_id,
            bs.store_name,
            bs.user_name,
            bs.phone,
            bs.start_time,
            bs.end_time,
            bs.longitude,
            bs.latitude,
            bs.address,
            bs.picture,
            bs.province,
            bs.city,
            bs.county,
            bs.create_time
        from bst_store bs
    </sql>


    <sql id="searchTables">
        bst_store bs
    </sql>


    <sql id="searchCondition">
        <if test="query.storeId != null "> and bs.store_id = #{query.storeId}</if>
        <if test="query.userId != null "> and  bs.user_id = #{query.userId}</if>
        <if test="query.storeName != null  and query.storeName != ''"> and  bs.store_name like concat('%', #{query.storeName}, '%')</if>
        <if test="query.userName != null  and query.userName != ''"> and  bs.user_name like concat('%', #{query.userName}, '%')</if>
        <if test="query.phone != null  and query.phone != ''"> and  bs.phone like concat('%', #{query.phone}, '%')</if>
        <if test="query.startTime != null "> and  bs.start_time = #{query.startTime}</if>
        <if test="query.endTime != null "> and  bs.end_time = #{query.endTime}</if>
        <if test="query.longitude != null "> and  bs.longitude = #{query.longitude}</if>
        <if test="query.latitude != null "> and  bs.latitude = #{query.latitude}</if>
        <if test="query.address != null  and query.address != ''"> and  bs.address like concat('%', #{query.address}, '%')</if>
        <if test="query.picture != null  and query.picture != ''"> and  bs.picture = #{query.picture}</if>
        <if test="query.province != null  and query.province != ''"> and  bs.province = #{query.province}</if>
        <if test="query.city != null  and query.city != ''"> and  bs.city = #{query.city}</if>
        <if test="query.county != null  and query.county != ''"> and  bs.county = #{query.county}</if>
        <if test="query.deleted != null"> and  bs.deleted = #{query.deleted}</if>
        <if test="query.storeIds != null and query.storeIds.size() > 0">
            and bs.store_id in
            <foreach item="id" collection="query.storeIds" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        ${@com.ruoyi.framework.util.DataScopeUtil@create(query.scope)
           .userSetAlias("bs.user_id")
           .storeAlias("bs.store_id",query.storePermissions)
           .build()
       }
        ${query.params.dataScope}
    </sql>

    <select id="selectStoreList" parameterType="StoreQuery" resultMap="StoreResult">
        <include refid="selectStoreVO"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectStoreByStoreId" parameterType="Long" resultMap="StoreResult">
        <include refid="selectStoreVO"/>
        where store_id = #{storeId}
    </select>

    <select id="selectCount" resultType="java.lang.Integer">
        select count(bs.store_id)
        from <include refid="searchTables"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>
    <select id="selectIdByQuery" resultType="java.lang.Long">
        select bs.store_id from <include refid="searchTables"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <insert id="insertStore" parameterType="Store" useGeneratedKeys="true" keyProperty="storeId">
        insert into bst_store
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="storeName != null">store_name,</if>
            <if test="userName != null">user_name,</if>
            <if test="phone != null">phone,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="longitude != null">longitude,</if>
            <if test="latitude != null">latitude,</if>
            <if test="address != null">address,</if>
            <if test="picture != null">picture,</if>
            <if test="province != null">province,</if>
            <if test="city != null">city,</if>
            <if test="county != null">county,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="storeName != null">#{storeName},</if>
            <if test="userName != null">#{userName},</if>
            <if test="phone != null">#{phone},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="longitude != null">#{longitude},</if>
            <if test="latitude != null">#{latitude},</if>
            <if test="address != null">#{address},</if>
            <if test="picture != null">#{picture},</if>
            <if test="province != null">#{province},</if>
            <if test="city != null">#{city},</if>
            <if test="county != null">#{county},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <insert id="batchInsert" parameterType="Store" useGeneratedKeys="true" keyProperty="storeId">
        insert into bst_store
        <trim prefix="(" suffix=")" suffixOverrides=",">
            user_id,
            store_name,
            user_name,
            phone,
            start_time,
            end_time,
            longitude,
            latitude,
            address,
            picture,
            create_time,
        </trim>
        values
        <foreach collection="list" item="i" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="i.userId != null ">#{i.userId},</if>
                <if test="i.userId == null ">default,</if>
                <if test="i.storeName != null ">#{i.storeName},</if>
                <if test="i.storeName == null ">default,</if>
                <if test="i.userName != null ">#{i.userName},</if>
                <if test="i.userName == null ">default,</if>
                <if test="i.phone != null ">#{i.phone},</if>
                <if test="i.phone == null ">default,</if>
                <if test="i.startTime != null ">#{i.startTime},</if>
                <if test="i.startTime == null ">default,</if>
                <if test="i.endTime != null ">#{i.endTime},</if>
                <if test="i.endTime == null ">default,</if>
                <if test="i.longitude != null ">#{i.longitude},</if>
                <if test="i.longitude == null ">default,</if>
                <if test="i.latitude != null ">#{i.latitude},</if>
                <if test="i.latitude == null ">default,</if>
                <if test="i.address != null ">#{i.address},</if>
                <if test="i.address == null ">default,</if>
                <if test="i.picture != null ">#{i.picture},</if>
                <if test="i.picture == null ">default,</if>
                <if test="i.createTime != null ">#{i.createTime},</if>
                <if test="i.createTime == null ">default,</if>
            </trim>
        </foreach>
    </insert>

    <update id="batchUpdate">
        update bst_store
        <trim prefix="SET" suffixOverrides=",">
            <foreach open="user_id = CASE store_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.userId != null ">
                        WHEN #{item.store_id} THEN #{item.userId}
                    </when>
                    <otherwise>
                        WHEN #{item.store_id} THEN `user_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="store_name = CASE store_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.storeName != null ">
                        WHEN #{item.store_id} THEN #{item.storeName}
                    </when>
                    <otherwise>
                        WHEN #{item.store_id} THEN `store_name`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="user_name = CASE store_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.userName != null ">
                        WHEN #{item.store_id} THEN #{item.userName}
                    </when>
                    <otherwise>
                        WHEN #{item.store_id} THEN `user_name`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="phone = CASE store_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.phone != null ">
                        WHEN #{item.store_id} THEN #{item.phone}
                    </when>
                    <otherwise>
                        WHEN #{item.store_id} THEN `phone`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="start_time = CASE store_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.startTime != null ">
                        WHEN #{item.store_id} THEN #{item.startTime}
                    </when>
                    <otherwise>
                        WHEN #{item.store_id} THEN `start_time`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="end_time = CASE store_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.endTime != null ">
                        WHEN #{item.store_id} THEN #{item.endTime}
                    </when>
                    <otherwise>
                        WHEN #{item.store_id} THEN `end_time`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="longitude = CASE store_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.longitude != null ">
                        WHEN #{item.store_id} THEN #{item.longitude}
                    </when>
                    <otherwise>
                        WHEN #{item.store_id} THEN `longitude`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="latitude = CASE store_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.latitude != null ">
                        WHEN #{item.store_id} THEN #{item.latitude}
                    </when>
                    <otherwise>
                        WHEN #{item.store_id} THEN `latitude`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="address = CASE store_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.address != null ">
                        WHEN #{item.store_id} THEN #{item.address}
                    </when>
                    <otherwise>
                        WHEN #{item.store_id} THEN `address`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="picture = CASE store_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.picture != null ">
                        WHEN #{item.store_id} THEN #{item.picture}
                    </when>
                    <otherwise>
                        WHEN #{item.store_id} THEN `picture`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="create_time = CASE store_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.createTime != null ">
                        WHEN #{item.store_id} THEN #{item.createTime}
                    </when>
                    <otherwise>
                        WHEN #{item.store_id} THEN `create_time`
                    </otherwise>
                </choose>
            </foreach>
        </trim>
        where store_id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item.storeId}
        </foreach>
    </update>

    <update id="updateStore" parameterType="Store">
        update bst_store
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where store_id = #{data.storeId}
    </update>

    <sql id="updateColumns">
        <if test="data.userId != null">user_id = #{data.userId},</if>
        <if test="data.storeName != null">store_name = #{data.storeName},</if>
        <if test="data.userName != null">user_name = #{data.userName},</if>
        <if test="data.phone != null">phone = #{data.phone},</if>
        <if test="data.startTime != null">start_time = #{data.startTime},</if>
        <if test="data.endTime != null">end_time = #{data.endTime},</if>
        <if test="data.longitude != null">longitude = #{data.longitude},</if>
        <if test="data.latitude != null">latitude = #{data.latitude},</if>
        <if test="data.address != null">address = #{data.address},</if>
        <if test="data.picture != null">picture = #{data.picture},</if>
        <if test="data.province != null">province = #{data.province},</if>
        <if test="data.city != null">city = #{data.city},</if>
        <if test="data.county != null">county = #{data.county},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
    </sql>

    <delete id="deleteStoreByStoreId" parameterType="Long">
        delete from bst_store where store_id = #{storeId}
    </delete>

    <delete id="deleteStoreByStoreIds" parameterType="String">
        delete from bst_store where store_id in
        <foreach item="storeId" collection="array" open="(" separator="," close=")">
            #{storeId}
        </foreach>
    </delete>

    <!--logicDel-->
    <update id="logicDel">
        update bst_store
        set deleted = true
        where store_id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
        and deleted = false
    </update>
</mapper>
