<?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.FieldTypeUserMapper">
    <cache></cache>
    <resultMap type="com.fibo.ddp.common.model.datax.datamanage.FieldTypeUser" id="fieldTypeUserMap">
        <id column="id" property="id"/>
        <result column="field_typeid" property="fieldTypeId"/>
        <result column="organ_id" property="organId"/>
        <result column="engine_id" property="engineId"/>
        <result column="user_id" property="userId"/>
    </resultMap>

    <insert id="createFieldTypeUserRel" useGeneratedKeys="true" keyProperty="id" parameterType="fieldTypeUser">
	   insert into t_field_type_user_rel ( field_typeid, organ_id, engine_id, user_id, created )
                                  values ( #{fieldTypeId}, #{organId}, #{engineId}, #{userId}, now() )
	</insert>

    <insert id="batchBindEngineFieldTypeUserRel" parameterType="map">
        insert into t_field_type_user_rel ( field_typeid, organ_id, engine_id, user_id, created )
        select field_typeid, organ_id, #{engineId}, #{userId}, now()
        from t_field_type_user_rel r
        where r.field_typeid in
        <foreach collection="fieldTypeIds" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
        and field_typeid not in ( select field_typeid from t_field_type_user_rel where engine_id = #{engineId})
        and engine_id is null
    </insert>

    <update id="updateFieldTypeUserRel">
        update t_field_type_user_rel
        set user_id = #{userId}, created = now()
        <if test="status != null">
            ,status = #{status}
        </if>
        where organ_id =#{organId}
        and field_typeid = #{id}
    </update>

</mapper>