<?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.zcxk.dao.UserMapper">
    <resultMap type="UserEntity" id="UserResult">
       <result property="id"    column="id"    />
       <result property="tenantId"    column="tenant_id"    />
       <result property="username"    column="username"    />
       <result property="phone"    column="phone"    />
       <result property="companyOrgId"    column="company_org_id"    />
       <result property="deptOrgId"    column="dept_org_id"    />
       <result property="photo"    column="photo"    />
       <result property="userType"    column="user_type"    />
       <result property="enableState"    column="enable_state"    />
       <result property="remark"    column="remark"    />
       <result property="email"    column="email"    />
       <result property="status"    column="status"    />
       <result property="dateCreate"    column="date_create"    />
       <result property="createBy"    column="create_by"    />
       <result property="dateUpdate"    column="date_update"    />
       <result property="updateBy"    column="update_by"    />
    </resultMap>
    
    <!--  实体栏位  --> 
    <sql id="userColumns">
         a.id as "id" ,
         a.tenant_id as "tenantId" ,
         a.username as "username" ,
         a.phone as "phone" ,
         a.company_org_id as "companyOrgId" ,
         a.dept_org_id as "deptOrgId" ,
         a.photo as "photo" ,
         a.user_type as "userType" ,
         a.enable_state as "enableState" ,
         a.remark as "remark" ,
         a.email as "email" ,
         a.status as "status" ,
         a.date_create as "dateCreate" ,
         a.create_by as "createBy" ,
         a.date_update as "dateUpdate" ,
         a.update_by as "updateBy",
         com.org_name as "companyOrgName" ,
         dep.org_name as "deptOrgName",
         userrole.ROLE_ID as roleId,
         role.role_name as "roleName"
     </sql>
    <!-- 外联表  -->
     <sql id="sysAreaJoins">
          left join uims_org com ON com.id = a.company_org_id
          left join uims_org dep ON dep.id = a.dept_org_id
          left join uims_user_role userrole ON userrole.USER_ID=a.id and  userrole.status=1
          left join uims_role role on role.id=userrole.ROLE_ID
     </sql>

    <!--  根据主键获取实体   -->
     <select id="findUserById"  resultType="com.zcxk.entity.UserEntity" >
          SELECT 
			<include refid="userColumns"/>
		FROM  uims_user a
		<include refid="sysAreaJoins"/>
		WHERE a.id = #{id} and  a.status=1
    </select>
     <!--  根据获取实体List   -->
     <select id="findList" resultType="com.zcxk.entity.UserEntity">
		SELECT 
			<include refid="userColumns"/>
		FROM uims_user a
         <include refid="sysAreaJoins"/>
		<where>
            a.status=1
            <if test="tenantId != null  and tenantId != ''"> and a.tenant_id = #{tenantId} </if>
            <if test="username != null  and username != ''">
                     and a.username LIKE  concat('%',#{username},'%')
			</if>
             <if test="phone != null  and phone != ''">
                     and a.phone like concat('%', #{phone},'%')
             </if>
		</where>
		order by a.date_create desc
	 </select>
	  <!--  根据获取实体 page   -->
     <select id="findPage" resultType="com.zcxk.entity.UserEntity">
		SELECT 
			<include refid="userColumns"/>
		FROM uims_user a
		<include refid="sysAreaJoins"/>
		<where>
            a.status=1 and a.user_type != -9999
            <if test="user.tenantId != null  and user.tenantId != ''"> and a.tenant_id = #{user.tenantId} </if>
            <if test="user.username != null  and user.username != ''">
                   and (a.username LIKE  concat('%',#{user.username},'%')
                      or a.phone like concat('%', #{user.phone},'%'))
			</if>
            <if test="user.deptOrgId != null  and user.deptOrgId != ''">
                 and (a.DEPT_ORG_ID = #{user.deptOrgId}
                     or a.COMPANY_ORG_ID = #{user.companyOrgId}
                  )
            </if>
            <if test="user.userType!=null and user.userType!=-999 and user.userType!=-9999 and  user.programItems != null and user.programItems.size() > 0">
               <if test="user.permissonType == 5 or user.permissonType == 2">
                and ( a.DEPT_ORG_ID in
                       <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                           #{item.orgId}
                       </foreach>
                       or
                     a.COMPANY_ORG_ID in
                        <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                            #{item.orgId}
                        </foreach>
                )
               </if>
                <if test="user.permissonType == 4 or user.permissonType == 3">
                    and a.DEPT_ORG_ID in
                    <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                </if>
                <if test="user.permissonType == 1">
                    and a.COMPANY_ORG_ID in
                    <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
                </if>
           </if>
		</where>
        order by a.date_create desc
	 </select>
    <select id="getUserIdsByOrgId" resultType="int">
        select distinct id from uims_user a where status=1 and (
        a.DEPT_ORG_ID in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        or
        (a.DEPT_ORG_ID is null and
        a.COMPANY_ORG_ID in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
            )
            )
    </select>
    <select id="findUserIdsByOrgId"  resultType="com.zcxk.entity.UserEntity">
        SELECT
        <include refid="userColumns"/>
        FROM uims_user a
        <include refid="sysAreaJoins"/>
        where a.status=1 and (
        a.DEPT_ORG_ID in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        or
        a.COMPANY_ORG_ID in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        )
    </select>
    <select id="findUserIdsByOrgIdAndType"  resultType="com.zcxk.entity.UserEntity">
        SELECT
        <include refid="userColumns"/>
        FROM uims_user a
        <include refid="sysAreaJoins"/>
        where a.status=1 and (
        a.DEPT_ORG_ID in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        or
        a.COMPANY_ORG_ID in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        )
        and a.user_type=3
    </select>
    <select id="findUserList" resultType="com.zcxk.entity.UserEntity">
        select a.id,a.DEPT_ORG_ID,a.COMPANY_ORG_ID,
               a.USERNAME,company.ORG_NAME
                   companyOrgName,department.ORG_NAME deptOrgName from uims_user a
            left join uims_org company on a.COMPANY_ORG_ID=company.ID
            left join uims_org  department on a.DEPT_ORG_ID=department.ID
            <where>
                a.status=1 and
                <if test="type!=null">
                    a.user_type != 9999
                </if>
                <if test="type==null">
                    a.user_type = 3
                </if>
                <if test="user.tenantId != null  and user.tenantId != ''"> and a.tenant_id = #{user.tenantId} </if>

                <if test="user.deptOrgId != null  and user.deptOrgId != ''">
                    and (a.DEPT_ORG_ID = #{user.deptOrgId}
                    or a.COMPANY_ORG_ID = #{user.companyOrgId}
                    )
                </if>
                <if test="user.userType!=null and user.userType!=-999 and user.userType!=-9999 and  user.programItems != null and user.programItems.size() > 0">
                    <if test="user.permissonType == 5 or user.permissonType == 2">
                        and ( a.DEPT_ORG_ID in
                        <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                            #{item.orgId}
                        </foreach>
                        or
                        a.COMPANY_ORG_ID in
                        <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                            #{item.orgId}
                        </foreach>
                        )
                    </if>
                    <if test="user.permissonType == 4 or user.permissonType == 3">
                        and a.DEPT_ORG_ID in
                        <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                            #{item.orgId}
                        </foreach>
                    </if>
                    <if test="user.permissonType == 1">
                        and a.COMPANY_ORG_ID in
                        <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                            #{item.orgId}
                        </foreach>
                        and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
                    </if>
                </if>
            </where>

    </select>
    <select id="findAllUserListByType" resultType="com.zcxk.entity.UserEntity">
        select a.id,a.DEPT_ORG_ID,a.COMPANY_ORG_ID,a.TENANT_ID,
        a.USERNAME,company.ORG_NAME
        companyOrgName,department.ORG_NAME deptOrgName,
        a.phone phone
        from uims_user a
        left join uims_org company on a.COMPANY_ORG_ID=company.ID
        left join uims_org  department on a.DEPT_ORG_ID=department.ID
        <where>
            a.status=1 and a.user_type = #{user.userType}
            <if test="user.tenantId != null  and user.tenantId != ''"> and a.tenant_id = #{user.tenantId} </if>
            <if test="user.companyOrgId != null  and user.companyOrgId != ''">
                and a.COMPANY_ORG_ID = #{user.companyOrgId}
            </if>
        </where>
    </select>
    <select id="findUserListByType" resultType="com.zcxk.entity.UserEntity">
        select a.id,a.DEPT_ORG_ID,a.COMPANY_ORG_ID,
        a.USERNAME,company.ORG_NAME
        companyOrgName,department.ORG_NAME deptOrgName,
        a.phone phone
        from uims_user a
        left join uims_org company on a.COMPANY_ORG_ID=company.ID
        left join uims_org  department on a.DEPT_ORG_ID=department.ID
        <where>
            a.status=1 and a.user_type = #{user.userType}
            <if test="user.tenantId != null  and user.tenantId != ''"> and a.tenant_id = #{user.tenantId} </if>

            <if test="user.deptOrgId != null  and user.deptOrgId != ''">
                and (a.DEPT_ORG_ID = #{user.deptOrgId}
                or a.COMPANY_ORG_ID = #{user.companyOrgId}
                )
            </if>
            <if test="user.userType!=null and user.userType!=-999 and user.userType!=-9999 and  user.programItems != null and user.programItems.size() > 0">
                <if test="user.permissonType == 5 or user.permissonType == 2">
                    and ( a.DEPT_ORG_ID in
                    <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    or
                    a.COMPANY_ORG_ID in
                    <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    )
                </if>
                <if test="user.permissonType == 4 or user.permissonType == 3">
                    and a.DEPT_ORG_ID in
                    <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                </if>
                <if test="user.permissonType == 1">
                    and a.COMPANY_ORG_ID in
                    <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
                </if>
            </if>
        </where>

    </select>
    <select id="findUserIdsByUserIds"  resultType="com.zcxk.entity.UserEntity">
        SELECT
        <include refid="userColumns"/>
        FROM uims_user a
        <include refid="sysAreaJoins"/>
        where a.status=1 and
        a.id in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>
    <select id="findUserIdsForOrg"  resultType="com.zcxk.entity.UserEntity">
        SELECT
        <include refid="userColumns"/>
        FROM uims_user a
        <include refid="sysAreaJoins"/>
        where a.status=1
        <if test="compayId != null and compayId != '' and deptId != null and deptId != ''">
            and ((
               a.COMPANY_ORG_ID = #{compayId}
                <if test="deptId != null and deptId != ''">
                   and (a.DEPT_ORG_ID in
                    <foreach collection="ids" item="item" open="(" close=")" separator=",">
                        #{item}
                    </foreach> or a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
                </if>
            )
           or(
                a.COMPANY_ORG_ID in
                <foreach collection="compids" item="item" open="(" close=")" separator=",">
                  #{item}
                </foreach>
            )
            )
        </if>
        <if test="compayId != null and compayId != '' and (deptId == null or deptId ==0 )">
            and (
            (
                a.COMPANY_ORG_ID = #{compayId}
                and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
            )
            or(
                a.COMPANY_ORG_ID in
                    <foreach collection="compids" item="item" open="(" close=")" separator=",">
                        #{item}
                    </foreach>
                )
            )
        </if>
    </select>

    <select id="findUserIdsByPermissonOrg" resultType="java.lang.Integer">
        select
        distinct
        u.id
        from uims_user u
        left join uims_user_role ur on u.id=ur.USER_ID
        left join uims_role r on ur.ROLE_ID=r.id
        left join uims_role_org ro on ro.ROLE_ID = r.ID
        where u.TENANT_ID =#{tenantId}
        and (
        u.USER_TYPE='-999'
        <if test="deptOrgId != null ">
            or (r.PERMISSION_TYPE = 5 and ro.ORG_ID in (#{companyOrgId},#{deptOrgId}))
            or (r.PERMISSION_TYPE = 3 and u.DEPT_ORG_ID = #{deptOrgId})
            <if test="deptOrgIds != null and deptOrgIds.size()>0">
                or (r.PERMISSION_TYPE = 4 and u.DEPT_ORG_ID in
                <foreach collection="deptOrgIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
                )
            </if>
        </if>
        <if test="deptOrgId == null ">
            or (r.PERMISSION_TYPE = 5 and ro.ORG_ID = #{companyOrgId})
            or (r.PERMISSION_TYPE = 1 and u.COMPANY_ORG_ID = #{companyOrgId})
        </if>

        <if test="companyOrgIds != null and companyOrgIds.size()>0">
            or (r.PERMISSION_TYPE = 2 and u.COMPANY_ORG_ID in
            <foreach collection="companyOrgIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
            )
        </if>

        )

    </select>
    <select id="findMaintainer" resultType="java.lang.Integer">
        select count(1) maintainerCount
        from uims_user a
        <where>
            a.status=1 and a.user_type = 3
            <if test="user.dateCreate != null"> and a.date_create &lt; #{user.dateCreate} </if>
            <if test="user.tenantId != null  and user.tenantId != ''"> and a.tenant_id = #{user.tenantId} </if>

            <if test="user.deptOrgId != null  and user.deptOrgId != ''">
                and (a.DEPT_ORG_ID = #{user.deptOrgId}
                or a.COMPANY_ORG_ID = #{user.companyOrgId}
                )
            </if>
            <if test="user.userType!=-999 and user.userType!=-9999 and  user.programItems != null and user.programItems.size() > 0">
                <if test="user.permissonType == 5 or user.permissonType == 2">
                    and ( a.DEPT_ORG_ID in
                    <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    or
                    a.COMPANY_ORG_ID in
                    <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    )
                </if>
                <if test="user.permissonType == 4 or user.permissonType == 3">
                    and a.DEPT_ORG_ID in
                    <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                </if>
                <if test="user.permissonType == 1">
                    and a.COMPANY_ORG_ID in
                    <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
                </if>
            </if>

        </where>

    </select>
</mapper>