<?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.tmerclub.cloud.user.mapper.UserLevelLogMapper">
  <resultMap id="userLevelLogMap" type="com.tmerclub.cloud.user.model.UserLevelLog">
    <id column="level_log_id" property="levelLogId" />
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="user_id" property="userId"/>
    <result column="level_io_type" property="levelIoType"/>
    <result column="level_change_reason" property="levelChangeReason"/>
    <result column="before_level" property="beforeLevel"/>
    <result column="before_level_type" property="beforeLevelType"/>
    <result column="after_level" property="afterLevel"/>
    <result column="after_level_type" property="afterLevelType"/>
    <result column="term_type" property="termType"/>
    <result column="pay_id" property="payId"/>
    <result column="is_payed" property="isPayed"/>
    <result column="pay_amount" property="payAmount"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `term_type`,`level_log_id`,`create_time`,`update_time`,`user_id`,`level_io_type`,`level_change_reason`,`before_level`,`before_level_type`,`after_level`,`after_level_type`,`pay_id`,`is_payed`,`pay_amount`
  </sql>

  <select id="getByLevelLogId" resultMap="userLevelLogMap">
    select <include refid="Vo_Column_List"/> from user_level_log where level_log_id = #{levelLogId}
  </select>
  <select id="listUserIdByEarliestRechargeTime" resultType="java.lang.Long">
    SELECT
    user_level_log.user_id
    FROM
    (SELECT
    user_id,
    MIN(create_time) AS earliest
    FROM
    user_level_log
    WHERE is_payed = #{isPayed} AND after_level_type = 1 AND before_level_type = 0
    AND create_time BETWEEN #{startDate}
    AND #{endDate}
    GROUP BY user_id) temp
    LEFT OUTER JOIN user_level_log
    ON temp.user_id = user_level_log.user_id
    AND temp.earliest = user_level_log.create_time
  </select>
  <insert id="save" useGeneratedKeys="true" keyProperty="levelLogId">
    insert into user_level_log (`user_id`,`level_io_type`,`level_change_reason`,`term_type`,`before_level`,`before_level_type`,`after_level`,`after_level_type`,`pay_id`,`is_payed`,`pay_amount`, level_name, `pay_sys_type`)
    values (#{userLevelLog.userId},#{userLevelLog.levelIoType},#{userLevelLog.levelChangeReason},#{userLevelLog.termType},#{userLevelLog.beforeLevel},#{userLevelLog.beforeLevelType},#{userLevelLog.afterLevel},#{userLevelLog.afterLevelType},#{userLevelLog.payId},#{userLevelLog.isPayed},#{userLevelLog.payAmount}, #{userLevelLog.levelName}, #{userLevelLog.paySysType});
  </insert>
    <insert id="batchSaveUserLevelLogs" parameterType="java.util.List">
      insert into user_level_log (`user_id`,`level_io_type`,`level_change_reason`,`term_type`,`before_level`,`before_level_type`,`after_level`,`after_level_type`,`pay_id`,`is_payed`,`pay_amount`)
      values
      <foreach collection="userLevelLogs" item="userLevelLog" separator=",">
        (#{userLevelLog.userId},#{userLevelLog.levelIoType},#{userLevelLog.levelChangeReason},#{userLevelLog.termType},#{userLevelLog.beforeLevel},#{userLevelLog.beforeLevelType},#{userLevelLog.afterLevel},#{userLevelLog.afterLevelType},#{userLevelLog.payId},#{userLevelLog.isPayed},#{userLevelLog.payAmount})
      </foreach>
    </insert>
    <update id="update">
    update user_level_log
    <set>
      <if test="userLevelLog.userId != null">
        `user_id` = #{userLevelLog.userId},
      </if>
      <if test="userLevelLog.levelIoType != null">
        `level_io_type` = #{userLevelLog.levelIoType},
      </if>
      <if test="userLevelLog.levelChangeReason != null">
        `level_change_reason` = #{userLevelLog.levelChangeReason},
      </if>
      <if test="userLevelLog.beforeLevel != null">
        `before_level` = #{userLevelLog.beforeLevel},
      </if>
      <if test="userLevelLog.beforeLevelType != null">
        `before_level_type` = #{userLevelLog.beforeLevelType},
      </if>
      <if test="userLevelLog.afterLevel != null">
        `after_level` = #{userLevelLog.afterLevel},
      </if>
      <if test="userLevelLog.afterLevelType != null">
        `after_level_type` = #{userLevelLog.afterLevelType},
      </if>
      <if test="userLevelLog.payId != null">
        `pay_id` = #{userLevelLog.payId},
      </if>
      <if test="userLevelLog.isPayed != null">
        `is_payed` = #{userLevelLog.isPayed},
      </if>
      <if test="userLevelLog.payAmount != null">
        `pay_amount` = #{userLevelLog.payAmount},
      </if>
    </set>
    where level_log_id = #{userLevelLog.levelLogId}
  </update>
  <delete id="deleteById">
    delete from user_level_log where level_log_id = #{levelLogId}
  </delete>

  <update id="updateToSuccess">
    update user_level_log set pay_id= #{payId} ,is_payed = 1, pay_type = #{payType}
    where level_log_id = #{levelLogId} and is_payed = 0
  </update>

  <select id="getMaxLevelByUserId" resultType="java.lang.Integer">
    SELECT MAX(`after_level`)
    FROM `user_level_log`
    WHERE user_id = #{userId}
      and after_level_type = 0
  </select>

  <delete id="deleteByUserId">
    delete from `user_level_log` WHERE user_id = #{userId}
    </delete>

  <select id="getUserTypeAtTime" resultType="com.tmerclub.cloud.api.user.vo.UserApiVO">
    SELECT ul.after_level_type AS level_type, u.nick_name FROM `user` u
    LEFT JOIN user_level_log ul ON ul.user_id = u.user_id AND ul.create_time &lt; #{createTime}
    WHERE u.user_id = #{userId}
    ORDER BY ul.create_time DESC
    LIMIT 1
  </select>

  <select id="listFinanceDetail" resultType="com.tmerclub.cloud.api.user.vo.FinanceDetailVO">
    SELECT tull.pay_type, tull.create_time AS trans_date, tull.create_time AS place_time,tull.level_log_id AS order_id,
    ul.level_name AS prod_name,tull.pay_amount AS trans_amount, 0 AS shop_id, tull.pay_id as pay_ids, 4 AS biz_type, tull.user_id, '官方店' as shop_name
    FROM `user_level_log` tull
    left join user_level ul on tull.after_level = ul.level and level_type = 1
    WHERE tull.is_payed = 1 and tull.level_change_reason in (3,4)
    <if test="financeDetailsParam.startTime != null and financeDetailsParam.endTime != null">
      AND tull.create_time >= #{financeDetailsParam.startTime} AND tull.create_time &lt;= #{financeDetailsParam.endTime}
    </if>
    <if test="financeDetailsParam.payType >= 0 and financeDetailsParam.payType != null">
      AND tull.pay_type = #{financeDetailsParam.payType}
    </if>
    <if test="financeDetailsParam.orderId !=null and financeDetailsParam.orderId !=''">
      AND trim(replace(tull.level_log_id,' ','')) like trim(replace(concat('%',#{financeDetailsParam.orderId},'%'),' ',''))
    </if>
    ORDER BY trans_date DESC
  </select>

  <select id="getPlatformAccountDetail" resultType="com.tmerclub.cloud.api.payment.vo.ShopAccountDetailVO">
    SELECT SUM(CASE WHEN pay_type IN (1,3,4,5,8) THEN pay_amount ELSE 0 END) AS wechat_amount,
           SUM(CASE WHEN pay_type IN (2,6,7) THEN pay_amount ELSE 0 END) AS alipay_amount,
           SUM(CASE WHEN pay_type IN (9) THEN pay_amount ELSE 0 END) AS balance_amount
    FROM user_level_log
    <where>
      level_change_reason in (3,4) AND is_payed = 1 and pay_sys_type = #{paySysType}
      <if test="startTime != null">
        and create_time <![CDATA[ >= ]]> #{startTime}
      </if>
      <if test="endTime != null">
        and create_time <![CDATA[ <= ]]> #{endTime}
      </if>
    </where>

  </select>
  <select id="listPayInfo" resultType="com.tmerclub.cloud.api.user.vo.UserPayInfoVO">
    SELECT level_log_id as orderIds, pay_id, 2 as pay_entry, pay_type,pay_amount,
    IF(pay_type IN (1,3,4,5,8), pay_amount, 0) AS wechat_amount,
    IF(pay_type IN (2,6,7), pay_amount, 0) AS alipay_amount,
    IF(pay_type IN (9), pay_amount, 0) AS balance_amount,0 AS score_count,create_time as payTime,0 as shopId, '官方店' as shopName
    FROM `user_level_log`
    WHERE level_change_reason in (3,4) and is_payed = 1 and pay_sys_type = #{paySysType}
    AND create_time &gt;= #{startTime} AND create_time &lt;= #{endTime}
    ORDER BY create_time DESC
  </select>
  <select id="pageBuyLevelLog" resultType="com.tmerclub.cloud.user.vo.UserLevelLogVO">
    SELECT ull.level_log_id, ull.create_time, ull.user_id, ull.level_io_type, ull.level_change_reason, ull.term_type, ull.before_level, ull.before_level_type, ull.after_level, ull.after_level_type, ull.pay_id, ull.pay_type, ull.is_payed,  ull.pay_amount,  if(isnull(ull.level_name), ul.level_name, ull.level_name) as level_name, if(isnull(u.user_id),  "用户已注销", u.nick_name) as nick_name, u.pic
    FROM user_level_log ull
    left JOIN user u ON ull.user_id = u.user_id
    left JOIN user_level ul on ul.level_type = ull.after_level_type and ul.level = ull.after_level
    <where>
      ull.is_payed = 1 and ull.pay_type is not null and ull.after_level_type = 1 and ull.level_change_reason in (3, 4)
      <if test="userLevelLogDTO.payType != null ">
        AND ull.pay_type = #{userLevelLogDTO.payType}
      </if>
    <if test="userLevelLogDTO.nickName != null and userLevelLogDTO.nickName != ''">
      AND u.nick_name LIKE CONCAT('%', #{userLevelLogDTO.nickName}, '%')
    </if>
    <if test="userLevelLogDTO.createStartTime != null">
      AND ull.create_time <![CDATA[ >= ]]> #{userLevelLogDTO.createStartTime}
    </if>
    <if test="userLevelLogDTO.createEndTime != null">
      AND ull.create_time <![CDATA[ <= ]]> #{userLevelLogDTO.createEndTime}
    </if>
    </where>
    ORDER BY ull.level_log_id DESC

  </select>
</mapper>
