<?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.tmerclub.cloud.multishop.mapper.ShopCollectionMapper">
  <resultMap id="userCollectionShopMap" type="com.tmerclub.cloud.multishop.model.ShopCollection">
    <id column="collection_id" property="collectionId" />
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="shop_id" property="shopId"/>
    <result column="user_id" property="userId"/>
  </resultMap>
  <sql id="Vo_Column_List">
    sc.`collection_id`,sc.`create_time`,sc.`update_time`,sc.`shop_id`,sc.`user_id`
  </sql>

  <select id="listUserCollectionShopPageByUserIdAndShopName" resultType="com.tmerclub.cloud.multishop.vo.ShopCollectionVO">
    select <include refid="Vo_Column_List"/>,s.shop_id,s.shop_name,s.shop_logo,s.shop_status,s.type,se.collection_num, sr.renovation_id from shop_collection sc
    left join shop_detail s on s.shop_id = sc.shop_id
    left join shop_extension se on se.shop_id = s.shop_id
    left join shop_renovation sr on s.shop_id = sr.shop_id and sr.renovation_type = 2 and sr.home_status = 1
    <where>
      <if test="userId != null">
        and user_id = #{userId}
      </if>
      <if test="shopName != null">
        and shop_name like concat('%', #{shopName}, '%')
      </if>
    </where>
    order by collection_id desc
  </select>

  <select id="getByCollectionId" resultType="com.tmerclub.cloud.multishop.vo.ShopCollectionVO">
    select <include refid="Vo_Column_List"/> from shop_collection sc where collection_id = #{collectionId}
  </select>

  <select id="countByShopIdAndUserId" resultType="int">
    select ifnull(count(*),0) from shop_collection
    <where>
      <if test="userId != null">
        and user_id = #{userId}
      </if>
      <if test="shopId != null">
        and shop_id = #{shopId}
      </if>
    </where>
  </select>

  <insert id="save">
    insert into shop_collection (`shop_id`,`user_id`)
    values (#{userCollectionShop.shopId},#{userCollectionShop.userId});
  </insert>
  <update id="update">
    update shop_collection
    <set>
      <if test="userCollectionShop.createTime != null">
        `create_time` = #{userCollectionShop.createTime},
      </if>
      <if test="userCollectionShop.updateTime != null">
        `update_time` = #{userCollectionShop.updateTime},
      </if>
      <if test="userCollectionShop.shopId != null">
        `shop_id` = #{userCollectionShop.shopId},
      </if>
      <if test="userCollectionShop.userId != null">
        `user_id` = #{userCollectionShop.userId},
      </if>
    </set>
    where collection_id = #{userCollectionShop.collectionId}
  </update>
  <delete id="deleteByShopIdAndUserId">
    delete from shop_collection
    where user_id = #{userId} and shop_id = #{shopId}
  </delete>

</mapper>
