<?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.ruoyi.oa.mapper.CmcBrandMapper">
    
    <resultMap type="CmcBrand" id="CmcBrandResult">
        <result property="brandId"    column="brand_id"    />
        <result property="projectId"    column="project_id"    />
        <result property="applyReason"    column="apply_reason"    />
        <result property="approvalDocument"    column="approval_document"    />
        <result property="applier"    column="applier"    />
        <result property="applyTime"    column="apply_time"    />
        <result property="cwTime"    column="cw_time"    />
        <result property="cwUserId"    column="cw_user_id"    />
        <association property="applierUser"    javaType="SysUser"         resultMap="ApplierUserResult" />
        <association property="cwUser"    javaType="SysUser"         resultMap="CwUserResult" />
    </resultMap>

    <resultMap type="SysUser" id="ApplierUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="applier_nick_name"    />
    </resultMap>

    <resultMap type="SysUser" id="CwUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="cw_nick_name"    />
    </resultMap>

    <sql id="selectCmcBrandVo">
        select b.brand_id, b.project_id, b.apply_reason, b.approval_document, b.applier, u.nick_name as applier_nick_name, b.apply_time, b.cw_time, b.cw_user_id, u1.nick_name as cw_nick_name from cmc_brand as b
        left join sys_user as u on u.user_id = b.applier
        left join sys_user as u1 on u1.user_id = b.cw_user_id
    </sql>

    <select id="selectCmcBrandList" parameterType="CmcBrand" resultMap="CmcBrandResult">
        <include refid="selectCmcBrandVo"/>
        <where>  
            <if test="projectId != null  and projectId != ''"> and b.project_id = #{projectId}</if>
            <if test="applyReason != null  and applyReason != ''"> and b.apply_reason = #{applyReason}</if>
            <if test="approvalDocument != null  and approvalDocument != ''"> and b.approval_document = #{approvalDocument}</if>
            <if test="applier != null "> and b.applier = #{applier}</if>
            <if test="applyTime != null "> and b.apply_time = #{applyTime}</if>
            <if test="cwTime != null "> and b.cw_time = #{cwTime}</if>
            <if test="cwUserId != null "> and b.cw_user_id = #{cwUserId}</if>
        </where>
        order by b.apply_time desc
    </select>
    
    <select id="selectCmcBrandByBrandId" parameterType="String" resultMap="CmcBrandResult">
        <include refid="selectCmcBrandVo"/>
        where b.brand_id = #{brandId}
    </select>
        
    <insert id="insertCmcBrand" parameterType="CmcBrand">
        insert into cmc_brand
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="brandId != null">brand_id,</if>
            <if test="projectId != null">project_id,</if>
            <if test="applyReason != null">apply_reason,</if>
            <if test="approvalDocument != null">approval_document,</if>
            <if test="applier != null">applier,</if>
            <if test="applyTime != null">apply_time,</if>
            <if test="cwTime != null">cw_time,</if>
            <if test="cwUserId != null">cw_user_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="brandId != null">#{brandId},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="applyReason != null">#{applyReason},</if>
            <if test="approvalDocument != null">#{approvalDocument},</if>
            <if test="applier != null">#{applier},</if>
            <if test="applyTime != null">#{applyTime},</if>
            <if test="cwTime != null">#{cwTime},</if>
            <if test="cwUserId != null">#{cwUserId},</if>
         </trim>
    </insert>

    <update id="updateCmcBrand" parameterType="CmcBrand">
        update cmc_brand
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="applyReason != null">apply_reason = #{applyReason},</if>
            <if test="approvalDocument != null">approval_document = #{approvalDocument},</if>
            <if test="applier != null">applier = #{applier},</if>
            <if test="applyTime != null">apply_time = #{applyTime},</if>
            <if test="cwTime != null">cw_time = #{cwTime},</if>
            <if test="cwUserId != null">cw_user_id = #{cwUserId},</if>
        </trim>
        where brand_id = #{brandId}
    </update>

    <delete id="deleteCmcBrandByBrandId" parameterType="String">
        delete from cmc_brand where brand_id = #{brandId}
    </delete>

    <delete id="deleteCmcBrandByBrandIds" parameterType="String">
        delete from cmc_brand where brand_id in 
        <foreach item="brandId" collection="array" open="(" separator="," close=")">
            #{brandId}
        </foreach>
    </delete>
</mapper>