<?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.ho.business.mapper.TabSortMapper">

    <resultMap id="baseResult" type="com.ho.business.entity.TabSort">
        <id property="tabId" column="tab_id" />
        <result property="tabName" column="tab_name"/>
        <result property="tabOrder" column="tab_order"/>
        <result property="tabStatus" column="tab_status"/>
        <result property="stationId" column="station_id"/>
        <result property="commissioning" column="commissioning"/>
        <result property="versionNo" column="version_no"/>
        <result property="status" column="status"/>
        <result property="tabFlag" column="tab_flag"/>
        <result property="sn" column="sn"/>
        <collection property="childs" column="tag_id" ofType="com.ho.business.entity.TabTag">
            <id property="tagId" column="tag_id"/>
            <result property="tagName" column="tag_name"/>
            <result property="tagNameEn" column="tag_name_en"/>
            <result property="tagType" column="tag_type"/>
            <result property="srcId" column="src_id"/>
            <result property="tabId" column="tab_id"/>
            <result property="col" column="col"/>
            <result property="maxValue" column="max_value"/>
            <result property="minValue" column="min_value"/>
            <result property="frame" column="frame"/>
            <result property="tagFlag" column="tag_flag"/>
            <collection property="values" column="val_id" ofType="com.ho.business.entity.TabTagDefaultValue">
                <id property="valId" column="val_id"/>
                <result property="defVal" column="def_val"/>
                <result property="tagId" column="tag_id"/>
                <result property="valFlag" column="val_flag"/>
            </collection>
        </collection>
    </resultMap>

    <select id="queryIssueDatas" resultMap="baseResult" parameterType="com.ho.business.entity.TabSort">
        select ts.tab_id,ts.tab_name,ts.tab_order,ts.tab_status,ts.station_id,ts.commissioning,ts.version_no,ts.status,s.sn,0 tab_flag,
        tt.tag_id,tt.tag_name,tt.tag_name_en,tt.tag_type,tt.src_id,tt.tab_id,tt.col,tt.max_value,tt.min_value,tt.frame,0 tag_flag,
        dv.val_id,def_val,dv.tag_id,0 val_flag
        from tab_sort ts
        left join tab_tag tt on ts.tab_id = tt.tab_id
        left join tab_tag_default_value dv on dv.tag_id = tt.tag_id
        left join station_sn s on s.station_id = ts.station_id
        where ts.station_id = #{stationId} and ts.status = 1
        <if test="tabId != null">
            and ts.tab_id = #{tabId}
        </if>
        <if test="tagId != null">
            and tt.tag_id = #{tagId}
        </if>
    </select>


    <select id="queryPlanControlIssueData" resultType="com.ho.business.entity.PlanningIssueComm">
        select model.col model_col,type.col type_col,type.max_value,type.min_value,model.col_name,d.src_id,d.station_id,d.device_type
        from model_device_col device
                 join device_type_col type on type.id = device.device_col_id
                 join model_type_col model on model.id = device.model_col_id
                 join device d on d.device_type = device.device_type
        where model.col like 'planControl%' and ( device.device_type=#{deviceType} or  device.device_type like 'agc%' )
    </select>

    <select id="getStationSn" resultType="java.lang.String">
        select sn from station_sn where station_id = #{stationId} limit 1
    </select>

    <select id="queryTabSortById" resultType="com.ho.business.entity.TabSort">
        select tab_id,tab_name,tab_order,tab_status,station_id,commissioning,version_no,status from tab_sort where tab_id = #{id}
    </select>

    <select id="queryExcelDatas" resultType="com.ho.business.vo.resp.tabSort.TabSortExcel">
        select ts.tab_name,tt.tag_name,tt.tag_name_en,tt.tag_type,tt.src_id,tt.device_type,tt.col,tt.max_value,tt.min_value,tt.frame,
               case when dv.def_val is not null then concat(dv.def_level,':',dv.def_val) else dv.def_level end as def_val
        from tab_sort ts
                 left join tab_tag tt on tt.tab_id = ts.tab_id
                 left join tab_tag_default_value dv on dv.tag_id = tt.tag_id
            where ts.station_id = #{stationId} and ts.status = 1
    </select>

    <select id="getColNameByCol" resultType="java.lang.String">
        select col_name from device_type_col where col = #{col} and device_type = #{deviceType}
    </select>
</mapper>
