<?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.yeejoin.amos.boot.module.jxiop.biz.mapper2.SjglZsjZsbtzMapper">
    <select id="getStationCapactityByStationWerks" resultType="Double">
        select SUM(CAPACITYL) from sjgl_zsj_zsbtz where WERKS = #{WERKS}
    </select>

    <select id="getStationInfoMapByStationWerks" resultType="map">

            SELECT LEFT
            ( SBMC, 4 ) AS NAME,
            GROUP_CONCAT(REPLACE (REPLACE( substring( SBMC, 5 ),'风机系统', '' ) ,"#","" )) as equipNum
        FROM
            `sjgl_zsj_zsbtz`
        WHERE
            MACHGENRE = #{DATAID} and WERKS = #{WERKS}
        GROUP BY
            FSB

</select>

    <select id="getStationInfoMapByStationGFWerks" resultType="map">

        SELECT LEFT
            ( b.SBMC, 4 ) AS NAME,
            GROUP_CONCAT( REPLACE ( SUBSTRING_INDEX( b.SBMC, "#",- 1 ), '光伏阵区系统', '' ) ) AS equipNum
        FROM
            (
            SELECT
            *
            FROM
            sjgl_zsj_zsbtz
            WHERE
            FSB IN ( SELECT DBID FROM `sjgl_zsj_zsbtz` WHERE MACHGENRE = ( SELECT DATAID FROM `tpri_dmp_databook` WHERE DATANAME = #{DATAID} ) AND WERKS = #{WERKS} )
            ) B
        GROUP BY
            B.FSB
    </select>

    <select id="getEquipQrcodeInfo" resultType="com.yeejoin.amos.boot.module.jxiop.api.entity.StationQrCodeStatistics">
        SELECT
            WERKS AS `code`,
            count( QRCODE_COLOR = 'red' OR NULL ) AS redNum,
            count( QRCODE_COLOR = 'yellow' OR NULL ) AS yellowNum,
            count( QRCODE_COLOR = 'green' OR NULL ) AS greenNum,
            'equip' AS type,
            CURRENT_DATE AS recordDate
        FROM
            sjgl_zsj_zsbtz
        GROUP BY
            WERKS
    </select>

    <select id="getTaskQrcodeInfo" resultType="com.yeejoin.amos.boot.module.jxiop.api.entity.StationQrCodeStatistics">
        SELECT
            WERKS AS `code`,
            count( QRCODE_COLOR = 'red' OR NULL ) AS redNum,
            count( QRCODE_COLOR = 'yellow' OR NULL ) AS yellowNum,
            count( QRCODE_COLOR = 'green' OR NULL ) AS greenNum,
            'job' AS type,
            CURRENT_DATE AS recordDate
        FROM
            fdgl_job_main
        GROUP BY
            WERKS
    </select>

    <select id="getEquipYardStatistics" resultType="java.util.Map">
        SELECT
        a.QRCODE_COLOR as qrCodeColor,
        count( 1 ) as value
        FROM
        sjgl_zsj_zsbtz a
        INNER JOIN privilege_company b on a.WERKS = b.COMPANY_CODE and b.IS_DELETED = false AND b.`LEVEL` = 'station'
        <where>
            <if test="parentCode != null and parentCode != ''">
                b.ORG_CODE  like concat(#{parentCode},'%')
            </if>
        </where>
        GROUP BY
        QRCODE_COLOR
    </select>

    <select id="getJobYardStatistics" resultType="java.util.Map">
        SELECT
            a.QRCODE_COLOR as qrCodeColor,
            count(1) as value
        FROM
        fdgl_job_main a
        INNER JOIN privilege_company b on a.WERKS = b.COMPANY_CODE and b.IS_DELETED = false AND b.`LEVEL` = 'station'
        <where>
            <if test="parentCode != null and parentCode != ''">
                b.ORG_CODE  like concat(#{parentCode},'%')
            </if>
        </where>
        GROUP BY a.QRCODE_COLOR
    </select>

    <select id="getEquipYardByPage" resultType="java.util.Map">
        SELECT
        ifnull(a.SBMC, '') as objectName ,
        ifnull(a.QRCODE_COLOR, '') AS qrCodeColor,
        ifnull(b.COMPANY_NAME, '') AS stationName,
        ifnull(a.UPDATE_STAMP, '') as recDate,
        a.SBBM as objectId,
        (case when a.QRCODE_COLOR = 'red' then 1
        when a.QRCODE_COLOR = 'yellow' then 2
        else 3 end) as sort
        FROM
        sjgl_zsj_zsbtz a
        INNER JOIN privilege_company b ON a.WERKS = b.COMPANY_CODE and b.IS_DELETED = false AND b.`LEVEL`  = 'station'
        <where>
            <if test="parentCode != null and parentCode != ''">
                AND b.ORG_CODE like concat(#{parentCode},'%')
            </if>
            <if test="date != null and date != ''">
                AND a.UPDATE_STAMP like concat(#{date},'%')
            </if>
            <if test="qrCodeColor != null and qrCodeColor != ''">
                AND a.QRCODE_COLOR = #{qrCodeColor}
            </if>
            <if test="ne != null and ne != ''">
                AND a.QRCODE_COLOR != #{ne}
            </if>
        </where>
        ORDER BY  sort asc, a.UPDATE_STAMP DESC
        limit #{current},#{size}
    </select>

    <select id="getEquipYardByPageCount" resultType="java.lang.Integer">
        SELECT
        count(1)
        FROM
        sjgl_zsj_zsbtz a
        INNER JOIN privilege_company b ON a.WERKS = b.COMPANY_CODE and b.IS_DELETED = false AND b.`LEVEL`  = 'station'
        <where>
            <if test="parentCode != null and parentCode != ''">
                AND b.ORG_CODE like concat(#{parentCode},'%')
            </if>
            <if test="date != null and date != ''">
                AND a.UPDATE_TIME like concat(#{date},'%')
            </if>
            <if test="qrCodeColor != null and qrCodeColor != ''">
                AND a.QRCODE_COLOR = #{qrCodeColor}
            </if>
            <if test="ne != null and ne != ''">
                AND a.QRCODE_COLOR != #{ne}
            </if>
        </where>
    </select>

    <select id="getJobYardByPage" resultType="java.util.Map">
        SELECT
        ifnull(a.JOB_DESCRIPTION, '') as objectName ,
        ifnull(a.qrcode_color, '') AS qrCodeColor,
        ifnull(b.COMPANY_NAME, '') AS stationName,
        ifnull(a.CREATE_TIME, '') as recDate,
        ifnull(a.UPDATE_STAMP, '') as updateTime,
        a.DBID AS objectId,
        c.SBBM as jobId,
        case when a.QRCODE_COLOR = 'red' then 1
        when a.QRCODE_COLOR = 'yellow' then 2
        else 3 end as sort
        FROM
        fdgl_job_main a
        INNER JOIN privilege_company b ON a.WERKS = b.COMPANY_CODE and b.IS_DELETED = false AND b.`LEVEL` = 'station'
        left join sjgl_zsj_zsbtz c on a.EQUIP_DBID = c.DBID
        <where>
            <if test="parentCode != null and parentCode != ''">
                AND b.ORG_CODE like concat(#{parentCode},'%')
            </if>
            <if test="date != null and date != ''">
                AND a.CREATE_TIME like concat(#{date},'%')
            </if>
            <if test="qrCodeColor != null and qrCodeColor != ''">
                AND a.QRCODE_COLOR = #{qrCodeColor}
            </if>
            <if test="ne != null and ne != ''">
                AND a.QRCODE_COLOR != #{ne}
            </if>
        </where>
        ORDER BY sort asc, a.CREATE_TIME DESC
        limit #{current},#{size}
    </select>

    <select id="getJobYardByPageCount" resultType="java.lang.Integer">
        SELECT
        count(1)
        FROM
        fdgl_job_main a
        INNER JOIN privilege_company b ON a.WERKS = b.COMPANY_CODE and b.IS_DELETED = false AND b.`LEVEL` = 'station'
        <where>
            <if test="parentCode != null and parentCode != ''">
                AND b.ORG_CODE like concat(#{parentCode},'%')
            </if>
            <if test="date != null and date != ''">
                AND a.CREATE_TIME like concat(#{date},'%')
            </if>
            <if test="qrCodeColor != null and qrCodeColor != ''">
                AND a.QRCODE_COLOR = #{qrCodeColor}
            </if>
            <if test="ne != null and ne != ''">
                AND a.QRCODE_COLOR != #{ne}
            </if>
        </where>
    </select>

    <select id="getEquipDetailInfoByObjectId" resultType="java.util.Map">
        SELECT
            IFNULL(`SBBM`, '') as `code`,
            IFNULL(SBMC, '') as `name`,
            IFNULL(GGXH, '') as typeCode,
            IFNULL(SCCJ, '') as source,
            '' as stationName,
            IFNULL(QRCODE_COLOR, 'green') as qrCodeColor,
            IFNULL(UPDATE_STAMP, '') AS recDate
        FROM
            sjgl_zsj_zsbtz
        where SBBM = #{objectId}
        limit 1
    </select>

    <select id="getJobDetailInfoByObjectId" resultType="java.util.Map">
        SELECT
            IFNULL(a.`JOB_DESCRIPTION`, '') as `name`,
            case
                when a.QRCODE_COLOR = 'red' then '重大'
                when a.QRCODE_COLOR = 'yellow' then '超时'
                else '正常' end as `status`,
            IFNULL(a.QRCODE_COLOR, 'green') as qrCodeColor,
            b.REAL_NAME as person,
            b.MOBILE as phone,
            IFNULL(a.CREATE_TIME, '') as `recDate`
        FROM
            fdgl_job_main a
            left join privilege_agency_user b on a.ZFZR = b.SEQUENCE_NBR
        where a.DBID = #{objectId}
    </select>
</mapper>
