<?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.nantian.cloud.jljc.mapper.TDualcontrolProgressMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.nantian.cloud.jljc.entity.TDualcontrolProgress">
        <id column="id" property="id" />
        <result column="year" property="year" />
        <result column="quarter" property="quarter" />
        <result column="item_code" property="itemCode" />
        <result column="item_name" property="itemName" />
        <result column="item_status" property="itemStatus" />
        <result column="gdp_total" property="gdpTotal" />
        <result column="energy_total" property="energyTotal" />
        <result column="energy_unit_gdp" property="energyUnitGdp" />
        <result column="energy_unit_gdp_reducerate" property="energyUnitGdpReducerate" />
        <result column="energy_growthrate" property="energyGrowthrate" />
        <result column="gdp_growthrate" property="gdpGrowthrate" />
        <result column="completion_basic" property="completionBasic" />
        <result column="completion_inspire" property="completionInspire" />
        <result column="user_name" property="userName" />
        <result column="userid" property="userid" />
        <result column="update_time" property="updateTime" />
        <result column="del_flag" property="delFlag" />
        <result column="dept_id" property="deptId" />
    </resultMap>

    <resultMap id="BaseVoMap" type="com.nantian.cloud.jljc.vo.dualControl.DualcontrolVisualizationVo">
        <result column="year" property="year" />
        <result column="quarter" property="quarter" />
        <result column="item_code" property="itemCode" />
        <result column="item_name" property="itemName" />
        <result column="gdp_total" property="gdpTotal" />
        <result column="energy_total" property="energyTotal" />
        <result column="energy_unit_gdp" property="energyUnitGdp" />
        <result column="energy_unit_gdp_reducerate" property="energyUnitGdpReducerate" />
        <result column="pre_gdp_total" property="preGdpTotal" />
        <result column="pre_energy_total" property="preEnergyTotal" />
        <result column="pre_energy_unit_gdp" property="preEnergyUnitGdp" />
        <result column="energy_total_basic" property="energyTotalBasic" />
        <result column="energy_total_inspire" property="energyTotalInspire" />
        <result column="energy_unit_gdp_basic" property="energyUnitGdpBasic" />
        <result column="energy_unit_gdp_inspire" property="energyUnitGdpInspire" />
        <result column="energy_unit_gdp_reducerate_basic" property="energyUnitGdpReducerateBasic" />
        <result column="energy_unit_gdp_reducerate_inspire" property="energyUnitGdpReducerateInspire" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, year, quarter, item_code, item_name, item_status, gdp_total, energy_total, energy_unit_gdp, energy_unit_gdp_reducerate, energy_growthrate, gdp_growthrate, completion_basic, completion_inspire, user_name, userid, update_time, del_flag, dept_id
    </sql>

    <select id="getLogById" resultType="com.nantian.cloud.jljc.entity.TDualcontrolProgressLog">
        select id as progress_id, year, quarter, item_code, item_name, item_status, gdp_total as old_gdp_total, energy_total as old_energy_total
        from t_dualcontrol_progress
        where id = #{id}
    </select>
    <select id="getProgressByQuarter" resultType="com.nantian.cloud.jljc.entity.TDualcontrolProgress">
        select year, "0" quarter, item_code, item_name, sum(gdp_total) gdp_total, sum(energy_total) energy_total
        from t_dualcontrol_progress
        where del_flag = '1' and quarter in ("1","2","3","4")
        <if test="query.year != null and query.year != ''">
            and year = #{query.year}
        </if>
        <if test="query.itemCode != null and query.itemCode != ''">
            and item_code = #{query.itemCode}
        </if>
        group by year
    </select>
    <select id="listByIndex" resultType="com.nantian.cloud.jljc.entity.TDualcontrolProgress">
        select distinct p.id, #{query.year} year, IFNULL(p.quarter,#{query.quarter}) quarter, d.id item_code, d.meaning2 item_name, IFNULL(p.item_status,0) item_status, p.gdp_total, p.energy_total, p.energy_unit_gdp, p.energy_unit_gdp_reducerate, p.user_name, p.userid, p.update_time, p.del_flag, p.dept_id,
        case when p.item_status = '4' then (select audit_comments from t_dualcontrol_progress_apply where progress_id = p.id order by audit_time desc limit 0,1) else '' end remark
        from dict_region d
        left join t_dualcontrol_progress p on d.id = p.item_code and p.year = #{query.year}
        <if test="query.quarter != null and query.quarter != ''">
            and p.quarter like #{query.quarter}
        </if>
        where d.version = 0
        <choose>
            <when test=' query.itemCode.contains("00") '>
                and (d.id = #{query.itemCode} or d.pid = #{query.itemCode})
            </when>
            <otherwise>
                and d.id = #{query.itemCode}
            </otherwise>
        </choose>
        order by d.id asc
    </select>
    <!--<select id="listByIndex" resultType="com.nantian.cloud.jljc.entity.TDualcontrolProgress">
        select distinct p.id, i.year, #{query.quarter} quarter, i.item_code, i.item_name, IFNULL(p.item_status,0) item_status, p.gdp_total, p.energy_total, p.energy_unit_gdp, p.energy_unit_gdp_reducerate, p.user_name, p.userid, p.update_time, p.del_flag, p.dept_id,
                case when p.item_status = '4' then (select audit_comments from t_dualcontrol_progress_apply where progress_id = p.id order by audit_time desc limit 0,1) else '' end remark
        from t_dualcontrol_index i
        left join t_dualcontrol_progress p on i.item_code = p.item_code and i.year = p.year
        <if test="query.quarter != null and query.quarter != ''">
            and p.quarter = #{query.quarter}
        </if>
        where i.del_flag = 1 and i.index_type = '2'
        <if test="query.year != null and query.year != ''">
            and i.year = #{query.year}
        </if>
        <choose>
            <when test=' query.itemCode.contains("0000") '>
                and right(i.item_code,4) != '0000' and right(i.item_code,2) = '00'
            </when>
            <when test=' query.itemCode.contains("00") '>
                and right(i.item_code,2) != '00' and left(i.item_code,4) = left(#{query.itemCode},4)
            </when>
            <otherwise>
                and i.item_code = #{query.itemCode}
            </otherwise>
        </choose>
        <if test=' query.itemCode.contains("00") or query.itemCode.contains("0000") '>
            union
            select distinct p.id, i.year, #{query.quarter} quarter, i.item_code, i.item_name, IFNULL(p.item_status,0) item_status, p.gdp_total, p.energy_total, p.energy_unit_gdp, p.energy_unit_gdp_reducerate, p.user_name, p.userid, p.update_time, p.del_flag, p.dept_id,
                   case when p.item_status = '4' then (select audit_comments from t_dualcontrol_progress_apply where progress_id = p.id order by audit_time desc limit 0,1) else '' end remark
            from t_dualcontrol_index i
            left join t_dualcontrol_progress p on i.item_code = p.item_code and i.year = p.year
            <if test="query.quarter != null and query.quarter != ''">
                and p.quarter = #{query.quarter}
            </if>
            where i.del_flag = 1 and i.index_type = '2' and i.item_code = #{query.itemCode}
            <if test="query.year != null and query.year != ''">
                and i.year = #{query.year}
            </if>
        </if>
        order by item_code asc
    </select>-->
    <select id="sumByIndex" resultType="com.nantian.cloud.jljc.entity.TDualcontrolProgress">
        select distinct p.id, i.year, #{query.quarter} quarter, #{query.itemCode} item_code, 3 item_status, sum(p.gdp_total) gdp_total, sum(p.energy_total) energy_total,
               (select item_name from t_dualcontrol_index where i.index_type = '2' and item_code = #{query.itemCode} and year = i.year) item_name,
                case when p.item_status = '4' then (select audit_comments from t_dualcontrol_progress_apply where progress_id = p.id order by audit_time desc limit 0,1) else '' end remark
        from t_dualcontrol_index i
        left join t_dualcontrol_progress p on i.item_code = p.item_code and i.year = p.year and p.item_status = 2
        <if test="query.quarter != null and query.quarter != ''">
            and p.quarter = #{query.quarter}
        </if>
        where i.del_flag = 1 and i.index_type = '2'
        <if test="query.year != null and query.year != ''">
            and i.year = #{query.year}
        </if>
        <choose>
            <when test=' query.itemCode.contains("0000") '>
                and right(i.item_code,4) != '0000' and right(i.item_code,2) = '00'
            </when>
            <otherwise>
                and right(i.item_code,2) != '00' and left(i.item_code,4) = left(#{query.itemCode},4)
            </otherwise>
        </choose>
        group by i.year
    </select>
    <select id="getVisualizationVoTmp" resultType="com.nantian.cloud.jljc.vo.dualControl.DualcontrolVisualizationVo">
        select i.year, #{quarter} `quarter`, i.item_code, i.item_name, i.energy_total_basic, i.energy_total_inspire, i.energy_unit_gdp_basic, i.energy_unit_gdp_inspire, i.energy_unit_gdp_reducerate_basic,
        i.energy_unit_gdp_reducerate_inspire, p.gdp_total, p.energy_total, p.energy_unit_gdp, pre.gdp_total pre_gdp_total, pre.energy_total pre_energy_total, pre.energy_unit_gdp pre_energy_unit_gdp,
        round(100*((pre.energy_total/pre.gdp_total)-(p.energy_total/p.gdp_total))/(pre.energy_total/pre.gdp_total),2) energy_unit_gdp_reducerate
        from t_dualcontrol_index i
        left join (
            select item_code, item_name, round(sum(gdp_total),2) gdp_total, round(sum(energy_total),2) energy_total, round(sum(energy_total)/sum(gdp_total),2) energy_unit_gdp
            from t_dualcontrol_progress where year between #{start} and #{end} and #{quarter} like concat('%',`quarter`,'%') and item_status = '2'
            group by item_code
         ) p on i.item_code = p.item_code
        left join (
            select item_code, item_name, round(sum(gdp_total),2) gdp_total, round(sum(energy_total),2) energy_total, round(sum(energy_total)/sum(gdp_total),2) energy_unit_gdp
            from t_dualcontrol_progress where year = #{pre} and `quarter` = '0' and item_status = '2'
            group by item_code
        ) pre on i.item_code = pre.item_code
        where i.year = #{plan} and i.index_type = '2'
       <choose>
           <when test=' region.contains("0000") '>
               and right(i.item_code,4) != '0000' and right(i.item_code,2) = '00'
           </when>
           <when test=' region.contains("00") '>
               and right(i.item_code,2) != '00' and left(i.item_code,4) = left(#{region},4)
           </when>
           <otherwise>
               and i.item_code = #{region}
           </otherwise>
       </choose>
       order by i.item_code asc
    </select>
    <select id="getVisualizationSumVoTmp" resultType="com.nantian.cloud.jljc.vo.dualControl.DualcontrolVisualizationVo">
         select i.year, #{quarter} `quarter`, i.item_code, i.item_name, i.energy_total_basic, i.energy_total_inspire, i.energy_unit_gdp_basic, i.energy_unit_gdp_inspire, i.energy_unit_gdp_reducerate_basic,
        i.energy_unit_gdp_reducerate_inspire, p.gdp_total, p.energy_total, p.energy_unit_gdp, pre.gdp_total pre_gdp_total, pre.energy_total pre_energy_total, pre.energy_unit_gdp pre_energy_unit_gdp,
        round(100*((pre.energy_total/pre.gdp_total)-(p.energy_total/p.gdp_total)/(pre.energy_total/pre.gdp_total)),2) energy_unit_gdp_reducerate
        from t_dualcontrol_index i
        left join (
            select item_code, item_name, round(sum(gdp_total),2) gdp_total, round(sum(energy_total),2) energy_total, round(sum(energy_total)/sum(gdp_total),2) energy_unit_gdp
            from t_dualcontrol_progress where year between #{start} and #{end} and #{quarter} like concat('%',`quarter`,'%') and item_status = '2'
            group by item_code
         ) p on i.item_code = p.item_code
        left join (
            select item_code, item_name, round(sum(gdp_total),2) gdp_total, round(sum(energy_total),2) energy_total, round(sum(energy_total)/sum(gdp_total),2) energy_unit_gdp
            from t_dualcontrol_progress where year = #{pre} and `quarter` = '0' and item_status = '2'
            group by item_code
        ) pre on i.item_code = pre.item_code
        where i.year = #{plan} and i.item_code = #{region} and i.index_type = '2'
    </select>
    <select id="getNoDataQuarter" resultType="com.nantian.cloud.jljc.vo.dualControl.DualcontrolNoDataQuarter">
        select t.item_code itemCode, t.item_name itemName, #{year} year
        <foreach collection="list" item="value" index="i">
            ,t.remark${i+1}
        </foreach>
        from (
        select i.item_code, i.item_name
        <foreach collection="list" item="value" index="i">
            ,case when p${i+1}.gdp_total is null or p${i+1}.gdp_total = '' then (
            case when #{value} = '1' then '第一季度' when #{value} = '2' then '第二季度' when #{value} = '3' then '第三季度' when
            #{value} = '4' then '第四季度' else '' end
            ) else '' end remark${i+1}
        </foreach>
        from t_dualcontrol_index i
        <foreach collection="list" item="value" index="i">
            left join t_dualcontrol_progress p${i+1} on p${i+1}.item_code = i.item_code and p${i+1}.year = i.year and p${i+1}.`quarter` = #{value} and p${i+1}.item_status = '2'
        </foreach>
        where i.year = #{year} and i.index_type = '2'
        <choose>
            <when test=' region.contains("0000") '>
                and right(i.item_code,4) != '0000' and right(i.item_code,2) = '00'
            </when>
            <when test=' region.contains("00") '>
                and right(i.item_code,2) != '00' and left(i.item_code,4) = left(#{region},4)
            </when>
            <otherwise>
                and i.item_code = #{region}
            </otherwise>
        </choose>
        ) t
        where 1=1
        <foreach collection="list" item="value" index="i" open=" and (" separator="or"  close=")">
            t.remark${i+1} != ''
        </foreach>
        order by t.item_code asc
    </select>
    <select id="getYearByPlan" resultType="java.lang.String">
        select distinct year
        from t_dualcontrol_progress
        where year between #{start} and #{end} and gdp_total is not null and gdp_total != '' and quarter = '0'
        order by year desc
    </select>
    <select id="getCompletionList" resultType="com.nantian.cloud.jljc.entity.TDualcontrolProgress">
        select d.meaning2 item_name, IFNULL(completion_basic,0) completion_basic, IFNULL(completion_inspire,0) completion_inspire
        from dict_region d
        left join t_dualcontrol_progress p on d.id = p.item_code and p.year = #{year} and p.quarter = #{quarter}
        where 1=1 and d.version = 0
        <choose>
            <when test=' region.contains("00") '>
                and right(d.id,4) != '0000' and right(d.id,2) = '00'
            </when>
            <otherwise>
                and right(d.id,2) != '00' and d.id like concat(left(#{region},4),'%')
            </otherwise>
        </choose>
        order by completion_basic desc;
    </select>
    <!--    <select id="getNoDataQuarter" resultType="com.nantian.cloud.jljc.vo.dualControl.DualcontrolNoDataQuarter">
            select t.item_code itemCode, t.item_name itemName, #{year} year, GROUP_CONCAT(case when t.type = '1' then '第一季度' when t.type = '2' then '第二季度'
            when t.type = '3' then '第三季度' when t.type = '4' then '第四季度' else '' end) quarter
            from(
            <foreach collection="list" item="value" index="i" separator="union">
                select i.item_code, i.item_name , case when p.gdp_total is null or p.gdp_total = '' then #{value} else '' end type
                from t_dualcontrol_index i
                left join t_dualcontrol_progress p on p.item_code = i.item_code and p.year = i.year and  p.`quarter` = #{value}
                where i.year = #{year} and i.index_type = '2'
                <choose>
                    <when test=' region.contains("0000") '>
                        and right(i.item_code,4) != '0000' and right(i.item_code,2) = '00'
                    </when>
                    <when test=' region.contains("00") '>
                        and right(i.item_code,2) != '00' and left(i.item_code,4) = left(#{region},4)
                    </when>
                    <otherwise>
                        and i.item_code = #{region}
                    </otherwise>
                </choose>
            </foreach>
            ) t where t.type != '' group by t.item_code order by t.item_code, t.type asc
        </select>-->
</mapper>
