<?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.diagbot.mapper.UserMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.diagbot.entity.User">
        <id column="id" property="id"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="creator" property="creator"/>
        <result column="modifier" property="modifier"/>
        <result column="password" property="password"/>
        <result column="username" property="username"/>
        <result column="linkman" property="linkman"/>
        <result column="email" property="email"/>
        <result column="type" property="type"/>
    </resultMap>

    <resultMap id="UserOrgDTO" type="com.diagbot.dto.UserOrgDTO">
        <id column="user_id" property="userId"/>
        <result column="username" property="username"/>
        <result column="linkman" property="linkman"/>
        <result column="email" property="email"/>
        <result column="org_id" property="orgId"/>
        <result column="org_name" property="orgName"/>
        <result column="is_reject" property="isReject"/>
        <result column="au_status" property="auStatus"/>
        <result column="sub_num" property="subNum"/>
    </resultMap>


    <select id="findByName" resultMap="BaseResultMap" parameterType="java.lang.String">
        select * from sys_user
        where username = #{username} and is_deleted = 'N'
    </select>

    <select id="selectUserListPage" resultMap="BaseResultMap">
        select u.*
        from sys_user u
        LEFT JOIN sys_user_role ur on u.id= ur.user_id
        LEFT JOIN sys_role r on ur.role_id=r.id
        where u.is_deleted = 'N'
        <if test="user.id != null">
            and u.id = #{user.id}
        </if>
    </select>


    <select id="indexPage" resultMap="BaseResultMap">
        select u.* from sys_user u where u.is_deleted = 'N'
        <if test="user.id != null">
            and u.id = #{user.id}
        </if>
        <if test="user.username != null and user.username != '' ">
            and u.username like concat('%',#{user.username},'%')
        </if>
    </select>

    <select id="getByIds" resultMap="BaseResultMap">
        select u.* from sys_user u where u.is_deleted = 'N'
        and u.id in
        <foreach collection="list" item="ids" open="(" close=")" separator=",">
            #{ids}
        </foreach>
    </select>


    <select id="getUserOrgDTOByIds" resultMap="UserOrgDTO">
        select u.id user_id ,u.username username, u.email email, u.linkman linkman , org.id org_id,
        org.name org_name, ua.is_reject is_reject, ua.status au_status,org.sub_num
        from sys_user u, sys_user_organization uo, sys_organization org , sys_user_authentication ua
        where u.is_deleted = 'N' and uo.is_deleted = 'N' and org.is_deleted = 'N'
        and u.id = uo.user_id and uo.organization_id = org.id and u.id = ua.user_id
        and u.id in
        <foreach collection="list" item="ids" open="(" close=")" separator=",">
            #{ids}
        </foreach>
    </select>


    <select id="getUserByOrgName" resultMap="BaseResultMap">
		select a.* from sys_user a, sys_organization org, sys_user_organization uo
		where a.is_deleted = 'N' and org.is_deleted = 'N' and uo.is_deleted = 'N'
		and a.id = uo.user_id and uo.organization_id = org.id and org.`name` like concat('%' ,#{orgName}, '%')
	</select>


    <!-- 	<select id="selectUserInfoListPage" resultMap="BaseResultMap">
            select u.*
            LEFT JOIN sys_user_role ur on u.id= ur.user_id
            LEFT JOIN
            sys_role r on ur.role_id=r.id
            where u.is_deleted = 'N' and type = "0"
            ORDER BY gmt_create DESC
        </select> -->

    <!--  <select id="selectUserInfoPage"  parameterType="com.diagbot.dto.UserInfoDTO" resultType="com.diagbot.dto.UserInfoDTO">
         SELECT
         u.id AS userId,
         u.gmt_create as userGmtCreate,
         u.username as userName,
         u.linkman as linKman,
         u.email as email,
         org.id AS orgId,
         org.gmt_create as orgGmtCreate,
         org.name as orgName
         FROM sys_user u
         LEFT JOIN sys_user_organization uorg ON u.id = uorg.user_id
         LEFT JOIN sys_organization org ON uorg.organization_id = org.id
         WHERE u.is_deleted = 'N' AND u.TYPE = 0 ORDER BY u.gmt_create DESC
     </select> -->
    <select id="selectUserInfoListPage" resultType="com.diagbot.dto.UserInfoDTO">
        SELECT
        u.id AS userId,
        u.gmt_create AS userGmtCreate,
        u.username AS userName,
        u.linkman AS linKman,
        u.email AS email,
        org.id AS orgId,
        org.gmt_create AS orgGmtCreate,
        org.name AS orgName,
        org.type as orgType,
        org.principal AS orgPrincipal,
        org.address as orgAddress,
        org.parent_id as orgParent_id,
        org.sub_num as orgSub_num,
        aut.position as autPosition,
        aut.is_reject as autIsReject,
        aut.status as autStatus
        FROM sys_user u
        LEFT JOIN sys_user_organization uorg ON u.id = uorg.user_id
        LEFT JOIN sys_organization org ON uorg.organization_id = org.id
        LEFT JOIN sys_user_authentication aut ON u.id = aut.user_id
        WHERE u.is_deleted = 'N' AND u.TYPE = 0
        <if test="userInfo.orgName != null">
            AND org.name LIKE CONCAT('%', #{userInfo.orgName}, '%')
        </if>
        <if test="userInfo.autStatus != null">
            AND aut.status = #{userInfo.autStatus}
        </if>
        ORDER BY u.gmt_create DESC
    </select>

    <update id="updateUserInfoAll" parameterType="java.util.Map">
        UPDATE sys_user u ,sys_user_organization a ,
        sys_organization b ,sys_user_authentication c
        SET u.remark = "0"
        <if test="userMap.email != null">
            <if test="userMap.gmtModified !=null">
                ,u.gmt_modified=#{userMap.gmtModified}
            </if>
            <if test="userMap.modifier != null">
                ,u.modifier=#{userMap.modifier}
            </if>
            ,u.email=#{userMap.email}
        </if>
        <if test="userMap.principal != null || userMap.address != null || userMap.type != null">
            <if test="userMap.gmtModified !=null">
                ,b.gmt_modified=#{userMap.gmtModified}
            </if>
            <if test="userMap.modifier != null">
                ,b.modifier=#{userMap.modifier}
            </if>
            <if test="userMap.principal != null">
                ,b.principal=#{userMap.principal}
            </if>
            <if test="userMap.address != null">
                ,b.address=#{userMap.address}
            </if>
            <if test="userMap.type != null">
                ,b.type=#{userMap.type}
            </if>
        </if>

        <!-- <if test="position != null || isReject != null || rejectType !=null || rejectComment !=null || status != null"> -->
        <if test="userMap.position != null">
            <if test="userMap.gmtModified !=null">
                ,c.gmt_modified=#{userMap.gmtModified}
            </if>
            <if test="userMap.modifier != null">
                ,c.modifier=#{userMap.modifier}
            </if>
            ,c.position=#{userMap.position}
        </if>
        <!-- <if test="isReject != null">
         ,c.is_reject=#{isReject}
        </if>
        <if test="rejectType !=null">
          ,c.reject_type=#{rejectType}
        </if>
        <if test="rejectComment !=null">
          ,c.reject_comment=#{rejectComment}
        </if>
        <if test="status != null">
          ,c.status = #{status}
        </if> -->
        <!-- </if> -->
        WHERE
        u.is_deleted = "N"
        AND u.id = #{userMap.userId}
        AND c.user_id= #{userMap.userId}
        AND a.user_id = #{userMap.userId}
        AND a.organization_id =b.id

    </update>


    <update id="updateDeleted" parameterType="java.util.Map">
        UPDATE sys_user u ,sys_user_organization a ,
        sys_organization b,sys_user_authentication c
        SET u.is_deleted = "Y",a.is_deleted= "Y",b.is_deleted= "Y",c.is_deleted="Y"
        <if test="map.gmtModified !=null">
            ,u.gmt_modified=#{map.gmtModified}
            ,b.gmt_modified=#{map.gmtModified}
            ,c.gmt_modified=#{map.gmtModified}
        </if>
        <if test="map.modifier != null">
            ,u.modifier=#{map.modifier}
            ,b.modifier=#{map.modifier}
            ,c.modifier=#{map.modifier}
        </if>
        WHERE u.id = #{map.userId}
        AND c.user_id= #{map.userId}
        AND a.user_id = #{map.userId}
        AND a.organization_id =b.id
    </update>
    <update id="auditUserInfoAll" parameterType="java.util.Map">
        UPDATE sys_user_authentication
        <trim prefix="set" suffixOverrides=",">
            <if test="auditMap.gmtModified !=null">gmt_modified=#{auditMap.gmtModified},</if>
            <if test="auditMap.modifier != null">modifier=#{auditMap.modifier},</if>
            <if test="auditMap.isReject != null">is_reject = #{auditMap.isReject} ,</if>
            <if test="auditMap.rejectType != null">reject_type =#{auditMap.rejectType},</if>
            <if test="auditMap.rejectComment != null">reject_comment= #{auditMap.rejectComment},
            </if>
            <if test="auditMap.status != null">STATUS= #{auditMap.status}</if>
        </trim>
        WHERE
        user_id = #{auditMap.userId}
        AND is_deleted = "N"

    </update>

    <select id="dependentuserInfoByTime" parameterType="java.util.Map"
            resultType="com.diagbot.dto.UserInfoDTO">

        SELECT
        u.id AS userId,
        u.gmt_create AS userGmtCreate,
        u.username AS userName,
        u.linkman AS linKman,
        u.email AS email,
        org.id AS orgId,
        org.gmt_create AS orgGmtCreate,
        org.name AS orgName,
        org.type as orgType,
        org.principal AS orgPrincipal,
        org.address as orgAddress,
        org.parent_id as orgParent_id,
        org.sub_num as orgSub_num,
        aut.position as autPosition,
        aut.is_reject as autIsReject,
        aut.status as autStatus
        FROM sys_user u
        LEFT JOIN sys_user_organization uorg ON u.id = uorg.user_id
        LEFT JOIN sys_organization org ON uorg.organization_id = org.id
        LEFT JOIN sys_user_authentication aut ON u.id = aut.user_id
        WHERE
        u.is_deleted = 'N'
        AND u.TYPE = "0"
        <if test="userExport.orgName != null">
            AND org.name LIKE CONCAT('%', #{userInfo.orgName}, '%')
        </if>
        <if test="userExport.autStatus != null">
            AND aut.status = #{userInfo.autStatus}
        </if>
        <if test="userExport.startTime != null and userExport.endTime != null">
            AND u.gmt_create BETWEEN #{userExport.startTime} and #{userExport.endTime}
        </if>
        ORDER BY u.gmt_create DESC
    </select>
</mapper>
