<?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.tofly.gpsboot.mapper.plan.PlanFactoryMapper">

    <resultMap id="planFactoryMap" type="com.tofly.pojo.plan.PlanFactory">
        <id property="id" column="ID"/>
        <result property="planId" column="PLAN_ID"/>
        <result property="belongStationId" column="BELONG_STATION_ID"/>
        <result property="belongPumpId" column="BELONG_PUMP_ID"/>
        <result property="category" column="CATEGORY"/>
        <result property="inspectTypeId" column="INSPECT_TYPE_ID"/>
        <result property="deviceId" column="DEVICE_Id"/>
        <result property="deviceType" column="DEVICE_TYPE"/>
        <result property="isOver" column="IS_OVER"/>
        <result property="isDeviceError" column="IS_DEVICE_ERROR"/>
        <result property="overDate" column="OVER_DATE"/>
        <result property="overUser" column="OVER_USER"/>
        <result property="longitude" column="LONGITUDE"/>
        <result property="latitude" column="LATITUDE"/>
        <result property="address" column="ADDRESS"/>
        <result property="fileIds" column="FILE_IDS"/>
        <result property="note" column="NOTE"/>
        <result property="overAddress" column="OVER_ADDRESS"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="createTime" column="CREATE_TIME"/>
        <association property="isOverName" column="IS_OVER" select="getIsOverName"/>
        <association property="isDeviceErrorName" column="IS_DEVICE_ERROR" select="getIsDeviceErrorName"/>
        <association property="overUserName" column="OVER_USER"
                     select="com.tofly.gpsboot.mapper.plan.HydrantrecordMapper.getRealName"/>
        <association property="createUserName" column="CREATE_USER"
                     select="com.tofly.gpsboot.mapper.plan.HydrantrecordMapper.getRealName"/>
        <association property="filePathList" column="FILE_IDS"
                     select="com.tofly.gpsboot.mapper.plan.HydrantrecordMapper.getFilePathList"/>
        <association property="belongStationName" column="BELONG_STATION_ID" select="getBelongStationName"/>
        <association property="belongPumpName" column="BELONG_PUMP_ID" select="getBelongPumpName"/>
        <association property="categoryName" column="CATEGORY" select="getCategoryName"/>
        <association property="deviceTypeName" column="DEVICE_TYPE" select="getDeviceTypeName"/>
        <association property="inspectTypeName" column="INSPECT_TYPE_ID"
                     select="com.tofly.gpsboot.mapper.plan.InspecttypeMapper.getInspectTypeName"/>
        <association property="device" column="DEVICE_Id" select="getDeviceInfo"/>
        <association property="stationSiteInfo" column="FACTORY_ID" select="getStationSiteInfo"/>
    </resultMap>

    <select id="getBelongPumpName" resultType="string">
        select name
        from tf_monitor_pump_station_w
        where id = #{belongPumpId}
    </select>
    <select id="getBelongStationName" resultType="string">
        select name
        from tf_ywpn_monitor_stationsiteinfo_w
        where id = #{belongStationId}
    </select>
    <select id="getIsOverName" resultType="string">
        SELECT case #{isOver} when '0' then '否' when '1' then '是' end
    </select>
    <select id="getIsDeviceErrorName" resultType="string">
        SELECT case#{isDeviceError} when '0' then '正常' when '1' then '异常' end
    </select>
    <select id="getCategoryName" resultType="string">
        select PCNAME categoryName
        from TF_YWPN_SYSCHILDCODE_D
        where PCID = 50700
          and ID = #{category}
    </select>

    <select id="getDeviceTypeName" resultType="string">
        select name deviceTypeName
        FROM tf_ywpn_monitor_devicetype_w
        where id = #{deviceType}
    </select>
    <select id="getDeviceInfo" resultType="com.tofly.pojo.Device">
        select *
        from TF_YWPN_MONITOR_DEVICE_W
        where id = #{deviceId}
    </select>
    <select id="getStationSiteInfo" resultType="com.tofly.pojo.Stationsiteinfo">
        select *
        from TF_YWPN_MONITOR_STATIONSITEINFO_W
        where id = #{factoryId}
    </select>

    <select id="getPlanDeviceList" resultMap="planFactoryMap">
        SELECT
        t1.belong_station_id,
        t1.belong_Pump_Id,
        t1.category,
        t1.id as device_Id,
        t1.type as device_Type,
        t1.latitude,
        t1.longitude,
        t1.location address,
        t2.name belongStationName,
        t3.name belongPumpName,
        t1.id as id,
        t4.is_Over,
        t4.OVER_DATE,
        t4.OVER_USER,
        t4.id as plan_Id
        FROM tf_ywpn_monitor_device_w t1
        LEFT JOIN tf_ywpn_monitor_stationsiteinfo_w t2
        on t1.belong_station_id = t2.id
        LEFT JOIN tf_monitor_pump_station_w t3
        on t1.belong_Pump_Id = t3.id
        RIGHT JOIN tf_ywpn_plan_factory t4
        on t1.id = t4.device_Id
        where 1=1
        <if test="dto.category != null ">
            and t1.category =#{dto.category}
        </if>
        <if test="dto.belongStationId != null ">
            and t1.belong_station_id =#{dto.belongStationId}
        </if>
        <if test="dto.belongPumpId != null ">
            and t1.belong_Pump_Id =#{dto.belongPumpId}
        </if>
        <if test="dto.isOver != null ">
            and t4.is_Over =#{dto.isOver}
        </if>
        <if test="dto.planId != null ">
            and t4.PLAN_ID =#{dto.planId}
        </if>
        <if test="dto.belongStationName != null and dto.belongStationName != '' ">
            and t2.name like CONCAT ('%',#{dto.belongStationName,jdbcType=VARCHAR},'%')
        </if>
        <if test="dto.belongPumpName != null and dto.belongPumpName != '' ">
            and t3.name like CONCAT ('%',#{dto.belongPumpName,jdbcType=VARCHAR},'%')
        </if>
    </select>

    <select id="getDeviceList" resultMap="planFactoryMap">
        SELECT
        t1.belong_station_id,
        t1.belong_Pump_Id,
        t1.category,
        t1.id as device_Id,
        t1.type as device_Type,
        t1.latitude,
        t1.longitude,
        t1.location address,
        t2.name belongStationName,
        t3.name belongPumpName,
        t1.id as id
        FROM tf_ywpn_monitor_device_w t1
        LEFT JOIN tf_ywpn_monitor_stationsiteinfo_w t2
        on t1.belong_station_id = t2.id
        LEFT JOIN tf_monitor_pump_station_w t3
        on t1.belong_Pump_Id = t3.id

        where 1=1
        <if test="dto.category != null ">
            and t1.category =#{dto.category}
        </if>
        <if test="dto.belongStationId != null ">
            and t1.belong_station_id =#{dto.belongStationId}
        </if>
        <if test="dto.belongPumpId != null ">
            and t1.belong_Pump_Id =#{dto.belongPumpId}
        </if>
        <if test="dto.belongStationName != null and dto.belongStationName != '' ">
            and t2.name like CONCAT ('%',#{dto.belongStationName,jdbcType=VARCHAR},'%')
        </if>
        <if test="dto.belongPumpName != null and dto.belongPumpName != '' ">
            and t3.name like CONCAT ('%',#{dto.belongPumpName,jdbcType=VARCHAR},'%')
        </if>
    </select>

    <select id="getMaintenanceStatisticsPlan" resultType="com.tofly.pojo.statement.vo.MaintenancePlanVo">
        select t1.CATEGORY category,
        (select PCNAME from tf_ywpn_syschildcode_d  where ID = t1.CATEGORY) categoryName,
        t1.BELONG_STATION_ID belongStationId,
        t4.name belongStationName,
        t1.BELONG_PUMP_ID belongPumpId,
        t5.name belongPumpName,
        t1.DEVICE_ID ID,
        t1.DEVICE_ID deviceId,
        t2.name deviceName,
        t2.no deviceCode,
        t1.DEVICE_TYPE deviceTypeId,
        t6.name deviceTypeName,
        t2.status status,
        (case t2.status when '0' then '离线' when '1' then '正常' when '2' then '报警'end) statusName,
        sum(CASE t3.PLAN_TYPE WHEN '0' THEN 1 else 0 END) inspectionNum,
        sum(CASE t3.PLAN_TYPE WHEN '1' THEN 1 else 0 END) maintainNum,

        sum(CASE t3.PLAN_TYPE WHEN '0' THEN
        (CASE t1.IS_DEVICE_ERROR WHEN '0' THEN 1 else 0 END) else 0 END) normalInspectionNum,
        sum(CASE t3.PLAN_TYPE WHEN '0' THEN
        (CASE t1.IS_DEVICE_ERROR WHEN '1' THEN 1 else 0 END) else 0 END) errorInspectionNum,

        sum(CASE t3.PLAN_TYPE WHEN '1' THEN
        (CASE t1.IS_DEVICE_ERROR WHEN '0' THEN 1 else 0 END) else 0 END) normalMaintainNum,
        sum(CASE t3.PLAN_TYPE WHEN '1' THEN
        (CASE t1.IS_DEVICE_ERROR WHEN '1' THEN 1 else 0 END) else 0 END) errorMaintainNum

        from tf_ywpn_plan_factory t1 left join tf_ywpn_monitor_device_w t2
        on t1.DEVICE_ID = t2.id right join tf_ywpn_plan_w t3
        on t1.PLAN_ID = t3.ID left join tf_ywpn_monitor_stationsiteinfo_w t4
        on t1.BELONG_STATION_ID = t4.ID left join tf_monitor_pump_station_w t5
        on t1.BELONG_PUMP_ID = t5.ID left join tf_ywpn_monitor_devicetype_w t6
        on t1.DEVICE_TYPE = t6.id

        where 1=1
        <if test="dto.category != null ">
            and t1.category =#{dto.category}
        </if>
        <if test="dto.belongPumpId != null ">
            and t1.BELONG_PUMP_ID =#{dto.belongPumpId}
        </if>
        <if test="dto.belongStationId != null ">
            and t1.BELONG_STATION_ID =#{dto.belongStationId}
        </if>
        <if test="dto.status != null and dto.status != '' ">
            and t2.status = #{dto.status}
        </if>
        <if test="dto.likStr != null and dto.likStr != '' ">
            and t2.name like CONCAT ('%',#{dto.likStr},'%')
            or deviceTypeName like CONCAT ('%',#{dto.likStr},'%')
        </if>

        GROUP BY
        t1.CATEGORY,t1.BELONG_STATION_ID,t1.BELONG_PUMP_ID,t1.DEVICE_ID,t2.name,t2.no,t1.DEVICE_TYPE,t1.IS_DEVICE_ERROR,t2.status,t3.PLAN_TYPE,t4.name,t5.name,t6.name
</select>
</mapper>