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

    <resultMap type="CmcContract" id="CmcContractResult">
        <result property="contractId"    column="contract_id"    />
        <result property="contractName"    column="contract_name"    />
        <result property="contractCode"    column="contract_code"    />
        <result property="contractNumber"    column="contract_number"    />
        <result property="tenderId"    column="tender_id"    />
        <result property="partyAId"    column="party_a_id"    />
        <result property="amount"    column="amount"    />
        <result property="deposit"    column="deposit"    />
        <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="tender"    javaType="CmcTender"         resultMap="CmcTenderResult" />
        <association property="partyA"    javaType="CmcPartyA"         resultMap="CmcPartyAResult" />
    </resultMap>

    <resultMap type="CmcTender" id="CmcTenderResult">
        <result property="tenderId"    column="tender_id"    />
        <result property="projectName"    column="project_name"    />
        <result property="partyAName"    column="party_a_name"    />
        <result property="aPerson"    column="a_person"    />
        <result property="aPhone"    column="a_phone"    />
        <result property="agent"    column="agent"    />
        <result property="agentPerson"    column="agent_person"    />
        <result property="agentPhone"    column="agent_phone"    />
    </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="CmcPartyA" id="CmcPartyAResult">
        <result property="partyAId"    column="party_a_id"    />
        <result property="partyAName"    column="party_a_name"    />
    </resultMap>

    <sql id="selectCmcContractVo">
        select c.contract_id, c.contract_code, c.contract_name, c.contract_number, c.tender_id, c.party_a_id, pa.party_a_name, t.project_name as project_name, t.a_person as a_person,
               t.a_phone as a_phone, t.agent as agent, t.agent_person as agent_person, t.agent_phone as agent_phone, c.amount, c.deposit, c.contract_document,
               c.drafter, u.nick_name as draft_nick_name, c.draft_time, c.remark, c.sign_date, c.sign_remark, c.sign_scan, c.comment_type, c.manager_comment, c.manager_user_id, u1.nick_name as manager_nick_name,
               c.manager_time, c.gm_user_id, u2.nick_name as gm_nick_name, c.gm_time, c.gm_comment from cmc_contract as c
        left join sys_user as u on u.user_id = c.drafter
        left join sys_user as u1 on u1.user_id = c.manager_user_id
        left join sys_user as u2 on u2.user_id = c.gm_user_id
        left join cmc_tender as t on t.tender_id = c.tender_id
        left join cmc_party_a as pa on pa.party_a_id = c.party_a_id
    </sql>

    <select id="selectCmcContractList" parameterType="CmcContract" resultMap="CmcContractResult">
        <include refid="selectCmcContractVo"/>
        <where>
            <if test="contractId != null  and contractId != ''"> and c.contract_id like concat('%', #{contractId}, '%')</if>
            <if test="contractName!= null  and contractName != ''"> and c.contract_name like concat('%', #{contractName}, '%')</if>
            <if test="contractCode!= null  and contractCode != ''"> and c.contract_code like concat('%', #{contractCode}, '%')</if>
            <if test="tenderId != null  and tenderId != ''"> and c.tender_id = #{tenderId}</if>
            <if test="contractNumber != null  and contractNumber != ''"> and c.contract_number = #{contractNumber}</if>
            <if test="amount != null "> and c.amount = #{amount}</if>
            <if test="deposit != null "> and c.deposit = #{deposit}</if>
            <if test="contractDocument != null  and contractDocument != ''"> and c.contract_document = #{contractDocument}</if>
            <if test="drafter != null "> and c.drafter = #{drafter}</if>
            <if test="draftTime != null "> and c.draft_time = #{draftTime}</if>
            <if test="signDate != null "> and c.sign_date = #{signDate}</if>
            <if test="signRemark != null  and signRemark != ''"> and c.sign_remark = #{signRemark}</if>
            <if test="signScan != null  and signScan != ''"> and c.sign_scan = #{signScan}</if>
            <if test="commentType != null  and commentType != ''"> and c.comment_type = #{commentType}</if>
        </where>
        order by c.draft_time desc
    </select>

    <select id="selectCmcContractByContractId" parameterType="String" resultMap="CmcContractResult">
        <include refid="selectCmcContractVo"/>
        where c.contract_id = #{contractId}
    </select>

    <insert id="insertCmcContract" parameterType="CmcContract">
        insert into cmc_contract
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="contractId != null">contract_id,</if>
            <if test="contractName != null">contract_name,</if>
            <if test="contractCode != null">contract_code,</if>
            <if test="tenderId != null">tender_id,</if>
            <if test="partyAId != null">party_a_id,</if>
            <if test="contractNumber != null">contract_number,</if>
            <if test="amount != null">amount,</if>
            <if test="deposit != null">deposit,</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="contractId != null">#{contractId},</if>
            <if test="contractName != null">#{contractName},</if>
            <if test="contractCode != null">#{contractCode},</if>
            <if test="tenderId != null">#{tenderId},</if>
            <if test="partyAId != null">#{partyAId},</if>
            <if test="contractNumber != null">#{contractNumber},</if>
            <if test="amount != null">#{amount},</if>
            <if test="deposit != null">#{deposit},</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="updateCmcContract" parameterType="CmcContract">
        update cmc_contract
        <trim prefix="SET" suffixOverrides=",">
            <if test="contractCode != null">contract_code = #{contractCode},</if>
            <if test="contractName != null">contract_name = #{contractName},</if>
            <if test="tenderId != null">tender_id = #{tenderId},</if>
            <if test="partyAId != null">party_a_id = #{partyAId},</if>
            <if test="contractNumber != null">contract_number = #{contractNumber},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="deposit != null">deposit = #{deposit},</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 contract_id = #{contractId}
    </update>

    <delete id="deleteCmcContractByContractId" parameterType="String">
        delete from cmc_contract where contract_id = #{contractId}
    </delete>

    <delete id="deleteCmcContractByContractIds" parameterType="String">
        delete from cmc_contract where contract_id in
        <foreach item="contractId" collection="array" open="(" separator="," close=")">
            #{contractId}
        </foreach>
    </delete>
</mapper>