<?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.distribution.comment.dao.DistributionLevelAuditingMapper">
    <resultMap id=" DistributionLevelAuditingMap" type="com.yami.shop.distribution.comment.model.DistributionLevelAuditing">
        <id property="id" column="id"/>
        <result property="distributionUserId" column="distribution_user_id"/>
        <result property="level" column="level"/>
        <result property="state" column="state"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="modifier" column="modifier"/>
        <result column="remarks" jdbcType="VARCHAR" property="remarks"/>
        <result column="reason" jdbcType="TINYINT" property="reason"/>
        <result column="level_name" jdbcType="VARCHAR" property="levelName"/>
    </resultMap>


    <resultMap id="DistributionLevelAuditing_DistributionUser_SysUser_Level"
               type="com.yami.shop.distribution.comment.model.DistributionLevelAuditing">
        <id property="id" column="id"/>
        <result property="distributionUserId" column="distribution_user_id"/>
        <result property="level" column="level"/>
        <result property="state" column="state"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="modifier" column="modifier"/>
        <result column="remarks" jdbcType="VARCHAR" property="remarks"/>
        <result column="reason" jdbcType="TINYINT" property="reason"/>
        <result column="level_name" jdbcType="VARCHAR" property="levelName"/>
        <association property="distributionUser" javaType="com.yami.shop.distribution.comment.vo.DistributionUserVO">
            <id column="distribution_user_id" jdbcType="BIGINT" property="distributionUserId"/>
            <result column="nick_name" jdbcType="VARCHAR" property="nickName"/>
            <result column="user_mobile" jdbcType="VARCHAR" property="userMobile"/>
            <association property="distributionLevel" javaType="com.yami.shop.distribution.comment.vo.DistributionLevelVO">
                <id column="now_level_id" jdbcType="BIGINT" property="levelId"/>
                <id column="now_name" jdbcType="VARCHAR" property="name"/>
            </association>
        </association>
        <association property="sysUser" javaType="com.yami.shop.bean.vo.SysUserVO">
            <id column="sys_user_id" jdbcType="BIGINT" property="userId"/>
            <result column="username" jdbcType="VARCHAR" property="username"/>
        </association>
    </resultMap>


    <!--<select id="distributionLevelAuditingPage" resultMap="DistributionLevelAuditing_DistributionUser_SysUser_Level">-->
    <!--select-->
    <!--dla.*,-->
    <!--&#45;&#45; info-->
    <!--du.user_mobile,-->
    <!--du.nick_name,-->
    <!--&#45;&#45; sys-->
    <!--sys.user_id as sys_user_id,-->
    <!--sys.username,-->
    <!--&#45;&#45; 申请的等级-->
    <!--dl.`name`,-->
    <!--&#45;&#45; 当前等级-->
    <!--now_dl.level as now_level,-->
    <!--now_dl.`name` as now_name-->
    <!--from-->
    <!--tz_distribution_level_auditing dla-->
    <!--&#45;&#45; 申请人信息-->
    <!--left join tz_distribution_user du on du.distribution_user_id = dla.distribution_user_id-->
    <!--&#45;&#45; 操作人信息-->
    <!--left join tz_sys_user sys on sys.user_id=dla.modifier-->
    <!--&#45;&#45; 申请的等级信息-->
    <!--left join tz_distribution_level dl on dl.level_id=dla.level_id-->
    <!--&#45;&#45; 当前等级信息-->
    <!--left join tz_distribution_level now_dl on now_dl.level_id = du.level_id-->
    <!--<where>-->
    <!--<if test="shopId != null">-->
    <!--and du.shop_id =#{shopId}-->
    <!--</if>-->
    <!--<if test="distributionLevelAuditing.state != null">-->
    <!--and dla.state=#{distributionLevelAuditing.state}-->
    <!--</if>-->
    <!--<if test="userMobile != null and userMobile!= ''">-->
    <!--and du.user_mobile like concat('%',#{userMobile},'%')-->
    <!--</if>-->
    <!--</where>-->
    <!--</select>-->

    <select id="distributionLevelAuditingPage" resultMap="DistributionLevelAuditing_DistributionUser_SysUser_Level">
        SELECT
        -- 申请信息
        dla.*,
        -- info
        du.user_mobile,
        du.nick_name,
        -- sys
        sys.user_id AS sys_user_id,
        sys.username,
        -- 当前等级
        now_dl.level AS now_level,
        now_dl.`name` AS now_name
        FROM
        tz_distribution_level_auditing dla
        -- 申请人信息
        LEFT JOIN tz_distribution_user du ON du.distribution_user_id = dla.distribution_user_id
        -- 操作人信息
        LEFT JOIN tz_sys_user sys ON sys.user_id=dla.modifier
        -- 当前等级信息
        LEFT JOIN tz_distribution_level now_dl ON now_dl.level = du.level AND du.shop_id = now_dl.shop_id
        <where>
            <if test="shopId != null">
                du.shop_id =#{shopId}
            </if>
            <if test="distributionLevelAuditing.state != null">
                AND dla.state=#{distributionLevelAuditing.state}
            </if>
            <if test="userMobile != null and userMobile!= ''">
                AND du.user_mobile like concat('%',#{userMobile},'%')
            </if>
        </where>
    </select>

    <update id="updateStateByShopId">
        update tz_distribution_level_auditing
        set state=2
        where shop_id = #{shopId} and state = 0
    </update>
</mapper>
