<?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.device.mapper.LineMarkMapper">

    <select id="getLineMark" resultType="LineMarkVO">
        SELECT
        area.NAME province,
        gd.NAME gdName,
        sub.NAME subName,
        dev.NAME devName,
        line.NAME lineName,
        line.id lineIndex,
        devDetail.com_flag,
        devDetail.run_flag,
        devDetail.update_time,
        lineDetail.line_grade lineGradeId,
        dic.name lineGrade
        FROM
        pq_line line
        INNER JOIN pq_line voltage ON line.pid = voltage.id
        INNER JOIN pq_line dev ON voltage.pid = dev.id
        INNER JOIN pq_line sub ON dev.pid = sub.id
        INNER JOIN pq_line gd ON sub.pid = gd.id
        INNER JOIN pq_line prov ON gd.pid = prov.id
        INNER JOIN pq_line_detail lineDetail ON line.id = lineDetail.id
        INNER JOIN pq_device devDetail ON dev.id = devDetail.id
        INNER JOIN sys_area area ON prov.NAME = area.id
        LEFT JOIN sys_dict_data dic ON lineDetail.line_grade = dic.id
        WHERE
        line.LEVEL = 6
        AND line.id = lineDetail.id
        AND dev.id = devDetail.id
        AND prov.NAME = area.id
        AND lineDetail.line_grade = dic.id
        and devDetail.dev_model = 1

        <if test="comFlag!=null">
            and devDetail.com_flag = #{comFlag}
        </if>
        <if test="lineGrade!=null and lineGrade!=''">
            and dic.id = #{lineGrade}
        </if>
        <if test="searchValue!=null and searchValue!=''">
            and area.name like CONCAT(CONCAT('%', #{searchValue}), '%') or gd.name like CONCAT(CONCAT('%', #{searchValue}), '%')
            or sub.name like CONCAT(CONCAT('%', #{searchValue}), '%') or dev.name like CONCAT(CONCAT('%', #{searchValue}), '%')
            or voltage.name like CONCAT(CONCAT('%', #{searchValue}), '%') or line.name like CONCAT(CONCAT('%', #{searchValue}), '%')
        </if>
        order by prov.sort asc,gd.sort asc,sub.sort asc,dev.sort asc,line.sort asc
    </select>

    <select id="exportLineMark" resultType="LineMark">
        SELECT
        area.NAME province,
        gd.NAME gdName,
        sub.NAME subName,
        dev.NAME devName,
        line.NAME lineName,
        dic.name mark
        FROM
        pq_line line
        INNER JOIN pq_line voltage ON line.pid = voltage.id
        INNER JOIN pq_line dev ON voltage.pid = dev.id
        INNER JOIN pq_line sub ON dev.pid = sub.id
        INNER JOIN pq_line gd ON sub.pid = gd.id
        INNER JOIN pq_line prov ON gd.pid = prov.id
        INNER JOIN pq_line_detail lineDetail ON line.id = lineDetail.id
        INNER JOIN pq_device devDetail ON dev.id = devDetail.id
        INNER JOIN sys_area area ON prov.NAME = area.id
        LEFT JOIN sys_dict_data dic ON lineDetail.line_grade = dic.id
        WHERE
        line.LEVEL = 6
        AND line.id = lineDetail.id
        AND dev.id = devDetail.id
        AND prov.NAME = area.id
        AND lineDetail.line_grade = dic.id
        and devDetail.dev_model = 1

        <if test="comFlag!=null">
            and devDetail.com_flag = #{comFlag}
        </if>
        <if test="lineGrade!=null and lineGrade!=''">
            and dic.id = #{lineGrade}
        </if>
        <if test="searchValue!=null and searchValue!=''">
            and area.name like CONCAT(CONCAT('%', #{searchValue}), '%') or gd.name like CONCAT(CONCAT('%', #{searchValue}), '%')
            or sub.name like CONCAT(CONCAT('%', #{searchValue}), '%') or dev.name like CONCAT(CONCAT('%', #{searchValue}), '%')
            or voltage.name like CONCAT(CONCAT('%', #{searchValue}), '%') or line.name like CONCAT(CONCAT('%', #{searchValue}), '%')
        </if>
        order by prov.sort,gd.sort,sub.sort,dev.sort,line.sort
    </select>

    <update id="singleOperate">
        update pq_line_detail set
            line_grade= #{lineGrade}
            where id = #{lineIndex}
    </update>

    <select id="getLineGrade" resultType="LineMarkVO">
      select
      a.id lineIndex,
      b.line_grade lineGrade
      from pq_line a
      left join pq_linedetail b on a.id = b.id
      where a.id=#{lineIndex}
    </select>

    <select id="getLineMarkData" resultType="LineMarkVO">
        SELECT
        gd.NAME gdName,
        sub.NAME subName,
        dev.NAME devName,
        line.NAME lineName,
        line.id lineIndex,
        dev.id devIndex
        FROM
        pq_line line
        INNER JOIN pq_line voltage ON line.pid = voltage.id
        INNER JOIN pq_line dev ON voltage.pid = dev.id
        INNER JOIN pq_line sub ON dev.pid = sub.id
        INNER JOIN pq_line gd ON sub.pid = gd.id
		LEFT JOIN pq_device devDetail ON dev.id = devDetail.id
        where devDetail.dev_model = 1
        and gd.name = #{gdName}
        and sub.name = #{subName}
        and dev.name = #{devName}
        and line.name = #{lineName}
    </select>

    <update id="updateMark">
        update pq_linedetail set line_grade = #{lineGrade} where id = #{lineIndex}
    </update>

    <update id="batchUpdateForLine"  parameterType="java.util.List">
        <foreach collection="list" item="item" index="index"  separator=";">
            update pq_line_detail set
            line_grade= #{item.mark}
            where id = #{item.lineIndex}
        </foreach>
    </update>
</mapper>
