<?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.house365.ws.dao.mapper.UserMapper">

    <resultMap id="infoMap" type="com.house365.beans.entity.UserEntity">
        <result property="id" column="id" />
        <result property="name" column="name" />
        <result property="realName" column="realName" />
        <result property="password" column="password" />
        <result property="weiXinId" column="weiXinId" />
        <result property="mediaId" column="mediaId" />
        <result property="mobile" column="mobile" />
        <result property="cornet" column="cornet"/>
        <result property="deptId" column="deptId" />
        <result property="deptUrlPath" column="deptUrlPath" />
        <result property="position" column="position" />
        <result property="gender" column="gender" />
        <result property="email" column="email" />
        <result property="extattr" column="extattr" />
        <result property="status" column="status" />
        <result property="enabled" column="enabled" />
        <result property="createUserid" column="createUserid" />
        <result property="createUsername" column="createUsername" />
        <result property="createTime" column="createTime" />
        <result property="createIp" column="createIp" />
        <result property="updateUserid" column="updateUserid" />
        <result property="updateUsername" column="updateUsername" />
        <result property="updateTime" column="updateTime" />
        <result property="updateIp" column="updateIp" />
        <result property="preLoginTime" column="preLoginTime" />
        <result property="lastLoginTime" column="lastLoginTime" />
        <result property="score" column="score"/>
        <result property="evaluateCount" column="evaluateCount"/>
        <result property="hotlinephone" column="hotlinephone" />
        <result property="goodat" column="goodat" />
        <result property="description" column="description" />
        <result property="headImgURL" column="headImgURL" />
        <result property="fullImgURL" column="fullImgURL" />
        <result property="isFreeMan" column="isFreeMan" />
        <result property="defaultVisit" column="defaultVisit" />
        <result property="accId" column="accid"/>
        <result property="identityType" column="identityType"/>
        <result property="token" column="token"/>
        <result property="wxNumber" column="wx_number"/>
        <result property="wxCodeImgUrl" column="wx_code_imgUrl"/>
        <result property="promoteId" column="promote_id"/>
        <result property="onlineStatus" column="online_status"/>
        <result property="showMobile" column="showMobile" />
    </resultMap>

    <resultMap id="infoMap2" type="com.house365.beans.entity.UserSimpleEntity">
        <result property="id" column="id" />
        <result property="name" column="name" />
        <result property="realName" column="realName" />
        <result property="mobile" column="mobile" />

        <result property="hotlinephone" column="hotlinephone" />
        <result property="enabled" column="enabled" />
        <result property="cornet" column="cornet"/>

        <result property="headImgURL" column="headImgURL" />
        <result property="fullImgURL" column="fullImgURL" />
        <result property="accId" column="accid"/>
        <result property="goodat" column="goodat" />
        <result property="description" column="description" />
        <result property="score" column="score"/>
    </resultMap>

    <resultMap id="infoMap3" type="com.house365.beans.entity.DutyStaffEntity">
        <result property="id" column="id" />
        <result property="name" column="name" />
        <result property="userId" column="userId" />
        <result property="onduty" column="onduty" />
    </resultMap>

    <resultMap id="infoMap4" type="com.house365.beans.entity.RequestLoginEntity">
        <result property="id" column="id" />
        <result property="name" column="name" />
        <result property="userId" column="userId" />
        <result property="realName" column="realName" />
        <result property="loginIp" column="loginIp" />
        <result property="loginCity" column="loginCity" />
        <result property="loginTime" column="loginTime" />
        <result property="status" column="status" />
        <result property="authorizationTime" column="authorizationTime" />
    </resultMap>


    <select id="getById" resultMap="infoMap" parameterType="int">
        SELECT id, name,realName, password, weiXinId, mediaId, mobile, cornet, deptId, deptUrlPath, position, gender,
        email, extattr, status, enabled, createUserid, createUsername, createTime, createIp, updateUserid,
        updateUsername, updateTime, updateIp, preLoginTime, lastLoginTime, hotlinephone, goodat,
        description, headImgURL, fullImgURL,isFreeMan,defaultVisit,accid,identityType,token,wx_number,wx_code_imgUrl,showMobile
        FROM user
        where id = #{id} AND enabled = 1
    </select>

    <select id="queryManagers" resultType="com.house365.beans.entity.UserEntity" parameterType="map">
        SELECT id, realName managerName
        FROM user
        where deptUrlPath like CONCAT('',#{deptUrlPath},'%')  AND enabled = 1
        <if test="identityType != null and identityType != ''">
            and identityType= #{identityType}
        </if>
        <if test="position != null and position != ''">
            and position = #{position}
        </if>
    </select>

    <select id="queryRuleManagers" resultType="com.house365.beans.entity.UserEntity" parameterType="map">
        select id, realName managerName from user
        where deptId in
        <foreach item="item" index="index" collection="deptIds" open="(" separator="," close=")">
            #{item,jdbcType=VARCHAR}
        </foreach>
    </select>

    <select id="queryByConditions" resultMap="infoMap" parameterType="map">
        SELECT *
        FROM user
        where enabled = 1
        <if test="userName != null and userName != ''">
            AND user_name = #{userName}
        </if>
        <if test="telephone != null and telephone != ''">
            AND mobile = #{telephone}
        </if>
        <if test="isFreeMan != null and isFreeMan >= 0 ">
            AND isFreeMan = #{isFreeMan}
        </if>
        <if test="deptUrlPath != null and deptUrlPath != ''">
            AND (deptUrlPath = #{deptUrlPath} OR deptUrlPath like CONCAT('',#{deptUrlPath},'/%') )
        </if>
        <if test="deptUrlPathNew != null and deptUrlPathNew != ''">
           AND (deptUrlPath REGEXP #{deptUrlPathNew} OR deptUrlPath = #{deptUrlPathNew})
        </if>
        <if test="position != null and position != ''">
            AND position = #{position}
        </if>
        <if test="identityType != null and identityType != ''">
            AND identityType = #{identityType}
        </if>
        <if test="accId != null and accId != ''">
            AND accid = #{accId}
        </if>
        <if test="onlineStatus != null and onlineStatus >= 0">
            AND online_status = #{onlineStatus}
        </if>
        <if test="showMobile != null and showMobile >= 0">
            AND showMobile = #{showMobile}
        </if>
        <if test="deptId != null and deptId >= 0 ">
            AND deptId = #{deptId}
        </if>
        <if test="deptIdList != null and deptIdList.size() > 0 ">
            AND deptId in
            <foreach collection="deptIdList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="order != null and order > 0 ">
            ORDER BY name ASC
        </if>
    </select>

    <select id="queryUserInfoByd" resultMap="infoMap2" parameterType="map">
        SELECT name,realName, mobile,hotlinephone,cornet,enabled,
        headImgURL, fullImgURL,accid,description,goodat,
        (select ifnull(format(AVG(eucl.score),1),0) from evaluate_user_customer_log eucl where eucl.user_id = u.id and
        eucl.check_status =1 and eucl.is_deleted = 0)
        score
        FROM user u
        where id = #{userId}
        AND enabled = 1
    </select>

    <select id="queryUserInfoByPhone" resultMap="infoMap2" parameterType="map">
        SELECT id,accid,name,realName, mobile,hotlinephone,cornet,enabled,
        headImgURL, fullImgURL,accid,description,goodat,
        (select ifnull(format(AVG(eucl.score),1),0) from evaluate_user_customer_log eucl where eucl.user_id = u.id and
        eucl.check_status =1 and eucl.is_deleted = 0)
        score
        FROM user u
        where mobile = #{phone}
        AND enabled = 1
    </select>

    <select id="getStaffDutyList" resultMap="infoMap3">
        select id,name,userId,onduty from duty_staff
    </select>

    <select id="getDutyByCondition" resultMap="infoMap3" parameterType="map">
        select id,name,userId,onduty
        FROM duty_staff
        where 1 = 1
        <if test="onduty != null and onduty >= 0 ">
            AND onduty = #{onduty}
        </if>
    </select>

    <select id="getOndutyById" resultMap="infoMap3" parameterType="map">
        select id,name,userId,onduty from duty_staff where userId = #{uid}#
    </select>

    <update id="updateDutyStaff" parameterType="com.house365.beans.entity.DutyStaffEntity">
        UPDATE duty_staff
        SET
        onduty = #{onduty},
        updateTime = #{updateTime}
        WHERE id = #{id}
    </update>

    <update id="authorizationById" parameterType="com.house365.beans.entity.RequestLoginEntity">
        UPDATE request_login
        SET
        status = #{status},
        authorizationTime = CURRENT_TIMESTAMP
        WHERE id = #{id}
    </update>

    <update id="updateRequestById" parameterType="com.house365.beans.entity.RequestLoginEntity">
        UPDATE request_login
        SET
        status = 2
        WHERE userId = #{userId} and status=1
    </update>

    <select id="getLimitLoginById" resultMap="infoMap">
        select id, name,realName, password, weiXinId, mediaId, mobile, cornet, deptId, deptUrlPath, position, gender,
        email, extattr, status, enabled, createUserid, createUsername, createTime, createIp, updateUserid,
        updateUsername, updateTime, updateIp, preLoginTime, lastLoginTime, hotlinephone, goodat,
        description, headImgURL, fullImgURL,isFreeMan,defaultVisit,accid,identityType,token,limitLogin,showMobile
        from user where name = #{name}

    </select>

    <select id="getManagerByPid" resultMap="infoMap" parameterType="map">
        select * from (
            SELECT id,realName FROM user WHERE depturlpath LIKE ( SELECT concat( depturlpath, '/%' ) FROM user WHERE id = #{id} )
            union
            select id,realName from user where id=#{id}
        ) t where 1=1
        <if test="realName != null and realName != '' ">
            AND realName like CONCAT('%', #{realName},'%')
        </if>
    </select>

    <select id="getRequestLoginInfoById" resultMap="infoMap4">
        select id,name,realName,loginIp,loginCity,loginTime,status,authorizationTime,userId
        from request_login where id = #{id}#

    </select>

    <insert id="addRequestLoginInfo" parameterType="com.house365.beans.entity.RequestLoginEntity">
            insert into request_login (name,realName,loginIp,loginCity,loginTime,authorizationTime, userId) values
            (
              #{name},#{realName},#{loginIp},#{loginCity},#{loginTime},#{authorizationTime}, #{userId}
            )
    </insert>

    <select id="getRequestLoginInfo" resultMap="infoMap4">
        select id,name,realName,loginIp,loginCity,loginTime,status,authorizationTime,userId
        from request_login where name = #{name}# and loginIp = #{loginIp}#
        order by loginTime desc
    </select>

    <update id="updateUserStatus" parameterType="map">
        UPDATE user
        SET
        online_status = #{onlineStatus}
        WHERE mobile = #{mobile}
    </update>

    <select id="getCityById" parameterType="String" resultType="String">
        select
            case c.city
            when 'zb001' then 'nj'
            else c.city end
            as city
       from user u inner join city_urlpath c on SUBSTRING_INDEX(u.deptUrlPath,'/',3)=c.urlpath where u.id=#{userId}
    </select>

</mapper>