<?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.MdBrandVisitMapper">

    <resultMap type="com.ximai.mes.md.domain.MdBrandVisit" id="MdBrandVisitResult">
        <result property="brandVisitId"    column="brand_visit_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="visitPerson"    column="visit_person"    />
        <result property="visitTime"    column="visit_time"    />
        <result property="visitSummary"    column="visit_summary"    />
        <result property="enableFlag"    column="enable_flag"    />
        <result property="remark"    column="remark"    />
        <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="selectMdBrandVisitVo">
        select c.brand_visit_id, c.brand_id,a.brand_code,a.brand_name, c.contact_name, c.visit_person, c.visit_time, c.visit_summary, c.enable_flag, c.remark, c.create_by, c.create_time, c.update_by, c.update_time  from md_brand_visit as c LEFT JOIN md_brand as a on c.brand_id = a.brand_id

    </sql>

    <select id="selectMdBrandVisitList" parameterType="com.ximai.mes.md.domain.MdBrandVisit" resultMap="MdBrandVisitResult">
        <include refid="selectMdBrandVisitVo"/>
        <where>
            <if test="brandId != null "> and c.brand_id = #{brandId}</if>
            <if test="brandCode != null  and brandCode != ''"> and a.brand_code like concat('%', #{brandCode}, '%')</if>
            <if test="brandName != null  and brandName != ''"> and a.brand_name like concat('%', #{brandName}, '%')</if>
            <if test="contactName != null  and contactName != ''"> and c.contact_name like concat('%', #{contactName}, '%')</if>
            <if test="visitPerson != null  and visitPerson != ''"> and c.visit_person = #{visitPerson}</if>
            <if test="visitTime != null "> and c.visit_time = #{visitTime}</if>
            <if test="visitSummary != null  and visitSummary != ''"> and c.visit_summary = #{visitSummary}</if>
            <if test="enableFlag != null  and enableFlag != ''"> and c.enable_flag = #{enableFlag}</if>
        </where>
    </select>

    <select id="selectMdBrandVisitByBrandVisitId" parameterType="Long" resultMap="MdBrandVisitResult">
        <include refid="selectMdBrandVisitVo"/>
        where brand_visit_id = #{brandVisitId}
    </select>

    <insert id="insertMdBrandVisit" parameterType="MdBrandVisit" useGeneratedKeys="true" keyProperty="brandVisitId">
        insert into md_brand_visit
        <trim prefix="(" suffix=")" suffixOverrides=",">

            <if test="brandId != null">brand_id,</if>
            <if test="contactName != null and contactName != ''">contact_name,</if>
            <if test="visitPerson != null and visitPerson != ''">visit_person,</if>
            <if test="visitTime != null">visit_time,</if>
            <if test="visitSummary != null">visit_summary,</if>
            <if test="enableFlag != null">enable_flag,</if>
            <if test="remark != null">remark,</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 and contactName != ''">#{contactName},</if>
            <if test="visitPerson != null and visitPerson != ''">#{visitPerson},</if>
            <if test="visitTime != null">#{visitTime},</if>
            <if test="visitSummary != null">#{visitSummary},</if>
            <if test="enableFlag != null">#{enableFlag},</if>
            <if test="remark != null">#{remark},</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="updateMdBrandVisit" parameterType="MdBrandVisit">
        update md_brand_visit
        <trim prefix="SET" suffixOverrides=",">
<!--            <if test="brandId != null">brand_id = #{brandId},</if>-->
            <if test="contactName != null and contactName != ''">contact_name = #{contactName},</if>
            <if test="visitPerson != null and visitPerson != ''">visit_person = #{visitPerson},</if>
            <if test="visitTime != null">visit_time = #{visitTime},</if>
            <if test="visitSummary != null">visit_summary = #{visitSummary},</if>
            <if test="enableFlag != null">enable_flag = #{enableFlag},</if>
            <if test="remark != null">remark = #{remark},</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_visit_id = #{brandVisitId}
    </update>

    <delete id="deleteMdBrandVisitByBrandVisitId" parameterType="Long">
        delete from md_brand_visit where brand_visit_id = #{brandVisitId}
    </delete>

    <delete id="deleteMdBrandVisitByBrandVisitIds" parameterType="String">
        delete from md_brand_visit where brand_visit_id in
        <foreach item="brandVisitId" collection="array" open="(" separator="," close=")">
            #{brandVisitId}
        </foreach>
    </delete>
</mapper>
