<?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.ym.mec.biz.dal.dao.TenantProxyInfoDao">
    <resultMap id="BaseResultMap" type="com.ym.mec.biz.dal.entity.TenantProxyInfo">
        <id column="id_" jdbcType="INTEGER" property="id"/>
        <result column="proxy_name_" jdbcType="VARCHAR" property="proxyName"/>
        <result column="proxy_user_id_" jdbcType="INTEGER" property="proxyUserId"/>
        <result column="state_" jdbcType="INTEGER" property="state"/>
        <result column="create_by_" jdbcType="INTEGER" property="createBy"/>
        <result column="create_time_" jdbcType="TIMESTAMP" property="createTime"/>
    </resultMap>

    <sql id="Base_Column_List">
        id_
        , proxy_name_, proxy_user_id_, state_, create_by_, create_time_
    </sql>

    <insert id="insertBatch" keyColumn="id_" keyProperty="id" useGeneratedKeys="true"
            parameterType="com.ym.mec.biz.dal.entity.TenantProxyInfo">
        insert into tenant_proxy_info(proxy_name_, proxy_user_id_, state_, create_by_, create_time_)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.proxyName}, #{entity.proxyUserId}, #{entity.state}, #{entity.createBy}, #{entity.createTime})
        </foreach>
    </insert>

    <!-- 向数据库增加一条记录 -->
    <insert id="insertProxySysUser" parameterType="com.ym.mec.auth.api.entity.SysUser" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        INSERT INTO sys_user
        (tenant_id_,username_,phone_,password_,create_time_,user_type_,real_name_)
        VALUES(#{tenantId},#{username},#{phone},#{password},now(),#{userType},#{realName})
    </insert>

    <update id="updateProxySysUserLock" parameterType="object">
        update sys_user set lock_flag_ = #{lock} where id_ = #{id}
    </update>

    <update id="updateSysUserPhone" parameterType="object">
        update sys_user
        set phone_     = #{phone},
            username_  = #{name},
            real_name_ = #{name}
        where id_ = #{id}
    </update>

    <select id="queryProxyUserStaff" resultType="com.ym.mec.biz.dal.vo.ProxyUserVo">
        select
            b.id_ as userId,
            a.parent_id_ as parentId,
            c.id_ as proxyId,
            c.proxy_name_ as proxyName,
            b.real_name_ as `name`,
            b.phone_ as `phone`,
            '员工' as `role`,
            c.state_ as state
        from tenant_proxy_user_relation as a
                 left join tenant_proxy_info as c on a.parent_id_ = c.proxy_user_id_
                 left join sys_user as b  on a.user_id_ = b.id_
        where a.parent_id_ = #{id}
    </select>

    <select id="queryProxyUser" parameterType="map" resultType="com.ym.mec.biz.dal.vo.ProxyUserVo">
        select
            b.id_ as userId,
            c.id_ as proxyId,
            c.proxy_name_ as proxyName,
            b.real_name_ as `name`,
            b.phone_ as `phone`,
            '负责人' as `role`,
            c.state_ as state
        from tenant_proxy_user_relation as a
                 left join tenant_proxy_info as c on a.user_id_ = c.proxy_user_id_
                 left join sys_user as b  on a.user_id_ = b.id_
        <where>
            a.parent_id_ is null
            <if test="param.state != null ">
               AND c.state_ = #{param.state}
            </if>
            <if test="param.search != null ">
                AND (
                c.proxy_name_ LIKE CONCAT('%', #{param.search},'%')
                OR b.`real_name_` LIKE CONCAT('%', #{param.search},'%')
                OR b.`phone_` LIKE CONCAT('%', #{param.search},'%')
                )
            </if>
        </where>
    </select>

    <select id="queryUserList" parameterType="map" resultType="com.ym.mec.auth.api.entity.SysUser">
        select id_ as id,
               real_name_ as realName,
               phone_ as phone
        from sys_user
        where tenant_id_ = -1
        <if test="param.id != null ">
            AND id_ = #{param.id}
        </if>
        <if test="param.search != null and param.search != ''">
          AND (
               `real_name_` LIKE CONCAT('%', #{param.search},'%')
                OR `phone_` LIKE CONCAT('%', #{param.search},'%')
            )
        </if>
    </select>

</mapper>
