<?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.TSupervisionUnitEnergyMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.nantian.cloud.jljc.entity.TSupervisionUnitEnergy">
        <id column="id" property="id"/>
        <result column="userid" property="userid"/>
        <result column="update_time" property="updateTime"/>
        <result column="del_flag" property="delFlag"/>
        <result column="dept_id" property="deptId"/>
        <result column="monitor_id" property="monitorId"/>
        <result column="remark" property="remark"/>
        <result column="product_name" property="productName"/>
        <result column="production" property="production"/>
        <result column="unit" property="unit"/>
        <result column="design_capacity" property="designCapacity"/>
        <result column="comprehensive_energy" property="comprehensiveEnergy"/>
        <result column="process" property="process"/>
        <result column="unit_product_limit_standard" property="unitProductLimitStandard"/>
        <result column="unit_product_limit_unit" property="unitProductLimitUnit"/>
        <result column="result" property="result"/>
        <result column="unit_product_energy" property="unitProductEnergy"/>
        <result column="code" property="code"/>
        <result column="file" property="file"/>
        <result column="calculation_results" property="calculationResults"/>
        <result column="type" property="type"/>
        <result column="evaluation_criteria" property="evaluationCriteria"/>
        <result column="tce" property="tce"/>
        <result column="symbol" property="symbol"/>
        <result column="production_num" property="productionNum"/>
        <result column="design_capacity_unit" property="designCapacityUnit" />
        <result column="line_number" property="lineNumber" />
        <result column="electrolyzer_specifications" property="electrolyzerSpecifications" />
        <result column="old_code" property="oldCode" />
        <result column="year" property="year" />
        <result column="pid" property="pid" />
        <result column="task_name" property="taskName" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, userid, update_time, del_flag, dept_id, monitor_id, remark, product_name, production, unit, design_capacity,
        comprehensive_energy, process, unit_product_limit_standard, result, unit_product_energy, code, file, calculation_results, type,
        evaluation_criteria, tce, symbol, unit_product_limit_unit,year,production_num,design_capacity_unit, line_number, electrolyzer_specifications, old_code, pid, task_name
    </sql>

    <insert id="saveTask" useGeneratedKeys="true" keyProperty="id">
        insert into t_supervision_unit_energy_task( pid, status, status2, codes, name, process, process_name, monitor_id, year)
        values( #{task.pid}, #{task.status}, #{task.status2}, #{task.codes}, #{task.name}, #{task.process}, #{task.processName}, #{task.monitorId}, #{task.year})
    </insert>

    <select id="getType" resultType="java.lang.String">
        select DISTINCT t.name
        from t_supervision_unit_energy e
        left join t_supervision_unit_energy_type t on e.type = t.id
        where e.monitor_id = #{monitorId}
    </select>

    <select id="initializeByMonitor" resultType="com.nantian.cloud.jljc.entity.TSupervisionUnitEnergy">
        select distinct null id, t1.userid, t1.update_time, t1.del_flag, t1.dept_id, t1.monitor_id, t1.remark, t1.product_name, t1.production, t1.unit, t1.design_capacity, t1.comprehensive_energy,
        t1.process, t1.unit_product_limit_standard, t1.result, t1.unit_product_energy, t1.code, t1.file, t1.calculation_results, t1.type, t1.evaluation_criteria, t1.tce, t1.symbol,
        t1.unit_product_limit_unit, null year, t1.production_num, t1.design_capacity_unit, t1.line_number, t1.electrolyzer_specifications, t1.old_code
        from t_supervision_unit_energy t1, (
            select t1.monitor_id, t1.id, count(t2.id) num, GROUP_CONCAT(t2.product_name) product_name
            from t_supervise_task  t1
            left join t_supervision_unit_energy t2 on t1.monitor_id = t2.monitor_id and t2.tce = 0 and t2.del_flag = '1'
            where t1.epc = (select epc from t_supervise_task where monitor_id = #{monitorId} and del_flag = '1')
            group by t1.monitor_id having num >= 1 order by id desc limit 0,1
        ) t2 where t1.monitor_id = t2.monitor_id and t1.tce = 0 and t1.del_flag = '1'
        <if test="lineNumber!=null and lineNumber!=''">
            and t1.line_number = #{lineNumber}
        </if>
    </select>

    <select id="initializeByIndustry" resultType="com.nantian.cloud.jljc.entity.TSupervisionUnitEnergy">
         select h2.code, h2.name product_name, h2.name process
         from h_kind h1
         left join h_kind h2 on h1.code = h2.pcode
         where	h1.name = (select industry from t_supervise_task where monitor_id = #{monitorId} and del_flag = '1')
    </select>
    <select id="selectTaskByMonitor" resultType="com.nantian.cloud.jljc.entity.TSupervisionUnitEnergyTask">
        select 1 pid, (select id from t_supervision_unit_energy_task where del_flag = '1' and monitor_id = #{query.monitorId} and year = #{query.year} and name = '综合能耗计算' limit 0,1) id,
         #{query.monitorId} monitor_id, '' process, '全厂' process_name, '综合能耗计算' name, '0' codes, null unit_energy_id,  #{query.year} year,
         (select case when count(1) >=1 then '已完成' else '未完成' end status from t_supervision_comprehensive_energy where del_flag = '1' and monitor_id = #{query.monitorId} and year = #{query.year}) status, 'false' status2
        union
        select IFNULL(pid,0) pid, t1.id, t1.monitor_id, t1.process, t1.process_name, t1.name, t1.codes, t1.unit_energy_id, t1.year,
        case when t1.status is not null then t1.status else (case when t1.unit_energy_id is not null then '已完成' else '未完成' end) end status, t1.status2
        from t_supervision_unit_energy_task t1
        where t1.del_flag = '1' and t1.monitor_id = #{query.monitorId} and year = #{query.year} and t1.name = '单位产品综合能耗计算' order by pid asc
    </select>
    <select id="initializeTaskByMonitor" resultType="com.nantian.cloud.jljc.entity.TSupervisionUnitEnergyTask">
        select 1 pid, '' process, '全厂' process_name, '综合能耗计算' name, '0' codes, #{query.monitorId} monitor_id, #{query.year} year, '未完成' status, 'false' status2
        union
        select distinct IFNULL(pid,0) pid, t1.process, t1.process_name, '单位产品综合能耗计算' name, t1.codes, #{query.monitorId} monitor_id, #{query.year} year, '未完成' status, 'false' status2
        from t_supervision_unit_energy_task t1, (
            select t1.monitor_id, t2.year, count(t2.id) num, GROUP_CONCAT(t2.process_name) process_name
            from t_supervise_task  t1
            left join t_supervision_unit_energy_task t2 on t1.monitor_id = t2.monitor_id and t2.del_flag = '1'
            where t1.epc = (select epc from t_supervise_task where monitor_id = #{query.monitorId} and del_flag = '1')
            group by t1.monitor_id, t2.year having num >= 1 order by t2.year desc limit 0,1
        ) t2
        where t1.monitor_id = t2.monitor_id and t1.year = t2.year and t1.name = '单位产品综合能耗计算' and t1.del_flag = '1' order by pid asc
    </select>
    <select id="initializeTaskByIndustry" resultType="com.nantian.cloud.jljc.entity.TSupervisionUnitEnergyTask">
         select 1 pid, '' process, '全厂' process_name, '综合能耗计算' name, '0' codes, #{query.monitorId} monitor_id, #{query.year} year, '未完成' status, 'false' status2
         union
         select 0 pid, h2.name process, h2.name process_name, '单位产品综合能耗计算' name, h2.code codes, #{query.monitorId} monitor_id, #{query.year} year, '未完成' status, 'false' status2
         from h_kind h1
         left join h_kind h2 on h1.code = h2.pcode
         where h1.name = (select industry from t_supervise_task where monitor_id = #{query.monitorId} and del_flag = '1')
    </select>

</mapper>
