<?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.rightsSetting.dao.impl.RightsSettingItemDaoImpl">

    <resultMap type="com.pcloud.book.rightsSetting.entity.RightsSettingItem" id="RightsSettingItemMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="rightsSettingId" column="rights_setting_id" jdbcType="INTEGER"/>
        <result property="rightsType" column="rights_type" jdbcType="VARCHAR"/>
        <result property="rightsClassifyId" column="rights_classify_id" jdbcType="INTEGER"/>
        <result property="description" column="description" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="title" column="title" jdbcType="VARCHAR"/>
        <result property="uuid" column="uuid" jdbcType="VARCHAR"/>
        <result property="openState" column="open_state" jdbcType="INTEGER" />
        <result property="serviceDesc" column="service_desc" jdbcType="VARCHAR"/>
        <result property="serveType" column="serve_type" jdbcType="VARCHAR"/>
        <result property="easyRead" column="easy_read" jdbcType="TINYINT" />
        <result property="efficientRead" column="efficient_read" jdbcType="TINYINT" />
        <result property="deepRead" column="deep_read" jdbcType="TINYINT" />
        <result property="collageState" column="collage_state" jdbcType="INTEGER" />
        <result property="displayType" column="display_type" jdbcType="INTEGER" />
    </resultMap>

    <sql id="Base_Column_List">
        id, rights_setting_id, rights_type, rights_classify_id, description, create_time, title, open_state, service_desc,
        serve_type, collage_state, display_type
    </sql>

    <!--通过实体作为筛选条件查询-->
    <select id="queryAll" resultType="com.pcloud.book.rightsSetting.entity.RightsSettingItem" >
        select
        i.id, i.rights_setting_id rightsSettingId,
        i.rights_type rightsType, i.rights_classify_id rightsClassifyId,
    i.description, i.create_time createTime, i.title,i.uuid,i.open_state openState,i.service_desc serviceDesc,i.serve_type serveType,
        c.book_id bookId, i.easy_read easyRead,i.efficient_read efficientRead,i.deep_read deepRead,i.collage_state collageState,
        i.display_type displayType
        from book.rights_setting_item i
        LEFT JOIN rights_setting c ON c.id=i.rights_setting_id
        <where>
            <if test="id != null">
                and i.id = #{id}
            </if>
            <if test="rightsSettingId != null">
                and i.rights_setting_id = #{rightsSettingId}
            </if>
            <if test="rightsType != null and rightsType != ''">
                and i.rights_type = #{rightsType}
            </if>
            <if test="rightsClassifyId != null ">
                and i.rights_classify_id = #{rightsClassifyId}
            </if>
            <choose>
                <when test="readType==1">
                    and i.easy_read=1
                </when>
                <when test="readType==2">
                    and i.efficient_read=1
                </when>
                <when test="readType==3">
                    and i.deep_read=1
                </when>
            </choose>
        </where>
        order by i.id asc
    </select>

    <select id="getItemsByClassify" resultType="com.pcloud.book.rightsSetting.entity.RightsSettingItem">
        select
        i.id, i.rights_setting_id rightsSettingId,
        i.rights_type rightsType, i.rights_classify_id rightsClassifyId,
        i.description, i.create_time createTime, i.title,i.uuid,i.open_state openState,i.service_desc serviceDesc,i.serve_type serveType,
        c.book_id bookId, i.easy_read easyRead,i.efficient_read efficientRead,i.deep_read deepRead, i.display_type displayType
        from book.rights_setting_item i
        LEFT JOIN rights_setting c ON c.id=i.rights_setting_id
        <where>
            <if test="rightsSettingType != null">
                and c.rights_setting_type = #{rightsSettingType}
            </if>
            <if test="firstClassify != null">
                and c.first_classify = #{firstClassify}
            </if>
            <if test="secondClassify != null">
                and c.second_classify = #{secondClassify}
            </if>
            <if test="thirdClassify != null">
                and c.third_classify = #{thirdClassify}
            </if>
            <if test="gradeLabelId != null">
                and c.grade_label_id = #{gradeLabelId}
            </if>
            <if test="subjectLabelId != null">
                and c.subject_label_id = #{subjectLabelId}
            </if>
            <if test="volLabelId != null" >
                and c.vol_label_id = #{volLabelId}
            </if>
            <if test="rightsType != null and rightsType != ''">
                and i.rights_type = #{rightsType}
            </if>
            <if test="rightsClassifyId != null ">
                and i.rights_classify_id = #{rightsClassifyId}
            </if>
            <choose>
                <when test="readType==1">
                    and i.easy_read=1
                </when>
                <when test="readType==2">
                    and i.efficient_read=1
                </when>
                <when test="readType==3">
                    and i.deep_read=1
                </when>
            </choose>
        </where>
        order by i.id asc
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into book.rights_setting_item(rights_setting_id, rights_type, rights_classify_id, description, create_time,title,uuid,open_state,service_desc,serve_type,
        easy_read,efficient_read,deep_read,collage_state,display_type)
        values (#{rightsSettingId}, #{rightsType}, #{rightsClassifyId}, #{description}, NOW(), #{title},#{uuid},#{openState},#{serviceDesc},#{serveType},
        #{easyRead},#{efficientRead},#{deepRead},#{collageState},#{displayType})
    </insert>

    <!--通过权益主键删除-->
    <delete id="deleteByRightsSettingId" parameterType="map">
        delete from book.rights_setting_item
        where rights_setting_id = #{rightsSettingId}
        <if test="rightsType!=null">
            and rights_type = #{rightsType}
        </if>
    </delete>

    <!--批量新增-->
    <insert id="batchInsert" useGeneratedKeys="true" parameterType="java.util.List">
        insert into book.rights_setting_item (
        rights_setting_id, rights_type, rights_classify_id, description, create_time,title,uuid,open_state,service_desc,serve_type,
        easy_read,efficient_read,deep_read,collage_state,display_type
        )
        values
        <foreach collection="list" item="item" index="index" separator="," >
            (
            #{item.rightsSettingId},
            #{item.rightsType,jdbcType=VARCHAR},
            #{item.rightsClassifyId,jdbcType=VARCHAR},
            #{item.description,jdbcType=VARCHAR},
            NOW(),
            #{item.title},
            #{item.uuid},
            #{item.openState},
            #{item.serviceDesc},
            #{item.serveType},
            #{item.easyRead},#{item.efficientRead},#{item.deepRead},
            #{item.collageState},
            #{item.displayType}
            )
        </foreach>
    </insert>

    <select id="getAllRightsClassify" parameterType="map" resultType="com.pcloud.book.rightsSetting.entity.RightsSettingClassify">
      SELECT id, classify, rights_type rightsType,pic_url picUrl
      FROM rights_setting_classify
      WHERE 1=1
      <if test="rightsType!=null">
          and rights_type=#{rightsType}
      </if>
    </select>

    <select id="getItemsByRightsSettingId4Read" parameterType="map" resultType="com.pcloud.book.rightsSetting.entity.RightsSettingItem">
        select
        i.id, i.rights_setting_id rightsSettingId,
        i.rights_type rightsType, i.rights_classify_id rightsClassifyId,
        i.description, i.create_time createTime, i.title,i.uuid,IFNULL(p.is_open,0) isOpen,p.service_desc serviceDesc,
        i.easy_read easyRead,i.efficient_read efficientRead,i.deep_read deepRead
        from book.rights_setting_item i
        left join book.rights_read_plan p on i.id=p.rights_setting_item_id
        <where>
            <if test="rightsSettingId != null">
                and i.rights_setting_id = #{rightsSettingId}
            </if>
            <if test="rightsType != null and rightsType != ''">
                and i.rights_type = #{rightsType}
            </if>
        </where>
        order by i.id asc
    </select>


    <select id="getCount4Item" parameterType="long" resultType="com.pcloud.book.rightsSetting.dto.ReadTypeCountDTO">
        SELECT
        rights_setting_id rightsSettingId,
        SUM(IFNULL(easy_read, 0)) easyReadCount,
        SUM(IFNULL(efficient_read, 0)) efficientReadCount,
        SUM(IFNULL(deep_read, 0)) deepReadCount
        FROM
        rights_setting_item
        WHERE
        rights_setting_id = #{rightsSettingId}
        GROUP BY
        rights_setting_id
    </select>

    <select id="getRightsClassifyMap" parameterType="list" resultType="com.pcloud.book.rightsSetting.entity.RightsSettingClassify">
        SELECT id, classify, rights_type rightsType, pic_url picUrl
        FROM rights_setting_classify
        <where>
            <if test="list != null">
                id in
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getRightSettingInfo" parameterType="list" resultMap="RightsSettingItemMap">
        SELECT id, rights_setting_id, serve_type , rights_type ,collage_state
        FROM rights_setting_item
        WHERE 1=1
        <if test="serveType!=null">
            and serve_type=#{serveType}
        </if>
        <if test="rightsType!=null">
            and rights_type=#{rightsType}
        </if>
        <if test="rightsSettingId!=null">
            and rights_setting_id=#{rightsSettingId}
        </if>
    </select>

    <select id="selectNewsByRightsSettingIds" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletNewsDTO">
        SELECT
            distinct c.id AS id,
            c.news_name AS newsName,
            c.source AS source,
            c.news_classify_id AS newsClassifyId,
            c.pro_label_id AS proLabelId,
            c.dep_label_id AS depLabelId,
            c.pur_label_id AS purLabelId,
            c.type AS type,
            c.digest AS digest,
            c.pic1 AS pic1,
            c.pic2 AS pic2,
            c.pic3 AS pic3,
            c.content AS content,
            c.show_state AS showState,
            c.create_time AS createTime,
            c.first_classify AS firstClassify,
            c.second_classify AS secondClassify,
        c.third_classify as thirdClassify,
        c.grade_label_id AS gradeLabelId,
            c.subject_label_id AS subjectLabelId,
            c.rights_classify_id AS rightsClassifyId,
            c.jump_type AS jumpType,
            c.jump_url AS jumpUrl,
            c.url_number AS urlNumber,
            c.custom_tag_id AS customTagId,
            c.show_source AS showSource,
            c.show_link AS showLink,
            c.crossed_price crossedPrice,
            c.appraisal_price appraisalPrice,
            c.appraisal_type appraisalType,
            c.appraisal_img appraisalImg
        FROM
            rights_setting_item a
            LEFT JOIN rights_setting_item_detail b ON a.id = b.rights_setting_item_id
            RIGHT JOIN applet_news c ON c.id = b.serve_id
        WHERE
            b.serve_type = 'NEWS'
            AND a.rights_type ='WEEK'
            AND a.open_state = 1
            AND a.rights_classify_id = 10
        <if test="deepRead!=null">
            AND a.deep_read = #{deepRead}
        </if>
        <if test="easyRead!=null">
            AND a.easy_read = #{easyRead}
        </if>
        <if test="efficientRead!=null">
            AND a.efficient_read = #{efficientRead}
        </if>
            AND rights_setting_id IN
        <foreach collection="list" item="item" separator="," open="(" close=")">
            ${item}
        </foreach>
        order by c.create_time
    </select>

    <select id="getRightsClassifyByIds" parameterType="map" resultType="com.pcloud.book.rightsSetting.entity.RightsSettingClassify">
        SELECT id, classify, rights_type rightsType,pic_url picUrl
        FROM rights_setting_classify
        WHERE
        id in
        <foreach collection="list" open="(" close=")" separator="," index="index" item="item">
            #{item}
        </foreach>
    </select>

    <select id="getRightSettingItemByIds" resultMap="RightsSettingItemMap" parameterType="map">
        SELECT <include refid="Base_Column_List"/>
        FROM rights_setting_item
        WHERE id in
        <foreach collection="list" open="(" close=")" separator="," index="index" item="item">
            #{item}
        </foreach>
    </select>

</mapper>