<?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.CsEquipmentDeliveryMapper">
    <resultMap id="BaseResultMap" type="com.njcn.csdevice.pojo.po.CsEquipmentDeliveryPO">
        <!--@mbg.generated-->
        <!--@Table cs_equipment_delivery-->
        <result column="id" jdbcType="VARCHAR" property="id"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="ndid" jdbcType="VARCHAR" property="ndid"/>
        <result column="mac" jdbcType="VARCHAR" property="mac"/>
        <result column="dev_use" jdbcType="VARCHAR" property="devUse"/>
        <result column="dev_type" jdbcType="VARCHAR" property="devType"/>
        <result column="dev_model" jdbcType="VARCHAR" property="devModel"/>
        <result column="debug_person" jdbcType="VARCHAR" property="debugPerson"/>
        <result column="producte_time" jdbcType="TIMESTAMP" property="producteTime"/>
        <result column="check_time" jdbcType="TIMESTAMP" property="checkTime"/>
        <result column="debug_time" jdbcType="TIMESTAMP" property="debugTime"/>
        <result column="cntract_no" jdbcType="VARCHAR" property="cntractNo"/>
        <result column="sales_manager" jdbcType="VARCHAR" property="salesManager"/>
        <result column="status" jdbcType="INTEGER" property="status"/>
        <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_by" jdbcType="VARCHAR" property="updateBy"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>
    <sql id="Base_Column_List">
        <!--@mbg.generated-->
        id, `name`, ndid, mac, dev_use, dev_type, dev_model, program_version, debug_person,
        producte_time, check_time, debug_time, cntract_no, sales_manager, `status`, create_by,
        create_time, update_by, update_time
    </sql>
    <select id="queryProjectEquipmentVO" resultType="com.njcn.csdevice.pojo.vo.ProjectEquipmentVO">
        SELECT
        a.id engineeringId,
        a.name engineeringName,
        b.id projectId,
        b.name projectName,
        c.id equipmentId,
        c.ndid nDid,
        c.name equipmentName,
        c.create_time createTime,
        c.run_status,
        c.mac mac,
        c.process process,
        c.sort sort,
        c.dev_type devType
        FROM
        (select * from cs_ledger where LEVEL = 0       and State =1
        ) a,
        (select * from cs_ledger where LEVEL = 1        and State =1
        ) b,
        (select * from cs_ledger where LEVEL = 2       and State =1
        ) d,
         cs_equipment_delivery c
        WHERE
        a.LEVEL = 0
        AND b.LEVEL = 1
        AND  a.id = b.pid
        AND b.id = d.pid
        AND c.id =d.id
            <if test="projectEquipmentQueryParm!=null and projectEquipmentQueryParm.projectType != null and projectEquipmentQueryParm.projectType !=''">
          AND c.project_type = #{projectEquipmentQueryParm.projectType}
          </if>
            <if test="projectEquipmentQueryParm!=null and projectEquipmentQueryParm.projectId != null and projectEquipmentQueryParm.projectId !=''">
          AND b.id = #{projectEquipmentQueryParm.projectId}
          </if>
        <if test="projectEquipmentQueryParm!=null and projectEquipmentQueryParm.engineerId != null and projectEquipmentQueryParm.engineerId !=''">
            AND a.id = #{projectEquipmentQueryParm.engineerId}
        </if>
        <if test="projectEquipmentQueryParm!=null and projectEquipmentQueryParm.runStatus != null and projectEquipmentQueryParm.runStatus !=''">
            AND c.run_status = #{projectEquipmentQueryParm.runStatus}
        </if>
        and c.id in
        <foreach collection="device" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        order by a.create_time desc

    </select>

    <select id="page" resultType="CsEquipmentDeliveryVO">
        select
            *
        from
            cs_equipment_delivery
        <where>
            1=1
            and ${ew.sqlSegment}
        </where>
    </select>

    <select id="getList" resultType="CsEquipmentDeliveryVO">
        select
            *
        from
            cs_equipment_delivery
        <where>
            dev_access_method = 'MQTT'
            <if test="queryParam.process != null and queryParam.process !=''">
                and process = #{queryParam.process}
            </if>
            <choose>
                <when test="queryParam.runStatus != null and queryParam.runStatus != ''">
                    AND run_status = #{queryParam.runStatus}
                </when>
                <otherwise>
                    AND run_status IN (1,2)
                </otherwise>
            </choose>
            <if test="queryParam.searchValue != null and queryParam.searchValue !=''">
                and (name like concat('%',#{queryParam.searchValue},'%') or ndid like concat('%',#{queryParam.searchValue},'%'))
            </if>
        </where>
    </select>

    <select id="getCounts" resultType="int">
        select
            count(1)
        from
            cs_equipment_delivery
        <where>
            1=1
            <if test="queryParam.process != null and queryParam.process !=''">
                and process = #{queryParam.process}
            </if>
            <if test="queryParam.runStatus != null and queryParam.runStatus !=''">
                and run_status = #{queryParam.runStatus}
            </if>
            <if test="queryParam.searchValue != null and queryParam.searchValue !=''">
                and (name like concat('%',#{queryParam.searchValue},'%') or ndid like concat('%',#{queryParam.searchValue},'%'))
            </if>
        </where>
    </select>

    <select id="getListByNodeProcess" resultType="int">
        select
            count(1)
        from
            cs_equipment_delivery
        where
            node_id = #{nodeId} and run_status != 0
            <if test="process != null and process !=''">
                and node_process > #{process}
            </if>
    </select>

    <select id="getDataByNodeIdAndProcessNo" resultType="com.njcn.csdevice.pojo.po.CsEquipmentDeliveryPO">
        select
            *
        from
            cs_equipment_delivery
        where
            node_id = #{nodeId} and run_status != 0
        <if test="process != null and process !=''">
            and node_process = #{process}
        </if>
    </select>

</mapper>