<?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.ss.mapping.mapper.StoreMappingMapper">

    <resultMap type="StoreMappingVO" id="StoreMappingResult" autoMapping="true" />

    <sql id="selectStoreMappingVo">
        select
            sm.store_id,
            sm.tp_store_id,
            s.name as storeName,
            sm.type,
            sm.create_time
        from ss_store_mapping sm
        left join ss_store s on sm.store_id = s.store_id
    </sql>

    <sql id="searchCondition">
        <if test="query.tpStoreId != null  and query.tpStoreId != ''"> and sm.tp_store_id = #{query.tpStoreId}</if>
        <if test="query.type != null  and query.type != ''"> and sm.type = #{query.type}</if>
    </sql>

    <select id="selectStoreMappingList" parameterType="StoreMappingQuery" resultMap="StoreMappingResult">
        <include refid="selectStoreMappingVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectStoreMappingByStoreId" parameterType="Long" resultMap="StoreMappingResult">
        <include refid="selectStoreMappingVo"/>
        where sm.store_id = #{storeId}
    </select>

    <insert id="insertStoreMapping" parameterType="StoreMapping">
        insert into ss_store_mapping
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="storeId != null">store_id,</if>
            <if test="tpStoreId != null and tpStoreId != ''">tp_store_id,</if>
            <if test="type != null and type != ''">type,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="storeId != null">#{storeId},</if>
            <if test="tpStoreId != null and tpStoreId != ''">#{tpStoreId},</if>
            <if test="type != null and type != ''">#{type},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

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

    <sql id="updateColumns">
        <if test="data.tpStoreId != null and data.tpStoreId != ''">tp_store_id = #{data.tpStoreId},</if>
        <if test="data.type != null and data.type != ''">type = #{data.type},</if>
    </sql>

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

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