<?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.securitycontrol.background.mapper.UserProMapper">
    <insert id="addUserPro">
            insert  into  tb_user_pro(user_id ,bid_cod ,del_flag,times)
                        values(#{userId}, #{bidCode},0, now())
    </insert>
    <update id="deleteUserProByCode" >
        UPDATE   tb_user_pro SET del_flag=1 where user_id=#{userId}
             <if test="bidCode!=null and bidCode!=''">
                 and  bid_cod=#{bidCode}
             </if>
    </update>
    <!--查询待分配的用户-->
    <select id="getUserList" resultType="com.securitycontrol.entity.background.vo.UserProVo">
      select su.user_id userId,ANY_VALUE(user_name) userName,ANY_VALUE(dept_name) deptName,
              ANY_VALUE(login_name) loginName, ANY_VALUE(org_id) orgId,ANY_VALUE(org_name)  orgName,
		      ANY_VALUE(su.role_name)  roleName ,count(pro.pro_id) proNum
            from sys_user  su
            left join sys_role sr on sr.role_id=su.role_id
            left join tb_user_pro tup on su.user_id=tup.user_id and tup.del_flag=0
            LEFT JOIN tb_project pro on tup.bid_cod=pro.bid_code and pro.del_flag=0
            where su.del_flag=0   and sr.role_code=#{roleCode}
                <if test="keyWord!=null and keyWord!=''">
                    AND (
                    INSTR(su.user_name,#{keyWord}) > 0 OR
                    INSTR(su.login_name,#{keyWord}) > 0
                    )
                </if>
                <if test='orgId!=null and orgId!=""'>
                    AND   su.org_id =#{orgId}
                </if>
            group BY su.user_id
    </select>
    <select id="getFpProList" resultType="com.securitycontrol.entity.background.vo.UserProVo">
        select  pro_id ,pro.bid_code bidCode,pro_name bidName,pro.status,plan_start_time planStartTime,plan_end_time planEndTime,
            org orgId,now_gx ,jl_unit jlUnit,sg_unit sgUnit,
            pro_type proType,sb.city_name orgName,pro.manager
            FROM tb_project pro
            left join sys_build sb on pro.org=sb.org_id
            left join tb_user_pro tup  on tup.bid_cod=pro.bid_code  and tup.del_flag=0
            where pro.del_flag=0
                <if test="userId!=null and userId!=''">
                     and tup.user_id=#{userId}
                </if>
                <if test='orgId!=null and orgId!=""'>
                    AND   pro.org =#{orgId}
                </if>
                <if test='keyWord!=null and keyWord!=""'>
                    AND (
                    INSTR(pro.bid_code,#{keyWord}) > 0 OR
                    INSTR(pro.pro_name,#{keyWord}) > 0 OR
                    INSTR(pro.sg_unit,#{keyWord}) > 0 OR
                    INSTR(pro.manager,#{keyWord}) > 0 OR
                    INSTR(pro.jl_unit,#{keyWord}) > 0
                    )
                </if>
    </select>
    <select id="getProList" resultType="com.securitycontrol.entity.background.vo.UserProVo">
          select  pro_id,pro.bid_code bidCode,pro_name bidName,pro.status,plan_start_time planStartTime,plan_end_time planEndTime,
                        org orgId,now_gx,jl_unit jlUnit,sg_unit sgUnit,
                        pro_type proType,sb.city_name orgName,pro.manager
                    FROM tb_project pro
                    left join sys_build sb on pro.org=sb.org_id
                    left join tb_user_pro tup  on tup.bid_cod=pro.bid_code   and tup.del_flag=0
                WHERE    pro.del_flag=0  and (tup.user_id!=4 or   tup.user_id is null)
                <if test='type=="1"'>
                            AND   tup.user_id is null
                        </if>
                        <if test='type=="2"'>
                            AND   tup.user_id is  not null
                        </if>
                        <if test='orgId!=null and orgId!=""'>
                            AND   pro.org =#{orgId}
                        </if>
                        <if test='keyWord!=null and keyWord!=""'>
                            AND (
                            INSTR(pro.bid_code,#{keyWord}) > 0 OR
                            INSTR(pro.pro_name,#{keyWord}) > 0 OR
                            INSTR(pro.sg_unit,#{keyWord}) > 0 OR
                            INSTR(pro.manager,#{keyWord}) > 0 OR
                            INSTR(pro.jl_unit,#{keyWord}) > 0
                            )
                        </if>
    </select>
</mapper>