<?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="cn.com.countrygarden.bi.main.dao.personnel.PersonnelTagMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.com.countrygarden.bi.main.model.personnel.PersonnelTag">
        <id column="id" property="id" />
        <result column="user_id" property="userId" />
        <result column="identity" property="identity" />
        <result column="graduates" property="graduates" />
        <result column="created_date" property="createdDate" />
        <result column="created_by" property="createdBy" />
        <result column="updated_date" property="updatedDate" />
        <result column="updated_by" property="updatedBy" />
    </resultMap>
    <sql id="sqlcoue">      
          <if test="userquyu!=null">
		    	
		        <foreach collection="userquyu" item="userquyu"
						index="index" open="(" close=")" separator="or">
					
						(t.Four_tissue like concat('%',#{userquyu},'%') or doc.PK_ORG  like concat('%',#{userquyu},'%')) 
					</foreach> 
						
		</if> 
		
		 <if test="rolesMap.identitys!=null">
					and pg.identity in
					<foreach collection="rolesMap.identitys" item="identitys"
						index="index" open="(" close=")" separator=",">
						#{identitys}
					</foreach>
			</if>   
     
     </sql>
    <select id="selectByNameOrIdentity"
            resultType="cn.com.countrygarden.bi.main.dto.personnelTagDTO">       
          <!--  select per.id id,per.user_id userId,per.name name,per.created_date createdDate,
          case when (t.Four_tissue is null) then doc.PK_ORG else t.Four_tissue end regionName from biapp.personnel per,
           performance_app.bd_psndoc doc left join 
           (select distinct Four_tissue,pk_org from performance_app.bd_type) t on t.pk_org=doc.PK_ORG
          where (select count(1) from biapp.personnel_tag tag where
          tag.user_id=per.user_id  )!=0 and doc.`CODE`=per.user_id -->
          
		 select distinct per.id id,per.user_id userId,per.name name,per.created_date createdDate,
          case when (t.Four_tissue is null) then doc.PK_ORG else t.Four_tissue end regionName 
          from biapp.personnel per 
          left join performance_app.bd_psndoc doc on doc.`CODE`=per.user_id
          left join (select distinct Four_tissue,pk_org from performance_app.bd_type) t on t.pk_org=doc.PK_ORG
          left join biapp.personnel_tag pg on pg.user_id=per.user_id
		  <where>
		  <include refid="sqlcoue"/> 
        <if test="obj.name != null and obj.name != ''">
            and per.name like concat('%',#{obj.name},'%')
        </if>
        <if test="obj.userId != null and obj.userId != ''">
            and per.user_id like concat('%',#{obj.userId},'%')
        </if>
        <if test="obj.regionName!=null and obj.regionName!=''">			
			and (t.Four_tissue like concat('%',#{obj.regionName},'%') or doc.PK_ORG like concat('%',#{obj.regionName},'%'))
			
		</if>
        
          <!-- rolesMap.role!=null没有相应的角色 	-->
		 <if test="rolesMap.role!=null">
			and per.id=-1
		</if>
		
		
		
		 <!-- rolesMap.gj_admin！=null管理员，有所有权限 -->
		 <!--<if test="rolesMap.gj_admin==null and rolesMap.role==null">
			rolesMap.gj_Area！=null区域管理员，只能查看登录账号区域的下的数据
			<if test="rolesMap.gj_Area!=null">
				and doc.code = #{rolesMap.gj_Area}
				  <if test="rolesMap.identitys!=null">
					or tag.identity in
					<foreach collection="rolesMap.identitys" item="identitys"
						index="index" open="(" close=")" separator=",">
						#{identitys}
					</foreach>
			</if>  

			 
			
		</if> -->
		
		</where>
        order by createdDate desc
    </select>
    
    <select id="findErpRegionByUserquyu" resultType="java.lang.String">
    
     SELECT DISTINCT CASE WHEN (t.Four_tissue IS NULL) THEN doc.PK_ORG ELSE t.Four_tissue
       END regionName FROM performance_app.bd_psndoc doc 
      LEFT JOIN (SELECT DISTINCT Four_tissue,pk_org FROM performance_app.bd_type) t ON t.pk_org = doc.PK_ORG
     WHERE 1 = 1 AND doc.CODE= #{userCode}
    </select>

    <select id="personneltag" resultType="cn.com.countrygarden.bi.main.dto.PersonnelTgDTO" parameterType="java.lang.String">
        select pg.id as personneltagId, pg.user_id as userId,pg.identity as identity,p.id as personnelId from personnel_tag pg,personnel p
        where pg.user_id=p.user_id
        <if test="userId!='' and userId!=null">
            and pg.user_id=#{userId}
        </if>
        <if test="identity!='' and identity!=null">
            and pg.identity=#{identity}
        </if>
    </select>
    <select id="selectDTOByPrimaryKey" resultType="cn.com.countrygarden.bi.main.dto.personnelTagDTO">
       <!--  select per.id as id,per.user_id as userId,per.name as name,t.Four_tissue as regionName from biapp.personnel per,performance_app.bd_psndoc doc,performance_app.bd_type t
         where doc.`CODE`=per.user_id and t.pk_org=doc.PK_ORG  -->
         
          select per.id id,per.user_id userId,per.name name,per.created_date createdDate,
          case when (t.Four_tissue is null) then doc.PK_ORG else t.Four_tissue end regionName from biapp.personnel per,
           performance_app.bd_psndoc doc left join 
           (select distinct Four_tissue,pk_org from performance_app.bd_type) t on t.pk_org=doc.PK_ORG
          where (select count(1) from biapp.personnel_tag tag where
          tag.user_id=per.user_id)!=0 and doc.`CODE`=per.user_id and per.id=#{id}
    </select>
    
       <select id="personneltaguserid" resultType="cn.com.countrygarden.bi.main.dto.personnelTagDTO">
        select id,user_id userId,`name` FROM personnel per WHERE user_id=#{userId}
    </select>

    <select id="selectByPersonInfoByCodeOrName" resultType="cn.com.countrygarden.bi.main.dto.PersonInfoVO">
       <!--  select distinct d.BID,d.`CODE`,d.`NAME`,w.pk_dept WORKDEPT,p.id personnelId from personnel p,performance_app.bd_psndoc d,performance_app.hi_psnjob w
        where d.PK_PSNDOC = w.PK_PSNDOC and p.user_id=d.`CODE` and w.endflag='N' and w.lastflag='Y' and w.ismainjob='Y' -->
       select distinct doc.BID,doc.`CODE`,doc.`NAME`,w.pk_dept WORKDEPT,p.id personnelId 
          from biapp.personnel p
          left join biapp.personnel_tag pg on pg.user_id=p.user_id
          left join performance_app.bd_psndoc doc on p.user_id=doc.`CODE`
          left join performance_app.hi_psnjob w on doc.PK_PSNDOC = w.PK_PSNDOC
          left join (select distinct Four_tissue,pk_org from performance_app.bd_type) t on t.pk_org=doc.PK_ORG 
          <where> 
          
          
          and  w.endflag='N' and w.lastflag='Y' and w.ismainjob='Y'
          <if test="userquyu!=null">
		    	and
		       (<foreach collection="userquyu" item="userquyu"
						index="index" open="(" close=")" separator="or">
					
						(t.Four_tissue like concat('%',#{userquyu},'%') or doc.PK_ORG  like concat('%',#{userquyu},'%')) 
					</foreach>)
						
		</if> 
        <if test="search != null and search != ''">
            and ( doc.`NAME` like concat('%', #{search}, '%') or doc.`CODE`  =  #{search} )
        </if>
        <!-- rolesMap.role!=null没有相应的角色 -->
		<if test=" rolesMap.role!=null">
			and p.id=-1
		</if>
		
		 
		
		<!-- rolesMap.gj_admin！=null管理员，有所有权限 -->
	<!-- 	<if test="rolesMap.gj_admin==null and rolesMap.role==null">
			rolesMap.gj_Area！=null区域管理员，只能查看登录账号区域的下的数据
			<if test="rolesMap.gj_Area!=null">
				and d.code = #{rolesMap.gj_Area}
				 <if test="rolesMap.identitys!=null">
					or pg.identity in
					<foreach collection="rolesMap.identitys" item="identitys"
						index="index" open="(" close=")" separator=",">
						#{identitys}
					</foreach>
			</if>

			 <if test="rolesMap.gj_Area==null">
				<if test="rolesMap.identitys!=null">
					and pg.identity in
					<foreach collection="rolesMap.identitys" item="identitys"
						index="index" open="(" close=")" separator=",">
						#{identitys}
					</foreach>
				</if>
			</if> 
			
		</if>
		
		</if> -->
        </where>
        limit 0,20

    </select>

    <select id="tagid" resultType="cn.com.countrygarden.bi.main.dto.PersonnelpascTagidDTO" parameterType="java.lang.Integer">
             select pa.personneltag_id as patagid,pc.personneltag_id as pctagid,ps.personnel_tag_id as pstagid,s.personneltag_id as stagid from personnel_tag t 
             left join personnel_advisor pa on pa.personneltag_id=t.id and pa.flag=0
             left join personnel_campusevaluation pc on pc.personneltag_id=t.id and pc.flag=0
             left join personnel_selectionscore ps on ps.personnel_tag_id=t.id and ps.flag=0
             left join personnel_sharppathevaluation s on s.personneltag_id=t.id and s.flag=0
             where t.id=#{id}
    
    </select>
    
    <select id="selectdeleperid" resultType="cn.com.countrygarden.bi.main.dto.PersonnelpascTagidDTO" parameterType="java.lang.String">
             select pa.personnel_id as papersonnelid,pc.personnel_id as pcpersonnelid,ps.personnel_id as pspersonnelid,s.personnel_id as spersonnelid from personnel p
             left join personnel_advisor pa on pa.personnel_id=p.id and pa.flag=0
             left join personnel_campusevaluation pc on pc.personnel_id=p.id and pc.flag=0
             left join personnel_selectionscore ps on ps.personnel_id=p.id and ps.flag=0
             left join personnel_sharppathevaluation s on s.personnel_id=p.id and s.flag=0
             where p.user_id=#{userId}
    </select>

    <delete id="deleteuserid" parameterType="java.lang.String">
        
        delete from personnel_tag where user_id=#{userId};
    
    </delete>

    <insert id="inserttag" parameterType="cn.com.countrygarden.bi.main.model.personnel.PersonnelTag">
        insert into personnel_tag(id,user_id,identity,graduates,created_date,created_by)
       values(#{id},#{userId},#{identity},#{graduates},#{createdDate},#{createdBy});
    
    </insert>
    
     <select id="findErpUserCode" resultType="String" parameterType="String">
    
    select user_id from personnel_tag where 1=1
    <if test="identitys!=null">
    and
    identity in 
    <foreach collection="identitys" item="identitys" index="index" open="(" close=")" separator=",">
    
    #{identitys}
    </foreach>
    </if>
     <if test="userIds!=null">
    and
    user_id in 
    <foreach collection="userIds" item="userIds" index="index" open="(" close=")" separator=",">
    
    #{userIds}
    </foreach>
    </if>
  
    </select>
    
    <delete id="delpingliang">
           delete from personnel 
           <where>
           <if test="uertid!=null">
               user_id in
           <foreach collection="uertid" item="uertid" index="index" open="(" close=")" separator=",">
    
                #{uertid}
           </foreach>
           
           </if>
           
           </where>
           
    </delete>
    
        <delete id="delpingliangtag">
           delete from personnel_tag 
           <where>
           <if test="uertid!=null">
               user_id in
           <foreach collection="uertid" item="uertid" index="index" open="(" close=")" separator=",">
    
                #{uertid}
           </foreach>
           
           </if>
           
           </where>
           
    </delete>

</mapper>