<?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.progress.mapper.YearProgressMapper">

    <sql id="progressColumns">
        a.id,
        a.projectYear,
		a.projectId,
		a.projectNO,
		a.projectName,
		a.assignedUnit,
		a.rate,
		a.constructionUnit,
		a.ownerProjectManager,
		a.constructionProjectManager,
		a.subcontractor,
		a.designer,
        a.drawingReceived,
        a.deliveryOfDrawings,
		a.problemsInConstruction,
		a.projectOperationStatus,
		a.projectOperationTime,
		a.isTheDesign,
		a.isTheSupervision,
		a.whetherIsIncurred,
		a.whetherIsApproval,
		a.approvedNot,
		a.approvedNotNot,
		a.whetherFlat,
		a.settlementBatch,
		a.projectCloseDate,
		a.assetCardNo
    </sql>

    <select id="get" resultType="YearProgress">
        SELECT
        <include refid="progressColumns"/>
        FROM xm_year_progress a
        WHERE a.id = #{id}
    </select>

    <select id="getByName" resultType="com.jeeplus.modules.sg.progress.entity.YearProgress">
        SELECT
        <include refid="progressColumns"/>
        FROM xm_year_progress a
        WHERE a.projectName = #{projectName}
    </select>

    <select id="findList" resultType="YearProgress">
        SELECT
        <include refid="progressColumns"/>
        FROM xm_year_progress a
        <where>
            a.del_flag = #{DEL_FLAG_NORMAL}
            ${dataScope}
            <if test="projectYear!=null and projectYear !=''">
                and a.projectYear = #{projectYear}
            </if>
            <if test="projectNO!=null and projectNO != ''">
                and a.projectNO = #{projectNO}
            </if>
            <if test="attrs != null and  attrs !='' ">
                and a.projectId in
                <foreach collection="attrs" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </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 update_date DESC
            </otherwise>
        </choose>

    </select>

    <select id="findStatisticPage" resultType="com.jeeplus.modules.sg.progress.entity.PlanningStatistics">
        select
            projectYear,
            constructionUnit,
            SUM(CASE WHEN projectOperationStatus='已竣工' THEN 1 ELSE 0 END) as projectOperationStatusT,
            SUM(CASE WHEN projectOperationStatus='未竣工' THEN 1 ELSE 0 END) as projectOperationStatusF,
            SUM(CASE WHEN whetherIsApproval='是' THEN 1 ELSE 0 END) as whetherIsApprovalT,
            SUM(CASE WHEN whetherIsApproval='否' THEN 1 ELSE 0 END) as whetherIsApprovalF,
            SUM(CASE WHEN approvedNot='是' THEN 1 ELSE 0 END) as approvedNotT,
            SUM(CASE WHEN approvedNot='否' THEN 1 ELSE 0 END) as approvedNotF,
            SUM(CASE WHEN approvedNotNot='已开票' THEN 1 ELSE 0 END) as approvedNotNotT,
            SUM(CASE WHEN approvedNotNot='未开票' THEN 1 ELSE 0 END) as approvedNotNotF
        from xm_year_progress GROUP BY projectYear,constructionUnit order by projectYear
    </select>

    <select id="findYearStatistic" resultType="com.jeeplus.modules.sg.progress.entity.PlanningStatistics">
        select
            projectYear,
            SUM(CASE WHEN projectOperationStatus='已竣工' THEN 1 ELSE 0 END) as projectOperationStatusT,
            SUM(CASE WHEN projectOperationStatus='未竣工' THEN 1 ELSE 0 END) as projectOperationStatusF,
            SUM(CASE WHEN whetherIsApproval='是' THEN 1 ELSE 0 END) as whetherIsApprovalT,
            SUM(CASE WHEN whetherIsApproval='否' THEN 1 ELSE 0 END) as whetherIsApprovalF,
            SUM(CASE WHEN approvedNot='是' THEN 1 ELSE 0 END) as approvedNotT,
            SUM(CASE WHEN approvedNot='否' THEN 1 ELSE 0 END) as approvedNotF,
            SUM(CASE WHEN approvedNotNot='已开票' THEN 1 ELSE 0 END) as approvedNotNotT,
            SUM(CASE WHEN approvedNotNot='未开票' THEN 1 ELSE 0 END) as approvedNotNotF
        from xm_year_progress GROUP BY projectYear order by projectYear
    </select>

    <select id="findAllYear" resultType="string">
        select distinct  projectYear from xm_year_progress ORDER BY projectYear;
    </select>

    <select id="showAllYear" resultType="com.jeeplus.modules.sg.progress.entity.ShowStatistics">
        select distinct  projectYear from xm_year_progress ORDER BY projectYear;
    </select>

    <select id="findAllList" resultType="YearProgress">
        SELECT
        <include refid="progressColumns"/>
        FROM xm_year_progress 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.create_date ASC
            </otherwise>
        </choose>
    </select>

    <insert id="insert">
        INSERT INTO xm_year_progress
        (id,
         create_by,
         create_date,
         update_by,
         update_date,
         del_flag,
         remarks,
         projectYear,
         projectId,
         projectNO,
         projectName,
         assignedUnit,
         rate,
         constructionUnit,
         ownerProjectManager,
         constructionProjectManager,
         subcontractor,
         designer,
         drawingReceived,
         deliveryOfDrawings,
         problemsInConstruction,
         projectOperationStatus,
         projectOperationTime,
         isTheDesign,
         isTheSupervision,
         whetherIsIncurred,
         whetherIsApproval,
         approvedNot,
         approvedNotNot,
         whetherFlat,
         settlementBatch,
         projectCloseDate,
         assetCardNo
         )
        VALUES
               (#{id},
                #{createBy.id},
                #{createDate},
                #{updateBy.id},
                #{updateDate},
                #{delFlag},
                #{remarks},
                #{projectYear},
                #{projectId},
                #{projectNO},
                #{projectName},
                #{assignedUnit},
                #{rate},
                #{constructionUnit},
                #{ownerProjectManager},
                #{constructionProjectManager},
                #{subcontractor},
                #{designer},
                #{drawingReceived},
                #{deliveryOfDrawings},
                #{problemsInConstruction},
                #{projectOperationStatus},
                #{projectOperationTime},
                #{isTheDesign},
                #{isTheSupervision},
                #{whetherIsIncurred},
                #{whetherIsApproval},
                #{approvedNot},
                #{approvedNotNot},
                #{whetherFlat},
                #{settlementBatch},
                #{projectCloseDate},
                #{assetCardNo}
               )
    </insert>

    <insert id="replaceAll">
        REPLACE INTO xm_year_progress
        (id,
         create_by,
         create_date,
         update_by,
         update_date,
         del_flag,
         remarks,
         projectYear,
         projectId,
         projectNO,
         projectName,
         assignedUnit,
         rate,
         constructionUnit,
         ownerProjectManager,
         constructionProjectManager,
         subcontractor,
         designer,
         drawingReceived,
         deliveryOfDrawings,
         problemsInConstruction,
         projectOperationStatus,
         projectOperationTime,
         isTheDesign,
         isTheSupervision,
         whetherIsIncurred,
         whetherIsApproval,
         approvedNot,
         approvedNotNot,
         whetherFlat,
         settlementBatch,
         projectCloseDate,
         assetCardNo
        )
        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.projectYear},
         #{item.projectId},
         #{item.projectNO},
         #{item.projectName},
         #{item.assignedUnit},
         #{item.rate},
         #{item.constructionUnit},
         #{item.ownerProjectManager},
         #{item.constructionProjectManager},
         #{item.subcontractor},
         #{item.designer},
         #{item.drawingReceived},
         #{item.deliveryOfDrawings},
         #{item.problemsInConstruction},
         #{item.projectOperationStatus},
         #{item.projectOperationTime},
         #{item.isTheDesign},
         #{item.isTheSupervision},
         #{item.whetherIsIncurred},
         #{item.whetherIsApproval},
         #{item.approvedNot},
         #{item.approvedNotNot},
         #{item.whetherFlat},
         #{item.settlementBatch},
         #{item.projectCloseDate},
         #{item.assetCardNo}
        )
        </foreach>
    </insert>

    <update id="update">
        UPDATE xm_year_progress
        SET
            projectYear = #{projectYear},
            projectId = #{projectId},
            projectNO = #{projectNO},
            assignedUnit = #{assignedUnit},
            rate = #{rate},
            constructionUnit = #{constructionUnit},
            ownerProjectManager = #{ownerProjectManager},
            constructionProjectManager = #{constructionProjectManager},
            subcontractor = #{subcontractor},
            designer = #{designer},
            drawingReceived = #{drawingReceived},
            deliveryOfDrawings = #{deliveryOfDrawings},
            problemsInConstruction = #{problemsInConstruction},
            projectOperationStatus = #{projectOperationStatus},
            projectOperationTime = #{projectOperationTime},
            isTheDesign = #{isTheDesign},
            isTheSupervision = #{isTheSupervision},
            whetherIsIncurred = #{whetherIsIncurred},
            whetherIsApproval = #{whetherIsApproval},
            approvedNot = #{approvedNot},
            approvedNotNot = #{approvedNotNot},
            whetherFlat = #{whetherFlat},
            settlementBatch = #{settlementBatch},
            projectCloseDate = #{projectCloseDate},
            assetCardNo = #{assetCardNo},
            update_by   = #{updateBy.id},
            update_date = #{updateDate}
        WHERE id = #{id}
    </update>


    <!--物理删除-->
    <update id="delete">
        DELETE FROM xm_year_progress
        <choose>
            <when test="id !=null and id != ''">
                WHERE id = #{id}
            </when>
        </choose>
    </update>

    <!--逻辑删除-->
    <update id="deleteByLogic">
        UPDATE xm_year_progress SET
        del_flag = #{DEL_FLAG_DELETE}
        <choose>
            <when test="id !=null and id != ''">
                WHERE id = #{id}
            </when>
        </choose>
    </update>

</mapper>