<?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.jeeplus.modules.sg.managementcenter.project.mapper.CapitalPlanMapper">

    <sql id="projectColumns">
        a.reserveProjectName,
        a.projectId,
        a.projectName
    </sql>

    <sql id="approvalColumns">
        c.projectBudget,
        c.constructionCost,
        c.materialCost,
        c.designFee,
        c.supervisionFee,
        c.policyFee as 'policyProcessingFee',
        c.otherExpenses
    </sql>

    <sql id="capitalPlanColumns">
        b.id,
        b.projectBatch,
        b.actualCost,
        b.projectDifference,
        b.actualConstructionCost,
        b.constructionDifference,
        b.actualMaterialCost,
        b.costDifference,
        b.DesignPayFee,
        b.actualDesignCost,
        b.designDifference,
        b.supervisionPayFee,
        b.actualSupervisionCost,
        b.supervisionDifference,
        b.ActualManagementFee,
        b.policyDifference,
        b.actualOtherCost,
        b.otherDifference,
        b.designRate,
        b.supervisionRate
    </sql>

    <select id="get" resultType="CapitalPlan">
        SELECT
        <include refid="projectColumns"/>,
        <include refid="capitalPlanColumns"/>
        FROM xm_material_project a
        left join xm_capital_plan b
        on a.projectId = b.projectId
        WHERE b.id = #{id}
    </select>

    <select id="findList" resultType="CapitalPlan">
        SELECT
        <include refid="projectColumns"/>,
        <include refid="approvalColumns"/>,
        <include refid="capitalPlanColumns"/>
        FROM xm_material_project a
        left join xm_capital_plan b
        on a.projectId = b.projectId
        left join xm_project_approval c
        on a.projectId = c.projectId
        <where>
            a.del_flag = #{DEL_FLAG_NORMAL}
            ${dataScope}
            and a.projectid != ''
            and a.secondPower = '0'
        </where>
        <choose>
            <when test="page !=null and page.orderBy != null and page.orderBy != ''">
                ORDER BY ${page.orderBy}
            </when>
            <otherwise>
                ORDER BY a.update_date DESC
            </otherwise>
        </choose>
    </select>

    <select id="findAllList" resultType="CapitalPlan">
        SELECT
        <include refid="projectColumns"/>,
        <include refid="capitalPlanColumns"/>
        FROM xm_material_project a
        left join xm_capital_plan b
        on a.projectId = b.projectId
        <where>
            a.del_flag = #{DEL_FLAG_NORMAL}
            ${dataScope}
        </where>
        <choose>
            <when test="page !=null and page.orderBy != null and page.orderBy != ''">
                ORDER BY ${page.orderBy}
            </when>
            <otherwise>
                ORDER BY a.update_date DESC
            </otherwise>
        </choose>
    </select>

    <insert id="insert">
        INSERT INTO xm_picking_contrast(
            id,
            create_by,
            create_date,
            update_by,
            update_date,
            remarks,
            del_flag,
            requisition,
            projectDefinition
            )
        VALUES (
            #{id},
            #{createBy.id},
            #{createDate},
            #{updateBy.id},
            #{updateDate},
            #{remarks},
            #{delFlag},
            #{requisition},
            #{projectDefinition}
            )
    </insert>

    <select id="findPlanProjectId" resultType="java.lang.String">
        select projectId from xm_capital_plan
    </select>

    <insert id="insertList">
        insert into xm_capital_plan
        (
            id,
            create_by,
            create_date,
            update_by,
            update_date,
            del_flag,
            remarks,
            projectId,
            actualCost,
            actualConstructionCost,
            actualMaterialCost,
            actualDesignCost,
            actualSupervisionCost,
            actualOtherCost
        )
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.id},
            #{item.createBy.id},
            #{item.createDate},
            #{item.updateBy.id},
            #{item.updateDate},
            #{item.delFlag},
            #{item.remarks},
            #{item.projectId},
            #{item.actualCost},
            #{item.actualConstructionCost},
            #{item.actualMaterialCost},
            #{item.actualDesignCost},
            #{item.actualSupervisionCost},
            #{item.actualOtherCost}
            )
        </foreach>
    </insert>

    <update id="updateList">
        <foreach collection="list" item="item" index="index" separator=";">
            update xm_capital_plan
            set
            actualCost = #{item.actualCost},
            actualConstructionCost = #{item.actualConstructionCost},
            actualMaterialCost = #{item.actualMaterialCost},
            actualDesignCost = #{item.actualDesignCost},
            actualSupervisionCost = #{item.actualSupervisionCost},
            actualOtherCost = #{item.actualOtherCost}
            where projectId =#{item.projectId}
        </foreach>
    </update>
    <update id="saveEdit">
        update xm_capital_plan
        set designRate = #{designRate},
            supervisionRate = #{supervisionRate}
        where id  = #{id}
    </update>
</mapper>