<?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.huashe.park.core.mapper.PileMachineInfoMapper">

    <resultMap type="PileMachineInfo" id="PileMachineInfoResult">
        <result property="id" column="id"/>
        <result property="byteId" column="byte_id"/>
        <result property="machineNum" column="machine_num"/>
        <result property="equipment" column="equipment"/>
        <result property="no" column="no"/>
        <result property="pwd" column="pwd"/>
        <result property="sn" column="sn"/>
        <result property="productKey" column="product_key"/>
        <result property="deviceSecret" column="device_secret"/>
        <result property="topic" column="topic"/>
        <result property="serverHost" column="server_host"/>
        <result property="serverPort" column="server_port"/>
        <result property="serverUserName" column="server_user_name"/>
        <result property="serverPassword" column="server_password"/>
        <result property="status" column="status"/>
        <result property="lng" column="lng"/>
        <result property="lat" column="lat"/>
        <result property="updateTime" column="update_time"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateBy" column="update_by"/>
        <result property="tenantId" column="tenant_id"/>
    </resultMap>

    <sql id="selectPileMachineInfoVo">
        select id,
               byte_id,
               machine_num,
               equipment,
               pwd,
               sn,
               product_key,
               device_secret,
               topic,
               server_host,
               server_port,
               server_user_name,
               server_password,
               status,
               lng,
               lat,
               update_time,
               create_time,
               create_by,
               update_by,
               `no`,
               tenant_id
        from cons_pile_machine_info
    </sql>

    <select id="selectPileMachineInfoList" parameterType="PileMachineInfo" resultMap="PileMachineInfoResult">
        <include refid="selectPileMachineInfoVo"/>
        <where>
            <if test="machineNum != null  and machineNum != ''">and machine_num = #{machineNum}</if>
            <if test="equipment != null  and equipment != ''">and equipment = #{equipment}</if>
            <if test="sn != null  and sn != ''">and sn = #{sn}</if>
            <if test="no != null  and no != ''">and no = #{no}</if>
            <if test="status != null  and status != ''">and status = #{status}</if>
        </where>
    </select>

    <select id="selectPileMachineInfoById" parameterType="Long" resultMap="PileMachineInfoResult">
        <include refid="selectPileMachineInfoVo"/>
        where id = #{id}
    </select>

    <select id="selectPileMachineInfoByByteId" parameterType="String" resultMap="PileMachineInfoResult">
        <include refid="selectPileMachineInfoVo"/>
        where byte_id = #{byteId}
    </select>

    <insert id="insertPileMachineInfo" parameterType="PileMachineInfo" useGeneratedKeys="true" keyProperty="id">
        insert into cons_pile_machine_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="byteId != null and byteId != ''">byte_id,</if>
            <if test="machineNum != null and machineNum != ''">machine_num,</if>
            <if test="equipment != null and equipment != ''">equipment,</if>
            <if test="no != null">no,</if>
            <if test="pwd != null">pwd,</if>
            <if test="sn != null">sn,</if>
            <if test="productKey != null">product_key,</if>
            <if test="deviceSecret != null">device_secret,</if>
            <if test="topic != null">topic,</if>
            <if test="serverHost != null">server_host,</if>
            <if test="serverPort != null">server_port,</if>
            <if test="serverUserName != null">server_user_name,</if>
            <if test="serverPassword != null">server_password,</if>
            <if test="status != null">status,</if>
            <if test="lng != null">lng,</if>
            <if test="lat != null">lat,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="tenantId != null">tenant_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="byteId != null and byteId != ''">#{byteId},</if>
            <if test="machineNum != null and machineNum != ''">#{machineNum},</if>
            <if test="equipment != null and equipment != ''">#{equipment},</if>
            <if test="no != null">#{no},</if>
            <if test="pwd != null">#{pwd},</if>
            <if test="sn != null">#{sn},</if>
            <if test="productKey != null">#{productKey},</if>
            <if test="deviceSecret != null">#{deviceSecret},</if>
            <if test="topic != null">#{topic},</if>
            <if test="serverHost != null">#{serverHost},</if>
            <if test="serverPort != null">#{serverPort},</if>
            <if test="serverUserName != null">#{serverUserName},</if>
            <if test="serverPassword != null">#{serverPassword},</if>
            <if test="status != null">#{status},</if>
            <if test="lng != null">#{lng},</if>
            <if test="lat != null">#{lat},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="tenantId != null">#{tenantId},</if>
        </trim>
    </insert>

    <update id="updatePileMachineInfo" parameterType="PileMachineInfo">
        update cons_pile_machine_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="byteId != null and byteId != ''">byte_id = #{byteId},</if>
            <if test="machineNum != null and machineNum != ''">machine_num = #{machineNum},</if>
            <if test="equipment != null and equipment != ''">equipment = #{equipment},</if>
            <if test="no != null">no = #{no},</if>
            <if test="pwd != null">pwd = #{pwd},</if>
            <if test="sn != null">sn = #{sn},</if>
            <if test="productKey != null">product_key = #{productKey},</if>
            <if test="deviceSecret != null">device_secret = #{deviceSecret},</if>
            <if test="topic != null">topic = #{topic},</if>
            <if test="serverHost != null">server_host = #{serverHost},</if>
            <if test="serverPort != null">server_port = #{serverPort},</if>
            <if test="serverUserName != null">server_user_name = #{serverUserName},</if>
            <if test="serverPassword != null">server_password = #{serverPassword},</if>
            <if test="status != null">status = #{status},</if>
            <if test="lng != null">lng = #{lng},</if>
            <if test="lat != null">lat = #{lat},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="tenantId != null">tenant_id = #{tenantId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePileMachineInfoById" parameterType="Long">
        delete
        from cons_pile_machine_info
        where id = #{id}
    </delete>

    <delete id="deletePileMachineInfoByIds" parameterType="String">
        delete from cons_pile_machine_info where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="cntMachineOnOffline">
        select sum(case when status = '00' then 1 else 0 end) as online,
               sum(case when status = '01' then 1 else 0 end) as offline
        from cons_pile_machine_info
    </select>
    <select id="qryUnitMachine" resultType="PileMachineInfo" parameterType="Long">
        select cpmi.*
        from cons_cons_unit_info unit
                 inner join cons_cons_unit_machine_rel rel on
            unit.id = rel.cons_unit_id
                 inner join cons_pile_machine_info cpmi on
            cpmi.id = rel.machine_id
        where unit.id = #{consUnitId}
    </select>
    <select id="selectMachineByConsUnitId" resultType="PileMachineInfo">
        select cpmi.*
        from cons_pile_machine_info cpmi
        inner join cons_cons_unit_machine_rel ccumr
        on
        cpmi.id = ccumr.machine_id
        inner join cons_cons_unit_info ccui
        on
        ccui.id = ccumr.cons_unit_id
        <where>
            <if test="consUnitId != null">
                and (ccumr.cons_unit_id = #{consUnitId} OR FIND_IN_SET(#{consUnitId}, ccui.ancestors))
            </if>
        </where>
    </select>

</mapper>