<?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"/>
        <result column="recommended" property="recommended"/>
        <result column="whether_banner" property="whetherBanner"/>
        <result column="size" property="size"/>
        <result column="article" property="article"/>
    </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,
                                                recommended,
                                                whether_banner,
                                                reading,
                                                size,
                                                article
                        </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},
        0,
        0,
        #{reading, jdbcType=BIGINT},
        #{size, jdbcType=BIGINT},
        0
        )
    </insert>

    <select id="fetchById" parameterType="java.lang.Long" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM wx_cms_cfarticle t
        WHERE t.id=#{id} and t.del_flag = 0
    </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,
        t.recommended,
        t.whether_banner,
        t.size,
        t.article,
        co.remarks,
        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>
            <if test="recommended != null">
                recommended = #{recommended, jdbcType=BIGINT},
            </if>
            <if test="whetherBanner != null">
                whether_banner = #{whetherBanner, jdbcType=BIGINT},
            </if>
            <if test="reading != null">
                reading = #{reading, jdbcType=BIGINT},
            </if>
            <if test="size != null">
                size = #{size, jdbcType=BIGINT},
            </if>
            <if test="article != null">
                article = #{article, jdbcType=BIGINT}
            </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>
    <update id="updatecom">
         update wx_cms_cfarticle
         <set>
             <if test="recommended!=null">
                 recommended = #{recommended},
             </if>
             <if test="whetherBanner!=null">
                 whether_banner = #{whetherBanner},
             </if>
             <if test="article!=null">
                 article = #{article},
             </if>
             <if test="updateTime!=null">
                 update_time= #{updateTime}
             </if>
         </set>
         where id = #{id}
    </update>

    <select id="recommended" resultType="com.cftech.cms.cfarticle.model.CfarticleVO" >
        select
a.id,
a.title,
a.author,
a.bigimg,
a.smallimg,
a.content,
a.desc,
a.create_time AS createTime,
a.reading,
b.name,
a.recommended,
a.whether_banner AS whetherBanner,
a.size
FROM wx_cms_cfarticle a
LEFT JOIN wx_cms_column b
ON b.id = a.`column`
WHERE a.del_flag = 0
<if test="id!=null">
and a.id = #{id}
</if>
        ORDER BY
        a.create_time
    </select>
    <update id="reading" parameterType="java.lang.Integer">
        update wx_cms_cfarticle
        <set>
            <if test="count!=null">
                reading = #{count}
            </if>
        </set>
        where id =#{id}
    </update>
    <select id="saveReading" resultType="com.cftech.cms.cfarticle.model.CfarticleVO">
        SELECT * FROM wx_cms_cfarticle WHERE id =#{id} and  del_flag=0
    </select>
    <select id="selectcom" resultType="java.lang.Integer">
SELECT count(*) FROM wx_cms_cfarticle WHERE recommended= 1 and  del_flag=0
    </select>
    <select id="selectBanner" resultType="java.lang.Integer">
SELECT count(*) FROM wx_cms_cfarticle WHERE whether_banner= 1 and  del_flag=0
    </select>
    <select id="gateway" resultType="com.cftech.cms.cfarticle.model.Cfarticle">
   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,
        t.recommended,
        t.whether_banner,
        t.size,
        t.reading,
        t.article,
		co.remarks
        FROM wx_cms_cfarticle t
        left join wx_cms_column co on co.id = t.`column`
        where t.del_flag = 0
        <if test="title!=null and title!=''">
            and   t.title like '%${title}%'
        </if>
        ORDER BY
        create_time
        DESC
        <if test="page!=null and pageSize !='' and pageSize!=null and page !=''">
            limit #{page},#{pageSize}
        </if>
    </select>
    <select id="column" resultType="com.cftech.cms.cfarticle.model.columnVO">
               SELECT
				id,
				number,
				name,
				img,
				remarks,
				status,
				create_time AS createTime,
				description
                 FROM wx_cms_column
				WHERE del_flag = 0
    </select>

    <select id="columnDetails" resultType="com.cftech.cms.cfarticle.model.CfarticleVO" >
 select
id,
title,
author,
bigimg,
smallimg,
content,
create_time AS createTime,
update_time AS  updatetime,
reading,
recommended,
whether_banner AS whetherBanner,
size
FROM wx_cms_cfarticle
WHERE del_flag=0
<if test="id!=null">
    AND `column`= #{id}
</if>
<if test="title!=null and title!=''">
    AND CONCAT(IFNULL(`title`,''),IFNULL(`author`,'')) LIKE  '%${title}%'
</if>
        ORDER BY
        create_time
        DESC
        <if test="page!=null and pageSize !='' and pageSize!=null and page !=''">
            limit #{page},#{pageSize}
        </if>
    </select>

</mapper>