<?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.CmcBrandProjectMapper">
    
    <resultMap type="CmcBrandProject" id="CmcBrandProjectResult">
        <result property="projectId"    column="project_id"    />
        <result property="projectNumber"    column="project_number"    />
        <result property="projectName"    column="project_name"    />
        <result property="partyAId"    column="party_a_id"    />
        <result property="aPerson"    column="a_person"    />
        <result property="aPhone"    column="a_phone"    />
        <result property="partnerId"    column="partner_id"    />
        <result property="contactPerson"    column="contact_person"    />
        <result property="telephone"    column="telephone"    />
        <result property="duration"    column="duration"    />
        <result property="amount"    column="amount"    />
        <result property="content"    column="content"    />
        <result property="remark"    column="remark"    />
        <result property="projectRegistrant"    column="project_registrant"    />
        <result property="registerTime"    column="register_time"    />
        <association property="registrantUser"    javaType="SysUser"         resultMap="RegistrantResult" />
        <association property="partner"    javaType="CmcPartner"         resultMap="CmcPartnerResult" />
        <association property="partyA"    javaType="CmcPartyA"         resultMap="CmcPartyAResult" />
    </resultMap>

    <resultMap type="SysUser" id="RegistrantResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="registrant_nick_name"    />
    </resultMap>

    <resultMap type="CmcPartner" id="CmcPartnerResult">
        <result property="partnerId"    column="partner_id"    />
        <result property="partnerName"    column="partner_name"    />
    </resultMap>

    <resultMap type="CmcPartyA" id="CmcPartyAResult">
        <result property="partyAId"    column="party_a_id"    />
        <result property="partyAName"    column="party_a_name"    />
    </resultMap>

    <sql id="selectCmcBrandProjectVo">
        select bp.project_id, bp.project_number, bp.project_name, bp.party_a_id, pa.party_a_name, bp.a_person, bp.a_phone, bp.partner_id, p.partner_name, bp.contact_person, bp.telephone,
               bp.duration, bp.amount, bp.content, bp.remark, bp.register_time, bp.project_registrant, u.nick_name as registrant_nick_name from cmc_brand_project as bp
        left join sys_user as u on u.user_id = bp.project_registrant
        left join cmc_partner as p on p.partner_id = bp.partner_id
        left join cmc_party_a as pa on pa.party_a_id = bp.party_a_id
    </sql>

    <select id="selectCmcBrandProjectList" parameterType="CmcBrandProject" resultMap="CmcBrandProjectResult">
        <include refid="selectCmcBrandProjectVo"/>
        <where>  
            <if test="projectNumber != null  and projectNumber != ''"> and bp.project_number = #{projectNumber}</if>
            <if test="projectName != null  and projectName != ''"> and bp.project_name like concat('%', #{projectName}, '%')</if>
            <if test="partyAId != null  and partyAId != ''"> and bp.party_a_id = #{partyAId}</if>
            <if test="aPerson != null  and aPerson != ''"> and bp.a_person = #{aPerson}</if>
            <if test="aPhone != null  and aPhone != ''"> and bp.a_phone = #{aPhone}</if>
            <if test="partnerId != null  and partnerId != ''"> and bp.partner_id = #{partnerId}</if>
            <if test="contactPerson != null  and contactPerson != ''"> and bp.contact_person = #{contactPerson}</if>
            <if test="telephone != null  and telephone != ''"> and bp.telephone = #{telephone}</if>
            <if test="duration != null  and duration != ''"> and bp.duration = #{duration}</if>
            <if test="amount != null "> and bp.amount = #{amount}</if>
        </where>
        order by bp.project_number desc
    </select>
    
    <select id="selectCmcBrandProjectByProjectId" parameterType="String" resultMap="CmcBrandProjectResult">
        <include refid="selectCmcBrandProjectVo"/>
        where bp.project_id = #{projectId}
    </select>
        
    <insert id="insertCmcBrandProject" parameterType="CmcBrandProject">
        insert into cmc_brand_project
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectId != null">project_id,</if>
            <if test="projectNumber != null">project_number,</if>
            <if test="projectName != null">project_name,</if>
            <if test="partyAId != null">party_a_id,</if>
            <if test="aPerson != null">a_person,</if>
            <if test="aPhone != null">a_phone,</if>
            <if test="partnerId != null">partner_id,</if>
            <if test="contactPerson != null">contact_person,</if>
            <if test="telephone != null">telephone,</if>
            <if test="duration != null">duration,</if>
            <if test="amount != null">amount,</if>
            <if test="content != null">content,</if>
            <if test="remark != null">remark,</if>
            <if test="projectRegistrant != null">project_registrant,</if>
            <if test="registerTime != null">register_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="projectId != null">#{projectId},</if>
            <if test="projectNumber != null">#{projectNumber},</if>
            <if test="projectName != null">#{projectName},</if>
            <if test="partyAId != null">#{partyAId},</if>
            <if test="aPerson != null">#{aPerson},</if>
            <if test="aPhone != null">#{aPhone},</if>
            <if test="partnerId != null">#{partnerId},</if>
            <if test="contactPerson != null">#{contactPerson},</if>
            <if test="telephone != null">#{telephone},</if>
            <if test="duration != null">#{duration},</if>
            <if test="amount != null">#{amount},</if>
            <if test="content != null">#{content},</if>
            <if test="remark != null">#{remark},</if>
            <if test="projectRegistrant != null">#{projectRegistrant},</if>
            <if test="registerTime != null">#{registerTime},</if>
         </trim>
    </insert>

    <update id="updateCmcBrandProject" parameterType="CmcBrandProject">
        update cmc_brand_project
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectNumber != null">project_number = #{projectNumber},</if>
            <if test="projectName != null">project_name = #{projectName},</if>
            <if test="partyAId != null">party_a_id = #{partyAId},</if>
            <if test="aPerson != null">a_person = #{aPerson},</if>
            <if test="aPhone != null">a_phone = #{aPhone},</if>
            <if test="partnerId != null">partner_id = #{partnerId},</if>
            <if test="contactPerson != null">contact_person = #{contactPerson},</if>
            <if test="telephone != null">telephone = #{telephone},</if>
            <if test="duration != null">duration = #{duration},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="content != null">content = #{content},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="projectRegistrant != null">project_registrant = #{projectRegistrant},</if>
            <if test="registerTime != null">register_time = #{registerTime},</if>
        </trim>
        where project_id = #{projectId}
    </update>

    <delete id="deleteCmcBrandProjectByProjectId" parameterType="String">
        delete from cmc_brand_project where project_id = #{projectId}
    </delete>

    <delete id="deleteCmcBrandProjectByProjectIds" parameterType="String">
        delete from cmc_brand_project where project_id in 
        <foreach item="projectId" collection="array" open="(" separator="," close=")">
            #{projectId}
        </foreach>
    </delete>
</mapper>