<?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="cn.cecep.talroad.mapper.AmProblemFiMapper">

    <select id="selectChartData" resultType="java.util.Map">
        select
            mapf.problem_type AS "problemType",
            rpt.name AS "problemTypeName",
            count(*) AS "count"
        from m_am_problem_fi mapf
        LEFT JOIN pc_factory pf ON pf.id = mapf.factory_id
        INNER JOIN pc_ra_problem_type rpt ON mapf.problem_type = rpt.id AND rpt.is_status = '1'
        <where>
            <if test="chartDataParam.factoryId != null and chartDataParam.factoryId != ''">
                AND mapf.factory_id = #{chartDataParam.factoryId}
            </if>
            <if test="chartDataParam.industry != null and chartDataParam.industry != ''">
                AND pf.industry_category = #{chartDataParam.industry}
            </if>
            <if test="chartDataParam.problemTypeStartWith != null and chartDataParam.problemTypeStartWith != ''">
                AND mapf.problem_type LIKE CONCAT(#{chartDataParam.problemTypeStartWith}, '%')
            </if>
            <if test="chartDataParam.problemType != null and chartDataParam.problemType.size() > 0">
                and mapf.problem_type IN
                <foreach collection="chartDataParam.problemType" open="(" separator="," close=")" item="item">
                    #{item}
                </foreach>
            </if>
            <if test="chartDataParam.startTime != null and chartDataParam.startTime != ''">
                and mapf.analyse_time >= to_timestamp(#{chartDataParam.startTime}, 'yyyy-mm-dd')
            </if>
            <if test="chartDataParam.endTime != null and chartDataParam.endTime != ''">
                and mapf.analyse_time &lt;= to_timestamp(#{chartDataParam.endTime}, 'yyyy-mm-dd hh24:MI:SS')
            </if>
            <if test="regionCodes != null and regionCodes.size() != 0">
                and pf.country_code IN
                <foreach collection="regionCodes" separator="," item="item" close=")" open="(">
                    #{item,jdbcType=VARCHAR}
                </foreach>
            </if>
        </where>
        group by mapf.problem_type, rpt.name
        ORDER BY mapf.problem_type
    </select>

    <sql id="selectProblemFi_SQL">
        SELECT
            t1.id,
            t1.problem_sources,
            t1.problem_type,
            t1.factory_id,
            t2.factory_name,
            t2.country_name AS region_name,
            t4.arguments_name AS industry_name,
            t1.station_name,
            t1.analyse_time,
            t1.analyse_time_end,
            t1.param_name,
            t1.problem_details,
            t1.feedback_date,
            t1.feedback_desc,
            t1.problem_status,
            t1.rectify_limit_start_time,
            t1.rectify_limit_end_time,
            t1.rectify_limit_sum,
            t1.rectify_time,
            t1.sign_time,
            t1.problem_audit_type,
            CASE WHEN pt.route = 'IssuesInfoPage' and t1.model_valid_person is null THEN '2' ELSE '1' END status
        FROM m_am_problem_fi t1
        LEFT JOIN pc_factory t2 ON t1.factory_id = t2.id
        left join (
            select
            plf.factory_id, plv.arguments_code, plv.arguments_name
            from pc_label_factory plf
            left join pc_label_value plv on plf.label_id = plv.label_id and plf.label_value = plv.arguments_code
            where plf.label_id = '4'
        ) t4 on t1.factory_id = t4.factory_id
        INNER JOIN pc_ra_problem_type pt ON t1.problem_type = pt.id AND pt.is_status = '1'
        <where>
            <if test="query.id != null and query.id != ''">
                and t1.id = #{query.id}
            </if>
            <if test="query.problemSources != null  and query.problemSources != ''">
                and t1.problem_sources = #{query.problemSources}
            </if>
            <if test="query.problemTypes != null and query.problemTypes.size() > 0">
                and t1.problem_type IN
                <foreach collection="query.problemTypes" open="(" separator="," close=")" item="item">
                    #{item}
                </foreach>
            </if>
            <if test="query.stationName != null  and query.stationName != ''">
                and t1.station_name like concat('%', #{query.stationName}, '%')
            </if>
            <if test="query.paramName != null  and query.paramName != ''">
                and t1.param_name like concat('%', #{query.paramName}, '%')
            </if>
            <if test="query.factoryId != null  and query.factoryId != ''">
                and t1.factory_id = #{query.factoryId}
            </if>
            <if test="query.factoryName != null  and query.factoryName != ''">
                and t2.factory_name like concat('%', #{query.factoryName}, '%')
            </if>
            <if test="query.industryCode != null and query.industryCode != ''">
                and t4.arguments_name = #{query.industryCode}
            </if>
            <if test="query.regionCode != null and query.regionCode != ''">
                and t2.country_code = #{query.regionCode}
            </if>
            <if test="query.regionCodeList != null and query.regionCodeList.size() > 0">
                and t2.country_code IN
                <foreach collection="query.regionCodeList" open="(" separator="," close=")" item="item">
                    #{item}
                </foreach>
            </if>
            <if test="query.analyseTimeStart != null and query.analyseTimeStart != ''">
                and (
                    t1.analyse_time >= to_timestamp(#{query.analyseTimeStart}, 'yyyy-mm-dd') or
                    t1.analyse_time_end >= to_timestamp(#{query.analyseTimeStart}, 'yyyy-mm-dd')
                )
            </if>
            <if test="query.analyseTimeEnd != null and query.analyseTimeEnd != ''">
                and (
                    t1.analyse_time &lt;= to_timestamp(concat(#{query.analyseTimeEnd}, ' 23:59:59'), 'yyyy-mm-dd hh24:MI:SS') or
                    t1.analyse_time_end &lt;= to_timestamp(concat(#{query.analyseTimeEnd}, ' 23:59:59'), 'yyyy-mm-dd hh24:MI:SS')
                )
            </if>
            <if test="query.pollutantSourceAi != null and query.pollutantSourceAi != ''">
                and t1.pollutant_source_ai = #{query.pollutantSourceAi}
            </if>
            <if test="query.status != null and query.status != ''">
                <choose>
                    <!-- 若路由是“IssuesInfoPage”表示需要验证，若反馈人为空，表示该问题未验证 -->
                    <when test='query.status == "2"'>
                        and (pt.route = 'IssuesInfoPage' and t1.model_valid_person is null)
                    </when>
                    <otherwise>
                        and (pt.route IS NULL OR pt.route != 'IssuesInfoPage' OR t1.model_valid_person IS NOT NULL)
                    </otherwise>
                </choose>
            </if>
           <!-- <if test="query.type != null   ">
                and t1.type = #{query.type,jdbcType=INTEGER}
            </if>-->
          <!--  <if test="query.type != null  and query.type == 1 ">
                and t1.type = 1
                and t1.rectify_limit_start_time is not null
                and t1.rectify_limit_end_time is not null
            </if>-->
            <if test="query.problemStatus != null  and query.problemStatus != ''">
                and t1.problem_status = #{query.problemStatus}
            </if>
            <if test="query.problemStatusList != null and query.problemStatusList.size() > 0">
                and t1.problem_status IN
                <foreach collection="query.problemStatusList" open="(" separator="," close=")" item="item">
                    #{item}
                </foreach>
            </if>
            <if test="query.problemAuditType != null  and query.problemAuditType != ''">
                and t1.problem_audit_type = #{query.problemAuditType}
            </if>
            <if test="query.topType != null  and query.topType != ''">
                and pt.top_type = #{query.topType}
            </if>
            <!-- 查询is_show为null和1的 -->
            AND (t1.is_show IS NULL OR t1.is_show LIKE '1%')
        </where>
        ORDER BY t1.analyse_time DESC, t1.id
    </sql>

    <select id="selectProblemFiPage" resultType="cn.cecep.talroad.vo.AmProblemFiVo">
        <include refid="selectProblemFi_SQL"/>
    </select>

    <select id="selectProblemFiList" resultType="cn.cecep.talroad.vo.AmProblemFiVo">
        <include refid="selectProblemFi_SQL"/>
    </select>
    <select id="selectTrendData" resultType="java.util.Map">
        <choose>
            <when test='dateType == "D"'>
                <bind name="groupColumn" value="'to_char(mapf.analyse_time, \'yyyy-mm-dd\')'"/>
            </when>
            <otherwise>
                <bind name="groupColumn" value="'to_char(mapf.analyse_time, \'yyyy-mm\')'"/>
            </otherwise>
        </choose>

        select
            ${groupColumn} AS "analyseTime",
            count(*) AS "count"
        from m_am_problem_fi mapf
        LEFT JOIN pc_factory pf ON pf.id = mapf.factory_id
        <where>
            <if test="factoryId != null and factoryId != ''">
                AND mapf.factory_id = #{factoryId}
            </if>
            <if test="industry != null and industry != ''">
                AND pf.industry_category = #{industry}
            </if>
            <if test="problemType != null and problemType.size() > 0">
                and mapf.problem_type IN
                <foreach collection="problemType" open="(" separator="," close=")" item="item">
                    #{item}
                </foreach>
            </if>
            <if test="startTime != null and startTime != ''">
                and mapf.analyse_time >= to_timestamp(#{startTime}, 'yyyy-mm-dd')
            </if>
            <if test="endTime != null and endTime != ''">
                and mapf.analyse_time &lt;= to_timestamp(#{endTime}, 'yyyy-mm-dd hh24:MI:SS')
            </if>
        </where>
        group by ${groupColumn}
    </select>

    <select id="selectByLastTimeAlarm" resultType="string">
        SELECT
            problem_details
        FROM
            m_am_problem_fi
        WHERE  station_id=#{pointCode} and
                problem_start_time between #{startTime} and #{endTime}
    </select>

    <select id="selectCountByParams" resultType="java.lang.Integer">
        SELECT
            count(1)
        FROM
            m_am_problem_fi
        WHERE
            station_id = #{amProblemFi.stationId}
            AND problem_sources = #{amProblemFi.problemSources}
            AND problem_type = #{amProblemFi.problemType}
            AND analyse_time &gt;= #{amProblemFi.analyseTime}
            AND analyse_time &lt;= #{amProblemFi.analyseTimeEnd}

    </select>
    <select id="selectAmProblemFiVideo" resultType="cn.cecep.talroad.domain.AmProblemFiVideo$Vo">
        SELECT
            t1.video_path,
            t1.start_time,
            t1.end_time,
            t1.video_time,
            t2.dev_id,
            t2.dev_source,
            t2.dev_name
        FROM m_am_problem_fi_video t1
        INNER JOIN m_am_video_dev_fi t2 ON t1.video_dev_fi_id = t2.id
        where t1.problem_id = #{id}
        ORDER BY t1.video_time, t1."id"
    </select>

    <insert id="insertAmProblemFis">
        INSERT INTO m_am_problem_fi (id,factory_id,problem_sources,problem_type,problem_details,station_name,problem_start_time,problem_end_time,analyse_time,
                                 analyse_time_end,problem_treatment_state,type,extra
        ) VALUES
        <foreach collection="collection" item="item" separator=",">
            (
                #{item.id},#{item.factoryId},#{item.problemSources},#{item.problemType},#{item.problemDetails},#{item.stationName},#{item.problemStartTime},#{item.problemEndTime},#{item.analyseTime},
                #{item.analyseTimeEnd},#{item.problemTreatmentState},#{item.type},#{item.extra}
            )
        </foreach>
    </insert>

    <select id="getProblemByTypeAndTime" resultType="cn.cecep.talroad.domain.AmProblemFi">
        select * from m_am_problem_fi
        where problem_type = #{problemType}
            and to_char(create_time, 'yyyy-mm-dd hh24:MI:SS') >= #{dateStr}
            and to_char(create_time, 'yyyy-mm-dd hh24:MI:SS') &lt; #{dateStrEnd}
    </select>

    <select id="selectAmProblemFiByFactoriesIds" resultType="cn.cecep.talroad.domain.AmProblemFi">
        select factory_id "factoryId",
        type "type",
        problem_type "problemType",
        station_name "stationName",
        analyse_time "analyseTime",
        extra "extra",
        station_id "stationId"
        from m_am_problem_fi
        where analyse_time &lt;= #{endTime}
        and analyse_time &gt;= #{startTime}
        and factory_id in
        <foreach collection="ids" separator="," item="item" close=")" open="(">
            #{item,jdbcType=VARCHAR}
        </foreach>
        and problem_type in
        <foreach collection="list" separator="," item="item" close=")" open="(">
            #{item,jdbcType=VARCHAR}
        </foreach>
    </select>

    <select id="selectAmProblemFiFactoriesIdsByDate" resultType="java.lang.String">
        select distinct factory_id
        from m_am_problem_fi
        where analyse_time &lt;= #{endTime}
        and analyse_time &gt;= #{startTime}
        and problem_type in
        <foreach collection="list" separator="," item="item" close=")" open="(">
            #{item,jdbcType=VARCHAR}
        </foreach>
    </select>

    <select id="selectPollutionWaterData" resultType="cn.cecep.talroad.domain.AmProblemFi">
        select
            m.*
        from m_am_problem_fi m
             inner join pc_outlet o on o.id = m.station_id and o.pollutant_type = '4' and pollutant_outlet_type ='2'
        where
                analyse_time >= #{startTime}
            and analyse_time &lt; #{endTime}
    </select>
    <select id="selectStatisDataByMonth" resultType="cn.cecep.talroad.vo.analysis.FactoryOutletStaticsVo">
        select
            factory_id as factoryId,
            station_id as outletId,
            station_name as outletName,
            param_name as paramName,
            COUNT(1) as actualCount
        from m_am_problem_fi m
        where
        m.problem_type = '003002'
        and m.analyse_time between #{startTime} and #{endTime}
        group by factory_id , station_id, station_name, param_name
    </select>


</mapper>
