<?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.member.dao.MemberTagsMapper">

    <resultMap id="resultMap" type="com.cftech.member.model.MemberTags">
        <id column="id" property="id"/>
        <result column="number" property="number"/>
        <result column="tag_code" property="tagCode"/>
        <result column="tag_name" property="tagName"/>
        <result column="accounts_id" property="accountsId"/>
        <result column="del_flag" property="delFlag"/>
        <result column="status" property="status"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="description" property="description"/>
        <result column="create_by" property="createBy"/>
        <result column="update_by" property="updateBy"/>
    </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,
                                                number,
                                                tag_code,
                                                tag_name,
                                                accounts_id,
                                                del_flag,
                                                status,
                                                create_time,
                                                update_time,
                                                description,
                                                create_by,
                                                update_by
                        </sql>


    <insert id="save" parameterType="com.cftech.member.model.MemberTags" useGeneratedKeys="true"
            keyProperty="id">
        insert into t_dc_member_tags
        (
        <include refid="sqlColumns"/>
        )
        values
        (
        #{id, jdbcType=BIGINT},
        #{number, jdbcType=VARCHAR},
        #{tagCode, jdbcType=VARCHAR},
        #{tagName, jdbcType=VARCHAR},
        #{accountsId, jdbcType=BIGINT},
        #{delFlag, jdbcType=TINYINT},
        #{status, jdbcType=VARCHAR},
        now(),
        now(),
        #{description, jdbcType=VARCHAR},
        #{createBy, jdbcType=BIGINT},
        #{updateBy, jdbcType=BIGINT}
        )
    </insert>

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

    <select id="count" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT
        count(0)
        FROM
        (
        SELECT
        memberTags.id,
        memberTags.number,
        memberTags.tag_code,
        memberTags.tag_name,
        memberTags.accounts_id,
        memberTags.del_flag,
        memberTags.STATUS,
        memberTags.create_time,
        memberTags.update_time,
        memberTags.description,
        memberTags.create_by,
        memberTags.update_by,
        group_concat(card.title SEPARATOR '|') cardNames
        FROM
        t_dc_member_tags memberTags
        LEFT JOIN t_dc_tag_card tagCard ON memberTags.id = tagCard.tag_id
        LEFT JOIN wx_mp_card card ON card.id = tagCard.card_id
        GROUP BY
        memberTags.id
        ) memberTagsTemp
        <include refid="sqlWhere"/>

    </select>

    <select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        *
        FROM
        (
        SELECT
        memberTags.id,
        memberTags.number,
        memberTags.tag_code,
        memberTags.tag_name,
        memberTags.accounts_id,
        memberTags.del_flag,
        memberTags.STATUS,
        memberTags.create_time,
        memberTags.update_time,
        memberTags.description,
        memberTags.create_by,
        memberTags.update_by,
        group_concat(card.title SEPARATOR '|') cardNames
        FROM
        t_dc_member_tags memberTags
        LEFT JOIN t_dc_tag_card tagCard ON memberTags.id = tagCard.tag_id
        LEFT JOIN wx_mp_card card ON card.id = tagCard.card_id
        GROUP BY
        memberTags.id
        ) memberTagsTemp
        <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="fetchSearchCardTagsByPage" parameterType="java.util.Map" resultType="com.cftech.member.model.TagsCards">
        select tagCard.card_id cardId,card.title cardName,tagCard.sort from t_dc_tag_card tagCard
        left join wx_mp_card card on card.id = tagCard.card_id
        where tagCard.tag_id=#{tagId}
    </select>
    <update id="update" parameterType="com.cftech.member.model.MemberTags">
        update t_dc_member_tags
        <set>
            <if test="id != null">
                id = #{id, jdbcType=BIGINT},
            </if>
            <if test="number != null">
                number = #{number, jdbcType=VARCHAR},
            </if>
            <if test="tagCode != null">
                tag_code = #{tagCode, jdbcType=VARCHAR},
            </if>
            <if test="tagName != null">
                tag_name = #{tagName, jdbcType=VARCHAR},
            </if>
            <if test="accountsId != null">
                accounts_id = #{accountsId, jdbcType=BIGINT},
            </if>
            <if test="delFlag != null">
                del_flag = #{delFlag, jdbcType=TINYINT},
            </if>
            <if test="status != null">
                status = #{status, jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                create_time = #{createTime, jdbcType=TIMESTAMP},
            </if>
            <if test="description != null">
                description = #{description, jdbcType=VARCHAR},
            </if>
            <if test="createBy != null">
                create_by = #{createBy, jdbcType=BIGINT},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy, jdbcType=BIGINT},
            </if>
        </set>
        where id=#{id,jdbcType=BIGINT}
    </update>

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

    <delete id="delTagCard" parameterType="java.util.Map">
      delete from t_dc_tag_card where tag_id = #{tagId}
    </delete>

    <insert id="saveBatchTagsCard" parameterType="java.util.Map">
        REPLACE into t_dc_tag_card
        (tag_id,card_id,sort) values
        <foreach collection="tagCards" item="tagCard"
                 separator=",">
            (
            #{tagsId, jdbcType=BIGINT},
            #{tagCard.cardId,jdbcType=BIGINT},
            #{tagCard.sort,jdbcType=VARCHAR}
            )
        </foreach>
    </insert>

    <select id="getMemberTagsCard" parameterType="java.util.Map" resultType="java.lang.String">
        select DISTINCT(sortTable.card_id) from (SELECT
        card.card_id,card.title,tagCard.sort,'1' s
        FROM
        wx_mp_card card
        INNER JOIN t_dc_tag_card tagCard ON tagCard.card_id = card.id
        LEFT JOIN t_dc_member_tags memberTags ON memberTags.id = tagCard.tag_id
        WHERE
        memberTags.del_flag = 0
        <if test="tags!=null">
            and
            memberTags.tag_code IN
            <foreach collection="tags" item="tag" separator="," open="(" close=")">
                #{tag}
            </foreach>
        </if>
        UNION All
        SELECT
        card.card_id,card.title,tagCard.sort,'2' s
        FROM
        wx_mp_card card
        INNER JOIN t_dc_tag_card tagCard ON tagCard.card_id = card.id
        LEFT JOIN t_dc_member_tags memberTags ON memberTags.id = tagCard.tag_id
        WHERE
        memberTags.id=0
        AND memberTags.del_flag = 0) sortTable
        order by sortTable.s asc,sortTable.sort



    </select>


</mapper>