<?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.dbcapp.carbon.mapper.InstrumentDao">


    <select id="getMeasurementList" resultType="com.nantian.cloud.dbcapp.carbon.domain.carbonAccount.InstrumentDetailVo">
        SELECT i.name,i.model,i.grade,i.RANGE,i.manufacturer,i.code,i.address,cat.category_name,lev.level_name,i.checkday,e.ent_code,e.ent_name,
               CASE WHEN i.status = 0 THEN '停用' WHEN i.status = 1 THEN '启用' END AS statu
        FROM instrument_info i
                 LEFT JOIN ent_info e ON e.ent_id = i.ent_id
                 LEFT JOIN dic_instrument_category cat ON cat.category_code = i.category
                 LEFT JOIN dic_instrument_level lev ON lev.level_code  = i.level
        WHERE e.ENT_ID  = #{entId}
    </select>


    <select id="selectEntList" resultType="com.nantian.cloud.dbcapp.carbon.domain.carbonAccount.EntInstrumentVo">
        SELECT ie.REQUIRE_NUM as `REQUIRE`,
            COALESCE(ie.ACTUAL, 0) AS actual,
            ie.INSPECT ,ans.ENT_NAME as entName,
            ans.`LEVEL` ,
            ans.`CATEGORY`,
            ans.region_name as regionName,
            ie.ENT_ID as entId,
            ans.INDUSTRY_CODE,
            ROUND(COALESCE(ie.ACTUAL, 0)*1.0 / IFNULL(ie.REQUIRE_NUM,0), 2)*100 AS ratio
        FROM instrucment_equipment ie JOIN
        (   SELECT  e.ENT_NAME,
                    i.`LEVEL` ,
                    i.`CATEGORY`,
                    e.ENT_ID,e.region_name,
                    e.INDUSTRY_CODE,
                    e.REGION_CODE
            FROM ent_info e
            LEFT JOIN instrument_info i ON e.ENT_ID = i.ENT_ID
        WHERE i.STATUS = 1
        <if test="entName != null and entName !=''">
            AND e.ENT_NAME like concat('%', #{entName}, '%')
        </if>
        <if test="industryCode != null and industryCode !=''">
            AND e.INDUSTRY_CODE = #{industryCode}
        </if>
        <if test="level != null and level !=''">
            AND i.LEVEL = #{level}
        </if>
        <if test="regionCode != null and regionCode !=''">
            AND e.REGION_CODE = #{regionCode}
        </if> ) AS ans ON ans.ENT_ID = ie.ENT_ID AND ans.CATEGORY = ie.CATEGORY_CODE
        GROUP BY ie.CATEGORY_CODE,ie.ENT_ID
    </select>

    <select id="selectEntListBySumList" resultType="com.nantian.cloud.dbcapp.carbon.domain.carbonAccount.EntInstrumentVo">
        SELECT ie.REQUIRE_NUM as `REQUIRE`,
            COALESCE(ie.ACTUAL, 0) AS actual,
            ie.INSPECT ,ans.ENT_NAME as entName,
            ans.`LEVEL` ,
            ans.`CATEGORY`,
            ans.region_name as regionName,
            ie.ENT_ID as entId,
            ans.INDUSTRY_CODE,
            ROUND(COALESCE(ie.ACTUAL, 0)*1.0 / IFNULL(ie.REQUIRE_NUM,0), 2)*100 AS ratio
        FROM instrucment_equipment ie JOIN
        (   SELECT  e.ENT_NAME,
                    i.`LEVEL` ,
                    i.`CATEGORY`,
                    e.ENT_ID,e.region_name,
                    e.INDUSTRY_CODE,
                    e.REGION_CODE
            FROM ent_info e
            LEFT JOIN instrument_info i ON e.ENT_ID = i.ENT_ID
        WHERE i.STATUS = 1
        <if test="entName != null and entName !=''">
            AND e.ENT_NAME like concat('%', #{entName}, '%')
        </if>
        <if test="industryCode != null and industryCode !=''">
            AND e.INDUSTRY_CODE = #{industryCode}
        </if>
        <if test="level != null and level !=''">
            AND i.LEVEL = #{level}
        </if>
        <if test="regionCode != null and regionCode !=''">
            AND e.REGION_CODE = #{regionCode}
        </if> ) AS ans ON ans.ENT_ID = ie.ENT_ID AND ans.CATEGORY = ie.CATEGORY_CODE
        GROUP BY ie.CATEGORY_CODE,ie.ENT_ID
    </select>

    <select id="selectLevelList" resultType="com.nantian.cloud.dbcapp.carbon.domain.carbonAccount.InstrumentLevel">
        select ID as id ,LEVEL_CODE as levelCode,LEVEL_NAME as levelName
        from dic_instrument_level
    </select>

    <select id="selectCategoryList"
            resultType="com.nantian.cloud.dbcapp.carbon.domain.carbonAccount.InstrumentCategory">
        select ID as id ,CATEGORY_CODE as categoryCode,CATEGORY_NAME as categoryName
        from dic_instrument_category
    </select>

    <select id="selectEntConsumeTypeList"
            resultType="com.nantian.cloud.dbcapp.carbon.domain.carbonAccount.EntConsumeType">
        select ID as id,CODE as code,NAME as name
        from dic_ent_consume_type
    </select>

</mapper>
