<?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.pcloud.book.applet.dao.impl.AppletNewsClassifyDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.applet.entity.AppletNewsClassify" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="news_classify" property="newsClassify" jdbcType="VARCHAR" />
    <result column="seq" property="seq" jdbcType="INTEGER" />
    <result column="show_state" property="showState" jdbcType="BOOLEAN" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
    <result column="pic" property="pic" jdbcType="VARCHAR" />
  </resultMap>

  <sql id="Base_Column_List" >
    id, news_classify, seq, show_state, create_time,update_time, pic
  </sql>

  <insert id="insert" parameterType="com.pcloud.book.applet.entity.AppletNewsClassify" useGeneratedKeys="true" keyProperty="id">
    insert into applet_news_classify (
        news_classify, seq, show_state, create_time, update_time, pic
      )
    values (
      #{newsClassify}, #{seq}, #{showState}, NOW(), NOW(), #{pic}
      )
  </insert>

  <update id="update" parameterType="com.pcloud.book.applet.entity.AppletNewsClassify">
    update applet_news_classify
    <set>
      <if test="newsClassify != null">
        news_classify = #{newsClassify,jdbcType=BIGINT},
      </if>
      <if test="seq != null">
        seq = #{seq},
      </if>
      <if test="showState != null">
        show_state = #{showState},
      </if>
      <if test="pic != null">
          pic=#{pic},
      </if>
      update_time=NOW()
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>

  <select id="getById" parameterType="long" resultMap="BaseResultMap">
    select <include refid="Base_Column_List"/>
    from applet_news_classify
    where id=#{id}
  </select>

  <delete id="deleteById" parameterType="long">
    delete from applet_news_classify
    where id=#{id}
  </delete>

  <select id="listNewsClassify" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletNewsClassifyDTO">
        SELECT
        b.id id,
        b.news_classify newsClassify,
        b.seq seq,
        COUNT(c.id) newsCount,
        b.show_state showState,
        b.pic pic
        FROM applet_news_classify b
        LEFT JOIN applet_news c ON b.id=c.news_classify_id
        WHERE 1=1
        <if test="name != null">
        AND b.news_classify LIKE CONCAT("%",#{name},"%")
        </if>
        GROUP BY b.id
        order by b.seq asc
  </select>


  <select id="getMaxSeq" resultType="Integer">
     SELECT IFNULL(MAX(seq),0) FROM applet_news_classify
  </select>

    <select id="getAllNewsClassify" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletNewsClassifyDTO">
    SELECT
    id, news_classify newsClassify, seq, show_state showState, pic
    FROM applet_news_classify
    where  1=1
    <if test="showState != null">
     AND show_state = #{showState}
    </if>
    ORDER BY seq ASC, id asc
    </select>

    <insert id="batchInsertUserNewsClassify" parameterType="list" keyProperty="id" useGeneratedKeys="true">
        insert into applet_news_classify_user (
        news_classify_id, wechat_user_id, create_time)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            ( #{item.newsClassifyId}, #{item.wechatUserId}, NOW() )
        </foreach>
    </insert>

    <delete id="deleteClassifyByWechatUesrId" parameterType="long">
        DELETE FROM applet_news_classify_user
        WHERE wechat_user_id = #{wechatUserId}
    </delete>

    <select id="getClassifyIdsByUserId" parameterType="long" resultType="long">
        SELECT news_classify_id
        FROM applet_news_classify_user
        WHERE wechat_user_id = #{wechatUserId}
    </select>

    <select id="getClassifysByUserId" parameterType="long" resultType="com.pcloud.book.applet.dto.AppletNewsClassifyDTO">
        SELECT
            c.id, c.news_classify newsClassify, c.seq, c.show_state showState, c.pic
        FROM
            applet_news_classify_user u
        LEFT JOIN applet_news_classify c ON u.news_classify_id = c.id
        WHERE
            u.wechat_user_id = #{wechatUserId}
        AND c.show_state = 1
        ORDER BY
            c.seq ASC, c.id asc
    </select>

</mapper>