<?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.dao.UserMapper">
    <resultMap id="BaseResultMap" type="com.yami.shop.bean.model.User">
        <!--
          WARNING - @mbg.generated
        -->
        <id column="user_id" jdbcType="VARCHAR" property="userId"/>
        <result column="nick_name" jdbcType="VARCHAR" property="nickName"/>
        <result column="real_name" jdbcType="VARCHAR" property="realName"/>
        <result column="user_mail" jdbcType="VARCHAR" property="userMail"/>
        <result column="login_password" jdbcType="VARCHAR" property="loginPassword"/>
        <result column="pay_password" jdbcType="VARCHAR" property="payPassword"/>
        <result column="user_mobile" jdbcType="VARCHAR" property="userMobile"/>
        <result column="modify_time" jdbcType="TIMESTAMP" property="modifyTime"/>
        <result column="user_regtime" jdbcType="TIMESTAMP" property="userRegtime"/>
        <result column="user_regip" jdbcType="VARCHAR" property="userRegip"/>
        <result column="user_memo" jdbcType="VARCHAR" property="userMemo"/>
        <result column="sex" jdbcType="CHAR" property="sex"/>
        <result column="birth_date" jdbcType="CHAR" property="birthDate"/>
        <result column="growth" jdbcType="BIGINT" property="growth"/>
        <result column="score" jdbcType="BIGINT" property="score"/>
        <result column="level" jdbcType="INTEGER" property="level"/>
        <result column="level_type" jdbcType="INTEGER" property="levelType"/>
        <result column="pic" jdbcType="VARCHAR" property="pic"/>
        <result column="status" jdbcType="INTEGER" property="status"/>
    </resultMap>

    <select id="getUserByBizUserId" resultMap="BaseResultMap">
        select *
        from tz_user u
        where u.user_id =
              (select user_id from tz_app_connect ac where ac.app_id = #{appId} and ac.biz_user_id = #{bizUserId})
    </select>
    <update id="setUserLevelBylevelId">
        UPDATE
            tz_user
        SET `level` = #{level} - 1
        WHERE `level` = #{level}
    </update>
    <update id="setMaxUserLevelBylevelId">
        UPDATE
        tz_user
        SET
        `level` = #{level}
        WHERE user_id IN
        (SELECT user_id FROM tz_user_extension WHERE growth &gt; #{minNeedGrowth}
        <if test="maxNeedGrowth != null">
            AND growth &lt; #{maxNeedGrowth}
        </if>)
    </update>

    <select id="selectMemberByEndTime" resultMap="BaseResultMap">
        select *
        from tz_user u
        where vip_end_time &lt; #{endOfDay}
          and level_type = 1
    </select>

    <update id="updateBatchById">
        <foreach collection="userList" item="user" separator=";">
            update tz_user set level =#{user.level},level_type=0 where user_id = #{user.userId}
        </foreach>
    </update>
    <select id="getUserPage" resultType="com.yami.shop.bean.model.User">
        SELECT u.nick_name,u.status,u.pic,u.level,u.level_type,ul.level_name,
        u.user_id,u.real_name,u.user_mobile,ue.score
        FROM tz_user u
        left JOIN tz_user_level ul ON ul.level = u.level AND ul.level_type = u.level_type
        JOIN tz_user_extension ue ON ue.`user_id` = u.user_id
        <where>
            <if test="user.nickName != null and user.nickName != ''">
                AND u.nick_name LIKE CONCAT("%",#{user.nickName},"%")
            </if>
            <if test="user.status != null">
                AND u.status = #{user.status}
            </if>
            <if test="user.levelType != null and user.levelType != ''">
                AND u.level_Type = #{user.levelType}
            </if>
            <if test="user.realName != null and user.realName != ''">
                AND u.real_name LIKE CONCAT("%",#{user.realName},"%")
            </if>
            <if test="user.userMobile != null and user.userMobile != ''">
                AND u.user_mobile LIKE CONCAT("%",#{user.userMobile},"%")
            </if>
        </where>
    </select>
    <select id="statisticsList" resultType="com.yami.shop.bean.vo.UserPointsVO">

        SELECT u.user_id,u.real_name,u.user_mobile,
        c.channel_name,
        sum(IF(pr.points_type = 1, pr.points, 0)) AS successStatus,
        sum(IF(pr.expiry_date IS NOT NULL AND pr.expiry_date &lt;= NOW() and pr.points_type = 1, pr.points -
        pr.variable_points, 0, 0)) AS availablePoints,
        sum(IF(pr.points_type = 1, pr.points, 0)) AS successStatus

        FROM tz_user u
        left JOIN tz_points_record pr ON pr.user_id = u.user_id
        left join tz_channel c ON c.id = pr.channel_id
        <where>

        </where>
    </select>


    <select id="enterpriseUserList" resultType="com.yami.shop.bean.vo.EnterpriseUserVo">
        SELECT a.user_id,a.`status`,a.real_name,a.user_mobile,a.channel_id,a.user_attr_type,
        (SELECT channel_name FROM tz_channel WHERE id= a.channel_id) channelName,
        IFNULL((SELECT SUM(points) FROM tz_points_recharge WHERE user_id=a.user_id),0) total, -- 总积分

        IFNULL(( select (
        SUM(CASE WHEN points_type = 1 OR points_type = 3 THEN points ELSE 0 END) -- 充值和退款的总积分
        -
        GREATEST(
        SUM( CASE WHEN expiry_date &lt;= NOW() THEN
        CASE WHEN points_type = 1 OR points_type = 3 THEN points
        WHEN points_type = 2 THEN -variable_points
        ELSE 0 END
        ELSE 0 END ), 0) -- 所以情况的过期积分
        -
        SUM(CASE WHEN points_type = 2 THEN variable_points ELSE 0 END) -- 下单的所有积分  未使用 -过期的 = 可用的
        )  from tz_points_record where user_id =  a.user_id  and channel_id = a.channel_id ),0) available, -- 当前可用积分

        IFNULL(( select  GREATEST(
        SUM(
        CASE WHEN expiry_date &lt;= NOW() THEN
        CASE  WHEN points_type = 1 OR points_type = 3 THEN points
        WHEN points_type = 2 THEN -variable_points
        ELSE 0 END
        ELSE 0 END ), 0 ) from tz_points_record where user_id =  a.user_id  and channel_id = a.channel_id),0) expired, -- 已过期积分

        IFNULL((select  (
        SUM(CASE WHEN points_type = 2 THEN variable_points ELSE 0 END)
        -
        SUM(CASE WHEN points_type = 3 THEN points ELSE 0 END))  from tz_points_record 	where user_id =  a.user_id  and channel_id = a.channel_id),0) used -- 已消耗积分
        FROM tz_user a
        <where>
            a.`status` = 1 and
            a.channel_id in (SELECT id FROM tz_channel)
            <if test="po.channelIdList != null and !po.channelIdList.isEmpty()">
                and a.channel_id  in
                <foreach collection="po.channelIdList" item="channelId" open="(" close=")" separator=",">
                    #{channelId}
                </foreach>
            </if>
            <if test="po.userAttrType != null and po.userAttrType != '' and po.userAttrType != 0">
                AND a.user_attr_type = #{po.userAttrType}
            </if>
            <if test="po.realName != null and po.realName != ''">
                AND a.real_name LIKE CONCAT("%",#{po.realName},"%")
            </if>
            <if test="po.userMobile != null and po.userMobile != ''">
                AND a.user_mobile LIKE CONCAT("%",#{po.userMobile},"%")
            </if>
        </where>
    </select>

    <insert id="addUserLog" parameterType="string">
        insert into tz_user_add_log(operator, batch_no, task_name, create_time, complete_time, `status`, channel,real_name, phone, user_attr_type)
        values (#{operator}, #{batchNo}, "员工导入", NOW(), NOW(), #{status}, #{channel}, #{realName}, #{phone},#{userAttrType})
    </insert>

    <select id="enterpriseUserLogList" resultType="com.yami.shop.bean.po.EnterpriseUserLogPo">
        SELECT b.batch_no,
               b.operator,
               b.task_name,
               b.create_time,
               b.complete_time,
               b.total,
               b.success,
               b.error
        FROM (SELECT batch_no,
                     ANY_VALUE(operator)  AS operator,
                     ANY_VALUE(task_name) AS task_name,
                     MIN(create_time)     AS create_time,
                     MAX(complete_time)   AS complete_time,
                     COUNT(1)             AS total,
                     SUM(`status` = 1)    AS success,
                     SUM(`status` = 0)    AS error
              FROM tz_user_add_log
              GROUP BY batch_no) b
        ORDER BY b.complete_time DESC
    </select>


    <select id="userList" resultType="com.yami.shop.bean.model.User">
        select * from tz_user
        <where>
            <if test="user.nickName != null and user.nickName != ''">
                AND nick_name LIKE CONCAT("%",#{user.nickName},"%")
            </if>
        </where>
    </select>

    <select id="enterpriseUserLogErrList" resultType="com.yami.shop.bean.po.EnterpriseUserLogPo">
        SELECT *
        FROM tz_user_add_log
        WHERE batch_no = #{batchNo}
          and `status` = 0
        ORDER BY create_time
    </select>
    <select id="exportList" resultType="com.yami.shop.bean.vo.EnterpriseUserVo">
        SELECT a.user_id,a.`status`,a.real_name,a.user_mobile,a.channel_id,
               (SELECT channel_name FROM tz_channel WHERE id= a.channel_id) channelName,
               IFNULL((SELECT SUM(points) FROM tz_points_recharge WHERE user_id=a.user_id),0) total, -- 总积分

               IFNULL(( select (
                                       SUM(CASE WHEN points_type = 1 OR points_type = 3 THEN points ELSE 0 END) -- 充值和退款的总积分
                                       -
                                       GREATEST(
                                               SUM( CASE WHEN expiry_date &lt;= NOW() THEN
                                                             CASE WHEN points_type = 1 OR points_type = 3 THEN points
                                                                  WHEN points_type = 2 THEN -variable_points
                                                                  ELSE 0 END
                                                         ELSE 0 END ), 0) -- 所以情况的过期积分
                                       -
                                       SUM(CASE WHEN points_type = 2 THEN variable_points ELSE 0 END) -- 下单的所有积分  未使用 -过期的 = 可用的
                                   )  from tz_points_record where user_id =  a.user_id  and channel_id = a.channel_id ),0) available, -- 当前可用积分

               IFNULL(( select  GREATEST(
                                        SUM(
                                                CASE WHEN expiry_date &lt;= NOW() THEN
                                                         CASE  WHEN points_type = 1 OR points_type = 3 THEN points
                                                               WHEN points_type = 2 THEN -variable_points
                                                               ELSE 0 END
                                                     ELSE 0 END ), 0 ) from tz_points_record where user_id =  a.user_id  and channel_id = a.channel_id),0) expired, -- 已过期积分

               IFNULL((select  (
                                       SUM(CASE WHEN points_type = 2 THEN variable_points ELSE 0 END)
                                       -
                                       SUM(CASE WHEN points_type = 3 THEN points ELSE 0 END))  from tz_points_record 	where user_id =  a.user_id  and channel_id = a.channel_id),0) used -- 已消耗积分
        FROM tz_user a
        <where>
            a.`status` = 1 and
            a.channel_id in (SELECT id FROM tz_channel)
            <if test="po.channelIdList != null and !po.channelIdList.isEmpty()">
                and a.channel_id  in
                <foreach collection="po.channelIdList" item="channelId" open="(" close=")" separator=",">
                    #{channelId}
                </foreach>
            </if>
            <if test="po.userAttrType != null and po.userAttrType != '' and po.userAttrType != 0">
                AND a.user_attr_type = #{po.userAttrType}
            </if>
            <if test="po.realName != null and po.realName != ''">
                AND a.real_name LIKE CONCAT("%",#{po.realName},"%")
            </if>
            <if test="po.userMobile != null and po.userMobile != ''">
                AND a.user_mobile LIKE CONCAT("%",#{po.userMobile},"%")
            </if>
        </where>
    </select>

    <select id="test" resultType="double">
        SELECT (SELECT SUM(IFNULL(offset_points,0)) FROM tz_order
        WHERE is_payed=1 and user_id=#{userId}
        ) -
        (
        SELECT  IFNULL(SUM(IFNULL(total_offset_points,0)),0) FROM (SELECT
        CASE
        WHEN refund_type=1 THEN total_offset_points
        ELSE total_offset_points
        END AS total_offset_points
        FROM tz_order_refund a
        LEFT JOIN (SELECT SUM(offset_points) total_offset_points ,order_number
        FROM tz_order_refund
        WHERE  return_money_sts=70 and user_id=#{userId}
        GROUP BY order_number) b on a.order_number=b.order_number
        WHERE return_money_sts=70 and user_id=#{userId}
        GROUP BY a.order_number) a
        )
    </select>

</mapper>
