<?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.fibo.ddp.common.dao.datax.datamanage.FieldUserMapper">
    <cache></cache>
     <resultMap type="com.fibo.ddp.common.model.datax.datamanage.FieldUser" id="fieldUserMap">
	     <id column="id" property="id"/>    
	     <result column="field_id" property="fieldId"/>    
	     <result column="organ_id" property="organId"/>
	     <result column="engine_id" property="engineId"/>
	     <result column="user_id" property="userId"/>
	     <result column="status" property="status"/>
     </resultMap>
     
    <insert id="createFieldUserRel" useGeneratedKeys="true" keyProperty="id" parameterType="fieldUser">
	   insert into t_field_user_rel (field_id, organ_id, engine_id, user_id, status, created, updated)
       values (#{fieldId}, #{organId}, #{engineId}, #{userId}, #{status}, now(), now())
	</insert>
	
	<insert id="batchCreateFieldUserRel" parameterType="map">
	   insert into t_field_user_rel (field_id, organ_id, engine_id, user_id, status, created, updated)
       select id, #{organId}, #{engineId}, #{userId}, #{status}, now(), now()
         from t_field t
        where t.author = #{author}
          and not exists ( select r.field_id from t_field_user_rel r where t.id = r.field_id )
	</insert>

	
	<insert id="batchBindEngineFieldUserRel" parameterType="map">
	   insert into t_field_user_rel (field_id, organ_id, engine_id, user_id, status, created, updated)
       select id, #{organId}, #{engineId}, #{userId}, 1, now(), now()
         from t_field f
        where f.id in
              <foreach collection="fieldIds" item="item" index="index" open="(" separator="," close=")">  
                #{item}
              </foreach>
              and not exists ( select 1 
                                 from (	select f.id,f.field_en,f.field_cn 
								          from t_field f,t_field_user_rel fu 
										 where f.id = fu.field_id 
										       and fu.organ_id = ( select organ_id from t_user where user_id = #{userId} )
											   and fu.engine_id = ${engineId} )y
								where f.field_en = y.field_en or f.field_cn = y.field_cn or f.id = y.id
                              )
	</insert>

    
	<update id="updateStatus" parameterType="map">
       update t_field_user_rel
          set status=#{status} 
        where organ_id = ( select organ_id from t_user where user_id = #{userId} )
              <if test="engineId != null">
                and engine_id = #{engineId}
              </if>
              <if test="engineId == null and status!=-1 and status!=0">
                and engine_id is null
              </if>
              and field_id in
              <foreach collection="Ids" item="item" index="index" open="(" separator="," close=")">  
                #{item}
              </foreach>
    </update>

    
    <update id="backFieldByIds" parameterType="map">
       update t_field_user_rel
          set status = 1
        where organ_id = ( select organ_id from t_user where user_id = #{userId} )
              and engine_id = #{engineId}
              and field_id in
                  <foreach collection="Ids" item="item" index="index" open="(" separator="," close=")">  
                  #{item}
                  </foreach>
              and status = -1
    </update>
    <select id="countFieldByOrganId" parameterType="map" resultType="int">
        select count(distinct tfur.field_id) from t_field_user_rel tfur inner join t_field tf on tfur.field_id = tf.id where tfur.status = 1 and tfur.organ_id = #{organId}
    </select>
    <select id="countFieldGroupByType" parameterType="map" resultType="map">
        select tft.type  fieldType,count(distinct tfur.field_id)  fieldCount
        from t_field_user_rel tfur
        inner join t_field tf on tfur.field_id = tf.id
        inner join t_field_type tft on tf.field_typeid = tft.id
        where  tfur.status = 1 and tfur.organ_id = #{organId}
        GROUP BY tft.type
    </select>
</mapper>