<?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.fs.project.mapper.ProjectContractMapper">
    
    <resultMap type="ProjectContract" id="ProjectContractResult">
        <result property="id"    column="id"    />
        <result property="companyId"    column="company_id"    />
        <result property="projectId"    column="project_id"    />
        <result property="contractCode"    column="contract_code"    />
        <result property="contractName"    column="contract_name"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="companyName" column="company_name"/>
        <result property="projectName" column="project_name"/>
    </resultMap>

    <sql id="selectProjectContractVo">
        SELECT
        pc.id,
        pc.company_id,
        pc.project_id,
        pc.contract_code,
        pc.contract_name,
        pc.create_time,
        pc.update_time,
        pc.create_by,
        pc.update_by,
        pc.del_flag,

        c.company_name AS company_name,
        p.project_name AS project_name
        FROM project_contract pc
        LEFT JOIN company c
        ON c.company_id = pc.company_id
        AND c.is_del = 0
        LEFT JOIN project p
        ON p.id = pc.project_id
        AND p.del_flag = 0
    </sql>


    <select id="selectProjectContractList" parameterType="ProjectContract" resultMap="ProjectContractResult">
        <include refid="selectProjectContractVo"/>
        <where>
            <if test="companyId != null"> and pc.company_id = #{companyId}</if>
            <if test="projectId != null"> and pc.project_id = #{projectId}</if>
            <if test="contractCode != null and contractCode != ''"> and pc.contract_code = #{contractCode}</if>
            <if test="contractName != null and contractName != ''"> and pc.contract_name like concat('%', #{contractName}, '%')</if>
        </where>
        GROUP BY pc.id

    </select>
    
    <select id="selectProjectContractById" parameterType="Long" resultMap="ProjectContractResult">
        <include refid="selectProjectContractVo"/>
        where pc.id = #{id}
    </select>
        
    <insert id="insertProjectContract" parameterType="ProjectContract" useGeneratedKeys="true" keyProperty="id">
        insert into project_contract
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyId != null">company_id,</if>
            <if test="projectId != null">project_id,</if>
            <if test="contractCode != null and contractCode != ''">contract_code,</if>
            <if test="contractName != null and contractName != ''">contract_name,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="delFlag != null">del_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyId != null">#{companyId},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="contractCode != null and contractCode != ''">#{contractCode},</if>
            <if test="contractName != null and contractName != ''">#{contractName},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="delFlag != null">#{delFlag},</if>
         </trim>
    </insert>

    <update id="updateProjectContract" parameterType="ProjectContract">
        update project_contract
        <trim prefix="SET" suffixOverrides=",">
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="contractCode != null and contractCode != ''">contract_code = #{contractCode},</if>
            <if test="contractName != null and contractName != ''">contract_name = #{contractName},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteProjectContractById" parameterType="Long">
        delete from project_contract where id = #{id}
    </delete>

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