<?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.cms.cfarticle.dao.CfarticleMapper">

    <resultMap id="resultMap" type="com.cftech.cms.cfarticle.model.CfarticleColumnDto">
        <id column="id" property="id"/>
        <result column="title" property="title"/>
        <result column="bigimg" property="bigimg"/>
        <result column="smallimg" property="smallimg"/>
        <result column="content" property="content"/>
        <result column="desc" property="desc"/>
        <result column="relation_article" property="relation_article"/>
        <result column="column" property="column"/>
        <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"/>
        <result column="author" property="author"/>
        <result column="content_source_url" property="contentSourceUrl"/>
        <result column="is_url" property="isUrl"/>
        <result column="type" property="type"/>
    </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,
                                                title,
                                                bigimg,
                                                smallimg,
                                                content,
                                                `desc`,
                                                relation_article,
                                                `column`,
                                                accounts_id,
                                                del_flag,
                                                status,
                                                create_time,
                                                update_time,
                                                description,
                                                create_by,
                                                update_by,
                                                content_source_url,
                                                author,
                                                is_url,
                                                type
                        </sql>


    <insert id="save" parameterType="com.cftech.cms.cfarticle.model.Cfarticle" useGeneratedKeys="true"
            keyProperty="id">
        insert into wx_cms_cfarticle
        (
        <include refid="sqlColumns"/>
        )
        values
        (
        #{id, jdbcType=BIGINT},
        #{title, jdbcType=VARCHAR},
        #{bigimg, jdbcType=VARCHAR},
        #{smallimg, jdbcType=VARCHAR},
        #{content, jdbcType=CLOB},
        #{desc, jdbcType=VARCHAR},
        #{relation_article, jdbcType=VARCHAR},
        #{column, jdbcType=BIGINT},
        #{accountsId, jdbcType=BIGINT},
        #{delFlag, jdbcType=TINYINT},
        #{status, jdbcType=VARCHAR},
        now(),
        now(),
        #{description, jdbcType=VARCHAR},
        #{createBy, jdbcType=BIGINT},
        #{updateBy, jdbcType=BIGINT},
        #{contentSourceUrl, jdbcType=VARCHAR},
        #{author, jdbcType=VARCHAR},
        #{isUrl, jdbcType=TINYINT},
        #{type, jdbcType=VARCHAR}
        )
    </insert>

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

    <select id="count" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM wx_cms_cfarticle t
        left join wx_cms_column co on co.id = t.column
        <include refid="sqlWhere"/>

    </select>


    <select id="countTemp" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM wx_cms_cfarticle t
        left join wx_cms_column co on co.id = t.column
        <include refid="sqlWhere"/>

    </select>

    <select id="countReview" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT COUNT(1) from wx_review r INNER JOIN wx_cms_cfarticle a on a.id =r.entity_id
        <include refid="sqlWhere"/>

    </select>

    <select id="fetchSearchByPageTemp" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        t.id,
        t.title,
        t.bigimg,
        t.smallimg,
        t.content,
        t.`desc`,
        t.relation_article,
        t.`column`,
        t.accounts_id,
        t.del_flag,
        t.status,
        t.create_time,
        t.update_time,
        t.description,
        t.create_by,
        t.update_by,
        t.author,
        t.is_url,
        t.content_source_url,
        t.type,
        co.name columnName
        FROM wx_cms_cfarticle t
        left join wx_cms_column co on co.id = t.`column`
        <include refid="sqlWhere"/>
        <if test="specialId!=null">
            order by t.id in (select article_id from wx_cms_special_article special where special_id = #{specialId})
            desc
        </if>
        <if test="limit>0">limit #{offset},#{limit}</if>
    </select>


    <select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        t.id,
        t.title,
        t.bigimg,
        t.smallimg,
        t.content,
        t.`desc`,
        t.relation_article,
        t.`column`,
        t.accounts_id,
        t.del_flag,
        t.status,
        t.create_time,
        t.update_time,
        t.description,
        t.create_by,
        t.update_by,
        t.author,
        t.is_url,
        t.content_source_url,
        t.type,
        co.name columnName
        FROM wx_cms_cfarticle t
        left join wx_cms_column co on co.id = t.`column`
        <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="fetchSearchByPageInOrder" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        t.id,
        t.title,
        t.bigimg,
        t.smallimg,
        t.content,
        t.`desc`,
        t.relation_article,
        t.`column`,
        t.accounts_id,
        t.del_flag,
        t.status,
        t.create_time,
        t.update_time,
        t.description,
        t.create_by,
        t.update_by,
        t.author,
        t.is_url,
        t.type,
        t.content_source_url
        FROM wx_cms_cfarticle t
        <include refid="sqlWhere"/>
        <if test="articles!=null and articles != ''">
            ORDER BY field(t.id,${articles})
        </if>

        <if test="limit>0">limit #{offset},#{limit}</if>
    </select>


    <update id="update" parameterType="com.cftech.cms.cfarticle.model.Cfarticle">
        update wx_cms_cfarticle
        <set>
            <if test="id != null">
                id = #{id, jdbcType=BIGINT},
            </if>
            <if test="title != null">
                title = #{title, jdbcType=VARCHAR},
            </if>
            <if test="bigimg != null">
                bigimg = #{bigimg, jdbcType=VARCHAR},
            </if>
            <if test="smallimg != null">
                smallimg = #{smallimg, jdbcType=VARCHAR},
            </if>
            <if test="content != null">
                content = #{content, jdbcType=CLOB},
            </if>
            <if test="desc != null">
                `desc` = #{desc, jdbcType=VARCHAR},
            </if>
            <if test="relation_article != null">
                relation_article = #{relation_article, jdbcType=VARCHAR},
            </if>
            <if test="column != null">
                `column` = #{column, jdbcType=BIGINT},
            </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>
            <if test="author != null">
                author = #{author, jdbcType=VARCHAR},
            </if>
            <if test="contentSourceUrl != null">
                content_source_url = #{contentSourceUrl, jdbcType=VARCHAR},
            </if>
            <if test="isUrl != null">
                is_url = #{isUrl, jdbcType=VARCHAR},
            </if>
            <if test="type != null">
                type = #{type, jdbcType=VARCHAR},
            </if>

        </set>
        where id=#{id,jdbcType=BIGINT}
    </update>

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