<?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.csdevice.mapper.CsGroupMapper">

    <select id="getGroupData" resultType="CsGroupVO">
        select
            id,
            '0' pid,
            group_name name,
            is_show isShow
        from
            cs_group
        where
            data_set_id = #{dataSet}
        order by sort
    </select>

    <select id="getArrayData" resultType="CsGroupVO">
        select
            t1.id,
            t0.group_id pid,
            case when t1.phase in ('A', 'B', 'C') then concat(t1.phase, "相", t1.another_name)
                when t1.phase in ('M', 'T') then t1.another_name
                else concat(t1.phase, t1.another_name)
                end name
        from
            cs_gro_arr t0
        left join
            cs_data_array t1 on t0.array_id = t1.id
        left join
            cs_group t2 on t0.group_id = t2.id
        where
            t0.group_id in
            <foreach collection='list' item='item' index="index" open='(' separator=',' close=')'>
                #{item}
            </foreach>
        order by t1.sort
    </select>

<!--    <insert id="insertList">-->
<!--        insert into cs_group-->
<!--            (-->
<!--                id,-->
<!--                data_set_id,-->
<!--                group_name,-->
<!--                sort,-->
<!--                is_show-->
<!--            )-->
<!--            values-->
<!--        <foreach collection="list" item="item" index="index" separator=",">-->
<!--            (-->
<!--                #{item.id},-->
<!--                #{item.dataSetId},-->
<!--                #{item.groupName},-->
<!--                #{item.sort},-->
<!--                #{item.isShow}-->
<!--            )-->
<!--        </foreach>-->
<!--    </insert>-->


    <select id="getGroupDataList" resultType="DataGroupTemplateVO">
        select
            id,
            data_set_id pid,
            group_name name,
            sort
        from
            cs_group
        where
            data_set_id = #{id} and is_show = 1
        <if test="searchValue != null and searchValue !=''">
            and group_name like concat('%',#{searchValue},'%')
        </if>
        order by sort
    </select>

    <select id="getGroupEventList" resultType="com.njcn.csdevice.pojo.vo.DataGroupEventVO">
            select REPLACE(UUID(),'-','') as id, ce.id as eventId,
            w1.item_name as projectName,w4.gc_name as gcName,
            w4.item_name as itemName,ce.persist_time as duration,ce.start_time as startTime,
            ce.tag,cl.name as lineName,cl.line_id as lineId
            from (select id,item_name,state from wl_record  where type = 0 and p_id is null) w1
            inner join (select id,p_id,state from wl_record where type = 0 and p_id is not null) w2 on w1.id = w2.p_id
            inner join wl_record_test_data w3 on w2.id = w3.test_item_id
            inner join wl_record w4 on w3.data_id = w4.id
            inner join cs_event ce on w4.dev_id = ce.device_id and w4.line_id = ce.line_id
            inner join cs_line cl on cl.line_id = ce.line_id
            where ce.type = 0 and w1.state = 1 and w2.state = 1 and w4.state = 1
            and ce.device_id = #{devId} and ce.line_id = #{lindId}
            order by w1.item_name
    </select>

</mapper>