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

    <resultMap type="com.ximai.mes.md.domain.MdBrand" id="MdBrandResult">
        <result property="brandId" column="brand_id"/>
        <result property="brandCode" column="brand_code"/>
        <result property="brandName" column="brand_name"/>
        <result property="groupName" column="group_name"/>
        <result property="address" column="address"/>
        <result property="salesContact" column="sales_contact"/>
        <result property="devContact" column="dev_contact"/>
        <result property="background" column="background"/>
        <result property="scope" column="scope"/>
        <result property="potentialProject" column="potential_project"/>
        <result property="integrityRegulations" column="integrity_regulations"/>
        <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="selectMdBrandVo">
        select brand_id, brand_code, brand_name, group_name, address, sales_contact, dev_contact, background, scope, potential_project, integrity_regulations, remark, enable_flag, create_by, create_time, update_by, update_time from md_brand
    </sql>

    <insert id="insertMdBrand" parameterType="MdBrand" useGeneratedKeys="true" keyProperty="brandId">
        insert into md_brand
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="brandId != null and brandId != ''">brand_id,</if>
            <if test="brandCode != null and brandCode != ''">brand_code,</if>
            <if test="brandName != null and brandName != ''">brand_name,</if>
            <if test="groupName != null">group_name,</if>
            <if test="address != null">address,</if>
            <if test="salesContact != null">sales_contact,</if>
            <if test="devContact != null">dev_contact,</if>
            <if test="background != null">background,</if>
            <if test="scope != null">scope,</if>
            <if test="potentialProject != null">potential_project,</if>
            <if test="integrityRegulations != null">integrity_regulations,</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 and brandId != ''">#{brandId},</if>
            <if test="brandCode != null and brandCode != ''">#{brandCode},</if>
            <if test="brandName != null and brandName != ''">#{brandName},</if>
            <if test="groupName != null">#{groupName},</if>
            <if test="address != null">#{address},</if>
            <if test="salesContact != null">#{salesContact},</if>
            <if test="devContact != null">#{devContact},</if>
            <if test="background != null">#{background},</if>
            <if test="scope != null">#{scope},</if>
            <if test="potentialProject != null">#{potentialProject},</if>
            <if test="integrityRegulations != null">#{integrityRegulations},</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="updateMdBrand" parameterType="MdBrand" >
        update md_brand
        <trim prefix="SET" suffixOverrides=",">
            <if test="brandId != null and brandId != ''">brand_id = #{brandId},</if>
            <if test="brandCode != null and brandCode != ''">brand_code = #{brandCode},</if>
            <if test="brandName != null and brandName != ''">brand_name = #{brandName},</if>
            <if test="groupName != null">group_name = #{groupName},</if>
            <if test="address != null">address = #{address},</if>
            <if test="salesContact != null">sales_contact = #{salesContact},</if>
            <if test="devContact != null">dev_contact = #{devContact},</if>
            <if test="background != null">background = #{background},</if>
            <if test="scope != null">scope = #{scope},</if>
            <if test="potentialProject != null">potential_project = #{potentialProject},</if>
            <if test="integrityRegulations != null">integrity_regulations = #{integrityRegulations},</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_id = #{brandId}
    </update>

    <!-- 批量根据id删除用途 -->
<!--    <delete id="deleteMdBrandByBrandId" parameterType="String">-->
<!--        delete from md_brand where brand_id in-->
<!--        <foreach item="usageId" collection="array" open="(" separator="," close=")">-->
<!--            #{usageId}-->
<!--        </foreach>-->
<!--    </delete>-->

    <delete id="deleteMdBrandByBrandId" parameterType="Long">
        delete from md_brand where brand_id = #{brandId}
    </delete>

    <!-- 根据条件查询列表 -->
    <select id="selectMdBrandList" parameterType="com.ximai.mes.md.domain.MdBrand" resultMap="MdBrandResult">
        <include refid="selectMdBrandVo"/>
        <where>
            <if test="brandCode != null  and brandCode != ''">and brand_code = #{brandCode}</if>
            <if test="brandName != null  and brandName != ''">and brand_name like concat('%', #{brandName}, '%')</if>
            <if test="groupName != null  and groupName != ''">and group_name like concat('%', #{groupName}, '%')</if>
            <if test="enableFlag != null  and enableFlag != ''">and enable_flag = #{enableFlag}</if>
            <if test="brandId != null "> and brand_id = #{brandId}</if>
        </where>
        order by create_time desc
    </select>

    <!-- 根据用途id查询用途详细信息 -->
    <select id="selectMdBrandByBrandId" parameterType="long" resultMap="MdBrandResult">
        <include refid="selectMdBrandVo"/>
        where brand_id = #{brandId}
    </select>

    <!-- 查询用途编码是否唯一 -->
    <select id="checkCodeUnique" parameterType="com.ximai.mes.md.domain.MdBrand" resultMap="MdBrandResult">
        <include refid="selectMdBrandVo"/>
        where brand_code = #{brandCode} limit 1
    </select>

    <!-- 查询用途名称是否唯一 -->
    <select id="checkNameUnique" parameterType="com.ximai.mes.md.domain.MdBrand" resultMap="MdBrandResult">
        <include refid="selectMdBrandVo"/>
        where brand_name = #{brandName} limit 1
    </select>
</mapper>
