<?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.njcn.gather.device.mapper.PqDevMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="DevResultMap" type="com.njcn.gather.device.pojo.vo.PreDetection">
        <id column="Id" property="devId"/>
        <id column="Name" property="devName"/>
        <id column="IP" property="devIP"/>
        <result column="Port" property="port"/>
        <result column="Dev_Type" property="devType"/>
        <result column="Series" property="devCode"/>
        <result column="Dev_Key" property="devKey"/>
        <result column="icdType" property="icdType"/>
        <result column="Dev_Chns" property="devChns"/>
        <result column="Dev_Volt" property="devVolt"/>
        <result column="Dev_Curr" property="devCurr"/>
        <result column="Angle" property="angle"/>
        <result column="Use_Phase_Index" property="usePhaseIndex"/>
        <result column="Wave_Cmd" property="waveCmd"/>

        <collection
                property="monitorList"
                column="{ devId = Id}"
                select="com.njcn.gather.monitor.mapper.PqMonitorMapper.selectMonitorInfo"
        >
        </collection>
    </resultMap>
    <select id="selectDevInfo" resultMap="DevResultMap">
        SELECT
        d.Id,
        d.Name,
        d.IP,
        d.Port,
        t.name as Dev_Type,
        d.Series,
        d.Dev_Key,
        p.name as icdType,
        t.Dev_Chns,
        t.Dev_Volt,
        t.Dev_Curr,
        p.Angle,
        p.Use_Phase_Index,
        t.Wave_Cmd
        FROM
        pq_dev d
        inner join pq_dev_type t on d.Dev_Type = t.id
        inner join pq_icd_path p on t.icd = p.id
        <where>
            <if test="devIds !=null and devIds.size()!=0">
                AND d.Id in
                <foreach collection="devIds" open="(" close=")" item="item" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <update id="finishPlan">
        update ad_plan
        set Test_State = 2
        where id = #{planId}
    </update>

    <update id="updateReportState">
        update pq_dev
        set Report_State = 1
        where id = #{id}
    </update>

    <update id="updatePlanCheckResult">
        update ad_plan
        set Result = #{checkResult}
        where id = #{planId}
    </update>

    <update id="updatePlanTestState">
        update ad_plan
        set Test_State = #{testState}
        where id = #{planId}
    </update>

    <select id="getScriptIdByPlanId" resultType="java.lang.String">
        SELECT Script_Id
        FROM ad_plan
        WHERE id = #{planId}
    </select>

    <select id="selectByQueryParam" resultType="com.njcn.gather.device.pojo.vo.PqDevVO">
        SELECT dev.*,dev_sub.* FROM pq_dev dev
        left JOIN pq_dev_sub dev_sub ON dev.Id = dev_sub.Dev_Id
        <where>
            dev.state = 1
            <if test="planIdList!= null and planIdList.size!=0">
                AND dev.Plan_Id in
                <foreach collection="planIdList" open="(" close=")" item="item" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="name!= null and name!= ''">
                AND dev.Name LIKE CONCAT('%',#{name},'%')
            </if>
            <if test="checkStateList!= null and checkStateList.size()!= 0 ">
                AND dev_sub.Check_State in
                <foreach collection="checkStateList" open="(" close=")" item="item" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="checkResult!= null">
                AND dev_sub.Check_Result = #{checkResult}
            </if>
            <if test="reportState!= null">
                AND dev_sub.Report_State = #{reportState}
            </if>
        </where>
        ORDER BY dev.Create_Time DESC,dev.Name
    </select>

    <select id="selectByDevId" resultType="com.njcn.gather.device.pojo.vo.PqDevVO">
        SELECT dev.*, dev_sub.*
        FROM pq_dev dev
                 left JOIN pq_dev_sub dev_sub ON dev.Id = dev_sub.Dev_Id
        WHERE dev.Id = #{devId}
    </select>

    <select id="listByDevIds" resultType="com.njcn.gather.device.pojo.vo.PqDevVO">
        SELECT dev.*, dev_sub.*
        FROM pq_dev dev
        left JOIN pq_dev_sub dev_sub ON dev.Id = dev_sub.Dev_Id
        WHERE dev.state = 1
        <if test="devIds!= null and devIds.size() != 0">
            AND dev.Id in
            <foreach collection="devIds" open="(" close=")" item="item" separator=",">
                #{item}
            </foreach>
        </if>
        order by dev.Create_Time DESC, dev.Name ASC
    </select>
</mapper>

