<?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.CmcSubContractMapper">
    
    <resultMap type="CmcSubContract" id="CmcSubContractResult">
        <result property="subContractId"    column="sub_contract_id"    />
        <result property="subContractName"    column="sub_contract_name"    />
        <result property="subAmount"    column="sub_amount"    />
        <result property="partnerId"    column="partner_id"    />
        <result property="contactPerson"    column="contact_person"    />
        <result property="telephone"    column="telephone"    />
        <result property="contractDocument"    column="contract_document"    />
        <result property="drafter"    column="drafter"    />
        <result property="draftTime"    column="draft_time"    />
        <result property="remark"    column="remark"    />
        <result property="signDate"    column="sign_date"    />
        <result property="signRemark"    column="sign_remark"    />
        <result property="signScan"    column="sign_scan"    />
        <result property="commentType"    column="comment_type"    />
        <result property="managerComment"    column="manager_comment"    />
        <result property="managerUserId"    column="manager_user_id"    />
        <result property="managerTime"    column="manager_time"    />
        <result property="gmUserId"    column="gm_user_id"    />
        <result property="gmTime"    column="gm_time"    />
        <result property="gmComment"    column="gm_comment"    />
        <association property="draftUser"    javaType="SysUser"         resultMap="DraftUserResult" />
        <association property="managerUser"    javaType="SysUser"         resultMap="ManagerUserResult" />
        <association property="gmUser"    javaType="SysUser"         resultMap="GmUserResult" />
        <association property="partner"    javaType="CmcPartner"         resultMap="CmcPartnerResult" />
    </resultMap>

    <resultMap type="SysUser" id="DraftUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="draft_nick_name"    />
    </resultMap>

    <resultMap type="SysUser" id="ManagerUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="manager_nick_name"    />
    </resultMap>

    <resultMap type="SysUser" id="GmUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="gm_nick_name"    />
    </resultMap>

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

    <sql id="selectCmcSubContractVo">
        select sc.sub_contract_id, sc.sub_contract_name, sc.sub_amount, sc.partner_id, p.partner_name, sc.contact_person, sc.telephone, sc.contract_document, sc.drafter, u.nick_name as draft_nick_name, sc.draft_time,
               sc.remark, sc.sign_date, sc.sign_remark, sc.sign_scan, sc.comment_type, sc.manager_comment, sc.manager_user_id, u1.nick_name as manager_nick_name, sc.manager_time, sc.gm_user_id, u2.nick_name as gm_nick_name,
               sc.gm_time, sc.gm_comment from cmc_sub_contract as sc
        left join sys_user as u on u.user_id = sc.drafter
        left join sys_user as u1 on u1.user_id = sc.manager_user_id
        left join sys_user as u2 on u2.user_id = sc.gm_user_id
        left join cmc_partner as p on p.partner_id = sc.partner_id
    </sql>

    <select id="selectCmcSubContractList" parameterType="CmcSubContract" resultMap="CmcSubContractResult">
        <include refid="selectCmcSubContractVo"/>
        <where>
            <if test="subContractName != null  and subContractName != ''"> and sc.sub_contract_name like concat('%', #{subContractName}, '%')</if>
            <if test="subAmount != null "> and sc.sub_amount = #{subAmount}</if>
            <if test="partnerId != null  and partnerId != ''"> and sc.partner_id = #{partnerId}</if>
            <if test="contactPerson != null  and contactPerson != ''"> and sc.contact_person = #{contactPerson}</if>
            <if test="telephone != null  and telephone != ''"> and sc.telephone = #{telephone}</if>
            <if test="contractDocument != null  and contractDocument != ''"> and sc.contract_document = #{contractDocument}</if>
            <if test="drafter != null "> and sc.drafter = #{drafter}</if>
            <if test="draftTime != null "> and sc.draft_time = #{draftTime}</if>
            <if test="signDate != null "> and sc.sign_date = #{signDate}</if>
            <if test="signRemark != null  and signRemark != ''"> and sc.sign_remark = #{signRemark}</if>
            <if test="signScan != null  and signScan != ''"> and sc.sign_scan = #{signScan}</if>
            <if test="commentType != null  and commentType != ''"> and sc.comment_type = #{commentType}</if>
        </where>
        order by sc.draft_time desc
    </select>
    
    <select id="selectCmcSubContractBySubContractId" parameterType="String" resultMap="CmcSubContractResult">
        <include refid="selectCmcSubContractVo"/>
        where sc.sub_contract_id = #{subContractId}
    </select>
        
    <insert id="insertCmcSubContract" parameterType="CmcSubContract">
        insert into cmc_sub_contract
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="subContractId != null">sub_contract_id,</if>
            <if test="subContractName != null">sub_contract_name,</if>
            <if test="subAmount != null">sub_amount,</if>
            <if test="partnerId != null">partner_id,</if>
            <if test="contactPerson != null">contact_person,</if>
            <if test="telephone != null">telephone,</if>
            <if test="contractDocument != null">contract_document,</if>
            <if test="drafter != null">drafter,</if>
            <if test="draftTime != null">draft_time,</if>
            <if test="remark != null">remark,</if>
            <if test="signDate != null">sign_date,</if>
            <if test="signRemark != null">sign_remark,</if>
            <if test="signScan != null">sign_scan,</if>
            <if test="commentType != null">comment_type,</if>
            <if test="managerComment != null">manager_comment,</if>
            <if test="managerUserId != null">manager_user_id,</if>
            <if test="managerTime != null">manager_time,</if>
            <if test="gmUserId != null">gm_user_id,</if>
            <if test="gmTime != null">gm_time,</if>
            <if test="gmComment != null">gm_comment,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="subContractId != null">#{subContractId},</if>
            <if test="subContractName != null">#{subContractName},</if>
            <if test="subAmount != null">#{subAmount},</if>
            <if test="partnerId != null">#{partnerId},</if>
            <if test="contactPerson != null">#{contactPerson},</if>
            <if test="telephone != null">#{telephone},</if>
            <if test="contractDocument != null">#{contractDocument},</if>
            <if test="drafter != null">#{drafter},</if>
            <if test="draftTime != null">#{draftTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="signDate != null">#{signDate},</if>
            <if test="signRemark != null">#{signRemark},</if>
            <if test="signScan != null">#{signScan},</if>
            <if test="commentType != null">#{commentType},</if>
            <if test="managerComment != null">#{managerComment},</if>
            <if test="managerUserId != null">#{managerUserId},</if>
            <if test="managerTime != null">#{managerTime},</if>
            <if test="gmUserId != null">#{gmUserId},</if>
            <if test="gmTime != null">#{gmTime},</if>
            <if test="gmComment != null">#{gmComment},</if>
         </trim>
    </insert>

    <update id="updateCmcSubContract" parameterType="CmcSubContract">
        update cmc_sub_contract
        <trim prefix="SET" suffixOverrides=",">
            <if test="subContractName != null">sub_contract_name = #{subContractName},</if>
            <if test="subAmount != null">sub_amount = #{subAmount},</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="contractDocument != null">contract_document = #{contractDocument},</if>
            <if test="drafter != null">drafter = #{drafter},</if>
            <if test="draftTime != null">draft_time = #{draftTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="signDate != null">sign_date = #{signDate},</if>
            <if test="signRemark != null">sign_remark = #{signRemark},</if>
            <if test="signScan != null">sign_scan = #{signScan},</if>
            <if test="commentType != null">comment_type = #{commentType},</if>
            <if test="managerComment != null">manager_comment = #{managerComment},</if>
            <if test="managerUserId != null">manager_user_id = #{managerUserId},</if>
            <if test="managerTime != null">manager_time = #{managerTime},</if>
            <if test="gmUserId != null">gm_user_id = #{gmUserId},</if>
            <if test="gmTime != null">gm_time = #{gmTime},</if>
            <if test="gmComment != null">gm_comment = #{gmComment},</if>
        </trim>
        where sub_contract_id = #{subContractId}
    </update>

    <delete id="deleteCmcSubContractBySubContractId" parameterType="String">
        delete from cmc_sub_contract where sub_contract_id = #{subContractId}
    </delete>

    <delete id="deleteCmcSubContractBySubContractIds" parameterType="String">
        delete from cmc_sub_contract where sub_contract_id in 
        <foreach item="subContractId" collection="array" open="(" separator="," close=")">
            #{subContractId}
        </foreach>
    </delete>
</mapper>