<?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">
<!-- namespace必须指向DAO接口 -->
<mapper namespace="com.jpsoft.shinestar.modules.base.dao.CompanyDeviceRelationDAO">
    <resultMap id="CompanyDeviceRelationMap" type="com.jpsoft.shinestar.modules.base.entity.CompanyDeviceRelation">
        <id property="id" column="id_"/>
        <result property="companyId" column="company_id"/>
        <result property="deviceId" column="device_id"/>
        <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="delFlag" column="del_flag"/>
        <result property="libraryId" column="library_id"/>
    </resultMap>
    <insert id="insert" parameterType="com.jpsoft.shinestar.modules.base.entity.CompanyDeviceRelation">
        <!--
        <selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id">
            select sys_guid() from dual
        </selectKey>
        -->
        <![CDATA[
		insert into base_company_device_relation
	    (id_,company_id,device_id,create_by,create_time,update_by,update_time,del_flag,library_id)
		values
		(
			#{id,jdbcType=VARCHAR}
			,#{companyId,jdbcType=VARCHAR}
			,#{deviceId,jdbcType=VARCHAR}
			,#{createBy,jdbcType=VARCHAR}
			,#{createTime,jdbcType= TIMESTAMP }
			,#{updateBy,jdbcType=VARCHAR}
			,#{updateTime,jdbcType= TIMESTAMP }
			,#{delFlag,jdbcType= NUMERIC }
			,#{libraryId,jdbcType=VARCHAR}
		)
	]]>
    </insert>
    <delete id="delete" parameterType="string">
        delete from base_company_device_relation where id_=#{id,jdbcType=VARCHAR}
    </delete>
    <delete id="deleteByCompanyId" parameterType="string">
        delete from base_company_device_relation where company_id=#{0}
    </delete>
    <delete id="deleteByDeviceId" parameterType="string">
        delete from base_company_device_relation where device_id=#{0}
    </delete>
    <delete id="deleteByCompanyIdAndDeviceId" parameterType="string">
        delete from base_company_device_relation
        where company_id=#{companyId} and device_id=#{deviceId}
    </delete>
    <update id="update" parameterType="com.jpsoft.shinestar.modules.base.entity.CompanyDeviceRelation">
        update base_company_device_relation
        <set>
            <if test="companyId!=null">
                company_id=#{companyId,jdbcType=VARCHAR},
            </if>
            <if test="deviceId!=null">
                device_id=#{deviceId,jdbcType=VARCHAR},
            </if>
            <if test="createBy!=null">
                create_by=#{createBy,jdbcType=VARCHAR},
            </if>
            <if test="createTime!=null">
                create_time=#{createTime,jdbcType= TIMESTAMP },
            </if>
            <if test="updateBy!=null">
                update_by=#{updateBy,jdbcType=VARCHAR},
            </if>
            <if test="updateTime!=null">
                update_time=#{updateTime,jdbcType= TIMESTAMP },
            </if>
            <if test="delFlag!=null">
                del_flag=#{delFlag,jdbcType= NUMERIC },
            </if>
            <if test="libraryId!=null">
                library_id=#{libraryId,jdbcType= VARCHAR },
            </if>
        </set>
        where id_=#{id}
    </update>
    <select id="get" parameterType="string" resultMap="CompanyDeviceRelationMap">
        select
        id_,company_id,device_id,create_by,create_time,update_by,update_time,del_flag from base_company_device_relation
        where id_=#{0}
    </select>
    <select id="exist" parameterType="string" resultType="int">
        select count(*) from base_company_device_relation where id_=#{0}
    </select>
    <select id="list" resultMap="CompanyDeviceRelationMap">
        select * from base_company_device_relation
    </select>
    <select id="search" parameterType="hashmap" resultMap="CompanyDeviceRelationMap">
        <![CDATA[
			select * from base_company_device_relation
		]]>
        <where>
            <if test="searchParams.id != null">
                and ID_ like #{searchParams.id}
            </if>
        </where>
        <foreach item="sort" collection="sortList" open="order by" separator=",">
            ${sort.name} ${sort.order}
        </foreach>
    </select>
    <select id="findDeviceByCompanyId" resultMap="com.jpsoft.shinestar.modules.base.dao.DeviceInfoDAO.DeviceInfoMap">
        select b.* from base_company_device_relation a,base_device_info b
        where a.device_id=b.id_ and a.company_id=#{0} order by b.create_time asc
    </select>

    <select id="findByCompanyIdAndDeviceId" parameterType="string" resultMap="CompanyDeviceRelationMap">
        select * from base_company_device_relation
        where device_id=#{deviceId} and company_id=#{companyId}
    </select>

    <select id="findByCompanyId" parameterType="string" resultMap="CompanyDeviceRelationMap">
        select * from base_company_device_relation
        where company_id=#{companyId}
    </select>

    <select id="findByDeviceId" parameterType="string" resultMap="CompanyDeviceRelationMap">
        select * from base_company_device_relation
        where device_id=#{deviceId}
    </select>
</mapper>