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

    <resultMap type="cn.cecep.talroad.domain.BasDeviceParam" id="BasDeviceParamResult">
        <id property="id"    column="id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="paramId"    column="param_id"    />
        <result property="industry"    column="industry"    />
        <result property="deviceType"    column="device_type"    />
        <result property="facilityType"    column="facility_type"    />
        <result property="normalValue"    column="normal_value"    />
        <result property="minValue"    column="min_value"    />
        <result property="maxValue"    column="max_value"    />
        <result property="q1Value"    column="q1_value"    />
        <result property="q2Value"    column="q2_value"    />
        <result property="isOnline"    column="is_online"    />
        <result property="isStatus"    column="is_status"    />
        <result property="standbyNum"    column="standby_num"    />
        <result property="multiNum"    column="multi_num"    />
        <result property="overMaxVal"    column="over_max_val"    />
        <result property="createTime"    column="create_time"    />
        <result property="releaseValue"    column="release_value"    />
        <result property="stopValue"    column="stop_value"    />
        <result property="paramName"    column="param_name"    />
        <result property="paramUnit"    column="param_unit"    />
    </resultMap>
    <resultMap type="cn.cecep.talroad.vo.BasDeviceParamListVo" id="BasDeviceParamListResult">
        <id property="id"    column="id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="paramId"    column="param_id"    />
        <result property="industry"    column="industry"    />
        <result property="deviceType"    column="device_type"    />
        <result property="facilityType"    column="facility_type"    />
        <result property="normalValue"    column="normal_value"    />
        <result property="minValue"    column="min_value"    />
        <result property="maxValue"    column="max_value"    />
        <result property="q1Value"    column="q1_value"    />
        <result property="q2Value"    column="q2_value"    />
        <result property="isOnline"    column="is_online"    />
        <result property="standbyNum"    column="standby_num"    />
        <result property="multiNum"    column="multi_num"    />
        <result property="overMaxVal"    column="over_max_val"    />
        <result property="createTime"    column="create_time"    />
        <result property="releaseValue"    column="release_value"    />
        <result property="stopValue"    column="stop_value"    />
        <result property="paramName"    column="param_name"    />
        <result property="paramUnit"    column="param_unit"    />
    </resultMap>


    <sql id="selectBasDeviceParamVo">
        select id, device_id, param_id, industry, device_type, facility_type, normal_value, min_value, max_value, q1_value, q2_value, is_online, standby_num, multi_num, over_max_val,release_value, stop_value,create_time,param_unit,param_name from bas_device_param
    </sql>
    <update id="updateValue" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" separator=";">
            UPDATE bas_device_param
            <set>
                <if test="item.paramName != null and item.paramName != ''">param_name=#{item.paramName},</if>
                <if test="item.paramUnit != null and item.paramUnit != ''">param_unit=#{item.paramUnit},</if>
                <if test="item.paramUnit == null or item.paramUnit == ''">param_unit = null,</if>
                <if test="item.normalValue != null and item.normalValue != ''">normal_value=#{item.normalValue},</if>
                <if test="item.normalValue == null or item.normalValue == ''">normal_value = null,</if>
                <if test="item.minValue != null and item.minValue != ''">min_value=#{item.minValue},</if>
                <if test="item.minValue == null or item.minValue == ''">min_value = null,</if>
                <if test="item.maxValue != null and item.maxValue != ''">max_value=#{item.maxValue},</if>
                <if test="item.maxValue == null or item.maxValue == ''">max_value = null,</if>
            </set>
            WHERE id=#{item.id}
        </foreach>
    </update>
    <update id="updateByIdList" parameterType="list">
        <foreach collection="list" item="item" index="index" separator=";">
            UPDATE bas_device_param
            <set>
                <if test="item.isStatus != null and item.isStatus != ''">is_status=#{isStatus},</if>
            </set>
            WHERE id=#{item.id}
        </foreach>
    </update>

    <select id="selectBasDeviceParamList" parameterType="cn.cecep.talroad.domain.BasDeviceParam" resultMap="BasDeviceParamResult">
        <include refid="selectBasDeviceParamVo"/>
        <where>
            <if test="deviceId != null  and deviceId != ''"> and device_id = #{deviceId}</if>
            <if test="paramId != null  and paramId != ''"> and param_id = #{paramId}</if>
            <if test="industry != null  and industry != ''"> and industry = #{industry}</if>
            <if test="deviceType != null  and deviceType != ''"> and device_type = #{deviceType}</if>
            <if test="facilityType != null  and facilityType != ''"> and facility_type = #{facilityType}</if>
            <if test="normalValue != null  and normalValue != ''"> and normal_value = #{normalValue}</if>
            <if test="minValue != null  and minValue != ''"> and min_value = #{minValue}</if>
            <if test="maxValue != null  and maxValue != ''"> and max_value = #{maxValue}</if>
            <if test="releaseValue != null  and releaseValue != ''"> and release_value = #{releaseValue}</if>
            <if test="q1Value != null  and q1Value != ''"> and q1_value = #{q1Value}</if>
            <if test="q2Value != null  and q2Value != ''"> and q2_value = #{q2Value}</if>
            <if test="isOnline != null "> and is_online = #{isOnline}</if>
            <if test="standbyNum != null "> and standby_num = #{standbyNum}</if>
            <if test="multiNum != null "> and multi_num = #{multiNum}</if>
            <if test="overMaxVal != null  and overMaxVal != ''"> and over_max_val = #{overMaxVal}</if>
            <if test="isStatus != null  and isStatus != ''"> and is_status = #{isStatus}</if>
        </where>
    </select>
    <select id="queryNeedUpdateParamInfo" resultType="cn.cecep.talroad.domain.BasDeviceParam">
        select
            d.device_id deviceId,
            d.industry  industry,
            p.param_id  paramId,
            d.type      deviceType,
            d.create_time createTime,
            p.name      paramName,
            p.unit_real paramUnit
        FROM bas_device d
                 INNER JOIN bas_param p ON d.industry = p.industry
        WHERE d.type = p.device_type
          AND d.type = p.device_type
          AND d.industry = p.industry
        ORDER BY d.device_id ASC
    </select>


    <update id="updateNeedUpdateParamInfo" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" separator=";">
            UPDATE bas_device_param
            SET param_name = #{item.name},
            param_unit =#{item.unit}
            WHERE id = #{item.id}
        </foreach>
    </update>

    <insert id="batchInsert" parameterType="list">
        INSERT INTO bas_device_param (id, device_id, param_id, industry, device_type, facility_type, is_online, standby_num, multi_num, over_max_val, create_time,param_name, param_unit)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.id},
            #{item.deviceId},
            #{item.paramId},
            #{item.industry},
            #{item.deviceType},
            #{item.facilityType},
            #{item.isOnline},
            #{item.standbyNum},
            #{item.multiNum},
            #{item.overMaxVal},
            #{item.createTime},
            #{item.paramName},
            #{item.paramUnit})
        </foreach>
    </insert>
    <select id="selectBasDeviceParamByCondition" resultType="cn.cecep.talroad.vo.BasDeviceParamVo">
        select
            bdp.device_id,
            bdp.param_id,
            bp.param_code_svc,
            bdp.industry,
            bdp.device_type,
            bdp.facility_type,
            COALESCE ( bdp.normal_value, 0 ) as normal_value,
            COALESCE ( bdp.min_value, 0 ) as min_value,
            COALESCE ( bdp.max_value, 0 ) as max_value,
            COALESCE ( bdp.over_max_val, '0' ) as over_max_val,
            COALESCE ( bdp.stop_value, '0' ) as stop_value
        from bas_device_param bdp
                 left join bas_param bp on bdp.param_id = bp.param_id and bdp.industry = bp.industry and bdp.device_type = bp.device_type and bdp.facility_type = bp.facility_type
        where bdp.device_id = #{deviceId} and bdp.normal_value is not null
          <if test="list!=null">
              and bp.param_code_svc in
              <foreach collection="list" open="(" separator="," close=")" item="item">
                #{item}
              </foreach>
          </if>
    </select>
    <select id="queryByDeviceIdListAndIndustry" resultMap="BasDeviceParamListResult">
        select
        bdp.id,
        bdp.device_id,
        bp.param_code_svc as param_id,
        bdp.industry,
        bdp.device_type,
        bdp.facility_type,
        bdp.normal_value,
        bdp.min_value,
        bdp.max_value,
        bdp.q1_value,
        bdp.q2_value,
        bdp.is_online,
        bdp.standby_num,
        bdp.multi_num,
        bdp.over_max_val,
        bdp.create_time,
        bdp.param_unit,
        bdp.param_name
        from bas_device_param bdp
        inner join bas_param bp on bdp.industry =bp.industry and bdp.device_type =bp.device_type and bdp.facility_type = bp.facility_type and bdp.param_id = bp.param_id
        <where>
            bdp.is_online = 1
            <if test="paramName != null  and paramName != ''">
                and bdp.param_name like concat('%', #{paramName}, '%')
            </if>
            <if test="deviceIdList!=null">
                and bdp.device_id in
                <foreach collection="deviceIdList" open="(" separator="," close=")" item="item">
                    #{item}
                </foreach>
            </if>
            <if test="industry != null  and industry != ''"> and bdp.industry = #{industry}</if>
        </where>
        ORDER BY bdp.id
    </select>
    <update id="updateValueParam">
        UPDATE bas_device_param
        SET is_status = #{isStatus}
        WHERE data_type = #{datetype} and  device_id = #{deviceId} and industry= #{el}
    </update>


    <select id="getParamCodeSvcByDeviceParamIds" resultType="string" parameterType="java.util.List">
        select b.param_code_svc from bas_device_param  a  inner join  bas_param b
        on  a.param_id = b.param_id
        and a.device_type = b.device_type
        and a.facility_type = b.facility_type
        and a.industry = b.industry
        where a.id in
        <foreach collection="list" open="(" separator="," close=")" item="item">
            #{item}
        </foreach>
    </select>

    <select id="selectBasDeviceParamByAnalyse" resultType="cn.cecep.talroad.vo.BasDeviceParamVo">
        select
            dp.device_id,
            dp.param_id,
            p.name as param_name,
            p.param_code_svc,
            dp.industry,
            dp.device_type,
            dp.facility_type,
            COALESCE ( dp.normal_value, 0 ) as normal_value,
            COALESCE ( dp.min_value, 0 ) as min_value,
            COALESCE ( dp.max_value, 0 ) as max_value
        from bas_device_param dp
            inner join bas_device_param_analyse dpa
                on dp.param_id = dpa.device_param_code
                and dp.industry = dpa.industry
                <!-- TODO ? el 不关联设备类型 -->
                -- and dp.device_type = dpa.device_cart_code
            inner join bas_param p
                on dp.param_id = p.param_id
                and dp.industry = p.industry
                and dp.device_type = p.device_type
                and dp.facility_type = p.facility_type
            inner join bas_device_sal_analysis dsa
                on dp.device_id = dsa.device_id
        where
            dp.is_online = '1'
            and dpa.data_logic_abnormal_ew = '1'
            and dsa.mon_status != '停产'
            <if test="dataType != null and dataType != ''">
                and dpa.data_type = #{dataType}
            </if>
            <if test="deviceId != null and deviceId != ''">
                and dp.device_id = #{deviceId}
            </if>
            <if test="codeList != null">
                and p.param_code_svc in
                <foreach collection="codeList" open="(" separator="," close=")" item="item">
                    #{item}
                </foreach>
            </if>
    </select>
    <select id="selectBasDeviceParam" resultMap="BasDeviceParamResult">
        select
            d.id id,
            d.device_id deviceId,
            d.industry  industry,
            d.data_type      dataType
        FROM bas_device_param d
        WHERE d.data_type = #{datetype} and  d.device_id =  #{deviceId} and d.industry= #{el}
    </select>
</mapper>
