<?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.fund.mapper.FundMapper">

    <sql id="FundColumns">
        a.id,
        a.create_by,
        a.create_date,
        a.update_by,
        a.update_date,
        a.del_flag,
        a.remarks,
        a.projectId,
        a.projectName,
        a.proc_ins_id,
        a.wbsNumber,
        a.reserveProjectName,
        a.wbsEncoding,
        a.assetCardNumber
    </sql>

    <sql id="basicColumns">
        b.id as `basicInformation.id`,
        b.reserveProjectName as `basicInformation.reserveProjectName`,
		b.municipalCompany AS `basicInformation.municipalCompany`,
		b.countyCompany AS `basicInformation.countyCompany`,
		b.projectNature AS `basicInformation.projectNature`,
		b.demandUnit AS `basicInformation.demandUnit`,
		b.operationUnit AS `basicInformation.operationUnit`,
		b.powerSupplyArea AS `basicInformation.powerSupplyArea`,
		b.gridNumber AS `basicInformation.gridNumber`,
		b.cellNumber AS `basicInformation.cellNumber`,
		b.projectReserveNo AS `basicInformation.projectReserveNo`,
		b.gridProjectNo AS `basicInformation.gridProjectNo`,
		b.erpNo AS `basicInformation.erpNo`,
		b.necessityContent AS `basicInformation.necessityContent`,
		b.subStationName AS `basicInformation.subStationName`,
		b.lineName AS `basicInformation.lineName`,
		b.projectType AS `basicInformation.projectType`,
		b.whetherSummer AS `basicInformation.whetherSummer`,
		b.basicDescription AS `basicInformation.basicDescription`,
		b.workAreaRemarks AS `basicInformation.workAreaRemarks`
    </sql>
    <delete id="deleteList">
        <foreach collection="array" item="item" separator=";" index="index">
        delete from xm_fund_process where id = #{item}
        </foreach>
    </delete>

    <select id="get" resultType="Fund">
        SELECT
        <include refid="FundColumns"/>,
        <include refid="basicColumns"/>
        from xm_fund_process a
        LEFT JOIN xm_project_basicinformation b
        on a.reserveProjectName = b.reserveProjectName
        WHERE a.id = #{id}
    </select>

    <select id="findList" resultType="Fund">
        SELECT
        <include refid="FundColumns"/>
        from xm_fund_process a
        <where>
            del_flag = #{DEL_FLAG_NORMAL}
            ${dataScope}
            <if test="projectId != null and projectId !=''">
                and a.projectId = #{projectId}
            </if>
            <if test="projectName != null and projectName !=''">
                and a.projectName = #{projectName}
            </if>
            <if test="reserveProjectName != null and reserveProjectName !=''">
                and a.reserveProjectName = #{reserveProjectName}
            </if>
            <if test="projectIdList != null and projectIdList != ''">
                and a.projectid in
                <foreach collection="projectIdList" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </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="Fund">
        SELECT
        <include refid="FundColumns"/>
        from xm_fund_process a
        <where>
            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_fund_process
        (id,
         create_by,
         create_date,
         update_by,
         update_date,
         del_flag,
         remarks,
         projectId,
         projectName,
         proc_ins_id,
         reserveProjectName,
         wbsEncoding,
         wbsNumber,
         assetCardNumber)
        VALUES (#{id},
                #{createBy.id},
                #{createDate},
                #{updateBy.id},
                #{updateDate},
                #{delFlag},
                #{remarks},
                #{projectId},
                #{projectName},
                #{procInsId},
                #{reserveProjectName},
                #{wbsEncoding},
                #{wbsNumber},
                #{assetCardNumber});
    </insert>

    <update id="update">
        UPDATE xm_fund_process
        SET wbsEncoding = #{wbsEncoding},
            assetCardNumber = #{assetCardNumber}
        WHERE id = #{id}
    </update>

    <update id="updateWbs">
        UPDATE xm_fund_process
        SET wbsEncoding = #{wbsEncoding},
            wbsNumber = #{wbsNumber}
        WHERE proc_ins_id = #{procInsId}
    </update>


    <update id="updateAsset">
        UPDATE xm_fund_process
        SET assetCardNumber = #{assetCardNumber}
        WHERE proc_ins_id = #{procInsId}
    </update>

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

    <!--逻辑删除-->
    <update id="deleteByLogic">
        UPDATE xm_fund_process
        SET del_flag = #{DEL_FLAG_DELETE}
        WHERE id = #{id}
    </update>
</mapper>