<?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.iamberry.wechat.service.mapper.UseIntegralMapper">
    <sql id="AllUseField">
        integral_id integralId,
        user_openid userOpenId,
        integral_type integralType,
        integral_number integralNumber,
        config_id configId,
        integral_remarks integralRemarks,
        integral_create_time integralCreateTime,
        integral_before_number integralBeforeNumber,
        integral_end_number integralEndNumber
    </sql>
    <!--全部积分记录-->
    <select id="listIntegralInfo" resultType="UseIntegral" parameterType="UseIntegral">
        SELECT
        i.integral_type integralType,
        i.integral_number integralNumber,
        i.integral_remarks integralRemarks,
        i.integral_create_time integralCreateTime,
        i.integral_before_number integralBeforeNumber,
        i.integral_end_number integralEndNumber,
        c.integral_config_name integralConfigName,
        s.stay_recorded_time stayRecordedTime
        FROM
        tb_iamberry_use_integral i
        LEFT JOIN tb_iamberry_integral_config c ON i.config_id = c.integral_config_id
        LEFT JOIN tb_iamberry_stay_integral s ON i.stay_id=s.stay_id
        WHERE i.user_openid=#{userOpenId}
        <if test="integralType != null and integralType != ''">
            AND i.integral_type = #{integralType}
        </if>
        ORDER BY i.integral_id DESC
        <if test="page!=null and page.recordBegin>0 and page.pageSize>0 ">
            limit ${page.recordBegin},${page.pageSize}
        </if>
        <if test="page!=null and page.recordBegin==0 and page.pageSize>0 ">
            limit ${page.pageSize}
        </if>
    </select>
    <!--获得的总积分数-->
    <select id="getGainIntegralCount" parameterType="String" resultType="Integer">
        SELECT SUM(integral_number)
        FROM tb_iamberry_use_integral
        WHERE user_openid=#{userOpenid} AND integral_type=1
    </select>
    <!--使用的总积分数-->
    <select id="getUseIntegralCount" parameterType="String" resultType="Integer">
        SELECT SUM(integral_number)
        FROM tb_iamberry_use_integral
        WHERE user_openid=#{userOpenid} AND integral_type=2
    </select>
    <!--待入账的总积分数-->
    <select id="getStayIntegralCount" parameterType="String" resultType="Integer">
        SELECT SUM(stay_number)
        FROM tb_iamberry_stay_integral
        WHERE user_openid=#{userOpenid} AND stay_status=1
    </select>
    <!--查询积分等级信息-->
    <select id="getRankRule" resultType="RankRule">
        SELECT *
        FROM tb_iamberry_rank_rule
    </select>
    <!-- 查询积分来源信息 -->
    <select id="listIntegralConfig" parameterType="IntegralConfig" resultType="IntegralConfig">
        SELECT
        *
        FROM
        tb_iamberry_integral_config
        <where>
            <if test="integralConfigId != null and integralConfigId != ''">
                integral_config_id = #{integralConfigId}
            </if>
        </where>
    </select>

    <!-- 添加积分日志 -->
    <insert id="addUseIntegral" parameterType="UseIntegral" keyProperty="integralId" useGeneratedKeys="true">
        INSERT INTO tb_iamberry_use_integral
        (
          user_openid,
          integral_type,
          stay_id,
          config_id,
          integral_number,
          integral_before_number,
          integral_end_number,
          integral_remarks,
          integral_create_time
        )
        VALUES
        (
          #{userOpenId},#{integralType},#{stayId},#{configId},#{integralNumber},
          #{integralBeforeNumber},#{integralEndNumber},#{integralRemarks},NOW()
        )
    </insert>

    <!-- 查询会员积分等级规则信息 -->
    <select id="listRankRule" parameterType="RankRule" resultType="RankRule">
      SELECT
        *
      FROM
        tb_iamberry_rank_rule
      ORDER BY rank_rule_integral DESC
    </select>

    <!-- 添加会员信息 -->
    <insert id="addMemberIntegral" parameterType="MemberIntegral"
            useGeneratedKeys="true" keyProperty="memberId">
        INSERT INTO tb_iamberry_member_integral
        (
          rank_rule_id, user_openid, member_status, member_all_integral,
          member_surplus_integral, member_used_integral, member_create_time,
          member_update_time
        )
        VALUES
        (
          #{rankRuleId},#{userOpenid},#{memberStatus},#{memberAllIntegral},
          #{memberSurplusIntegral},#{memberUsedIntegral},NOW(),NOW()
        )
    </insert>

    <!-- 查询所有待入账积分 -->
    <select id="listStayIntegral" parameterType="StayIntegral" resultType="StayIntegral">
        SELECT
        t.stay_id AS  stayId,
        t.user_openid AS  userOpenId,
        t.config_id AS  configId,
        t.sales_orderid AS  orderId,
        t.stay_number AS  stayNumber,
        t.stay_recorded_time AS stayRecordedTime,
        t.stay_status AS  stayStatus,
        t.stay_remark AS stayRemark,
        t.stay_create_time AS stayCreateTime,
        t.stay_update_time AS stayUpdateTime
        FROM
        tb_iamberry_stay_integral t
        <where>
            <if test="stayId != null">
                AND stay_id = #{stayId}
            </if>
            <if test="stayStatus != null">
                AND stay_status = #{stayStatus}
            </if>
            <if test="orderId != null and orderId != ''">
                AND sales_orderid = #{orderId}
            </if>
            <if test="userOpenId != null">
                AND user_openid = #{userOpenId}
            </if>
            <if test="configId != null">
                AND config_id = #{configId}
            </if>
            <if test="stayRecordedTime != null and stayRecordedTime != ''">
                AND stay_recorded_time &lt;= #{stayRecordedTime}
            </if>
        </where>
    </select>

    <!-- 修改待入账积分状态 -->
    <update id="updateStayIntegral" parameterType="StayIntegral">
        UPDATE tb_iamberry_stay_integral
        <set>
            <if test="configId != null">
                config_id = #{configId},
            </if>
            <if test="orderId != null">
                sales_orderid = #{orderId},
            </if>
            <if test="stayNumber != null">
                stay_number = #{stayNumber},
            </if>
            <if test="stayRecordedTime != null">
                stay_recorded_time = #{stayRecordedTime},
            </if>
            <if test="stayStatus != null">
                stay_status = #{stayStatus},
            </if>
            <if test="stayRemark != null">
                stay_remark = #{stayRemark}
            </if>
        </set>
        <where>
            stay_id = #{stayId}
        </where>
    </update>

    <!-- 根据banner图片ID，删除图片 -->
    <delete id="delUseIntegral" parameterType="UseIntegral">
        DELETE FROM
        tb_iamberry_use_integral
        <where>
            <if test="integralType != null">
                AND integral_type = #{integralType}
            </if>
            <if test="stayId != null">
                AND stay_id = #{stayId}
            </if>
            <if test="userOpenId != null">
                AND user_openid = #{userOpenId}
            </if>
        </where>
    </delete>

    <!-- 积分日志记录列表查询 -->
    <select id="listUseIntegral" parameterType="UseIntegral" resultType="UseIntegral">
        SELECT
        i.*,
        u.user_nickname,
        u.user_name
        FROM
        tb_iamberry_use_integral i
        LEFT JOIN tb_iamberry_user_userinfo u ON i.user_openid = u.user_openid
        WHERE
        i.integral_type IN (1, 2)
        <if test="integralNumberMax!=null and integralNumberMax>0">
            AND  integral_number &lt;= #{integralNumberMax}
        </if>
        <if test="integralNumberMin!=null and integralNumberMin>0">
            AND  integral_number &gt;= #{integralNumberMin}
        </if>
        <if test="userName!=null and userName != ''">
            AND  u.user_name like CONCAT('%',#{userName},'%')
        </if>
        <if test="userNickName!=null and userNickName != ''">
            AND  u.user_nickname like CONCAT('%',#{userNickName},'%')
        </if>
    </select>
    <!--待入账积分列表查询-->
    <select id="listAdminStayIntegral" parameterType="StayIntegral" resultType="StayIntegral">
        SELECT
        i.*, u.user_nickname,
        u.user_name,
        u.user_integral,
        u.user_surplus_integral,
        u.user_use_integral,
        u.user_stay_integral,
        ui.integral_remarks
        FROM
        tb_iamberry_stay_integral i
        LEFT JOIN tb_iamberry_user_userinfo u ON i.user_openid = u.user_openid
        LEFT JOIN tb_iamberry_use_integral ui on i.stay_id = ui.stay_id
        <where>
            <if test="integralRemarks != null and integralRemarks != ''">
                ui.integral_remarks = #{integralRemarks}
            </if>
            <if test="userName!=null and userName != ''">
                AND  u.user_name like CONCAT('%',#{userName},'%')
            </if>
            <if test="userNickName!=null and userNickName != ''">
                AND  u.user_nickname like CONCAT('%',#{userNickName},'%')
            </if>
            <if test="stayRecordedTime != null and stayRecordedTime != ''">
                <![CDATA[AND DATE_FORMAT(stay_recorded_time, '%Y-%m-%d') = DATE_FORMAT(#{stayRecordedTime},'%Y-%m-%d')]]>
            </if>
            <if test="changeIntegralMax!=null and changeIntegralMax>0">
                AND  i.stay_number &lt;= #{changeIntegralMax}
            </if>
            <if test="changeIntegralMin!=null and changeIntegralMin>0">
                AND  i.stay_number &gt;= #{changeIntegralMin}
            </if>
        </where>
    </select>

    <!--修改待入账积分状态-->
    <update id="updatestayIntegralStatus" parameterType="StayIntegral" >
        UPDATE tb_iamberry_stay_integral SET stay_status = #{stayStatus} WHERE stay_id = #{stayId}
    </update>
</mapper>