<?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.cab.dao.ContentMapper" >

  <resultMap id="BriefResultMap" type="com.cab.model.Content" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="title" property="title" jdbcType="VARCHAR" />
    <result column="sub_title" property="subTitle" jdbcType="VARCHAR" />
    <result column="url" property="url" jdbcType="VARCHAR" />
    <result column="thumb" property="thumb" jdbcType="VARCHAR" />
  </resultMap>
  
  <resultMap id="BaseResultMap" type="com.cab.model.Content" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="title" property="title" jdbcType="VARCHAR" />
    <result column="sub_title" property="subTitle" jdbcType="VARCHAR" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="content_brief" property="contentBrief" jdbcType="VARCHAR" />
    <result column="content_type" property="contentType" jdbcType="VARCHAR" />
    <result column="content_type2" property="contentType2" jdbcType="VARCHAR" />
    <result column="content_type3" property="contentType3" jdbcType="VARCHAR" />
    <result column="url" property="url" jdbcType="VARCHAR" />
    <result column="video_url" property="videoUrl" jdbcType="VARCHAR" />
    <result column="video_time" property="videoTime" jdbcType="VARCHAR" />
    <result column="thumb" property="thumb" jdbcType="VARCHAR" />
    <result column="thumb_min" property="thumbMin" jdbcType="VARCHAR" />
    <result column="status" property="status" jdbcType="INTEGER" />
    <result column="isfirst" property="isFirst" jdbcType="INTEGER" />
    <result column="istop" property="isTop" jdbcType="INTEGER" />
    <result column="iscreate" property="isCreate" jdbcType="INTEGER" />
    <result column="header_banner" property="headerBanner" jdbcType="VARCHAR" />
    <result column="up_count" property="upCount" jdbcType="BIGINT" />
    <result column="comment_count" property="commentCount" jdbcType="BIGINT" />
    <result column="favorite_count" property="favoriteCount" jdbcType="BIGINT" />
    <result column="view_count" property="viewCount" jdbcType="BIGINT" />
    <association property="sysUser" column="user_id" javaType="com.sys.model.SysUser">
      <result column="user_name" property="userName"></result>
    </association>
  </resultMap>
  
  <sql id="Base_Column_List" >
    C.id, title, sub_title, C.user_id, create_time, content_brief, content_type, content_type2,
    content_type3, url, thumb,thumb_min,video_url,video_time, status,isfirst,istop,iscreate, header_banner, up_count, comment_count, view_count
  </sql>  
  <select id="selectByContentType" resultMap="BaseResultMap" >
    select 
    <include refid="Base_Column_List" />
    from cab_content C
    where content_type2 = #{0} limit #{1}, #{2}
  </select>  
  <select id="selectCountByContentType" resultType="java.lang.Long" >
    select count(*) from cab_content C
    where content_type3 = #{0}
  </select>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
    select 
    <include refid="Base_Column_List" />
    from cab_content C
    where C.id = #{id,jdbcType=BIGINT}
  </select>
  <insert id="insertSelective" parameterType="com.cab.model.Content" useGeneratedKeys="true" keyProperty="id">
    insert into cab_content
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="title != null" >
        title,
      </if>
      <if test="subTitle != null" >
        sub_title,
      </if>
      <if test="userId != null" >
        user_id,
      </if>
      <if test="createTime != null" >
        create_time,
      </if>
      <if test="contentBrief != null" >
        content_brief,
      </if>
      <if test="contentType != null" >
        content_type,
      </if>
      <if test="contentType2 != null" >
        content_type2,
      </if>
      <if test="contentType3 != null" >
        content_type3,
      </if>
      <if test="url != null" >
        url,
      </if>
      <if test="thumb != null" >
        thumb,
      </if>
      <if test="thumbMin != null" >
        thumb_min,
      </if>
      <if test="videoUrl != null" >
      video_url,
      </if>
      <if test="videoTime != null" >
      video_time,
      </if>
      <if test="isFirst != null" >
        isfirst,
      </if>
      <if test="isTop != null" >
        istop,
      </if>
      <if test="isCreate != null" >
        iscreate,
      </if>
      <if test="status != null" >
        status,
      </if>
      <if test="headerBanner != null" >
        header_banner,
      </if>
      <if test="upCount != null" >
        up_count,
      </if>
      <if test="commentCount != null" >
        comment_count,
      </if>
      <if test="viewCount != null" >
        view_count,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=BIGINT},
      </if>
      <if test="title != null" >
        #{title,jdbcType=VARCHAR},
      </if>
      <if test="subTitle != null" >
        #{subTitle,jdbcType=VARCHAR},
      </if>
      <if test="userId != null" >
        #{userId,jdbcType=BIGINT},
      </if>
      <if test="createTime != null" >
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="contentBrief != null" >
        #{contentBrief,jdbcType=VARCHAR},
      </if>
      <if test="contentType != null" >
        #{contentType,jdbcType=VARCHAR},
      </if>
      <if test="contentType2 != null" >
        #{contentType2,jdbcType=VARCHAR},
      </if>
      <if test="contentType3 != null" >
        #{contentType3,jdbcType=VARCHAR},
      </if>
      <if test="url != null" >
        #{url,jdbcType=VARCHAR},
      </if>
      <if test="thumb != null" >
        #{thumb,jdbcType=VARCHAR},
      </if>
      <if test="thumbMin != null" >
        #{thumbMin,jdbcType=VARCHAR},
      </if>
      <if test="videoUrl != null" >
      	#{videoUrl,jdbcType=VARCHAR},
      </if>
      <if test="videoTime != null" >
      	#{videoTime,jdbcType=VARCHAR},
      </if>
      <if test="isFirst != null" >
         #{isFirst,jdbcType=INTEGER},
      </if>
      <if test="isTop != null" >
         #{isTop,jdbcType=INTEGER},
      </if>
      <if test="isCreate != null" >
        #{isCreate,jdbcType=INTEGER},
      </if>
      <if test="status != null" >
        #{status,jdbcType=INTEGER},
      </if>
      <if test="headerBanner != null" >
        #{headerBanner,jdbcType=VARCHAR},
      </if>
      <if test="upCount != null" >
        #{upCount,jdbcType=BIGINT},
      </if>
      <if test="commentCount != null" >
        #{commentCount,jdbcType=BIGINT},
      </if>
      <if test="viewCount != null" >
        #{viewCount,jdbcType=BIGINT},
      </if>
    </trim>
  </insert>
  <update id="increaseCommentCount" parameterType="java.lang.Long">
      update cab_content set comment_count = comment_count + 1 where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="increaseUpCount" parameterType="java.lang.Long">
      update cab_content set up_count = up_count + 1 where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="increaseFavCount" parameterType="java.lang.Long">
      update cab_content set favorite_count = favorite_count + 1 where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="decreaseUpCount" parameterType="java.lang.Long">
      update cab_content set up_count = up_count - 1 where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="decreaseFavCount" parameterType="java.lang.Long">
      update cab_content set favorite_count = favorite_count - 1 where id = #{id,jdbcType=BIGINT}
  </update>
<!--********************************************************* 自定义********************************************* -->
 <!-- 修改内容管理 -->
  <update id="update"  parameterType="com.cab.model.Content">
  	update cab_content
  	<set>
  	 <if test="title != null" >
      title=  #{title,jdbcType=VARCHAR},
      </if>
      <if test="subTitle != null" >
       sub_title =#{subTitle,jdbcType=VARCHAR},
      </if>
      <if test="userId != null" >
     user_id  = #{userId,jdbcType=BIGINT},
      </if>
      <if test="contentBrief != null" >
       content_brief =#{contentBrief,jdbcType=VARCHAR},
      </if>
      <if test="contentType != null" >
     content_type  = #{contentType,jdbcType=VARCHAR},
      </if>
      <if test="contentType2 != null" >
       content_type2 =#{contentType2,jdbcType=VARCHAR},
      </if>
      <if test="contentType3 != null" >
       content_type3 =#{contentType3,jdbcType=VARCHAR},
      </if>
      <if test="url != null" >
       url = #{url,jdbcType=VARCHAR},
      </if>
      <if test="thumb != null" >
       thumb = #{thumb,jdbcType=VARCHAR},
      </if>
      <if test="thumbMin != null" >
       thumb_min =#{thumbMin,jdbcType=VARCHAR},
      </if>
      <if test="videoUrl != null" >
      	video_url =#{videoUrl,jdbcType=VARCHAR},
      </if>
      <if test="videoTime != null" >
      	video_time =#{videoTime,jdbcType=VARCHAR},
      </if>
      <if test="isFirst != null" >
       isfirst = #{isFirst,jdbcType=INTEGER},
      </if>
      <if test="isTop != null" >
        istop =#{isTop,jdbcType=INTEGER},
      </if>
      <if test="isCreate != null" >
       iscreate =#{isCreate,jdbcType=INTEGER},
      </if>
      <if test="status != null" >
       status =#{status,jdbcType=INTEGER},
      </if>
      <if test="headerBanner != null" >
      header_banner = #{headerBanner,jdbcType=VARCHAR},
      </if>
      <if test="upCount != null" >
      up_count  = #{upCount,jdbcType=BIGINT},
      </if>
      <if test="commentCount != null" >
      comment_count = #{commentCount,jdbcType=BIGINT},
      </if>
      <if test="viewCount != null" >
       view_count = #{viewCount,jdbcType=BIGINT},
      </if>
  	</set>
  	where id=#{id,jdbcType=INTEGER}
  </update>
  
  <!-- 分页得到推荐消息 -->
    <select id="getAllIsTop" resultMap="BaseResultMap" >
    select 
    <include refid="Base_Column_List" />
    from cab_content C 
    where istop = 1 limit #{page},#{limit}
  </select>  
  <!-- 推荐消息总数 -->
  <select id="getAllIsTopCount" resultType="java.lang.Long">
  select count(id) FROM cab_content where istop='1'
  </select>
  
  <select id="getIsFirst" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from cab_content C 
    where content_type = #{paramsType,jdbcType=VARCHAR} and isfirst = 1
  </select>
    <!-- 按分类得到内容 -->
  <select id="getByContentType" resultMap="BaseResultMap" >
    select 
    <include refid="Base_Column_List" />
    from cab_content C
    where 
      <if test="contentTypeParentId != null and contentTypeParentId != ''" >
        content_type2 = #{contentTypeParentId,jdbcType=VARCHAR}
      </if>
      <if test="contentTypeId != null and contentTypeId != ''" >
        content_type3 = #{contentTypeId,jdbcType=VARCHAR}
      </if>
      <if test="title != null and title != ''">
      	and title = #{title,jdbcType=VARCHAR}
      </if>
      order by create_time desc
    limit #{start,jdbcType=BIGINT}, #{count,jdbcType=BIGINT}
  </select>  
  
    <select id="getByContentTypeCount" resultType="java.lang.Long" >
    select count(*) from cab_content C
    where 
      <if test="contentTypeParentId != null" >
        content_type2 = #{contentTypeParentId,jdbcType=VARCHAR}
      </if>
      <if test="contentTypeId != null" >
        content_type3 = #{contentTypeId,jdbcType=VARCHAR}
      </if>
      <if test="title != null">
       and	title = #{title,jdbcType=VARCHAR}
      </if>
  </select>
  <resultMap id="getContentInfoByIdMap" type="com.cab.model.Content" extends="BaseResultMap" >
  
  </resultMap>
  <select id="getContentInfoById" resultMap="BaseResultMap" parameterType="java.lang.Long" >
    select 
   C.id, title, sub_title, C.user_id, create_time, content_brief, content_type, content_type2,
    content_type3, url, thumb,video_url, status,isfirst,istop, header_banner
    from cab_content C
    where C.id = #{id,jdbcType=BIGINT}
  </select>
  
  
  <select id="getContentPage" resultMap="BaseResultMap" >
    select 
    <include refid="Base_Column_List" />,U.user_name
    from cab_content C left join sys_user U  on C.user_id = U.user_id
    where 1=1 
      <if test="contentType1 != null and contentType1 != ''" >
        and content_type = #{contentType1,jdbcType=VARCHAR} 
      </if>
      <if test="contentType2 != null and contentType2 != ''" >
        and content_type2 = #{contentType2,jdbcType=VARCHAR} 
      </if>
      <if test="contentType3 != null and contentType3 != ''" >
        and content_type3 = #{contentType3,jdbcType=VARCHAR} 
      </if>
      <if test="title != null and title != ''" >
        and title like  '%${title}%'
      </if>
      <if test="isFirst == 'yes'" >
        and isfirst = 1
      </if>
      <if test="isFirst == 'no'" >
        and isfirst = 0
      </if>
      <if test="isTop =='yes'" >
        and istop = 1
      </if>
      <if test="isTop =='no'" >
        and istop = 0
      </if>
      <if test="isCreate == 'yes'" >
        and iscreate = 1
      </if>
      <if test="isCreate == 'no'" >
        and iscreate = 0
      </if>
      order by create_time desc 
    limit #{page,jdbcType=BIGINT}, #{limit,jdbcType=BIGINT}
  </select>  
  <select id="getContentPageCount" resultType="java.lang.Long" >
    select 
    count(*) 
    from cab_content C 
    where 1=1 
      <if test="contentType1 != null and contentType1 != ''" >
        and content_type = #{contentType1,jdbcType=VARCHAR} 
      </if>
      <if test="contentType2 != null and contentType2 != ''" >
        and content_type2 = #{contentType2,jdbcType=VARCHAR} 
      </if>
      <if test="contentType3 != null and contentType3 != ''" >
        and content_type3 = #{contentType3,jdbcType=VARCHAR} 
      </if>
      <if test="title != null and title != ''" >
        and title like '%${title}%'
      </if>
      <if test="isFirst == 'yes'" >
        and isfirst = 1
      </if>
      <if test="isFirst == 'no'" >
        and isfirst = 0
      </if>
      <if test="isTop =='yes'" >
        and istop = 1
      </if>
      <if test="isTop =='no'" >
        and istop = 0
      </if>
       <if test="isCreate == 'yes'" >
        and iscreate = 1
      </if>
      <if test="isCreate == 'no'" >
        and iscreate = 0
      </if>
  </select>
  
  <update id="changeIsTop" parameterType="java.lang.Long">
      update cab_content set istop = #{isVal,jdbcType=BIGINT} where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="changeIsFirst" parameterType="java.lang.Long">
      update cab_content set isfirst = #{isVal,jdbcType=BIGINT} where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateTime" parameterType="java.lang.Long">
      update cab_content set video_time = #{time,jdbcType=VARCHAR} where id = #{id,jdbcType=BIGINT}
  </update>
	<delete id="del">
		DELETE from cab_content where id= #{id,jdbcType=BIGINT}
	</delete>
</mapper>