<?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.vo.ServerUserVo" id="ServerUserResultCustom" extends="ServerUserResult">
        <result property="serverLevelName"    column="server_level_name"    />
        <result property="stopOrder"    column="stop_order"    />
        <result property="merchantName"    column="merchant_name"    />
        <result property="merchantType"    column="merchant_type"    />
        <result property="serviceDistance"    column="service_distance"    />
        <result property="detail"    column="detail"    />
    </resultMap>

    <select id="findByPhone" resultMap="ServerUserResult">
        <include refid="selectServerUserVo"/>
        where phone = #{phone}
        and del_flag = 0
    </select>
    <select id="findByOpenid" resultMap="ServerUserResult">
        <include refid="selectServerUserVo"/>
        where open_id = #{openid}
        and del_flag = 0
    </select>
    <select id="selectUserInfo" resultMap="ServerUserResultCustom">
        select su.server_id, su.merchant_id, su.server_age, su.order_count,su.score ,su.rate, server_level,
               su.phone, su.open_id, su.apple_id, su.nike_name, su.real_name, su.age, su.sex, su.avatar, su.close_time,
               su.close_reason, su.password, su.status, su.login_ip, su.login_date, su.del_flag, su.create_by,
               su.create_time, su.update_by, su.update_time, su.column1, su.column2, su.column3,sc.Decode_Value as server_level_name,
               st.stop_order,m.merchant_name,su.desc,m.detail,m.service_distance,m.merchant_type,m.order_total_amount_show
        from server_user su
        left join sys_code sc on su.server_level = sc.Decode_Id and sc.Code_Id = '007'
        left join server_time st on st.server_id = su.server_id and st.del_flag = 0
        left join merchant m on m.merchant_id = su.merchant_id and m.del_flag = 0
        where su.server_id = #{serverId}
        and su.del_flag = 0
    </select>

    <select id="getServerUserList" parameterType="ServerUserSearchParam" resultMap="ServerUserResult">
        <include refid="selectServerUserVo"/>
        <where>
            del_flag = 0
            <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>
    </select>
    <select id="existsPhone" resultType="java.lang.Boolean">
        select exists(
            select * from server_user
            where phone = #{phone}
              and del_flag = 0
        <if test="serverId != null  and serverId != ''"> and server_id != #{serverId}</if>
                   )
    </select>
    <update id="updateServerUsers" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" separator=";">
            update server_user set score = #{item.score} ,rate = #{item.rate} ,order_count = #{item.orderCount}
            where server_id = #{item.serverId}
        </foreach>
    </update>

    <select id="getServerUserCount" resultType="java.lang.Integer">
        SELECT
        count(*)
        FROM server_user
        WHERE del_flag = '0'
    </select>
<!--    根据服务人员id查询服务人员数据-->
    <select id="serverUserTime" parameterType="Long" resultMap="ServerUserResultCustom">
        select
            su.server_id,
            su.merchant_id,
            su.`desc`,
            su.server_age,
            su.order_count,
            su.score,
            su.rate,
            su.server_level,
            su.follow_all,
            su.phone,
            su.open_id,
            su.apple_id,
            su.nike_name,
            real_name,
            su.age,
            su.sex,
            su.avatar,
            su.close_time,
            su.close_reason,
            su.password,
            su.status,
            su.login_ip,
            su.login_date,
            su.del_flag,
            su.create_by,
            su.create_time,
            su.update_by,
            su.update_time,
            st.stop_order
            from server_user su
            left join server_time st on st.server_id = su.server_id and st.del_flag = 0
            where su.del_flag=0
            AND su.server_id=#{serverId}
    </select>
</mapper>
