<?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.mapper.RightsSettingTitleMapper">
  <resultMap id="BaseResultMap" type="com.pcloud.book.rightsSetting.entity.RightsSettingTitle">
    <result column="id" jdbcType="BIGINT" property="id" />
    <result column="rights_setting_id" jdbcType="BIGINT" property="rightsSettingId" />
    <result column="rights_setting_title" jdbcType="VARCHAR" property="rightsSettingTitle" />
    <result column="rights_setting_desc" jdbcType="VARCHAR" property="rightsSettingDesc" />
    <result column="open_state" jdbcType="TINYINT" property="openState" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="rights_setting_now_type" jdbcType="TINYINT" property="rightsSettingNowType" />
    <result column="serve_type" jdbcType="VARCHAR" property="serveType" />
    <result column="easy_read" jdbcType="TINYINT" property="easyRead" />
    <result column="efficient_read" jdbcType="TINYINT" property="efficientRead" />
    <result column="deep_read" jdbcType="TINYINT" property="deepRead" />
    <result column="seq_num" jdbcType="INTEGER" property="seqNum" />
    <result column="collage_state" jdbcType="TINYINT" property="collageState" />
    <result column="display_type" jdbcType="INTEGER" property="displayType"/>
  </resultMap>

  <sql id="baseSql">
    id, rights_setting_id, rights_setting_title,
    rights_setting_desc, open_state, create_time,
    update_time,rights_setting_now_type,serve_type,easy_read,efficient_read,deep_read,seq_num,collage_state,display_type
  </sql>
  <insert id="insert" parameterType="com.pcloud.book.rightsSetting.entity.RightsSettingTitle">
    insert into rights_setting_title (id, rights_setting_id, rights_setting_title, 
      rights_setting_desc, open_state, create_time, 
      update_time,rights_setting_now_type,serve_type,easy_read,efficient_read,deep_read)
    values (#{id,jdbcType=BIGINT}, #{rightsSettingId,jdbcType=BIGINT}, #{rightsSettingTitle,jdbcType=VARCHAR}, 
      #{rightsSettingDesc,jdbcType=VARCHAR}, #{openState,jdbcType=TINYINT}, #{createTime,jdbcType=TIMESTAMP}, 
      #{updateTime,jdbcType=TIMESTAMP},#{rightsSettingNowType},#{serveType},#{easyRead},#{efficientRead},#{deepRead})
  </insert>
  <insert id="insertSelective" parameterType="com.pcloud.book.rightsSetting.entity.RightsSettingTitle">
    insert into rights_setting_title
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="rightsSettingId != null">
        rights_setting_id,
      </if>
      <if test="rightsSettingTitle != null">
        rights_setting_title,
      </if>
      <if test="rightsSettingDesc != null">
        rights_setting_desc,
      </if>
      <if test="openState != null">
        open_state,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="serveType != null">
        serve_type,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=BIGINT},
      </if>
      <if test="rightsSettingId != null">
        #{rightsSettingId,jdbcType=BIGINT},
      </if>
      <if test="rightsSettingTitle != null">
        #{rightsSettingTitle,jdbcType=VARCHAR},
      </if>
      <if test="rightsSettingDesc != null">
        #{rightsSettingDesc,jdbcType=VARCHAR},
      </if>
      <if test="openState != null">
        #{openState,jdbcType=TINYINT},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="serveType != null">
        #{serve_type,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>


  <delete id="deleteByRightSettingId" parameterType="long">
    delete
    from
    rights_setting_title
    where
    rights_setting_id = #{rightsSettingId}
  </delete>

  <insert id="batchInsert" parameterType="com.pcloud.book.rightsSetting.entity.RightsSettingTitle" useGeneratedKeys="true" keyProperty="id">
    insert into rights_setting_title (rights_setting_id, rights_setting_title,
    rights_setting_desc, open_state, create_time, update_time,rights_setting_now_type,serve_type,
    easy_read,efficient_read,deep_read,seq_num, collage_state,display_type)
    values
    <foreach collection="list" item="item" index="index" separator=",">
    (#{item.rightsSettingId,jdbcType=BIGINT}, #{item.rightsSettingTitle,jdbcType=VARCHAR},
    #{item.rightsSettingDesc,jdbcType=VARCHAR}, #{item.openState,jdbcType=TINYINT}, now(), now(),#{item.rightsSettingNowType},#{item.serveType},
    #{item.easyRead},#{item.efficientRead},#{item.deepRead},#{item.seqNum}, #{item.collageState}, #{item.displayType})
    </foreach>
  </insert>


  <update id="batchUpdate" parameterType="list">
    update rights_setting_title
    <trim prefix="set" suffixOverrides=",">
      <trim prefix="rights_setting_title =case" suffix="end,">
        <foreach collection="list" item="item" index="index">
            when id=#{item.id} then #{item.rightsSettingTitle}
        </foreach>
      </trim>
      <trim prefix="rights_setting_desc =case" suffix="end,">
        <foreach collection="list" item="item" index="index">
            when id=#{item.id} then #{item.rightsSettingDesc}
        </foreach>
      </trim>
      <trim prefix="open_state =case" suffix="end,">
        <foreach collection="list" item="item" index="index">
            when id=#{item.id} then #{item.openState}
        </foreach>
      </trim>
      <trim prefix="serve_type =case" suffix="end,">
        <foreach collection="list" item="item" index="index">
          when id=#{item.id} then #{item.serveType}
        </foreach>
      </trim>
      <trim prefix="easy_read =case" suffix="end,">
        <foreach collection="list" item="item" index="index">
          when id=#{item.id} then #{item.easyRead}
        </foreach>
      </trim>
      <trim prefix="efficient_read =case" suffix="end,">
        <foreach collection="list" item="item" index="index">
          when id=#{item.id} then #{item.efficientRead}
        </foreach>
      </trim>
      <trim prefix="deep_read =case" suffix="end,">
        <foreach collection="list" item="item" index="index">
          when id=#{item.id} then #{item.deepRead}
        </foreach>
      </trim>
    </trim>
    where id in
    <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
      #{item.id,jdbcType=BIGINT}
    </foreach>
  </update>

  <select id="getByRightSettingId" resultMap="BaseResultMap" parameterType="long">
    select
    <include refid="baseSql"/>
    from
    rights_setting_title
    where
    rights_setting_id = #{rightsSettingId}
    <choose>
      <when test="readType==1">
        and easy_read=1
      </when>
      <when test="readType==2">
        and efficient_read=1
      </when>
      <when test="readType==3">
        and deep_read=1
      </when>
    </choose>
  </select>

  <select id="getByRightSettingIdAndType" resultMap="BaseResultMap" parameterType="map">
    select
    <include refid="baseSql"/>
    from
    rights_setting_title
    where
    rights_setting_id = #{rightsSettingId}
    and
    rights_setting_now_type = #{type}
    <choose>
      <when test="readType==1">
        and easy_read=1
      </when>
      <when test="readType==2">
        and efficient_read=1
      </when>
      <when test="readType==3">
        and deep_read=1
      </when>
    </choose>
    limit 1
  </select>

  <select id="getCount4Title" resultType="com.pcloud.book.rightsSetting.dto.ReadTypeCountDTO" parameterType="long">
    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_title
    GROUP BY
    rights_setting_id
    HAVING
    rights_setting_id = #{rightsSettingId};
  </select>

</mapper>