<?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.younike.system.mapper.ServerUserMapper">

    <resultMap type="com.younike.system.domain.ServerUser" id="ServerUserResult">
        <result property="serverId"    column="server_id"    />
        <result property="merchantId"    column="merchant_id"    />
        <result property="serverAge"    column="server_age"    />
        <result property="orderCount"    column="order_count"    />
        <result property="score"    column="score"    />
        <result property="rate"    column="rate"    />
        <result property="serverLevel"    column="server_level"    />
        <result property="phone"    column="phone"    />
        <result property="openId"    column="open_id"    />
        <result property="appleId"    column="apple_id"    />
        <result property="nikeName"    column="nike_name"    />
        <result property="realName"    column="real_name"    />
        <result property="desc"    column="desc"    />
        <result property="age"    column="age"    />
        <result property="sex"    column="sex"    />
        <result property="followAll"    column="follow_all"    />
        <result property="avatar"    column="avatar"    />
        <result property="closeTime"    column="close_time"    />
        <result property="closeReason"    column="close_reason"    />
        <result property="password"    column="password"    />
        <result property="status"    column="status"    />
        <result property="loginIp"    column="login_ip"    />
        <result property="loginDate"    column="login_date"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="column1"    column="column1"    />
        <result property="column2"    column="column2"    />
        <result property="column3"    column="column3"    />

    </resultMap>

    <resultMap type="com.younike.system.domain.vo.ServerUserListVo" id="ServerUserListResult">
        <result property="serverId"    column="server_id"    />
        <result property="merchantId"    column="merchant_id"    />
        <result property="serverAge"    column="server_age"    />
        <result property="orderCount"    column="order_count"    />
        <result property="score"    column="score"    />
        <result property="rate"    column="rate"    />
        <result property="serverLevel"    column="server_level"    />
        <result property="phone"    column="phone"    />
        <result property="openId"    column="open_id"    />
        <result property="appleId"    column="apple_id"    />
        <result property="nikeName"    column="nike_name"    />
        <result property="realName"    column="real_name"    />
        <result property="age"    column="age"    />
        <result property="sex"    column="sex"    />
        <result property="followAll"    column="follow_all"    />
        <result property="avatar"    column="avatar"    />
        <result property="closeTime"    column="close_time"    />
        <result property="closeReason"    column="close_reason"    />
        <result property="password"    column="password"    />
        <result property="status"    column="status"    />
        <result property="loginIp"    column="login_ip"    />
        <result property="loginDate"    column="login_date"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="column1"    column="column1"    />
        <result property="column2"    column="column2"    />
        <result property="column3"    column="column3"    />
        <result property="goodsCount" column="goodsCount"/>
        <result property="merchantName" column="merchant_name"/>
        <result property="phonenumber" column="phonenumber"/>
        <result property="serverLevelName" column="serverLevelName"/>
        <result property="StopOrderFor"    column="stop_order"/>
        <result property="desc"    column="desc"    />
        <result property="merchantType"    column="merchant_type"    />
        <result property="merchantIndex"    column="merchant_index"    />

    </resultMap>

    <sql id="selectServerUserVo">
        select server_id, merchant_id,`desc`,server_age, order_count,score,rate, server_level,follow_all, phone, open_id, apple_id, nike_name, real_name, age, sex, avatar, close_time, close_reason, password, status, login_ip, login_date, del_flag, create_by, create_time, update_by, update_time, column1, column2, column3 from server_user

    </sql>

    <select id="adminMerchantServerUserList" parameterType="com.younike.system.param.ServerUserQueryParam" resultMap="ServerUserListResult">
        select s.*,m.merchant_name, m.merchant_index, count(case when g.del_flag = 0 then 1 else null end) as goodsCount, u.phonenumber, c.decode_value as serverLevelName,st.stop_order
        from server_user s
        left join goods_server g on s.server_id = g.server_id and g.del_flag = 0
        left join merchant m on s.merchant_id = m.merchant_id and m.del_flag = 0
        left join sys_user u on m.admin_id = u.user_id and u.del_flag = 0
        left join sys_code c on s.server_level = c.decode_id and c.code_id = '007'
        left join server_time st on st.server_id=s.server_id
        where s.del_flag = 0
        and m.merchant_type = '0'
        <if test="merchantId != null  and merchantId != ''"> and s.merchant_id = #{merchantId}</if>
        <if test="merchantIndex != null and merchantIndex != ''"> and m.merchant_index = #{merchantIndex}</if>
        <if test="merchantName != null  and merchantName != ''"> and m.merchant_name like concat('%', #{merchantName}, '%')</if>
        <if test="phone != null  and phone != ''"> and s.phone = #{phone}</if>
        <if test="realName != null  and realName != ''"> and s.real_name like concat('%', #{realName}, '%')</if>
        <if test="serverLevel != null  and serverLevel != ''"> and s.server_level = #{serverLevel}</if>
        <if test="status != null  and status != ''"> and s.status = #{status}</if>
        <if test="serverAgeStart != null  and serverAgeStart != ''"> and s.server_age &gt;= #{serverAgeStart}</if>
        <if test="serverAgeEnd != null  and serverAgeEnd != ''"> and server_age &lt;= #{serverAgeEnd}</if>
        <if test="phonenumber != null  and phonenumber != ''"> and u.phonenumber = #{phonenumber}</if>
        <if test="StopOrderFor != null  and StopOrderFor != ''"> and st.stop_order = #{StopOrderFor}</if>
        group by s.server_id
        ORDER BY s.create_time DESC
    </select>

    <select id="adminUserList" parameterType="com.younike.system.param.ServerUserQueryParam" resultMap="ServerUserListResult">
        select s.*,m.merchant_name, m.merchant_index, count(case when g.del_flag = 0 then 1 else null end) as goodsCount, u.phonenumber, c.decode_value as serverLevelName,st.stop_order
        from server_user s
        left join goods_server g on s.server_id = g.server_id and g.del_flag = 0
        left join merchant m on s.merchant_id = m.merchant_id and m.del_flag = 0
        left join sys_user u on m.admin_id = u.user_id and u.del_flag = 0
        left join sys_code c on s.server_level = c.decode_id and c.code_id = '007'
        left join server_time st on st.server_id=s.server_id
        where s.del_flag = 0
        and m.merchant_type = '1'
        <if test="merchantId != null  and merchantId != ''"> and s.merchant_id = #{merchantId}</if>
        <if test="merchantIndex != null  and merchantIndex != ''">and m.merchant_index like concat('%',
            #{merchantIndex}, '%')
        </if>
        <if test="merchantName != null  and merchantName != ''"> and m.merchant_name like concat('%', #{merchantName}, '%')</if>
        <if test="phone != null  and phone != ''">and s.phone like concat('%', #{phone}, '%')</if>
        <if test="realName != null  and realName != ''"> and s.real_name like concat('%', #{realName}, '%')</if>
        <if test="serverLevel != null  and serverLevel != ''"> and s.server_level = #{serverLevel}</if>
        <if test="status != null  and status != ''"> and s.status = #{status}</if>
        <if test="serverAgeStart != null  and serverAgeStart != ''"> and s.server_age &gt;= #{serverAgeStart}</if>
        <if test="serverAgeEnd != null  and serverAgeEnd != ''"> and server_age &lt;= #{serverAgeEnd}</if>
        <if test="phonenumber != null  and phonenumber != ''">and u.phonenumber like concat('%', #{phonenumber}, '%')
        </if>
        <if test="StopOrderFor != null  and StopOrderFor != ''"> and st.stop_order = #{StopOrderFor}</if>
        group by s.server_id
        ORDER BY s.create_time DESC
    </select>

    <select id="merchantUserList" parameterType="com.younike.system.param.ServerUserQueryParam" resultMap="ServerUserListResult">
        select s.*,m.merchant_name, count(case when g.del_flag = 0 then 1 else null end) as goodsCount, u.phonenumber, c.decode_value as serverLevelName,st.stop_order
        from server_user s
        left join goods_server g on s.server_id = g.server_id and g.del_flag = 0
        left join merchant m on s.merchant_id = m.merchant_id and m.del_flag = 0
        left join sys_user u on m.admin_id = u.user_id and u.del_flag = 0
        left join sys_code c on s.server_level = c.decode_id and c.code_id = '007'
        left join server_time st on st.server_id=s.server_id
        where s.del_flag = 0
        <if test="merchantId != null  and merchantId != ''"> and s.merchant_id = #{merchantId}</if>
        <if test="merchantName != null  and merchantName != ''"> and m.merchant_name like concat('%', #{merchantName}, '%')</if>
        <if test="phone != null  and phone != ''"> and s.phone = #{phone}</if>
        <if test="realName != null  and realName != ''"> and s.real_name like concat('%', #{realName}, '%')</if>
        <if test="serverLevel != null  and serverLevel != ''"> and s.server_level = #{serverLevel}</if>
        <if test="status != null  and status != ''"> and s.status = #{status}</if>
        <if test="serverAgeStart != null  and serverAgeStart != ''"> and s.server_age &gt;= #{serverAgeStart}</if>
        <if test="serverAgeEnd != null  and serverAgeEnd != ''"> and server_age &lt;= #{serverAgeEnd}</if>
        <if test="phonenumber != null  and phonenumber != ''"> and u.phonenumber = #{phonenumber}</if>
        <if test="StopOrderFor != null  and StopOrderFor != ''"> and st.stop_order = #{StopOrderFor}</if>
        group by s.server_id
        ORDER BY s.create_time DESC
    </select>

    <select id="selectServerUserList" parameterType="ServerUser" resultMap="ServerUserResult">
        <include refid="selectServerUserVo"/>
        <where>
            <if test="merchantId != null  and merchantId != ''"> and merchant_id = #{merchantId}</if>
            <if test="serverAge != null "> and server_age = #{serverAge}</if>
            <if test="orderCount != null "> and order_count = #{orderCount}</if>
            <if test="score != null "> and score = #{score}</if>
            <if test="rate != null "> and rate = #{rate}</if>
            <if test="serverLevel != null  and serverLevel != ''"> and server_level = #{serverLevel}</if>
            <if test="phone != null  and phone != ''"> and phone = #{phone}</if>
            <if test="openId != null  and openId != ''"> and open_id = #{openId}</if>
            <if test="appleId != null  and appleId != ''"> and apple_id = #{appleId}</if>
            <if test="nikeName != null  and nikeName != ''"> and nike_name like concat('%', #{nikeName}, '%')</if>
            <if test="realName != null  and realName != ''"> and real_name like concat('%', #{realName}, '%')</if>
            <if test="age != null "> and age = #{age}</if>
            <if test="sex != null "> and sex = #{sex}</if>
            <if test="avatar != null  and avatar != ''"> and avatar = #{avatar}</if>
            <if test="closeTime != null "> and close_time = #{closeTime}</if>
            <if test="closeReason != null  and closeReason != ''"> and close_reason = #{closeReason}</if>
            <if test="password != null  and password != ''"> and password = #{password}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="loginIp != null  and loginIp != ''"> and login_ip = #{loginIp}</if>
            <if test="loginDate != null "> and login_date = #{loginDate}</if>
            <if test="column1 != null  and column1 != ''"> and column1 = #{column1}</if>
            <if test="column2 != null  and column2 != ''"> and column2 = #{column2}</if>
            <if test="column3 != null  and column3 != ''"> and column3 = #{column3}</if>
        </where>
        ORDER BY create_time DESC
    </select>

    <select id="selectServerUserByServerId" parameterType="Long" resultMap="ServerUserResult">
        <include refid="selectServerUserVo"/>
        where server_id = #{serverId}
    </select>

    <insert id="insertServerUser" parameterType="ServerUser" useGeneratedKeys="true" keyProperty="serverId">
        insert into server_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="merchantId != null">merchant_id,</if>
            <if test="serverAge != null">server_age,</if>
            <if test="orderCount != null">order_count,</if>
            <if test="score != null">score,</if>
            <if test="rate != null">rate,</if>
            <if test="serverLevel != null">server_level,</if>
            <if test="phone != null and phone != ''">phone,</if>
            <if test="openId != null">open_id,</if>
            <if test="appleId != null">apple_id,</if>
            <if test="nikeName != null">nike_name,</if>
            <if test="realName != null">real_name,</if>
            <if test="desc != null">`desc`,</if>
            <if test="age != null">age,</if>
            <if test="sex != null">sex,</if>
            <if test="followAll != null">follow_all,</if>
            <if test="avatar != null">avatar,</if>
            <if test="closeTime != null">close_time,</if>
            <if test="closeReason != null">close_reason,</if>
            <if test="password != null">password,</if>
            <if test="status != null">status,</if>
            <if test="loginIp != null">login_ip,</if>
            <if test="loginDate != null">login_date,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="column1 != null">column1,</if>
            <if test="column2 != null">column2,</if>
            <if test="column3 != null">column3,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="merchantId != null">#{merchantId},</if>
            <if test="serverAge != null">#{serverAge},</if>
            <if test="orderCount != null">#{orderCount},</if>
            <if test="score != null">#{score},</if>
            <if test="rate != null">#{rate},</if>
            <if test="serverLevel != null">#{serverLevel},</if>
            <if test="phone != null and phone != ''">#{phone},</if>
            <if test="openId != null">#{openId},</if>
            <if test="appleId != null">#{appleId},</if>
            <if test="nikeName != null">#{nikeName},</if>
            <if test="realName != null">#{realName},</if>
            <if test="desc != null">#{desc},</if>
            <if test="age != null">#{age},</if>
            <if test="sex != null">#{sex},</if>
            <if test="followAll != null">followAll,</if>
            <if test="avatar != null">#{avatar},</if>
            <if test="closeTime != null">#{closeTime},</if>
            <if test="closeReason != null">#{closeReason},</if>
            <if test="password != null">#{password},</if>
            <if test="status != null">#{status},</if>
            <if test="loginIp != null">#{loginIp},</if>
            <if test="loginDate != null">#{loginDate},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="column1 != null">#{column1},</if>
            <if test="column2 != null">#{column2},</if>
            <if test="column3 != null">#{column3},</if>
         </trim>
    </insert>

    <update id="updateServerUser" parameterType="ServerUser">
        update server_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="merchantId != null">merchant_id = #{merchantId},</if>
            <if test="serverAge != null">server_age = #{serverAge},</if>
            <if test="orderCount != null">order_count = #{orderCount},</if>
            <if test="score != null">score = #{score},</if>
            <if test="rate != null">rate = #{rate},</if>
            <if test="serverLevel != null">server_level = #{serverLevel},</if>
            <if test="phone != null and phone != ''">phone = #{phone},</if>
            <if test="openId != null">open_id = #{openId},</if>
            <if test="appleId != null">apple_id = #{appleId},</if>
            <if test="nikeName != null">nike_name = #{nikeName},</if>
            <if test="realName != null">real_name = #{realName},</if>
            <if test="age != null">age = #{age},</if>
            <if test="desc != null">`desc` = #{desc},</if>
            <if test="sex != null">sex = #{sex},</if>
            <if test="followAll != null">follow_all = #{followAll},</if>
            <if test="avatar != null">avatar = #{avatar},</if>
            <if test="closeTime != null">close_time = #{closeTime},</if>
            <if test="closeReason != null">close_reason = #{closeReason},</if>
            <if test="password != null">password = #{password},</if>
            <if test="status != null">status = #{status},</if>
            <if test="loginIp != null">login_ip = #{loginIp},</if>
            <if test="loginDate != null">login_date = #{loginDate},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="column1 != null">column1 = #{column1},</if>
            <if test="column2 != null">column2 = #{column2},</if>
            <if test="column3 != null">column3 = #{column3},</if>
        </trim>
        where server_id = #{serverId}
    </update>

    <delete id="deleteServerUserByServerId" parameterType="Long">
        delete from server_user where server_id = #{serverId}
    </delete>

    <delete id="deleteServerUserByServerIds" parameterType="String">
        delete from server_user where server_id in
        <foreach item="serverId" collection="array" open="(" separator="," close=")">
            #{serverId}
        </foreach>
    </delete>

    <select id="pageDownSelectList" parameterType="com.younike.system.param.ServerUserQueryParam" resultMap="ServerUserListResult">
        select s.*,m.merchant_name, count(case when g.del_flag = 0 then 1 else null end) as goodsCount, u.phonenumber, c.decode_value as serverLevelName,st.stop_order
        from server_user s
        left join goods_server g on s.server_id = g.server_id and g.del_flag = 0
        left join merchant m on s.merchant_id = m.merchant_id and m.del_flag = 0
        left join sys_user u on m.admin_id = u.user_id and u.del_flag = 0
        left join sys_code c on s.server_level = c.decode_id and c.code_id = '007'
        left join server_time st on st.server_id=s.server_id
        where s.del_flag = 0
        <if test="merchantId != null  and merchantId != ''"> and s.merchant_id = #{merchantId}</if>
        <if test="merchantName != null  and merchantName != ''"> and m.merchant_name like concat('%', #{merchantName}, '%')</if>
        <if test="phone != null  and phone != ''"> and s.phone = #{phone}</if>
        <if test="realName != null  and realName != ''"> and s.real_name like concat('%', #{realName}, '%')</if>
        <if test="serverLevel != null  and serverLevel != ''"> and s.server_level = #{serverLevel}</if>
        <if test="status != null  and status != ''"> and s.status = #{status}</if>
        <if test="serverAgeStart != null  and serverAgeStart != ''"> and s.server_age &gt;= #{serverAgeStart}</if>
        <if test="serverAgeEnd != null  and serverAgeEnd != ''"> and server_age &lt;= #{serverAgeEnd}</if>
        <if test="phonenumber != null  and phonenumber != ''"> and u.phonenumber = #{phonenumber}</if>
        <if test="StopOrderFor != null  and StopOrderFor != ''"> and st.stop_order = #{StopOrderFor}</if>
        group by s.server_id
        ORDER BY s.create_time DESC
    </select>

    <select id="getByServerId" parameterType="java.lang.Long" resultType="com.younike.system.domain.vo.ServerUserListVo">
        SELECT
               su.*,
               m.merchant_name,
               sc.decode_value AS serverLevelName
        FROM server_user su
        LEFT JOIN merchant m ON su.merchant_id = m.merchant_id AND m.del_flag = 0
        LEFT JOIN sys_code sc ON su.server_level = sc.decode_id AND sc.code_id = '007'
        WHERE su.del_flag = 0
        AND su.server_id = #{serverId}
    </select>
</mapper>
