<?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.ximai.mes.md.mapper.MdBrandContactMapper">

    <resultMap type="com.ximai.mes.md.domain.MdBrandContact" id="MdBrandContactResult">
        <result property="brandContactId"    column="brand_contact_id"/>
        <result property="brandId"    column="brand_id"/>
        <result property="brandCode"    column="brand_code"/>
        <result property="brandName"    column="brand_name"/>
        <result property="contactName"    column="contact_name"/>
        <result property="contactNameEn"    column="contact_name_en"/>
        <result property="contactPhone"    column="contact_phone"/>
        <result property="officeArea"    column="office_area"/>
        <result property="department"    column="department"/>
        <result property="position"    column="position"/>
        <result property="mailbox"    column="mailbox"/>
        <result property="remark"    column="remark"/>
        <result property="enableFlag"    column="enable_flag"/>
        <result property="createBy"    column="create_by"/>
        <result property="createTime"    column="create_time"/>
        <result property="updateBy"    column="update_by"/>
        <result property="updateTime"    column="update_time"/>
    </resultMap>

    <sql id="selectMdBrandContactVo">
        select bc.brand_contact_id, bc.brand_id,b.brand_code,b.brand_name, bc.contact_name, bc.contact_name_en, bc.contact_phone, bc.office_area, bc.department, bc.position, bc.mailbox, bc.remark, bc.enable_flag, bc.create_by, bc.create_time, bc.update_by, bc.update_time from md_brand_contact as bc left join md_brand as b on bc.brand_id = b.brand_id
    </sql>

    <select id="selectMdBrandContactList" parameterType="com.ximai.mes.md.domain.MdBrandContact" resultMap="MdBrandContactResult">
        <include refid="selectMdBrandContactVo"/>
        <where>
            <if test="brandId != null "> and bc.brand_id = #{brandId}</if>
            <if test="brandName != null  and brandName != ''"> and b.brand_name like concat('%', #{brandName}, '%')</if>
            <if test="contactName != null  and contactName != ''"> and bc.contact_name like concat('%', #{contactName}, '%')</if>
            <if test="contactNameEn != null  and contactNameEn != ''"> and bc.contact_name_en = #{contactNameEn}</if>
            <if test="contactPhone != null  and contactPhone != ''"> and bc.contact_phone = #{contactPhone}</if>
            <if test="officeArea != null  and officeArea != ''"> and bc.office_area = #{officeArea}</if>
            <if test="department != null  and department != ''"> and bc.department = #{department}</if>
            <if test="position != null  and position != ''"> and bc.position = #{position}</if>
            <if test="mailbox != null  and mailbox != ''"> and bc.mailbox = #{mailbox}</if>
            <if test="enableFlag != null  and enableFlag != ''"> and bc.enable_flag = #{enableFlag}</if>
        </where>
                     order by bc.create_time desc
    </select>

    <select id="selectMdBrandContactByBrandContactId" parameterType="Long" resultMap="MdBrandContactResult">
        <include refid="selectMdBrandContactVo"/>
        where bc.brand_contact_id = #{brandContactId}
    </select>

    <insert id="insertMdBrandContact" parameterType="MdBrandContact" useGeneratedKeys="true" keyProperty="brandContactId">
        insert into md_brand_contact
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="brandId != null">brand_id,</if>
            <if test="contactName != null">contact_name,</if>
            <if test="contactNameEn != null">contact_name_en,</if>
            <if test="contactPhone != null">contact_phone,</if>
            <if test="officeArea != null">office_area,</if>
            <if test="department != null">department,</if>
            <if test="position != null">position,</if>
            <if test="mailbox != null">mailbox,</if>
            <if test="remark != null">remark,</if>
            <if test="enableFlag != null">enable_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="brandId != null">#{brandId},</if>
            <if test="contactName != null">#{contactName},</if>
            <if test="contactNameEn != null">#{contactNameEn},</if>
            <if test="contactPhone != null">#{contactPhone},</if>
            <if test="officeArea != null">#{officeArea},</if>
            <if test="department != null">#{department},</if>
            <if test="position != null">#{position},</if>
            <if test="mailbox != null">#{mailbox},</if>
            <if test="remark != null">#{remark},</if>
            <if test="enableFlag != null">#{enableFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateMdBrandContact" parameterType="MdBrandContact">
        update md_brand_contact
        <trim prefix="SET" suffixOverrides=",">
            <if test="brandId != null">brand_id = #{brandId},</if>
            <if test="contactName != null">contact_name = #{contactName},</if>
            <if test="contactNameEn != null">contact_name_en = #{contactNameEn},</if>
            <if test="contactPhone != null">contact_phone = #{contactPhone},</if>
            <if test="officeArea != null">office_area = #{officeArea},</if>
            <if test="department != null">department = #{department},</if>
            <if test="position != null">position = #{position},</if>
            <if test="mailbox != null">mailbox = #{mailbox},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="enableFlag != null">enable_flag = #{enableFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where brand_contact_id = #{brandContactId}
    </update>

    <delete id="deleteMdBrandContactByBrandContactId" parameterType="Long">
        delete from md_brand_contact where brand_contact_id = #{brandContactId}
    </delete>

    <delete id="deleteMdBrandContactByBrandContactIds" parameterType="String">
        delete from md_brand_contact where brand_contact_id in
        <foreach item="brandContactId" collection="array" open="(" separator="," close=")">
            #{brandContactId}
        </foreach>
    </delete>
</mapper>
