<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.pcloud.book.rightsSetting.dao.impl.RightsItemGroupDaoImpl">

    <resultMap  id="BaseResultMap" type="com.pcloud.book.rightsSetting.entity.RightsItemGroup">
        <id property="id" column="id" jdbcType="BIGINT"/>
        <result property="rightsSettingId" column="rights_setting_id" jdbcType="BIGINT"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="showState" column="show_state" jdbcType="TINYINT"/>
        <result property="type" column="type" jdbcType="TINYINT"/>
        <result property="pic" column="pic" jdbcType="VARCHAR"/>
        <result property="desc1" column="desc1" jdbcType="VARCHAR"/>
        <result property="desc2" column="desc2" jdbcType="VARCHAR"/>
        <result property="desc3" column="desc3" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result column="easy_read" jdbcType="TINYINT" property="easyRead" />
        <result column="efficient_read" jdbcType="TINYINT" property="efficientRead" />
        <result column="deep_read" jdbcType="TINYINT" property="deepRead" />
    </resultMap>

    <sql id="Base_Column_List">
        id, rights_setting_id, name, show_state, type, pic, desc1, desc2, desc3, create_time, easy_read,efficient_read,deep_read
    </sql>

    <select id="getById" resultMap="BaseResultMap" parameterType="java.lang.Long">
        select
        <include refid="Base_Column_List"/>
        from rights_item_group
        where id = #{id,jdbcType=BIGINT}
    </select>

    <delete id="deleteById" parameterType="long">
        delete from rights_item_group
        where id = #{id,jdbcType=BIGINT}
    </delete>

    <insert id="insert" parameterType="com.pcloud.book.rightsSetting.entity.RightsItemGroup" useGeneratedKeys="true" keyProperty="id">
        insert into rights_item_group
        <trim prefix="(" suffix=")" suffixOverrides=",">
            rights_setting_id, name, show_state, type, pic, desc1, desc2, desc3, create_time,
            easy_read,efficient_read,deep_read
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{rightsSettingId,jdbcType=BIGINT},
            #{name,jdbcType=VARCHAR},
            #{showState,jdbcType=TINYINT},
            #{type,jdbcType=TINYINT},
            #{pic,jdbcType=VARCHAR},
            #{desc1,jdbcType=VARCHAR},
            #{desc2,jdbcType=VARCHAR},
            #{desc3,jdbcType=VARCHAR},
            NOW(),
            #{easyRead},#{efficientRead},#{deepRead}
        </trim>
    </insert>

    <insert id="batchInsert"  parameterType="com.pcloud.book.rightsSetting.entity.RightsItemGroup" useGeneratedKeys="true" keyProperty="id">
        insert into rights_item_group (
        rights_setting_id, name, show_state, type, pic, desc1, desc2, desc3, create_time,
        easy_read,efficient_read,deep_read
        ) values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.rightsSettingId,jdbcType=BIGINT},
            #{item.name,jdbcType=VARCHAR},
            #{item.showState,jdbcType=TINYINT},
            #{item.type,jdbcType=TINYINT},
            #{item.pic,jdbcType=VARCHAR},
            #{item.desc1,jdbcType=VARCHAR},
            #{item.desc2,jdbcType=VARCHAR},
            #{item.desc3,jdbcType=VARCHAR},
            NOW(),
            #{item.easyRead},#{item.efficientRead},#{item.deepRead}
            )
        </foreach>
    </insert>

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

    <select id="getListByRightSettingId" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from rights_item_group
        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="getCount4Group" 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_item_group
        WHERE
        rights_setting_id = #{rightsSettingId}
        GROUP BY
        rights_setting_id
    </select>

</mapper>