<?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.kmall.admin.dao.CollectDao">

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.kmall.admin.entity.CollectEntity" id="collectMap">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="valueId" column="value_id"/>
        <result property="valueName" column="value_name"/>
        <result property="addTime" column="add_time"/>
        <result property="isAttention" column="is_attention"/>
        <result property="typeId" column="type_id"/>
    </resultMap>

    <select id="queryObject" resultType="com.kmall.admin.entity.CollectEntity">
		select * from mall_collect where id = #{value}
	</select>

    <select id="queryList" resultType="com.kmall.admin.entity.CollectEntity">
        select distinct
          a.* , b.nickname as user_name,c.name as value_name
        from mall_collect a
        left join mall_user b on a.user_id = b.id
        left join mall_goods c on a.value_id = c.id
        LEFT JOIN mall_merch_user d ON a.user_id=d.user_id
        WHERE 1=1
        <if test="storeId != null">
            and d.store_id = #{storeId}
        </if>
        <if test="merchSn != null and merchSn.trim() != ''">
            and d.merch_sn = #{merchSn}
        </if>
        <if test="thirdPartyMerchCode != null and thirdPartyMerchCode.trim() != ''">
            AND c.third_party_merch_code = #{thirdPartyMerchCode}
        </if>
        <if test="name != null and name != ''">
            AND b.username LIKE concat('%',#{name},'%')
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by a.id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="queryTotal" resultType="int">
		select count(distinct a.id) from mall_collect a
        left join mall_user b on a.user_id = b.id
        left join mall_goods c on a.value_id = c.id
        LEFT JOIN mall_merch_user d ON a.user_id=d.user_id
        WHERE 1=1
        <if test="storeId != null">
            and d.store_id = #{storeId}
        </if>
        <if test="merchSn != null and merchSn.trim() != ''">
            and d.merch_sn = #{merchSn}
        </if>
        <if test="thirdPartyMerchCode != null and thirdPartyMerchCode.trim() != ''">
            AND c.third_party_merch_code = #{thirdPartyMerchCode}
        </if>
        <if test="name != null and name != ''">
            AND b.username LIKE concat('%',#{name},'%')
        </if>
	</select>

    <insert id="save" parameterType="com.kmall.admin.entity.CollectEntity" useGeneratedKeys="true" keyProperty="id">
		insert into mall_collect
		(
			`user_id`, 
			`value_id`, 
			`add_time`, 
			`is_attention`, 
			`type_id`
		)
		values
		(
			#{userId}, 
			#{valueId}, 
			#{addTime}, 
			#{isAttention}, 
			#{typeId}
		)
	</insert>

    <update id="update" parameterType="com.kmall.admin.entity.CollectEntity">
        update mall_collect
        <set>
            <if test="userId != null">`user_id` = #{userId},</if>
            <if test="valueId != null">`value_id` = #{valueId},</if>
            <if test="addTime != null">`add_time` = #{addTime},</if>
            <if test="isAttention != null">`is_attention` = #{isAttention},</if>
            <if test="typeId != null">`type_id` = #{typeId}</if>
        </set>
        where id = #{id}
    </update>

    <delete id="delete">
		delete from mall_collect where id = #{value}
	</delete>

    <delete id="deleteBatch">
        delete from mall_collect where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>