<?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="org.springblade.modules.project.mapper.ProjectInfoMapper">
    <update id="updateAllYearlyInvest">
        update d_project_invest_plan set start_finish_invest=#{allYearlyInvest} where project_id=#{projectId} and is_deleted=0;
    </update>
    <delete id="deleteWarning">
        delete from d_project_warning
    </delete>
    <delete id="deleteProgressByProjectId">
        delete from d_project_plan_progress where project_id = #{projectId}
    </delete>

    <select id="detail" resultType="org.springblade.modules.project.pojo.vo.ProjectInfoDetailVO">
        select * from d_project_info where id = #{id}
    </select>
    <select id="detail2" resultType="org.springblade.modules.project.pojo.vo.ProjectAndPlanDetailVO">
        select *,
               (select dict_value from blade_dict_biz where code = 'projectStage' and dict_key = dpi.project_stage and is_deleted = 0) as projectStageName,
               (select dict_value from blade_dict_biz where code = 'projectType' and dict_key = dpi.project_type and is_deleted = 0) as projectTypeName
        from d_project_info dpi where id = #{id}
    </select>
    <select id="getAllPlanByProjectId"
            resultType="org.springblade.modules.project.pojo.vo.ProjectInvestPlanVO">
        select * from d_project_invest_plan
        where project_id = #{id} and is_deleted = 0
        order by  plan_year
    </select>
    <select id="getAllProgressByProjectId"
            resultType="org.springblade.modules.project.pojo.vo.ProjectPlanProgressVO">
        select * from d_project_plan_progress
        where project_id = #{id} and is_deleted = 0
    </select>
    <select id="page" resultType="org.springblade.modules.project.pojo.vo.ProjectInfoVO">
        select *,
               if (build_scale_unit = 1,concat(build_scale,'(公里)'),build_scale) as buildScaleName,
               if (is_focus_project = 1,'是','否') as isFocusProjectName,
               if (is_pilot_plan = 1,'是','否') as isPilotPlanName,
               (select sum(year_finish_invest) from d_project_invest_plan where is_deleted = 0 and project_id = dpi.id ) as finishedInvestMoney,
               (select sum(plan_ratio) from d_project_plan_progress where is_deleted = 0 and project_id = dpi.id) as Progress,
               (select sum(fill_field) from d_project_plan_progress where is_deleted = 0 and project_id = dpi.id) as finishedFill,
               (select count(1) from d_project_invest_plan where is_deleted = 0 and is_plan = 1 and project_id = dpi.id) as hasPlanYearTotal,
               (select dict_value from blade_dict_biz where code = 'projectStage' and dict_key = dpi.project_stage and is_deleted = 0) as projectStageName,
               (select dict_value from blade_dict_biz where code = 'projectType' and dict_key = dpi.project_type and is_deleted = 0) as projectTypeName
        from d_project_info dpi where is_deleted = 0
        <if test="dto.projectStage != null">
            and project_stage = #{dto.projectStage}
        </if>
        <if test="dto.projectType != null">
            and project_type = #{dto.projectType}
        </if>
        <if test="dto.searchValue != null and dto.searchValue != ''">
            and dpi.name like concat('%', #{dto.searchValue},'%')
        </if>
        <if test="dto.startYear != null">
            and start_year &gt;= #{dto.startYear}
        </if>
        <if test="dto.endYear != null ">
            and end_year &lt;= #{dto.endYear}
        </if>

    </select>
    <select id="dataProjectStat" resultType="org.springblade.modules.project.pojo.vo.DataProjectStatVO2">
        select id
        from d_project_info dpi
        where is_deleted = 0 and #{dto.year} between start_year and end_year
        <if test="dto.projectStage != null ">
            and project_stage = #{dto.projectStage}
        </if>
        <if test="dto.projectType != null ">
            and project_type = #{dto.projectType}
        </if>
    </select>
    <select id="dataInvestStat" resultType="org.springblade.modules.project.pojo.vo.DataInvestStatVO2">
        select id,project_stage,project_type,
            (select dict_value from blade_dict_biz where code = 'projectStage' and dict_key = dpi.project_stage and is_deleted = 0) as projectStageName,
            (select dict_value from blade_dict_biz where code = 'projectType' and dict_key = dpi.project_type and is_deleted = 0) as projectTypeName
        from d_project_info dpi
        where is_deleted = 0 and #{dto.year} between start_year and end_year
        <if test="dto.projectStage != null ">
            and project_stage = #{dto.projectStage}
        </if>
        <if test="dto.projectType != null ">
            and project_type = #{dto.projectType}
        </if>
    </select>
    <select id="dataScheduleStat" resultType="org.springblade.modules.project.pojo.vo.DataScheduleStatVO2">
        select id,project_stage,project_type,
        (select dict_value from blade_dict_biz where code = 'projectStage' and dict_key = dpi.project_stage and is_deleted = 0) as projectStageName,
        (select dict_value from blade_dict_biz where code = 'projectType' and dict_key = dpi.project_type and is_deleted = 0) as projectTypeName
        from d_project_info dpi
        where is_deleted = 0 and #{dto.year} between start_year and end_year
        <if test="dto.projectStage != null ">
            and project_stage = #{dto.projectStage}
        </if>
        <if test="dto.projectType != null ">
            and project_type = #{dto.projectType}
        </if>
    </select>
    <select id="dataEndDate" resultType="java.time.LocalDate">
        select update_time from d_project_plan_progress order by update_time desc limit 1
    </select>
    <select id="getAllYearPlan" resultType="org.springblade.modules.project.pojo.vo.InvestPlanWarningVO">
        select *,(select name from d_project_info dpi where dpi.id = pip.project_id) as projectName
        from d_project_invest_plan pip
        where is_deleted = 0
    </select>
    <select id="getBuildTotal" resultType="java.lang.Long">
        select ifnull(sum(build_scale),0)
        from d_project_info dpi where is_deleted = 0 and build_scale_unit = 1
        <if test="dto.projectStage != null">
            and project_stage = #{dto.projectStage}
        </if>
        <if test="dto.projectType != null">
            and project_type = #{dto.projectType}
        </if>
        <if test="dto.searchValue != null and dto.searchValue != ''">
            and name like concat('%', #{dto.searchValue},'%')
        </if>
        <if test="dto.startYear != null">
            and start_year &gt;= #{dto.startYear}
        </if>
        <if test="dto.endYear != null ">
            and end_year &lt;= #{dto.endYear}
        </if>
    </select>
    <select id="getProjectYear" resultType="org.springblade.modules.project.pojo.entity.ProjectInfo">
        SELECT min(start_year) AS startYear, max(end_year) AS endYear
        FROM d_project_info;
    </select>
    <select id="getAllYearlyInvestByProjectId" resultType="java.math.BigDecimal">
        select ifnull(sum(yearly_invest),0)
        from d_project_invest_plan
        where is_deleted = 0 and project_id = #{projectId}
    </select>

</mapper>
