<?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.dsk.system.mapper.BusinessRelateCompanyMapper">

    <resultMap type="com.dsk.common.core.domain.entity.BusinessRelateCompany" id="BusinessRelateCompanyResult">
        <result property="id" column="id"/>
        <result property="businessId" column="business_id"/>
        <result property="companyId" column="company_id"/>
        <result property="companyName" column="company_name"/>
        <result property="companyRole" column="company_role"/>
        <result property="responsiblePerson" column="responsible_person"/>
        <result property="phone" column="phone"/>
        <result property="isProprietor" column="is_proprietor"/>
        <result property="remark" column="remark"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="depth" column="depth"/>
        <result property="companyUipId" column="company_uip_id"/>
    </resultMap>

    <sql id="selectBusinessRelateCompanyVo">
        select id,
               business_id,
               company_id,
               company_name,
               company_role,
               responsible_person,
               phone,
               depth,
               is_proprietor,
               remark,
               create_time,
               update_time,
               company_uip_id
        from business_relate_company
    </sql>

    <select id="selectBusinessRelateCompanyList" parameterType="com.dsk.common.core.domain.entity.BusinessRelateCompany"
            resultMap="BusinessRelateCompanyResult">
        <include refid="selectBusinessRelateCompanyVo"/>
        <where>
            <if test="businessId != null ">and business_id = #{businessId}</if>
            <if test="companyId != null ">and company_id = #{companyId}</if>
            <if test="companyName != null  and companyName != ''">and company_name like concat('%', #{companyName},
                '%')
            </if>
            <if test="companyRole != null  and companyRole != ''">and company_role = #{companyRole}</if>
            <if test="depth != null  and depth != ''">and depth = #{depth}</if>
            <if test="responsiblePerson != null  and responsiblePerson != ''">and responsible_person =
                #{responsiblePerson}
            </if>
            <if test="phone != null  and phone != ''">and phone = #{phone}</if>
        </where>
    </select>

    <select id="selectBusinessRelateCompanyById" parameterType="Long" resultMap="BusinessRelateCompanyResult">
        <include refid="selectBusinessRelateCompanyVo"/>
        where id = #{id}
    </select>

    <insert id="insertBusinessRelateCompany" parameterType="com.dsk.common.core.domain.entity.BusinessRelateCompany" useGeneratedKeys="true"
            keyProperty="id">
        insert into business_relate_company
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="businessId != null">business_id,</if>
            <if test="companyId != null">company_id,</if>
            <if test="companyName != null">company_name,</if>
            <if test="depth != null">depth,</if>
            <if test="companyRole != null">company_role,</if>
            <if test="responsiblePerson != null">responsible_person,</if>
            <if test="phone != null">phone,</if>
            <if test="isProprietor != null">is_proprietor,</if>
            <if test="remark != null and remark != '' ">remark,</if>
            <if test="companyUipId != null">company_uip_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="businessId != null">#{businessId},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="companyName != null">#{companyName},</if>
            <if test="depth != null">#{depth},</if>
            <if test="companyRole != null">#{companyRole},</if>
            <if test="responsiblePerson != null">#{responsiblePerson},</if>
            <if test="phone != null">#{phone},</if>
            <if test="isProprietor != null">#{isProprietor},</if>
            <if test="remark != null and remark != ''">#{remark},</if>
            <if test="companyUipId != null">#{companyUipId},</if>
        </trim>
    </insert>

    <update id="updateBusinessRelateCompany" parameterType="com.dsk.common.core.domain.entity.BusinessRelateCompany">
        update business_relate_company
        <set>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="companyUipId != null">company_uip_id = #{companyUipId},</if>
            <if test="companyName != null">company_name = #{companyName},</if>
            <if test="depth != null">depth = #{depth},</if>
            <if test="companyRole != null">company_role = #{companyRole},</if>
            <if test="responsiblePerson != null">responsible_person = #{responsiblePerson},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="remark != null">remark = #{remark},</if>
        </set>
        where id = #{id}
    </update>

    <delete id="deleteBusinessRelateCompanyById" parameterType="Long">
        delete
        from business_relate_company
        where id = #{id}
    </delete>

    <delete id="deleteBusinessRelateCompanyByIds" parameterType="String">
        delete from business_relate_company where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectByProprietor" resultType="com.dsk.common.core.domain.entity.BusinessRelateCompany">
        <include refid="selectBusinessRelateCompanyVo"/>
        where business_id = #{businessId} and is_proprietor = 1
    </select>
</mapper>
