<?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.cftech.collection.dao.CollectionMapper">

    <resultMap id="resultMap" type="com.cftech.collection.model.Collection">
        <id column="id" property="id"/>
        <result column="accounts_id" property="accountsId"/>
        <result column="module_type" property="moduleType"/>
        <result column="entity_id" property="entityId"/>
        <result column="type" property="type"/>
        <result column="user_id" property="userId"/>
        <result column="create_time" property="createTime"/>
        <result column="del_flag" property="delFlag"/>
    </resultMap>

    <resultMap id="resultMapDto" type="com.cftech.collection.model.CollectionDto">
        <id column="id" property="id"/>
        <result column="accounts_id" property="accountsId"/>
        <result column="module_type" property="moduleType"/>
        <result column="entity_id" property="entityId"/>
        <result column="type" property="type"/>
        <result column="user_id" property="userId"/>
        <result column="create_time" property="createTime"/>
        <result column="del_flag" property="delFlag"/>
        <result column="entityName" property="entityName"/>
        <result column="userName" property="userName"/>
        <result column="flashpoint" property="flashpoint"/>
        <result column="likeNo" property="likeNo"/>
        <result column="collectionNo" property="collectionNo"/>
        <result column="scanNo" property="scanNo"/>
        <result column="optUpdateTime" property="optUpdateTime"/>
        <result column="title" property="title"/>
        <result column="store_id" property="storeId"/>
    </resultMap>
    <sql id="sqlWhere">
        <if test="conds!=null">
            <trim prefix="WHERE" prefixOverrides="AND|OR">
                <foreach collection="conds.conds" index="index" item="cond">
                    ${cond.linkType}
                    <if test="cond.condType == 'EQUAL'">${cond.param} = #{cond.value}</if>
                    <if test="cond.condType == 'NOTEQUAL'">${cond.param} &lt;&gt; #{cond.value}</if>
                    <if test="cond.condType == 'GREATEQUAL'">${cond.param} &gt;= #{cond.value}</if>
                    <if test="cond.condType == 'GREATTHAN'">${cond.param} &gt; #{cond.value}</if>
                    <if test="cond.condType == 'LESSEQUAL'">${cond.param} &lt;= #{cond.value}</if>
                    <if test="cond.condType == 'LESSTHAN'">${cond.param} &lt; #{cond.value}</if>
                    <if test="cond.condType == 'BETWEEN'">${cond.param} BETWEEN #{cond.startValue} AND
                        #{cond.endValue}
                    </if>
                    <if test="cond.condType == 'ISNULL'">${cond.param} IS NULL</if>
                    <if test="cond.condType == 'NOTNULL'">${cond.param} IS NOT NULL</if>
                    <if test="cond.condType == 'LIKE'">${cond.param} LIKE #{cond.value}</if>
                    <if test="cond.condType == 'IN'">${cond.param} IN
                        <foreach item="item" index="index" collection="cond.value" open="(" separator="," close=")">
                            #{item}
                        </foreach>
                    </if>
                </foreach>
            </trim>
        </if>
    </sql>

    <sql id="sqlColumns">
		id, accounts_id, module_type, entity_id, type, user_id, create_time, del_flag
	</sql>

    <insert id="save" parameterType="com.cftech.collection.model.Collection" useGeneratedKeys="true" keyProperty="id">
		insert into wx_collection
		(
		id, accounts_id, module_type, entity_id, type, user_id, create_time, del_flag,store_id
		)
		values
		(
		#{id, jdbcType=BIGINT},  #{accountsId, jdbcType=BIGINT},  #{moduleType, jdbcType=CHAR},  #{entityId, jdbcType=BIGINT},  
		#{type, jdbcType=CHAR},  #{userId, jdbcType=VARCHAR},  now(),  #{delFlag, jdbcType=TINYINT},  #{storeId, jdbcType=BIGINT}
		)
	</insert>

    <select id="fetchById" parameterType="java.lang.Long" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM wx_collection t
        WHERE t.id=#{id}
    </select>

    <select id="count" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM wx_collection
        <include refid="sqlWhere"/>
    </select>

    <select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM wx_collection t
        <include refid="sqlWhere"/>
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
        <if test="limit>0">limit #{offset},#{limit}</if>
    </select>

    <select id="findCollectionByPageVoCount" parameterType="java.util.Map"
            resultType="java.lang.Integer">
        SELECT
        count(1)
        FROM
        wx_collection c
        LEFT JOIN wx_mp_member m ON m.open_id = c.user_id
        LEFT JOIN wx_mp_exhibi e ON e.id = c.entity_id
        <include refid="sqlWhere"/>
        <if test="entityName!=null">
            and e.exhibi_title like #{entityName}
        </if>
        <if test="userName != null">
            and m.`name` like #{userName}
        </if>
    </select>

    <select id="findCollectionByPageVo" parameterType="java.util.Map"
            resultMap="resultMapDto">

        SELECT c.*,m.`name` userName,e.exhibi_title entityName FROM
        wx_collection c
        LEFT JOIN wx_mp_member m ON m.open_id = c.user_id
        LEFT JOIN wx_mp_exhibi e ON e.id = c.entity_id
        <include refid="sqlWhere"/>
        <if test="entityName!=null">
            and e.exhibi_title like #{entityName}
        </if>
        <if test="userName != null">
            and m.`name` like #{userName}
        </if>
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
        <if test="limit>0">limit #{offset},#{limit}</if>
    </select>

    <select id="findCollectionByPageVoCar" parameterType="java.util.Map"
            resultMap="resultMapDto">
        SELECT
        c.*, cars. NAME entityName,
        store. NAME storeName,
        store.address storeAddress,
        store.longitude,
        store.latitude,
        cars.guide_price price,
        substring_index(cars.imgs, ',',1) img
        FROM
        wx_collection c
        inner JOIN t_fours_base_cars cars ON cars.id = c.entity_id
        AND c.module_type = 1
        LEFT JOIN t_dc_store store ON store.id = c.store_id
        <include refid="sqlWhere"/>
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
        <if test="limit>0">limit #{offset},#{limit}</if>
    </select>

    <select id="findCollectionByPageVoCfarticle" parameterType="java.util.Map"
            resultMap="resultMapDto">
        SELECT
        c.*, cfarticle. title entityName,cfarticle.smallimg img
        FROM
        wx_collection c
        inner JOIN wx_cms_cfarticle cfarticle ON cfarticle.id = c.entity_id AND c.module_type = 2
        <include refid="sqlWhere"/>
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
        <if test="limit>0">limit #{offset},#{limit}</if>
    </select>

    <select id="findCollectionByPageVoActivity" parameterType="java.util.Map"
            resultMap="resultMapDto">
        SELECT
        c.*, m. NAME entityName,
        store. NAME storeName,
        store.id storeId,
        m.smallimg img,
        m.actstarttime,
        m.actendtime,
        (
        SELECT
        count(0)
        FROM
        wx_activity_market_sign
        WHERE
        activityid = c.entity_id
        ) signSum,
        (
        SELECT
        COUNT(1)
        FROM
        wx_activity_market_sign e
        WHERE
        e.activityid =c.entity_id
        AND e.storeid = store.id
        <if test="openId!=null">
            AND e.openid=#{openId}
        </if>
        ) isenroll
        FROM
        wx_collection c
        INNER JOIN wx_activity_market m ON m.id = c.entity_id
        AND c.module_type = 3
        LEFT JOIN t_dc_store store ON store.id = c.store_id
        <include refid="sqlWhere"/>
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
        <if test="limit>0">limit #{offset},#{limit}</if>
    </select>

    <update id="update" parameterType="com.cftech.collection.model.Collection">
        update wx_collection
        <set>
            <if test="id != null">
                id = #{id, jdbcType=BIGINT},
            </if>
            <if test="accountsId != null">
                accounts_id = #{accountsId, jdbcType=BIGINT},
            </if>
            <if test="moduleType != null">
                module_type = #{moduleType, jdbcType=CHAR},
            </if>
            <if test="entityId != null">
                entity_id = #{entityId, jdbcType=BIGINT},
            </if>
            <if test="type != null">
                type = #{type, jdbcType=CHAR},
            </if>
            <if test="userId != null">
                user_id = #{userId, jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                create_time = #{createTime, jdbcType=TIMESTAMP},
            </if>
            <if test="delFlag != null">
                del_flag = #{delFlag, jdbcType=TINYINT},
            </if>
        </set>
        where id=#{id,jdbcType=BIGINT}
    </update>

    <update id="delete" parameterType="java.lang.Long">
		update wx_collection set delflag=1 where id=#{id,jdbcType=BIGINT}
	</update>

    <!--删除-->
    <delete id="deleteForever" parameterType="java.util.Map">
        update wx_collection set del_flag=1
        <include refid="sqlWhere"/>
    </delete>

    <!--生成log-->
    <insert id="saveLog" parameterType="com.cftech.collection.model.Collection" useGeneratedKeys="true"
            keyProperty="id">
		insert into wx_collection_log
		(
		id, accounts_id, module_type, entity_id, type, user_id, create_time, del_flag,store_id
		)
		values
		(
		#{id, jdbcType=BIGINT},  #{accountsId, jdbcType=BIGINT},  #{moduleType, jdbcType=CHAR},  #{entityId, jdbcType=BIGINT},
		#{type, jdbcType=CHAR},  #{userId, jdbcType=VARCHAR},  now(),  #{delFlag, jdbcType=CHAR},  #{storeId, jdbcType=BIGINT}
		)
	</insert>
    <delete id="deleteLog" parameterType="java.util.Map">
        update wx_collection_log set del_flag = 1
        <include refid="sqlWhere"/>
    </delete>

    <!--统计实体收藏总数-->
    <select id="getCollectionCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT
        COUNT(1)
        FROM
        wx_collection t
        <include refid="sqlWhere"/>
    </select>

    <select id="updateexhibi" parameterType="java.util.Map">
        UPDATE wx_mp_exhibi set selects = selects + #{count} WHERE  id = #{id}
    </select>

</mapper>