<?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.yami.shop.user.comment.dao.UserLevelMapper">

  <resultMap id="userLevelMap" type="com.yami.shop.user.comment.model.UserLevel">
    <id column="id" property="id" />
    <result column="level" property="level"/>
    <result column="level_name" property="levelName"/>
    <result column="level_type" property="levelType"/>
    <result column="need_growth" property="needGrowth"/>
    <result column="need_amount" property="needAmount"/>
    <result column="term" property="term"/>
    <result column="img" property="img"/>
    <result column="discount_range" property="discountRange"/>
    <result column="discount_type" property="discountType"/>
    <result column="discount" property="discount"/>
    <result column="pres_score" property="presScore"/>
  </resultMap>
  <resultMap id="couponOrderDtoMap" type="com.yami.shop.user.comment.model.UserLevel">
    <id column="id" property="id" />
    <result column="level" property="level"/>
    <result column="level_name" property="levelName"/>
    <result column="level_type" property="levelType"/>
    <result column="need_growth" property="needGrowth"/>
    <result column="need_amount" property="needAmount"/>
    <result column="is_free_fee" property="isFreeFee"/>
    <result column="rate_score" property="rateScore"/>
    <result column="term" property="term"/>
    <result column="img" property="img"/>
    <result column="discount_range" property="discountRange"/>
    <result column="discount_type" property="discountType"/>
    <result column="discount" property="discount"/>
    <result column="pres_score" property="presScore"/>
    <collection property="categoryIds" javaType="java.util.List" ofType="long">
      <result column="categoryIds"></result>
    </collection>
  </resultMap>

  <resultMap id="levelAndRightMap" type="com.yami.shop.user.comment.dto.LevelDetailDto">
    <id column="id" property="id" />
    <result column="level" property="level"/>
    <result column="level_name" property="levelName"/>
    <result column="level_type" property="levelType"/>
    <result column="need_growth" property="needGrowth"/>
    <result column="need_amount" property="needAmount"/>
    <result column="term" property="term"/>
    <result column="img" property="img"/>
    <!--<result column="discount_range" property="discountRange"/>-->
    <!--<result column="term" property="term"/>-->
    <result column="term_type" property="termType"/>
    <!--<result column="discount_type" property="discountType"/>-->
    <!--<result column="discount" property="discount"/>-->
    <!--<result column="pres_score" property="presScore"/>-->
    <collection property="userRights" javaType="list"  ofType="com.yami.shop.user.comment.dto.UserRightsDto">
      <id column="rights_id" jdbcType="BIGINT" property="rightsId"/>
      <result column="rights_name" jdbcType="VARCHAR" property="rightsName"/>
      <result column="icon" jdbcType="VARCHAR" property="icon"/>
      <result column="description" jdbcType="VARCHAR" property="description"/>
    </collection>
  </resultMap>

  <resultMap id="levelMap" type="com.yami.shop.user.comment.model.UserLevel">
    <id column="id" property="id" />
    <result column="level" property="level"/>
    <result column="level_name" property="levelName"/>
    <result column="level_type" property="levelType"/>
    <result column="need_growth" property="needGrowth"/>
    <result column="need_amount" property="needAmount"/>
    <result column="term" property="term"/>
    <result column="img" property="img"/>
    <result column="discount_range" property="discountRange"/>
    <result column="term" property="term"/>
    <result column="term_type" property="termType"/>
    <result column="discount_type" property="discountType"/>
    <result column="discount" property="discount"/>
    <result column="pres_score" property="presScore"/>
    <collection property="couponIds" javaType="java.util.List" ofType="long">
      <result column="couponIds"></result>
    </collection>
  </resultMap>

    <select id="getList" resultType="com.yami.shop.user.comment.dto.UserLevelDto">
      SELECT ul.*  FROM tz_user_level ul
      WHERE ul.level_type = #{userLevelType}
    </select>
  <select id="selectOneAndCategory" resultMap="couponOrderDtoMap">
    SELECT ul.*,ulc.category_id as categoryIds FROM `tz_user_level` ul
    LEFT JOIN `tz_user_level_category` ulc ON ul.`id` = ulc. level_id
    WHERE  ul.`level` = #{user.level} and ul.level_type =#{user.levelType}
  </select>

  <select id="selectListAndCoupons" resultMap="levelMap">
    SELECT ul.*,ulc.coupon_id as couponIds FROM `tz_user_level` ul
    LEFT JOIN `tz_user_level_coupon` ulc ON ulc.`level_id` = ul.`id`
    WHERE ul.level > #{level} AND ul.need_growth <![CDATA[ <= ]]> #{nowGrowth} AND ul.level_type = 0
  </select>

  <select id="selectLevelAndCoupons" resultMap="levelMap">
    SELECT ul.*,ulc.coupon_id as couponIds FROM `tz_user_level` ul
    LEFT JOIN `tz_user_level_coupon` ulc ON ulc.`level_id` = ul.`id`
    WHERE ul.level = #{level} AND ul.level_type = 1
  </select>

  <select id="selectLevelAndRights" resultMap="levelAndRightMap">
    SELECT ul.*,ur.rights_name,ur.icon,ur.description,ur.rights_id FROM `tz_user_level`  ul
    LEFT JOIN `tz_user_level_rights` ulr ON  ul.`id` = ulr.`level_id`
    LEFT JOIN `tz_user_rights` ur ON ulr.`rights_id` = ur.`rights_id`
    <where>
      <if test="levelType != null" >
        ul.level_type = #{levelType}
      </if>
    </where>
    ORDER BY ul.id,ur.service_type,ur.seq DESC,ur.`rights_id`
  </select>
    <update id="setStatusByLevelType">
      UPDATE
        tz_user_level
      SET
        `status` = 1
      WHERE level_type = 0
    </update>

</mapper>
