<?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.yanzhu.jh.project.mapper.SurProjectUserinfoMapper">
    
    <resultMap type="SurProjectUserinfo" id="SurProjectUserinfoResult">
        <result property="id"    column="id"    />
        <result property="projectId"    column="project_id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
        <result property="deptType"    column="dept_type"    />
        <result property="jobType"    column="job_type"    />
        <result property="jobTypeName"    column="job_type_name"    />
        <result property="userId"    column="user_id"    />
        <result property="isDel"    column="is_del"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="projectName"    column="projectName"    />
        <result property="nickName"    column="nick_name"    />
        <result property="phonenumber"    column="phonenumber"    />
        <result property="deptTypeFlag"    column="dept_type_flag"    />
        <result property="deptAncestors"    column="deptAncestors"    />
    </resultMap>

    <sql id="selectSurProjectUserinfoVo">
        select id, project_id, dept_id, dept_name, dept_type, job_type, user_id, is_del, create_by, create_time, update_by, update_time, remark from sur_project_userinfo
    </sql>

    <select id="selectSurProjectUserinfoList" parameterType="SurProjectUserinfo" resultMap="SurProjectUserinfoResult">
        select spu.id, spu.project_id, spu.dept_id, spu.dept_name, spu.dept_type, spu.job_type, sdd.dict_label as job_type_name, spu.user_id, spu.is_del, spu.create_by, spu.create_time, spu.update_by, spu.update_time, spu.remark, sp.projectName, su.nick_name, su.phonenumber from sur_project_userinfo spu
        left join sur_project sp on spu.project_id = sp.id
        left join sys_user su on spu.user_id = su.user_id
        left join sys_dict_data sdd on sdd.dict_type='sys_job_type' and spu.job_type = sdd.dict_value
        <!--监理单位/总包公司/分包单位-->
        <where>
            and spu.is_del=0
            <if test="projectId != null "> and spu.project_id = #{projectId}</if>
            <if test="projectName != null "> and sp.projectName like concat('%', #{projectName}, '%')</if>
            <if test="nickName != null "> and (su.nick_name like concat('%', #{nickName}, '%') or su.phonenumber like concat('%', #{nickName}, '%') )</if>
            <if test="deptId != null "> and spu.dept_id = #{deptId}</if>
            <if test="deptName != null  and deptName != ''"> and spu.dept_name like concat('%', #{deptName}, '%')</if>
            <if test="deptType != null  and deptType != ''"> and spu.dept_type = #{deptType}</if>
            <if test="jobType != null  and jobType != ''"> and spu.job_type = #{jobType}</if>
            <if test="userId != null "> and spu.user_id = #{userId}</if>
            <if test="isDel != null "> and spu.is_del = #{isDel}</if>
            <!-- 查询条件-项目部门 -->
            <if test="projectDeptId != null "> and sp.deptId = #{projectDeptId}</if>
            <!--子部门数据-->
            <if test='nowRole == "4"'> and sp.deptId = #{nowDept}</if>
            <!--监理单位/总包公司/分包单位查询当前关联数据-->
            <if test='nowRole == "5" or nowRole == "6" or nowRole == "7"'> and sp.id in (select spui.projectId from sur_project_unit_info spui where spui.unitId = #{nowDept})</if>
            <if test='nowRole == "15" or nowRole == "16" or nowRole == "17" or nowRole == "99"'> and sp.id in (select spu.project_id from sur_project_userinfo spu where spu.user_id = #{nowUser} and spu.is_del=0)</if>
            <if test="createBy != null  and createBy != ''"> and su.phonenumber != #{createBy}</if>
        </where>
    </select>

    <select id="selectProjectDeptUserinfoList" parameterType="SurProjectUserinfo" resultMap="SurProjectUserinfoResult">
        select spu.id, spu.project_id, spu.dept_id, spu.dept_name, spu.dept_type, spu.job_type, sdd.dict_label as job_type_name, spu.user_id, spu.is_del, spu.create_by, spu.create_time, spu.update_by, spu.update_time, spu.remark, sp.projectName, su.nick_name, su.phonenumber, sd.type_flag as dept_type_flag, sd.ancestors as deptAncestors from
        sys_user su
        left join sur_project_userinfo spu on spu.user_id = su.user_id
        left join sys_dept sd on sd.dept_id = su.dept_id
        left join sur_project sp on spu.project_id = sp.id
        left join sys_dict_data sdd on sdd.dict_type='sys_job_type' and spu.job_type = sdd.dict_value
        <where>
            and spu.is_del=0
            and su.user_id not in (select spuuu.user_id from sur_project_userinfo spuuu where spuuu.project_id = #{projectId})
            <if test="deptId != null "> and (su.dept_id = #{deptId} OR su.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE find_in_set(#{deptId}, ancestors) ))</if>
            <if test="createBy != null  and createBy != ''"> and su.phonenumber != #{createBy}</if>
        </where>
        group by su.phonenumber
    </select>

    <!-- 查询项目用户信息 -->
    <select id="querySurProjectUserinfoByParams" parameterType="SurProjectUserinfo" resultMap="SurProjectUserinfoResult">
        select spu.id,spu.project_id,spu.dept_id,spu.dept_name,spu.dept_type,spu.job_type,spu.user_id,spu.is_del from sur_project_userinfo spu
        left join sys_user su on su.user_id=spu.user_id
        where su.del_flag=0 and spu.is_del=0 and spu.dept_type=11 and spu.project_id=#{projectId} and su.phonenumber=#{createBy}
    </select>

    <select id="selectSurProjectUserinfoById" parameterType="Long" resultMap="SurProjectUserinfoResult">
        <include refid="selectSurProjectUserinfoVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertSurProjectUserinfo" parameterType="SurProjectUserinfo">
        insert into sur_project_userinfo
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="projectId != null">project_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="deptName != null">dept_name,</if>
            <if test="deptType != null">dept_type,</if>
            <if test="jobType != null">job_type,</if>
            <if test="userId != null">user_id,</if>
            <if test="isDel != null">is_del,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="deptName != null">#{deptName},</if>
            <if test="deptType != null">#{deptType},</if>
            <if test="jobType != null">#{jobType},</if>
            <if test="userId != null">#{userId},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateSurProjectUserinfo" parameterType="SurProjectUserinfo">
        update sur_project_userinfo
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="deptName != null">dept_name = #{deptName},</if>
            <if test="deptType != null">dept_type = #{deptType},</if>
            <if test="jobType != null">job_type = #{jobType},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="deleteSurProjectUserinfoById" parameterType="Long">
        update sur_project_userinfo set is_del=1 where id = #{id}
    </update>

    <update id="deleteSurProjectUserinfoByIds" parameterType="String">
        update sur_project_userinfo set is_del=1 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <resultMap type="SysUser" id="SysUserResult">
        <id     property="userId"       column="user_id"      />
        <result property="deptId"       column="dept_id"      />
        <result property="userName"     column="user_name"    />
        <result property="nickName"     column="nick_name"    />
        <result property="email"        column="email"        />
        <result property="phonenumber"  column="phonenumber"  />
        <result property="sex"          column="sex"          />
        <result property="avatar"       column="avatar"       />
        <result property="password"     column="password"     />
        <result property="status"       column="status"       />
        <result property="delFlag"      column="del_flag"     />
        <result property="loginIp"      column="login_ip"     />
        <result property="loginDate"    column="login_date"   />
        <result property="createBy"     column="create_by"    />
        <result property="createTime"   column="create_time"  />
        <result property="updateBy"     column="update_by"    />
        <result property="updateTime"   column="update_time"  />
        <result property="remark"       column="remark"       />
    </resultMap>

    <select id="selectProjectUserList" parameterType="SurProject" resultMap="SysUserResult">
        select u.user_id, u.dept_id, u.nick_name, u.user_name, u.email, u.avatar, u.phonenumber, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark, d.dept_name, d.leader from sys_user u
        left join sys_dept d on u.dept_id = d.dept_id
        where u.del_flag = '0'
        and u.user_id not in (select spu.user_id from sur_project_userinfo spu where spu.is_del=0 and spu.project_id = #{id} and spu.dept_id =#{deptId} )
        <if test="deptId != null and deptId != 0">
            AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE find_in_set(#{deptId}, ancestors) ))
        </if>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>

</mapper>