<?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.financial.expense.mapper.ExpenseMapper">

    <sql id="expenseColumns">
		a.id,
		a.projectName,
		a.projectId,
		a.projectBatch,
		a.constructionCost,
		a.designCost,
		a.designTypeicalCost,
		a.supervisionCost,
		a.supervisionTypeicalCost,
		a.documentDesignCost,
		a.documentSupervisionCost
	</sql>

    <select id="get" resultType="Expense">
        SELECT
        <include refid="expenseColumns"/>
        FROM js_expense_entry a
        WHERE a.id = #{id}
    </select>

    <select id="findList" resultType="Expense">
		SELECT
		<include refid="expenseColumns"/>
		FROM js_expense_entry a
		where
		1=1
			<if test="projectId!=null and projectId != '' ">
				and projectId=${projectId}
			</if>
		<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="Expense">
        SELECT
        <include refid="expenseColumns"/>
        FROM js_expense_entry a
        <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="insertData">
		insert INTO js_expense_entry(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			del_flag,
			remarks,
			projectName,
			projectId,
			projectBatch,
			constructionCost,
			designCost,
			designTypeicalCost,
			supervisionCost,
			supervisionTypeicalCost,
			documentDesignCost,
			documentSupervisionCost
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{delFlag},
			#{remarks},
			#{projectName},
			#{projectId},
			#{projectBatch},
			#{constructionCost},
			#{designCost},
			#{designTypeicalCost},
			#{supervisionCost},
			#{supervisionTypeicalCost},
			#{documentDesignCost},
			#{documentSupervisionCost}
		)
	</insert>

    <update id="updateData">
		UPDATE js_expense_entry SET
			id = #{id},
			create_by = #{createBy.id},
			create_date = #{createDate},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			del_flag = #{delFlag},
			remarks = #{remarks},
			projectName = #{projectName},
			projectId = #{projectId},
			projectBatch = #{projectBatch},
			constructionCost = #{constructionCost},
			designCost = #{designCost},
			designTypeicalCost = #{designTypeicalCost},
			supervisionCost = #{supervisionCost},
			supervisionTypeicalCost = #{supervisionTypeicalCost},
			documentDesignCost = #{documentDesignCost},
			documentSupervisionCost = #{documentSupervisionCost}
		WHERE id = #{id}
	</update>


    <!--物理删除-->
    <update id="delete">
		DELETE FROM js_expense_entry
		WHERE id = #{id}
	</update>

    <!--逻辑删除-->
    <update id="deleteByLogic">
		UPDATE js_expense_entry SET
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	<!--批量删除-->
	<update id="deleteData">
		DELETE FROM js_expense_entry
		WHERE id = #{id}
	</update>

	<select id="selectByProjectId" resultType="com.jeeplus.modules.sg.financial.expense.entity.Expense">
		select id from js_expense_entry where projectId  = #{projectId};
	</select>

	<insert id="insertList">
		replace into js_expense_entry (
		id,
		create_by,
		create_date,
		update_by,
		update_date,
		del_flag,
		remarks,
		projectName,
		projectId,
		projectBatch,
		constructionCost,
		designCost,
		designTypeicalCost,
		supervisionCost,
		supervisionTypeicalCost,
		documentDesignCost,
		documentSupervisionCost
		)
		values
		<foreach collection="expense" item="item" index="index" separator="," >
		(
			#{item.id},
			#{item.createBy.id},
			#{item.createDate},
			#{item.updateBy.id},
			#{item.updateDate},
			#{item.delFlag},
			#{item.remarks},
			#{item.projectName},
			#{item.projectId},
			#{item.projectBatch},
			#{item.constructionCost},
			#{item.designCost},
			#{item.designTypeicalCost},
			#{item.supervisionCost},
			#{item.supervisionTypeicalCost},
			#{item.documentDesignCost},
			#{item.documentSupervisionCost}
		)
		</foreach>
	</insert>
</mapper>