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

    <select id="getDataArray" resultType="LineTargetVO">
        select
            pid,
            name,
            another_name anotherName,
            idx sort
        from
            cs_data_array
        where
            pid in
            <foreach collection='list' item='item' index="index" open='(' separator=',' close=')'>
                #{item}
            </foreach>
        group by pid,name,another_name,idx
        order by idx
    </select>

    <select id="getData" resultType="DataArrayDTO">
        select
            t1.id dataSetId,
            t1.another_name dataSetName,
            t0.name dataArrayName,
            t0.another_name dataArrayShowName,
            group_concat(distinct t0.stat_method) statMethod,
            group_concat(distinct t0.phase) phase
        from
            cs_data_array t0
            left join cs_data_set t1 on t0.pid = t1.id
        where
            t1.id IN
            <foreach collection='list' item='item' index="index" open='(' separator=',' close=')'>
                #{item}
            </foreach>
        group by t1.id,t1.another_name,t0.name,t0.another_name
        order by convert (substring_index(group_concat(distinct t0.sort),',',-1),unsigned)
    </select>

    <select id="getDictData" resultType="CsDataArray">
        select
            distinct data_id,name
        from
            cs_data_array t0
        where
            pid = #{id}
    </select>

    <select id="getGroupData" resultType="CsDataArray">
        select
            id ,
            name,
            another_name,
            phase
        from
            cs_data_array t0
        where
            pid = #{id} and (stat_method = 'avg' or stat_method is null)
        group by
            id,
            name,
            another_name,
            phase
        order by min(sort)
    </select>

    <select id="getArrayByGroup" resultType="EnergyTemplateVO">
        select
            t1.id,
            t0.group_id pid,
            t1.data_id dataId,
            t1.name,
            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 anotherName,
            t1.stat_method statMethod,
            t1.phase
        from
            cs_gro_arr t0
        left join cs_data_array t1 on
            t0.array_id = t1.id
        where
            t0.group_id in
            <foreach collection='list' item='item' index="index" open='(' separator=',' close=')'>
                #{item}
            </foreach>
        order by t1.sort
    </select>

    <select id="findListByParam" resultType="CsDataArray">
        select
            t4.*
        from
            cs_equipment_delivery t0
        left join
            cs_dev_model_relation t1 on t0.id = t1.dev_id and t1.status = 1
        left join
            cs_dev_model t2 on t1.model_id = t2.id and t2.status = 1
        left join cs_data_set t3 on t3.pid = t2.id
        left join cs_data_array t4 on t3.id = t4.pid
        where
            t0.ndid = #{param.id}
            and t1.did = #{param.did}
            and t3.cl_dev = #{param.cldId}
            and (t3.data_type = 'Stat' or t3.data_type is NULL)
            and t3.idx = #{param.idx}
            and t4.stat_method = #{param.statMethod}
        order by t4.sort
    </select>
</mapper>