<?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.materialproject.mapper.MaterialProjectMapper">

    <sql id="infoColumns">
        a.id AS "id",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.reserveProjectName,
		a.reserveCode,
		a.projectType,
		a.batchPlan,
		a.issuePlan,
		a.monthPlan,
		a.hasPlan,
		a.halfPlan,
		a.projectName,
		a.projectId,
        a.attributionYear
    </sql>



    <select id="get" resultType="MaterialProject">
        SELECT
        <include refid="infoColumns"/>
        FROM xm_material_project a
        WHERE a.id = #{id}
    </select>

    <select id="findList" resultType="MaterialProject">
        SELECT
        <include refid="infoColumns"/>
        FROM xm_material_project a
        <where>
            a.del_flag = #{DEL_FLAG_NORMAL}
            ${dataScope}
            and secondPower = 0
            <if test="reserveCodes != null ">
                AND a.reserve_code in
                <foreach collection="reserveCodes" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="projectCodes != null ">
                AND a.project_code in
                <foreach collection="projectCodes" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="reserveProjectName != null and reserveProjectName != ''">
                AND a.reserveProjectName LIKE CONCAT(CONCAT('%',#{reserveProjectName},'%'))
            </if>
            <if test="reserveCode != null and reserveCode != ''">
                AND a.reserveCode = #{reserveCode}
            </if>
            <if test="projectType != null and projectType != ''">
                AND a.projectType = #{projectType}
            </if>
            <if test="batchPlan != null and batchPlan != ''">
                AND a.batchPlan = #{batchPlan}
            </if>
            <if test="issuePlan != null and issuePlan != ''">
                AND a.issuePlan = #{issuePlan}
            </if>
            <if test="monthPlan != null and monthPlan != ''">
                AND a.monthPlan = #{monthPlan}
            </if>
            <if test="hasPlan != null and hasPlan != ''">
                AND a.hasPlan = #{hasPlan}
            </if>
            <if test="halfPlan != null and halfPlan != ''">
                AND a.halfPlan = #{halfPlan}
            </if>
            <if test="projectId != null and projectId != ''">
                AND a.projectId = #{projectId}
            </if>
            <if test="projectName != null and projectName != ''">
                AND a.projectName = #{projectName}
            </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="findJugdeReserve" resultType="com.jeeplus.modules.sg.managementcenter.materialproject.entity.MaterialProject">
        SELECT
        <include refid="infoColumns"/>
        FROM xm_material_project a
        <where>
            a.del_flag = #{DEL_FLAG_NORMAL}
            ${dataScope}
            <if test="reserveProjectName != null and reserveProjectName != ''">
                AND a.reserveProjectName = #{reserveProjectName}
            </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="findByList" resultType="com.jeeplus.modules.sg.managementcenter.materialproject.entity.MaterialProject">
        SELECT
        <include refid="infoColumns"/>
        FROM xm_material_project a
        <where>
            a.reserveProjectName in
            <foreach collection="list" item="item" open="(" close=")" separator=",">
                #{item.reserveProjectName}
            </foreach>
        </where>
    </select>

    <select id="findAllList" resultType="MaterialProject">
        SELECT
        <include refid="infoColumns"/>
        FROM xm_material_project 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>

    <select id="findBatch" resultType="com.jeeplus.modules.sg.managementcenter.materialproject.entity.MaterialProject">
        SELECT DISTINCT(batch) AS suppliesBatch
        from xm_material_standard
    </select>

    <insert id="insert">
        INSERT INTO xm_material_project(id,
                                        create_by,
                                        create_date,
                                        update_by,
                                        update_date,
                                        remarks,
                                        del_flag,
                                        reserveProjectName,
                                        reserveCode,
                                        projectType,
                                        batchPlan,
                                        issuePlan,
                                        monthPlan,
                                        hasPlan,
                                        halfPlan,
                                        projectName,
                                        projectId,
                                        attributionYear)
        VALUES (#{id},
                #{createBy.id},
                #{createDate},
                #{updateBy.id},
                #{updateDate},
                #{remarks},
                '0',
                #{reserveProjectName},
                #{reserveCode},
                #{projectType},
                #{batchPlan},
                #{issuePlan},
                #{monthPlan},
                #{hasPlan},
                #{halfPlan},
                #{projectName},
                #{projectId},
                #{attributionYear}
                )
    </insert>

    <insert id="insertList">
        INSERT INTO xm_material_project(
        id,
        create_by,
        create_date,
        update_by,
        update_date,
        remarks,
        del_flag,
        reserveProjectName,
        reserveCode,
        projectType,
        batchPlan,
        issuePlan,
        monthPlan,
        hasPlan,
        halfPlan,
        projectName,
        projectId,
        attributionYear
        ) VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.id},
            #{item.createBy.id},
            #{item.createDate},
            #{item.updateBy.id},
            #{item.updateDate},
            #{item.remarks},
            '0',
            #{item.reserveProjectName},
            #{item.reserveCode},
            #{item.projectType},
            #{item.batchPlan},
            #{item.issuePlan},
            #{item.monthPlan},
            #{item.hasPlan},
            #{item.halfPlan},
            #{item.projectName},
            #{item.projectId},
            #{item.attributionYear}
            )
        </foreach>
    </insert>

    <insert id="replaceList">
        replace INTO xm_material_project(
        id,
        create_by,
        create_date,
        update_by,
        update_date,
        del_flag,
        remarks,
        reserveProjectName,
        reserveCode,
        projectType,
        batchPlan,
        issuePlan,
        monthPlan,
        hasPlan,
        halfPlan,
        projectName,
        projectId,
        attributionYear
        ) VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.id},
            #{item.createBy.id},
            #{item.createDate},
            #{item.updateBy.id},
            #{item.updateDate},
            '0',
            #{item.remarks},
            #{item.reserveProjectName},
            #{item.reserveCode},
            #{item.projectType},
            #{item.batchPlan},
            #{item.issuePlan},
            #{item.monthPlan},
            #{item.hasPlan},
            #{item.halfPlan},
            #{item.projectName},
            #{item.projectId},
            #{item.attributionYear}
            )
        </foreach>
    </insert>
    <update id="update">
        UPDATE xm_material_project
        SET update_by          = #{updateBy.id},
            update_date        = #{updateDate},
            reserveProjectName = #{reserveProjectName},
            reserveCode        = #{reserveCode},
            projectType        = #{projectType},
            batchPlan          = #{batchPlan},
            issuePlan          = #{issuePlan},
            monthPlan          = #{monthPlan},
            hasPlan            = #{hasPlan},
            halfPlan           = #{halfPlan},
            projectName        = #{projectName},
            projectId          = #{projectId},
            attributionYear    = #{attributionYear}
        WHERE id = #{id}
    </update>

    <update id="updateList">
        <foreach collection="list" item="item" index="index" separator=";">
            update xm_material_project
            set
            reserveCode = #{item.reserveCode},
            projectType = #{item.projectType},
            batchPlan = #{item.batchPlan},
            issuePlan = #{item.issuePlan},
            monthPlan = #{item.monthPlan},
            hasPlan = #{item.hasPlan},
            halfPlan = #{item.halfPlan},
            projectName = #{item.projectName},
            projectId = #{item.projectId},
            attributionYear = #{item.attributionYear}
            where reserveProjectName = #{item.reserveProjectName}
        </foreach>
    </update>

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

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



    <!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
    <select id="findUniqueByProperty" resultType="TestNote" statementType="STATEMENT">
        select *
        FROM xm_material_project
        where ${propertyName} = '${value}'
    </select>

    <select id="findAllReserveName" resultType="java.lang.String">
        select reserveProjectName
        from xm_material_project
    </select>

    <delete id="deleteConstruction">
        delete
        from xm_construction_clarificaiton
        where reserveProjectName = #{reserveProjectName}
    </delete>
    <delete id="deleteXmTearDown">
        delete
        from xm_tear_down
        where reserveProjectName = #{reserveProjectName}
    </delete>
    <delete id="deleteEngineeringAmount">
        delete
        from xm_engineering_amount
        where reserveProjectName = #{reserveProjectName}
    </delete>
    <delete id="deleteOtherAmount">
        delete
        from xm_other_amount
        where reserveProjectName = #{reserveProjectName}
    </delete>
    <delete id="deleteAcqProcess">
        delete
        from md_acquisition_process
        where reserveProjectName = #{reserveProjectName}
    </delete>
    <delete id="deleteNonProcess">
        delete
        from xm_nonpower_process
        where reserveProjectName = #{reserveProjectName}
    </delete>
    <delete id="deleteConProcess">
        delete
        from sg_construction_process
        where reserveProjectName = #{reserveProjectName}
    </delete>
    <delete id="deleteFundProcess">
        delete
        from xm_fund_process
        where reserveProjectName = #{reserveProjectName}
    </delete>
    <delete id="deleteSettProcess">
        delete
        from xm_settlement_process
        where reserveProjectName = #{reserveProjectName}
    </delete>
    <delete id="deleteConUpload">
        delete
        from xm_construction_upload
        where reserveProjectName = #{reserveProjectName}
    </delete>
    <delete id="deleteUploadImages">
        delete
        from xm_upload_images
        where reserveProjectName = #{reserveProjectName}
    </delete>
    <delete id="deleteProjectAss">
        delete
        from xm_project_assignment
        where reserveProjectName = #{reserveProjectName}
    </delete>
    <delete id="deleteProjectDis">
        delete
        from xm_project_disclosureplan
        where reserveProjectName = #{reserveProjectName}
    </delete>
    <delete id="deleteProjectBas">
        delete
        from xm_project_basicinformation
        where reserveProjectName = #{reserveProjectName}
    </delete>
    <update id="resetProject">
        update xm_material_project
        set status              = '0',
            acquisitionStatus   = '0',
            advanceStatus       = '0',
            constructionStatus  = '0',
            whetherCloseStatus  = '0',
            settStatus          = '0',
            policyStatus        = '0',
            earlyMaterial       = '0',
            preConstruction     = '0',
            canEnter            = '0',
            advanceConstruction = '0',
            arrageProject       = '0'
        where id = #{id}
    </update>
</mapper>