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

    <resultMap id="baseResult" type="com.ho.business.vo.resp.ForwardColTemplateResp">
        <id property="tempId" column="id" />
        <result property="templateName" column="template_name"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <collection property="forwardCols" ofType="com.ho.business.entity.ForwardCol">
            <id property="id" column="id"/>
            <result property="templateId" column="template_id"/>
            <result property="modelColId" column="model_col_id"/>
            <result property="colName" column="col_name"/>
        </collection>
    </resultMap>

    <!-- 新增模板 -->
    <insert id="addForwardColTemplate" keyColumn="id" keyProperty="id" useGeneratedKeys="true" parameterType="com.ho.business.vo.req.ForwardColTemplateReq">
        insert into forward_col_template (template_name,create_time)
        values(#{templateName},now())
    </insert>

    <!-- 新增测点 -->
    <insert id="addForwardCol" parameterType="com.ho.business.entity.ForwardCol">
        insert into forward_col ( template_id,model_col_id,col_name) values
        <foreach collection="list" item="item" index="index" separator=",">
            ( #{planningTemplateId},#{item.modelColId},#{item.colName})
        </foreach>
    </insert>

    <!-- 编辑模板 -->
    <update id="modifyForwardColTemplate" parameterType="com.ho.business.vo.req.ForwardColTemplateReq">
        update forward_col_template set
            <if test="templateName != null and templateName != ''">
                template_name = #{templateName},
            </if>
            update_time = sysdate()
            where id = #{id}
    </update>

    <!-- 删除测点数据 -->
    <delete id="deleteForwardCol">
        delete from forward_col where template_id = #{id}
    </delete>

    <delete id="deleteForwardColTemplates">
        delete from forward_col_template
        where id in
            <foreach collection="ids" item="item" index="index" open="(" separator="," close=")">
                    #{item}
            </foreach>
    </delete>

    <delete id="deleteForwardCols">
        delete from forward_col
        where template_id in
            <foreach collection="ids" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
    </delete>

    <select id="checkName" resultType="java.lang.Integer">
        select count(1) from forward_col_template where template_name = #{templateName}
        <if test="temId != null">
            and id = #{temId}
        </if>
    </select>

    <!-- 查询模板列表接口 -->
    <select id="getForwardColTemplateDetail" resultMap="baseResult">
        select tem.id temp_id,tem.template_name template_name,tem.create_time create_time, tem.update_time update_time,
               cur.id id,cur.template_id,cur.model_col_id,cur.col_name
        from (
        select id,template_name,create_time,update_time
        from forward_col_template
        <where>
            <if test="id != null and id != ''">
                id = #{id}
            </if>
            <if test="templateName != null and templateName != ''">
                and template_name like concat('%',#{templateName},'%')
            </if>
        </where>
             ) tem left join forward_col cur on cur.template_id = tem.id
        order by tem.create_time,tem.update_time desc
    </select>

    <select id="getForwardCol" resultType="com.ho.business.entity.DeviceTypeCol">
        SELECT
            device_type,
            col,
            col_name
        FROM
            device_type_col
        WHERE
            id IN (
                SELECT
                    m.device_col_id
                FROM
                    forward_col f
                        LEFT JOIN model_device_col m ON f.model_col_id = m.model_col_id
                <where>
                    <if test="tempId != null">
                        and template_id = #{tempId}
                    </if>
                    <if test="typeList != null and typeList.size >0 ">
                        and  m.device_type in
                        <foreach collection="typeList" item="item" index="index" open="(" separator="," close=")">
                            #{item}
                        </foreach>
                    </if>
                </where>
            )
    </select>
</mapper>
