<?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.custom.mapper.CustomPlanRightsSettingMapper" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.custom.entity.CustomPlanRightsSetting" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="custom_plan_id" property="customPlanId" jdbcType="BIGINT" />
    <result column="module_type" property="moduleType" jdbcType="INTEGER" />
    <result column="module_title" property="moduleTitle" jdbcType="VARCHAR" />
    <result column="button_name" property="buttonName" jdbcType="VARCHAR" />
    <result column="default_url" property="defaultUrl" jdbcType="VARCHAR" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
  </resultMap>

  <resultMap id="VOMap" type="com.pcloud.book.custom.vo.EditCustomRightsSettingVO">
    <id column="custom_plan_id" property="planId"/>
    <collection property="customPlanRightsSettings" ofType="com.pcloud.book.custom.entity.CustomPlanRightsSetting"
      resultMap="RightsSettingMap"/>
  </resultMap>

  <resultMap id="RightsSettingMap" type="com.pcloud.book.custom.entity.CustomPlanRightsSetting">
    <id column="csid" property="id"/>
    <result column="custom_plan_id" property="customPlanId"/>
    <result column="module_type" property="moduleType"/>
    <result column="module_title" property="moduleTitle"/>
    <result column="button_name" property="buttonName"/>
    <result column="default_url" property="defaultUrl"/>
    <collection property="customRightsSettingItems" ofType="com.pcloud.book.custom.entity.CustomRightsSettingItem" resultMap="ItemMap"/>
  </resultMap>

  <resultMap id="ItemMap" type="com.pcloud.book.custom.entity.CustomRightsSettingItem">
    <id column="crid" property="id"/>
    <result column="custom_rights_setting_id" property="customRightsSettingId"/>
    <result column="rights_setting_classify_id" property="rightsSettingClassifyId"/>
    <result column="item_title" property="itemTitle"/>
    <collection property="customRightsItemDescriptions" ofType="com.pcloud.book.custom.entity.CustomRightsItemDescription" 
      resultMap="descriptionMap"/>
  </resultMap> 
  
  <resultMap id="descriptionMap" type="com.pcloud.book.custom.entity.CustomRightsItemDescription">
    <id column="cdid" property="id"/>
    <result column="custom_rights_setting_item_id" property="customRightsSettingItemId"/>
    <result column="descripiton" property="descripiton"/>
  </resultMap>
  

  <sql id="Base_Column_List" >
    id, custom_plan_id, module_type, module_title, button_name, default_url, create_time, 
    update_time
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >

  </select>
  <select id="getCustomRightsSettingModule" resultMap="VOMap" parameterType="integer">
    SELECT
    cs.id csid,
    custom_plan_id,
    module_type,
    module_title,
    button_name,
    default_url,
    cr.id crid,
    cr.custom_rights_setting_id,
    cr.rights_setting_classify_id,
    cr.item_title,
    cd.id cdid,
    cd.custom_rights_setting_item_id ,
    cd.descripiton
    FROM
    `custom_plan_rights_setting` cs
    LEFT JOIN custom_rights_setting_item cr ON cs.id = cr.custom_rights_setting_id
    left join custom_rights_item_description cd on cr.id = cd.custom_rights_setting_item_id
    where cs.custom_plan_id = #{planId}
    order by cs.id, cr.id, cd.id;
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
    delete from custom_plan_rights_setting
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <delete id="deleteByPlanId" parameterType="integer">
    delete from
    custom_plan_rights_setting
    where custom_plan_id = #{planId,jdbcType=BIGINT}
  </delete>
  <insert id="insert" parameterType="com.pcloud.book.custom.entity.CustomPlanRightsSetting" useGeneratedKeys="true" keyProperty="id">
    insert into custom_plan_rights_setting (custom_plan_id, module_type,
      module_title, button_name, default_url, 
      create_time, update_time)
    values (#{customPlanId,jdbcType=BIGINT}, #{moduleType,jdbcType=INTEGER},
      #{moduleTitle,jdbcType=VARCHAR}, #{buttonName,jdbcType=VARCHAR}, #{defaultUrl,jdbcType=VARCHAR}, 
      now(),now())
  </insert>
  <insert id="insertSelective" parameterType="com.pcloud.book.custom.entity.CustomPlanRightsSetting" >
    insert into custom_plan_rights_setting
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="customPlanId != null" >
        custom_plan_id,
      </if>
      <if test="moduleType != null" >
        module_type,
      </if>
      <if test="moduleTitle != null" >
        module_title,
      </if>
      <if test="buttonName != null" >
        button_name,
      </if>
      <if test="defaultUrl != null" >
        default_url,
      </if>
      <if test="createTime != null" >
        create_time,
      </if>
      <if test="updateTime != null" >
        update_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=BIGINT},
      </if>
      <if test="customPlanId != null" >
        #{customPlanId,jdbcType=BIGINT},
      </if>
      <if test="moduleType != null" >
        #{moduleType,jdbcType=INTEGER},
      </if>
      <if test="moduleTitle != null" >
        #{moduleTitle,jdbcType=VARCHAR},
      </if>
      <if test="buttonName != null" >
        #{buttonName,jdbcType=VARCHAR},
      </if>
      <if test="defaultUrl != null" >
        #{defaultUrl,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null" >
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null" >
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.pcloud.book.custom.entity.CustomPlanRightsSetting" >
    update custom_plan_rights_setting
    <set >
      <if test="customPlanId != null" >
        custom_plan_id = #{customPlanId,jdbcType=BIGINT},
      </if>
      <if test="moduleType != null" >
        module_type = #{moduleType,jdbcType=INTEGER},
      </if>
      <if test="moduleTitle != null" >
        module_title = #{moduleTitle,jdbcType=VARCHAR},
      </if>
      <if test="buttonName != null" >
        button_name = #{buttonName,jdbcType=VARCHAR},
      </if>
      <if test="defaultUrl != null" >
        default_url = #{defaultUrl,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null" >
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null" >
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.pcloud.book.custom.entity.CustomPlanRightsSetting" >
    update custom_plan_rights_setting
    set custom_plan_id = #{customPlanId,jdbcType=BIGINT},
      module_type = #{moduleType,jdbcType=INTEGER},
      module_title = #{moduleTitle,jdbcType=VARCHAR},
      button_name = #{buttonName,jdbcType=VARCHAR},
      default_url = #{defaultUrl,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_time = #{updateTime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=BIGINT}
  </update>
</mapper>