<?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.AppletNewsDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.applet.entity.AppletNews" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="news_name" property="newsName" jdbcType="VARCHAR" />
    <result column="source" property="source" jdbcType="VARCHAR" />
    <result column="news_classify_id" property="newsClassifyId" jdbcType="BIGINT" />
    <result column="pro_label_id" property="proLabelId" jdbcType="BIGINT" />
    <result column="dep_label_id" property="depLabelId" jdbcType="BIGINT" />
    <result column="pur_label_id" property="purLabelId" jdbcType="BIGINT" />
    <result column="type" property="type" jdbcType="INTEGER" />
    <result column="digest" property="digest" jdbcType="VARCHAR" />
    <result column="pic1" property="pic1" jdbcType="VARCHAR" />
    <result column="pic2" property="pic2" jdbcType="VARCHAR" />
    <result column="pic3" property="pic3" jdbcType="VARCHAR" />
    <result column="content" property="content" jdbcType="VARCHAR" />
    <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="first_classify" property="firstClassify" jdbcType="BIGINT" />
    <result column="second_classify" property="secondClassify" jdbcType="BIGINT" />
    <result column="grade_label_id" property="gradeLabelId" jdbcType="BIGINT" />
    <result column="subject_label_id" property="subjectLabelId" jdbcType="BIGINT" />
    <result column="rights_classify_id" property="rightsClassifyId" jdbcType="BIGINT" />
    <result column="jump_type" property="jumpType" jdbcType="INTEGER" />
    <result column="jump_url" property="jumpUrl" jdbcType="VARCHAR" />
    <result column="url_number" property="urlNumber" jdbcType="VARCHAR" />
    <result column="custom_tag_id" property="customTagId" jdbcType="BIGINT" />
    <result column="show_source" property="showSource" jdbcType="INTEGER" />
    <result column="show_link" property="showLink" jdbcType="INTEGER" />
    <result column="business_card_id" property="businessCardId" jdbcType="BIGINT" />
    <result column="business_card_type" property="businessCardType" jdbcType="TINYINT" />
    <result column="business_card_isOpen" property="businessCardIsOpen" jdbcType="TINYINT" />
    <result column="city_code" property="cityCode" jdbcType="VARCHAR" />
    <result column="agent_id" property="agentId" jdbcType="BIGINT" />
    <result column="crossed_price" property="crossedPrice" jdbcType="VARCHAR" />
    <result column="appraisal_price" property="appraisalPrice" jdbcType="VARCHAR" />
    <result column="appraisal_type" property="appraisalType" jdbcType="TINYINT" />
    <result column="appraisal_img" property="appraisalImg" jdbcType="VARCHAR" />
  </resultMap>

  <sql id="Base_Column_List">
     news.id, news.news_name, news.source, news.news_classify_id, news.pro_label_id, news.dep_label_id, news.pur_label_id, news.type, news.digest,
     pic1, pic2, pic3, news.content, show_state, news.create_time, news.update_time, news.first_classify, news.second_classify, news.grade_label_id, news.subject_label_id, news.rights_classify_id,
     news.jump_type, news.jump_url, news.url_number, news.custom_tag_id, news.show_source, news.show_link,news.business_card_id,news.business_card_type,news.business_card_isOpen,city_code,agent_id,
    crossed_price,
    appraisal_price,
    appraisal_type,
    appraisal_img
  </sql>
    <sql id="sql_news">
      m.id,
    newsName,
    source,
    newsClassifyId,
    proLabelId,
    depLabelId,
    purLabelId,
    m.type,
    m.digest,
    m.pic1,
    m.pic2,
    m.pic3,
    m.content,
    showState,
    createTime,
    firstClassify,
    secondClassify,
    gradeLabelId,
    subjectLabelId,
    rightsClassifyId,
    jumpType,
    jumpUrl,
    urlNumber,
    showSource,
    showLink,
    cityCode
    </sql>

  <insert id="insert" parameterType="com.pcloud.book.applet.entity.AppletNews" useGeneratedKeys="true" keyProperty="id">
    insert into applet_news(
     news_name, source, news_classify_id, pro_label_id, dep_label_id, pur_label_id, type, digest,
     pic1, pic2, pic3, content, show_state, create_time, update_time , first_classify, second_classify, grade_label_id, subject_label_id, rights_classify_id,
     jump_type, jump_url,url_number, custom_tag_id, show_source, show_link,business_card_id,business_card_type,business_card_isOpen,city_code, agent_id,
    crossed_price,
    appraisal_price,
    appraisal_type,
    appraisal_img
      )
    values (
      #{newsName}, #{source}, #{newsClassifyId}, #{proLabelId}, #{depLabelId}, #{purLabelId}, #{type}, #{digest},
      #{pic1}, #{pic2}, #{pic3}, #{content}, #{showState}, NOW(), NOW(), #{firstClassify,jdbcType=BIGINT}, #{secondClassify,jdbcType=BIGINT},
      #{gradeLabelId,jdbcType=BIGINT}, #{subjectLabelId,jdbcType=BIGINT}, #{rightsClassifyId,jdbcType=BIGINT},
      #{jumpType}, #{jumpUrl}, #{urlNumber}, #{customTagId}, #{showSource}, #{showLink}, #{businessCardId}, #{businessCardType}, #{businessCardIsOpen}, #{cityCode}, #{agentId},
      #{crossedPrice},
      #{appraisalPrice},
      #{appraisalType},
      #{appraisalImg}
      )
  </insert>

  <update id="update" parameterType="com.pcloud.book.applet.entity.AppletNews">
    update applet_news
    <set>
      <if test="newsName != null">
        news_name = #{newsName},
      </if>
      <if test="source != null">
        source = #{source},
      </if>
        news_classify_id = #{newsClassifyId},
        pro_label_id = #{proLabelId},
        dep_label_id = #{depLabelId},
        pur_label_id = #{purLabelId},
      <if test="type != null">
        type = #{type},
      </if>
      digest = #{digest},
      pic1 = #{pic1},
      pic2 = #{pic2},
      pic3 = #{pic3},
      <if test="content != null">
        content = #{content},
      </if>
      <if test="showState != null">
        show_state = #{showState,jdbcType=BOOLEAN},
      </if>
      first_classify = #{firstClassify,jdbcType=BIGINT},
      second_classify = #{secondClassify,jdbcType=BIGINT},
      grade_label_id = #{gradeLabelId,jdbcType=BIGINT},
      subject_label_id = #{subjectLabelId,jdbcType=BIGINT},
      rights_classify_id = #{rightsClassifyId,jdbcType=BIGINT},
      <if test="jumpType != null">
        jump_type = #{jumpType},
      </if>
      <if test="showSource != null">
        show_source = #{showSource},
      </if>
      <if test="showLink != null">
        show_link = #{showLink},
      </if>
      <if test="cityCode != null">
        city_code = #{cityCode},
      </if>
      jump_url = #{jumpUrl},
      url_number = #{urlNumber},
      business_card_id = #{businessCardId},
      business_card_type = #{businessCardType},
      business_card_isOpen = #{businessCardIsOpen},
      update_time=NOW(),
      <if test="crossedPrice != null">
          crossed_price = #{crossedPrice},
      </if>
      <if test="appraisalPrice != null">
          appraisal_price = #{appraisalPrice},
      </if>
      <if test="appraisalType != null">
          appraisal_type = #{appraisalType},
      </if>
      <if test="appraisalImg != null">
          appraisal_img = #{appraisalImg},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>

  <update id="updateNewsShowState" parameterType="com.pcloud.book.applet.entity.AppletNews">
     update applet_news
     set show_state = #{showState,jdbcType=BOOLEAN},
     update_time=NOW()
     where id = #{id,jdbcType=BIGINT}
  </update>

  <update id="updateCardShowState" parameterType="com.pcloud.book.applet.entity.AppletNews">
     update applet_news
     set business_card_isOpen = #{businessCardIsOpen,jdbcType=TINYINT},
     update_time=NOW()
     where id = #{id,jdbcType=BIGINT}
  </update>

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

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

  <select id="listAppletNewsCount" parameterType="map"  resultType="integer">
    SELECT count(DISTINCT n.id)
    FROM applet_news n
      <if test="classifySelect == 1">
          LEFT JOIN applet_news_category category on n.id = category.applet_news_id
      </if>
      WHERE 1=1 and n.is_delete = 0
    <if test="name != null">
    AND (n.news_name LIKE CONCAT("%",#{name},"%") OR n.url_number LIKE CONCAT("%",#{name},"%"))
    </if>
    <if test="newsClassifyId >0">
      AND n.news_classify_id = #{newsClassifyId}
    </if>
    <if test="showState != null">
      AND n.show_state = #{showState}
    </if>
    <if test="rightsClassifyId!=null">
      AND n.rights_classify_id =#{rightsClassifyId}
    </if>
    <if test="source != null">
     AND n.source = #{source}
    </if>
    <if test="customTagId != null">
     AND n.custom_tag_id = #{customTagId}
    </if>
    <if test="classifySelect == 1">
        <if test="firstClassify!=null">
            AND category.first_classify =#{firstClassify}
        </if>
        <if test="secondClassify!=null">
            AND category.second_classify =#{secondClassify}
        </if>
        <if test="thirdClassify!=null">
            AND category.third_classify =#{thirdClassify}
        </if>
        <if test="gradeLabelId != null">
            AND category.grade_label_id = #{gradeLabelId}
        </if>
        <if test="subjectLabelId != null">
            AND category.subject_label_id = #{subjectLabelId}
        </if>
    </if>
  </select>

  <select id="listAppletNews" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletNewsDTO">
    SELECT
    n.id,
    n.news_name newsName,
    n.source,
    n.news_classify_id newsClassifyId,
    n.show_state showState,
    c.news_classify newsClassify,
    n.create_time createTime,
    n.update_time updateTime,
    n.content content,
    n.rights_classify_id rightsClassifyId,
    d.classify rightsClassifyContent,
    n.jump_type jumpType,
    n.jump_url jumpUrl,
    n.url_number urlNumber,
    n.custom_tag_id customTagId,
    n.city_code cityCode,
    t.custom_tag customTag,
    IF(n.business_card_id is null,2,n.business_card_isOpen) businessCardIsOpen
    FROM applet_news n
      LEFT JOIN applet_news_classify c ON n.news_classify_id=c.id
      LEFT JOIN rights_setting_classify d ON n.rights_classify_id = d.id
      LEFT JOIN applet_news_custom_tag t ON n.custom_tag_id = t.id
      <if test="classifySelect == 1">
      LEFT JOIN applet_news_category category on n.id = category.applet_news_id
      </if>
      WHERE 1=1 and n.is_delete = 0
    <if test="cityCode != null">
        AND n.city_code = #{cityCode}
    </if>
    <if test="name != null">
      AND (n.news_name LIKE CONCAT("%",#{name},"%")
      OR n.url_number LIKE CONCAT("%",#{name},"%")
      <if test="cityCodes != null">
        OR  n.city_code IN
        <foreach collection="cityCodes" item="item" open="(" close=")" separator=",">
          ${item}
        </foreach>
      </if>
      )
    </if>
    <if test="newsClassifyId >0">
      AND n.news_classify_id = #{newsClassifyId}
    </if>
    <if test="showState != null">
      AND n.show_state = #{showState}
    </if>
    <if test="rightsClassifyId!=null">
      AND n.rights_classify_id =#{rightsClassifyId}
    </if>
    <if test="source != null">
     AND n.source = #{source}
    </if>
    <if test="customTagId != null">
     AND n.custom_tag_id = #{customTagId}
    </if>
      <choose>
          <when test="agentId != null">
              and n.agent_id = #{agentId}
          </when>
          <otherwise>
              and n.agent_id = 0
          </otherwise>
      </choose>
    <if test="classifySelect == 1">
        <if test="firstClassify!=null">
            AND category.first_classify =#{firstClassify}
        </if>
        <if test="secondClassify!=null">
            AND category.second_classify =#{secondClassify}
        </if>
        <if test="thirdClassify!=null">
            AND category.third_classify =#{thirdClassify}
        </if>
        <if test="gradeLabelId != null">
            AND category.grade_label_id = #{gradeLabelId}
        </if>
        <if test="subjectLabelId != null">
            AND category.subject_label_id = #{subjectLabelId}
        </if>
        GROUP BY n.id
    </if>
      ORDER BY n.update_time DESC
  </select>

  <select id="getCountByBusinessCardId" parameterType="long" resultType="long">
    select count(1)
    from applet_news
    where business_card_id=#{businessCardId} and  is_delete = 0
  </select>


    <select id="getByNewsClassifyId" parameterType="long" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from applet_news news
        where news.news_classify_id=#{newsClassifyId} and is_delete = 0
    </select>

    <select id="getByNewsByTempletLabel" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletNewsDTO">
        SELECT
        n.id,
        n.news_name newsName,
        n.source,
        n.news_classify_id newsClassifyId,
        n.pro_label_id proLabelId,
        n.dep_label_id depLabelId,
        n.pur_label_id purLabelId,
        n.type,
        n.digest,
        n.pic1,
        n.pic2,
        n.pic3,
    --    n.content,
        n.show_state showState,
        n.create_time createTime,
        n.first_classify firstClassify,
        n.second_classify secondClassify,
        n.grade_label_id gradeLabelId,
        n.subject_label_id subjectLabelId,
        n.rights_classify_id rightsClassifyId,
        n.jump_type jumpType,
        n.jump_url jumpUrl,
        n.url_number urlNumber,
        n.show_source showSource,
        n.show_link showLink,
        n.crossed_price crossedPrice,
        n.appraisal_price appraisalPrice,
        n.appraisal_type appraisalType,
        n.appraisal_img appraisalImg
        FROM applet_news n
        LEFT JOIN applet_news_category category on n.id = category.applet_news_id
        WHERE 1=1
        and n.show_state = 1 and n.is_delete = 0
        <if test="firstClassify != null">
            and category.first_classify = #{firstClassify}
        </if>
        <if test="secondClassify != null">
            and category.second_classify = #{secondClassify}
        </if>
        <if test="thirdClassify!=null">
            AND category.third_classify =#{thirdClassify}
        </if>
        <if test="gradeLabelId != null">
            and category.grade_label_id = #{gradeLabelId}
        </if>
        <if test="subjectLabelId != null">
            and category.subject_label_id = #{subjectLabelId}
        </if>
        AND n.rights_classify_id IN(
        SELECT id FROM rights_setting_classify WHERE 1=1
        <if test="rightsType != null">
            AND rights_type = #{rightsType}
        </if>
        )
        <choose>
            <when test="agentId >0">
                and n.agent_id in (#{agentId} ,0)
            </when>
            <otherwise>
                and n.agent_id = 0
            </otherwise>
        </choose>
        group by n.id
        ORDER BY n.agent_id desc, n.create_time DESC
    </select>


  <select id="getByTempletAndClassify" parameterType="map" resultMap="BaseResultMap">
    SELECT
        <include refid="Base_Column_List"/>
        FROM
        applet_news news
        LEFT JOIN applet_news_category category on news.id = category.applet_news_id
        WHERE
        news.show_state = 1 and news.is_delete = 0
        <if test="firstClassify!=null">
            AND category.first_classify =#{firstClassify}
        </if>
        <if test="secondClassify!=null">
            AND category.second_classify =#{secondClassify}
        </if>
        <if test="thirdClassify!=null">
          AND category.third_classify =#{thirdClassify}
        </if>
        <if test="gradeLabelId != null">
            AND category.grade_label_id = #{gradeLabelId}
        </if>
        <if test="subjectLabelId != null">
            AND category.subject_label_id = #{subjectLabelId}
        </if>
        <if test="rightsClassifyId!=null">
            AND news.rights_classify_id =#{rightsClassifyId}
        </if>
      <choose>
          <when test="agentId >0">
              and news.agent_id in (#{agentId} ,0)
          </when>
          <otherwise>
              and news.agent_id = 0
          </otherwise>
      </choose>
        group by news.id
      ORDER BY news.agent_id desc, news.update_time DESC
        LIMIT #{top}
  </select>

    <update id="batchUpdateClassify" parameterType="map">
        UPDATE applet_news
        SET
        rights_classify_id = #{rightsClassifyId,jdbcType=BIGINT},
        <if test="newsClassifyId != null">
            news_classify_id = #{newsClassifyId},
        </if>
        city_code = #{cityCode},
        update_time=NOW()
        WHERE id IN
        <foreach collection="ids" item="item" separator="," open="(" close=")">
            ${item}
        </foreach>
    </update>

  <update id="batchUpdateShowState" parameterType="map">
    UPDATE applet_news
    SET
    show_state = #{showState},
    update_time=NOW()
    WHERE id IN
    <foreach collection="ids" item="item" separator="," open="(" close=")" >
      ${item}
    </foreach>
  </update>

  <update id="batchUpdateCardShowState" parameterType="map">
    UPDATE applet_news
    SET
    business_card_isOpen = #{businessCardIsOpen},
    update_time=NOW()
    WHERE id IN
    <foreach collection="ids" item="item" separator="," open="(" close=")" >
      ${item}
    </foreach>
  </update>

  <delete id="batchDeleteAppletNews" parameterType="list">
    DELETE FROM applet_news
    WHERE id IN
    <foreach collection="list" item="item" separator="," open="(" close=")" >
      ${item}
    </foreach>
  </delete>

  <insert id="insertSource" parameterType="map">
      INSERT INTO applet_news_source (source, agent_id, create_time) VALUES (#{source}, #{agentId}, now())
  </insert>

  <select id="getNewsSourceList" resultType="string" parameterType="map">
    SELECT source FROM applet_news_source
    WHERE 1=1
    <choose>
      <when test="agentId != null">
        AND agent_id = #{agentId}
      </when>
      <otherwise>
        AND agent_id = 0
      </otherwise>
    </choose>
  </select>

  <select id="getAppletNewsSourceCount" resultType="integer" parameterType="string">
    SELECT COUNT(1) FROM applet_news WHERE source = #{source} and is_delete = 0
    <choose>
      <when test="agentId != null">
        AND agent_id = #{agentId}
      </when>
      <otherwise>
        AND agent_id = 0
      </otherwise>
    </choose>
  </select>

  <delete id="deleteSource" parameterType="map">
    DELETE FROM applet_news_source WHERE source = #{source} and agent_id = #{agentId}
  </delete>

  <select id="newsSourceExist" parameterType="map" resultType="boolean">
    SELECT EXISTS(
    SELECT id FROM applet_news_source
    WHERE source=#{source}
    <choose>
      <when test="agentId != null">
        AND agent_id = #{agentId}
      </when>
      <otherwise>
        AND agent_id = 0
      </otherwise>
    </choose>
    )
  </select>

  <select id="getUrlNumberByJumpUrl" parameterType="map" resultType="string">
    SELECT url_number
    FROM applet_news
    WHERE jump_url = #{jumpUrl}
    AND jump_type = 2
    AND url_number IS NOT NULL
    and is_delete = 0
    AND agent_id = #{agentId}
    LIMIT 1
  </select>

  <select id="urlNumberExist" parameterType="map" resultType="boolean">
    SELECT EXISTS (
      SELECT id
      FROM  applet_news
      WHERE
          url_number = #{urlNumber} and is_delete = 0
      <if test="id>0">
        AND id != #{id}
      </if>
    <choose>
      <when test="agentId != null">
        AND agent_id = #{agentId}
      </when>
      <otherwise>
        AND agent_id = 0
      </otherwise>
    </choose>
    )
  </select>

    <select id="listAppletNews4Analysis" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletNewsDTO">
       SELECT
        n.id,
        n.news_name newsName,
        n.source,
        n.news_classify_id newsClassifyId,
        n.pro_label_id proLabelId,
        n.dep_label_id depLabelId,
        n.pur_label_id purLabelId,
        n.type,
        n.digest,
        n.pic1,
        n.pic2,
        n.pic3,
        n.content,
        n.show_state showState,
        c.news_classify newsClassify,
        n.create_time createTime,
        n.first_classify firstClassify,
        n.second_classify secondClassify,
        n.third_classify thirdClassify,
        n.grade_label_id gradeLabelId,
        n.subject_label_id subjectLabelId,
        n.rights_classify_id rightsClassifyId,
        d.classify rightsClassifyContent,
        n.jump_type jumpType,
        n.jump_url jumpUrl,
        n.url_number urlNumber,
        n.show_source showSource,
        n.show_link showLink,
        n.crossed_price crossedPrice,
        n.appraisal_price appraisalPrice,
        n.appraisal_type appraisalType,
        n.appraisal_img appraisalImg
        FROM applet_news n
        LEFT JOIN applet_news_classify c ON n.news_classify_id=c.id
        LEFT JOIN rights_setting_classify d ON n.rights_classify_id = d.id
        LEFT JOIN applet_news_category category on n.id = category.applet_news_id
        WHERE 1=1 and n.is_delete = 0
        <if test="name != null">
            AND (n.news_name LIKE CONCAT("%",#{name},"%") or n.url_number LIKE CONCAT("%",#{name},"%"))
        </if>
        <if test="firstClassify!=null">
            AND category.first_classify =#{firstClassify}
        </if>
        <if test="secondClassify!=null">
            AND category.second_classify =#{secondClassify}
        </if>
        <if test="thirdClassify!=null">
            AND category.third_classify =#{thirdClassify}
        </if>
        <if test="gradeLabelId != null">
            AND category.grade_label_id = #{gradeLabelId}
        </if>
        <if test="subjectLabelId != null">
            AND category.subject_label_id = #{subjectLabelId}
        </if>
<!--        <if test="linkOnly == 1">-->
<!--            and n.jump_type = 2-->
<!--        </if>-->
        <if test="rightsClassifyId!=null">
            AND n.rights_classify_id =#{rightsClassifyId}
        </if>
        <if test="source!=null">
            AND n.source =#{source}
        </if>
        <choose>
            <when test="agentId != null">
                and n.agent_id = #{agentId}
            </when>
            <otherwise>
                and n.agent_id = 0
            </otherwise>
        </choose>
        group by n.id
        ORDER BY n.update_time DESC
    </select>

  <select id="getByIds" resultType="com.pcloud.book.applet.dto.AppletNewsDTO">
    SELECT
    n.id,
    n.news_name newsName,
    n.source,
    n.news_classify_id newsClassifyId,
    n.pro_label_id proLabelId,
    n.dep_label_id depLabelId,
    n.pur_label_id purLabelId,
    n.type,
    n.digest,
    n.pic1,
    n.pic2,
    n.pic3,
    n.show_state showState,
    n.create_time createTime,
    n.first_classify firstClassify,
    n.second_classify secondClassify,
    n.grade_label_id gradeLabelId,
    n.subject_label_id subjectLabelId,
    n.rights_classify_id rightsClassifyId,
    n.jump_type jumpType,
    n.jump_url jumpUrl,
    n.url_number urlNumber,
    n.show_source showSource,
    n.show_link showLink,
    n.crossed_price crossedPrice,
    n.appraisal_price appraisalPrice,
      n.appraisal_type appraisalType,
      n.appraisal_img appraisalImg,
    count(n.id) browseCount
    FROM applet_news n
    left join
    applet_link_click c on n.id = c.from_id and type_id = 1 AND record_type = 2
    WHERE n.show_state = 1 and n.is_delete = 0
    AND n.id IN
    <foreach collection="list" item="item" separator="," open="(" close=")" >
      ${item}
    </foreach>
    group by n.id
    ORDER BY n.update_time DESC
  </select>


  <select id="listAppletNews4Wechat" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletNewsDTO">
    SELECT
    n.id,
    n.news_name newsName,
    n.source,
    n.news_classify_id newsClassifyId,
    n.pro_label_id proLabelId,
    n.dep_label_id depLabelId,
    n.pur_label_id purLabelId,
    n.type,
    n.digest,
    n.pic1,
    n.pic2,
    n.pic3,
    n.content,
    n.show_state showState,
    c.news_classify newsClassify,
    n.create_time createTime,
    n.first_classify firstClassify,
    n.second_classify secondClassify,
    n.grade_label_id gradeLabelId,
    n.subject_label_id subjectLabelId,
    n.rights_classify_id rightsClassifyId,
    n.jump_type jumpType,
    n.jump_url jumpUrl,
    n.show_source showSource,
    n.show_link showLink,
    n.crossed_price crossedPrice,
    n.appraisal_price appraisalPrice,
    n.appraisal_type appraisalType,
    n.appraisal_img appraisalImg
    FROM applet_news n
    LEFT JOIN applet_news_classify c ON n.news_classify_id=c.id
    WHERE n.show_state = 1 and n.is_delete = 0
    <if test="newsClassifyId >0">
      AND n.news_classify_id = #{newsClassifyId}
    </if>
    <if test="newsClassifyIdList != null">
      AND n.news_classify_id in
      <foreach collection="newsClassifyIdList" item="item" open="(" separator="," close=")">
        ${item}
      </foreach>
    </if>
    <if test="firstPageIds != null and firstPageIds.size() > 0">
      and n.id not in
      <foreach collection="firstPageIds" item="item" open="(" separator="," close=")">
        ${item}
      </foreach>
    </if>
    <choose>
      <when test="order>0 and order == 1">
        group by n.id
        ORDER BY RAND()
      </when>
      <otherwise>
        group by n.id
        ORDER BY n.update_time DESC
      </otherwise>
    </choose>
  </select>


  <update id="batchUpdateNewsClassify" parameterType="map">
    UPDATE applet_news
    SET
      news_classify_id = #{newsClassifyId},
      update_time=NOW()
    WHERE id IN
    <foreach collection="ids" item="item" separator="," open="(" close=")" >
      ${item}
    </foreach>
  </update>

  <update id="batchUpdateCustomTagId" parameterType="map">
    UPDATE applet_news
    SET
    custom_tag_id = #{customTagId},
    update_time=NOW()
    WHERE id IN
    <foreach collection="ids" item="item" separator="," open="(" close=")" >
      ${item}
    </foreach>
  </update>

  <insert id="insertCustomTag" parameterType="com.pcloud.book.applet.entity.AppletNewsCustomTag" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO applet_news_custom_tag(custom_tag, create_time, agent_id) VALUES (#{customTag}, now(), #{agentId})
  </insert>

  <update id="updateCustomTag" parameterType="com.pcloud.book.applet.entity.AppletNewsCustomTag">
    UPDATE applet_news_custom_tag SET custom_tag = #{customTag}, create_time = now()
    where id = #{id}
  </update>

  <select id="getCustomTagList" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletNewsCustomTagDTO">
    SELECT
    id customTagId,
	custom_tag customTag,
	agent_id agentId
    FROM
        applet_news_custom_tag
    WHERE 1=1
    <choose>
      <when test="agentId != null">
        and agent_id = #{agentId}
      </when>
      <otherwise>
        and agent_id = 0
      </otherwise>
    </choose>
    ORDER BY
        create_time DESC, id DESC
  </select>

  <select id="customTagExist" parameterType="map" resultType="boolean">
    SELECT EXISTS (
      SELECT id
      FROM  applet_news_custom_tag
      WHERE
      custom_tag = #{customTag}
      and agent_id = #{agentId}
    )
  </select>

  <select id="getNewsIdListByCustomTagId" parameterType="long" resultType="long">
    SELECT id from applet_news
    WHERE custom_tag_id = #{customTagId} and is_delete = 0
    ORDER BY create_time DESC
  </select>

    <select id="getPageByNewsByTempletLabel" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletNewsDTO">
        SELECT
        n.id,
        n.news_name newsName,
        n.source,
        n.news_classify_id newsClassifyId,
        n.pro_label_id proLabelId,
        n.dep_label_id depLabelId,
        n.pur_label_id purLabelId,
        n.type,
        n.digest,
        n.pic1,
        n.pic2,
        n.pic3,
        n.content,
        n.show_state showState,
        n.create_time createTime,
        n.first_classify firstClassify,
        n.second_classify secondClassify,
        n.grade_label_id gradeLabelId,
        n.subject_label_id subjectLabelId,
        n.rights_classify_id rightsClassifyId,
        n.jump_type jumpType,
        n.jump_url jumpUrl,
        n.url_number urlNumber,
        n.show_source showSource,
        n.show_link showLink,
        n.crossed_price crossedPrice,
        n.appraisal_price appraisalPrice,
        n.appraisal_type appraisalType,
        n.appraisal_img appraisalImg
        FROM applet_news n
        LEFT JOIN applet_news_category category on n.id = category.applet_news_id
        WHERE 1=1
        and n.show_state = 1 and n.is_delete = 0
        <if test="firstClassify != null">
            and category.first_classify = #{firstClassify}
        </if>
        <if test="secondClassify != null">
            and category.second_classify = #{secondClassify}
        </if>
        <if test="thirdClassify!=null">
            AND category.third_classify =#{thirdClassify}
        </if>
        <if test="gradeLabelId != null">
            and category.grade_label_id = #{gradeLabelId}
        </if>
        <if test="subjectLabelId != null">
            and category.subject_label_id = #{subjectLabelId}
        </if>
        AND n.rights_classify_id IN(
        SELECT id FROM rights_setting_classify WHERE 1=1
        <if test="rightsType != null">
            AND rights_type = #{rightsType}
        </if>
        )
        group by n.id
        ORDER BY n.update_time DESC
    </select>

  <select id="getClassifyNewsByTempletLabel" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletNewsDTO">
      select
      <include refid="sql_news"/>
       from (
      select distinct
      n.id,
      n.news_name newsName,
      n.source,
      n.news_classify_id newsClassifyId,
      n.pro_label_id proLabelId,
      n.dep_label_id depLabelId,
      n.pur_label_id purLabelId,
      n.type,
      n.digest,
      n.pic1,
      n.pic2,
      n.pic3,
      n.content,
      n.show_state showState,
      n.create_time createTime,
      n.first_classify firstClassify,
      n.second_classify secondClassify,
      n.grade_label_id gradeLabelId,
      n.subject_label_id subjectLabelId,
      n.rights_classify_id rightsClassifyId,
      n.jump_type jumpType,
      n.jump_url jumpUrl,
      n.url_number urlNumber,
      n.show_source showSource,
      n.show_link showLink,
      n.city_code cityCode,
      n.crossed_price crossedPrice,
      n.appraisal_price appraisalPrice,
      n.appraisal_type appraisalType,
      n.appraisal_img appraisalImg
    FROM
      applet_news n
        LEFT JOIN rights_setting_item_detail d ON d.serve_id = n.id
        LEFT JOIN rights_setting_item i ON d.rights_setting_item_id = i.id
        LEFT JOIN rights_setting s ON s.id = i.rights_setting_id
    WHERE
      s.rights_setting_type = 1 and  n.is_delete = 0
      AND i.open_state = 1 AND i.rights_type = 'WEEK' AND i.rights_classify_id = 10 AND d.serve_type = 'NEWS'
      <if test="firstClassify != null">
        and s.first_classify = #{firstClassify}
      </if>
      <if test="secondClassify != null">
        and s.second_classify = #{secondClassify}
      </if>
      <if test="gradeLabelId != null">
        and s.grade_label_id = #{gradeLabelId}
      </if>
      <if test="subjectLabelId != null">
        and s.subject_label_id = #{subjectLabelId}
      </if>
      <if test="deepRead!=null">
        AND i.deep_read = #{deepRead}
      </if>
      <if test="easyRead!=null">
        AND i.easy_read = #{easyRead}
      </if>
      <if test="efficientRead!=null">
        AND i.efficient_read = #{efficientRead}
      </if>
    ORDER BY n.update_time DESC) m

  </select>

  <insert id="batchInsert" parameterType="com.pcloud.book.applet.entity.AppletNews" useGeneratedKeys="true"
          keyProperty="id">
    insert into applet_news(
    news_name, source, news_classify_id, pro_label_id, dep_label_id, pur_label_id, type, digest,
    pic1, pic2, pic3, content, show_state, create_time, update_time , first_classify, second_classify, grade_label_id,
    subject_label_id, rights_classify_id,
    city_code,
    jump_type, jump_url,url_number, custom_tag_id, show_source, show_link
    )
    values
    <foreach collection="list" item="item" index="index" separator=",">
      (
      #{item.newsName}, #{item.source}, #{item.newsClassifyId}, #{item.proLabelId}, #{item.depLabelId}, #{item.purLabelId}, #{item.type}, #{item.digest},
      #{item.pic1}, #{item.pic2}, #{item.pic3}, #{item.content}, #{item.showState}, NOW(), NOW(), #{item.firstClassify,jdbcType=BIGINT},
      #{item.secondClassify,jdbcType=BIGINT},
      #{item.gradeLabelId,jdbcType=BIGINT}, #{item.subjectLabelId,jdbcType=BIGINT}, #{item.rightsClassifyId,jdbcType=BIGINT},
      #{item.cityCode},
      #{item.jumpType}, #{item.jumpUrl}, #{item.urlNumber}, #{item.customTagId}, #{item.showSource}, #{item.showLink}
      )
    </foreach>
  </insert>

  <select id="listAppletNews4WechatCount" parameterType="map" resultType="integer">
    select
    count(1)
    FROM applet_news n
    LEFT JOIN applet_news_classify c ON n.news_classify_id=c.id
    WHERE n.show_state = 1 and n.is_delete = 0
    <if test="newsClassifyId >0">
      AND n.news_classify_id = #{newsClassifyId}
    </if>
    <if test="newsClassifyIdList != null">
      AND n.news_classify_id in
      <foreach collection="newsClassifyIdList" item="item" open="(" separator="," close=")">
        ${item}
      </foreach>
    </if>
  </select>


    <select id="listNewsByCityCode" resultType="com.pcloud.book.applet.dto.AppletNewsDTO">
        SELECT
        n.id,
        n.news_name newsName,
        n.source,
        n.news_classify_id newsClassifyId,
        n.pro_label_id proLabelId,
        n.dep_label_id depLabelId,
        n.pur_label_id purLabelId,
        n.type,
        n.digest,
        n.pic1,
        n.pic2,
        n.pic3,
        n.show_state showState,
        n.create_time createTime,
        n.first_classify firstClassify,
        n.second_classify secondClassify,
        n.grade_label_id gradeLabelId,
        n.subject_label_id subjectLabelId,
        n.rights_classify_id rightsClassifyId,
        n.jump_type jumpType,
        n.jump_url jumpUrl,
        n.url_number urlNumber,
        n.show_source showSource,
        n.city_code cityCode,
        n.show_link showLink,
        n.crossed_price crossedPrice,
        n.appraisal_price appraisalPrice,
        n.appraisal_type appraisalType,
        n.appraisal_img appraisalImg
        FROM applet_news n
        WHERE n.show_state = 1 and n.is_delete = 0
        AND n.city_code = #{cityId}
        ORDER BY n.update_time DESC
    </select>

    <update id="deleteByNews" parameterType="list">
        update
        applet_news
        set
        is_delete = 1
        where
        id in
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>

    <select id="getByIds4Record" resultType="com.pcloud.book.applet.dto.AppletNewsDTO">
        SELECT
        n.id,
        n.news_name newsName,
        n.source,
        n.news_classify_id newsClassifyId,
        n.pro_label_id proLabelId,
        n.dep_label_id depLabelId,
        n.pur_label_id purLabelId,
        n.type,
        n.digest,
        n.pic1,
        n.pic2,
        n.pic3,
        n.show_state showState,
        n.create_time createTime,
        n.first_classify firstClassify,
        n.second_classify secondClassify,
        n.grade_label_id gradeLabelId,
        n.subject_label_id subjectLabelId,
        n.rights_classify_id rightsClassifyId,
        n.jump_type jumpType,
        n.jump_url jumpUrl,
        n.url_number urlNumber,
        n.show_source showSource,
        n.show_link showLink,
        n.is_delete  isDelete,
        n.crossed_price crossedPrice,
        n.appraisal_price appraisalPrice,
        n.appraisal_type appraisalType,
        n.appraisal_img appraisalImg,
        count(n.id) browseCount
        FROM applet_news n
        left join
        applet_link_click c on n.id = c.from_id and type_id = 1 AND record_type = 2
        WHERE
        n.id IN
        <foreach collection="list" item="item" separator="," open="(" close=")" >
            #{item}
        </foreach>
        group by n.id
    </select>

</mapper>
