<?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.gyjiot.iot.mapper.ProductMapper">

    <resultMap type="com.gyjiot.iot.domain.Product" id="ProductResult">
        <result property="productId"    column="product_id"    />
        <result property="productName"    column="product_name"    />
        <result property="protocolCode" column="protocol_code"/>
        <result property="categoryId"    column="category_id"    />
        <result property="categoryName"    column="category_name"    />
        <result property="tenantId"    column="tenant_id"    />
        <result property="tenantName"    column="tenant_name"    />
        <result property="isSys"    column="is_sys"    />
        <result property="isAuthorize"    column="is_authorize"    />
        <result property="mqttAccount"    column="mqtt_account"    />
        <result property="mqttPassword"    column="mqtt_password"    />
        <result property="mqttSecret"    column="mqtt_secret"    />
        <result property="status"    column="status"    />
        <result property="deviceType"    column="device_type"    />
        <result property="networkMethod"    column="network_method"    />
        <result property="vertificateMethod"    column="vertificate_method"    />
        <result property="imgUrl"    column="img_url"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="transport" column="transport"/>
    </resultMap>

    <resultMap type="com.gyjiot.iot.model.IdAndName" id="ProductShortResult">
        <result property="id"    column="product_id"    />
        <result property="name"    column="product_name"    />
    </resultMap>

    <sql id="selectProductVo">
        select product_id, product_name,protocol_code,transport, category_id, category_name, tenant_id, tenant_name, is_sys, is_authorize, mqtt_account,mqtt_password,mqtt_secret ,status, device_type, network_method, vertificate_method, create_time, update_time, img_url,remark from iot_product
    </sql>

    <select id="selectProductList" parameterType="com.gyjiot.iot.domain.Product" resultMap="ProductResult">
        <include refid="selectProductVo"/>
        <where>
            <if test="productName != null  and productName != ''"> and product_name like concat('%', #{productName}, '%')</if>
            <if test="categoryId != null "> and category_id = #{categoryId}</if>
            <if test="categoryName != null  and categoryName != ''"> and category_name like concat('%', #{categoryName}, '%')</if>
            <if test="tenantName != null  and tenantName != ''"> and tenant_name like concat('%', #{tenantName}, '%')</if>
            <if test="isAuthorize != null "> and is_authorize = #{isAuthorize}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="deviceType != null "> and device_type = #{deviceType}</if>
            <if test="networkMethod != null "> and network_method = #{networkMethod}</if>
            <if test="tenantId != null  and tenantId != 0"> and tenant_id = #{tenantId}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectProductShortList" parameterType="com.gyjiot.iot.domain.Product" resultMap="ProductShortResult">
        select product_id,product_name from iot_product
        <where>
            <if test="tenantId != null  and tenantId != 0"> and tenant_id = #{tenantId}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectProductByProductId" parameterType="Long" resultMap="ProductResult">
        <include refid="selectProductVo"/>
        where product_id = #{productId}
    </select>

    <insert id="insertProduct" parameterType="com.gyjiot.iot.domain.Product" useGeneratedKeys="true" keyProperty="productId">
        insert into iot_product
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="productName != null and productName != ''">product_name,</if>
            <if test="categoryId != null">category_id,</if>
            <if test="categoryName != null and categoryName != ''">category_name,</if>
            <if test="tenantId != null">tenant_id,</if>
            <if test="tenantName != null and tenantName != ''">tenant_name,</if>
            <if test="isSys != null">is_sys,</if>
            <if test="isAuthorize != null">is_authorize,</if>
            <if test="mqttAccount != null and mqttAccount != ''">mqtt_account,</if>
            <if test="mqttPassword != null and mqttPassword != ''">mqtt_password,</if>
            <if test="mqttSecret != null and mqttSecret != ''">mqtt_secret,</if>
            <if test="status != null">status,</if>
            <if test="deviceType != null">device_type,</if>
            <if test="networkMethod != null">network_method,</if>
            <if test="vertificateMethod != null">vertificate_method,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="imgUrl != null">img_url,</if>
            <if test="protocolCode != null">protocol_code,</if>
            <if test="transport != null" >transport,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="productName != null and productName != ''">#{productName},</if>
            <if test="categoryId != null">#{categoryId},</if>
            <if test="categoryName != null and categoryName != ''">#{categoryName},</if>
            <if test="tenantId != null">#{tenantId},</if>
            <if test="tenantName != null and tenantName != ''">#{tenantName},</if>
            <if test="isSys != null">#{isSys},</if>
            <if test="isAuthorize != null">#{isAuthorize},</if>
            <if test="mqttAccount != null and mqttAccount != ''">#{mqttAccount},</if>
            <if test="mqttPassword != null and mqttPassword != ''">#{mqttPassword},</if>
            <if test="mqttSecret != null and mqttSecret != ''">#{mqttSecret},</if>
            <if test="status != null">#{status},</if>
            <if test="deviceType != null">#{deviceType},</if>
            <if test="networkMethod != null">#{networkMethod},</if>
            <if test="vertificateMethod != null">#{vertificateMethod},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="imgUrl != null">#{imgUrl},</if>
            <if test="protocolCode != null">#{protocolCode,jdbcType=VARCHAR}, </if>
            <if test="transport != null" >#{transport,jdbcType=VARCHAR},</if>
         </trim>
    </insert>

    <update id="updateProduct" parameterType="com.gyjiot.iot.domain.Product">
        update iot_product
        <trim prefix="SET" suffixOverrides=",">
            <if test="productName != null and productName != ''">product_name = #{productName},</if>
            <if test="categoryId != null">category_id = #{categoryId},</if>
            <if test="categoryName != null and categoryName != ''">category_name = #{categoryName},</if>
            <if test="tenantId != null">tenant_id = #{tenantId},</if>
            <if test="tenantName != null and tenantName != ''">tenant_name = #{tenantName},</if>
            <if test="isSys != null">is_sys = #{isSys},</if>
            <if test="isAuthorize != null">is_authorize = #{isAuthorize},</if>
            <if test="mqttAccount != null and mqttAccount != ''">mqtt_account = #{mqttAccount},</if>
            <if test="mqttPassword != null and mqttPassword != ''">mqtt_password = #{mqttPassword},</if>
            <if test="mqttSecret != null and mqttSecret != ''">mqtt_secret = #{mqttSecret},</if>
            <if test="status != null">status = #{status},</if>
            <if test="deviceType != null">device_type = #{deviceType},</if>
            <if test="networkMethod != null">network_method = #{networkMethod},</if>
            <if test="vertificateMethod != null">vertificate_method = #{vertificateMethod},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="imgUrl != null">img_url = #{imgUrl},</if>
            <if test="protocolCode != null">protocol_code = #{protocolCode,jdbcType=VARCHAR}, </if>
            <if test="transport != null" >transport = #{transport,jdbcType=VARCHAR},</if>
        </trim>
        where product_id = #{productId}
    </update>

    <update id="changeProductStatus" parameterType="com.gyjiot.iot.model.ChangeProductStatusModel">
        update iot_product set status=#{status}
        where product_id = #{productId}
    </update>

    <update id="updateThingsModelJson" parameterType="com.gyjiot.iot.domain.Product">
        update iot_product set things_models_json=#{thingsModelsJson}
        where product_id = #{productId}
    </update>

    <delete id="deleteProductByProductId" parameterType="Long">
        delete from iot_product where product_id = #{productId}
    </delete>

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

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

    <select id="deviceCountInProducts" parameterType="String" resultType="int">
        select count(*) from iot_device where product_id in
        <foreach item="productId" collection="array" open="(" separator="," close=")">
            #{productId}
        </foreach>
    </select>

    <select id="thingsCountInProduct" parameterType="Long" resultType="int">
        select count(model_id) from iot_things_model
        where product_id = #{productId}
    </select>

    <select id="thingsRepeatCountInProduct" parameterType="Long" resultType="int">
        SELECT count( identifier )
        FROM ( SELECT identifier
               FROM iot_things_model
               WHERE product_id = #{productId}
               GROUP BY identifier,product_id
               HAVING count( identifier )> 1 ) AS identifiers
    </select>

    <select id="getProductBySerialNumber" resultMap="ProductResult" parameterType="String">
        <include refid="selectProductVo" />
        where product_id =
        (select product_id from iot_device
          where serial_number = #{serialNumber,jdbcType=VARCHAR}
        )
    </select>

    <select id="getProtocolBySerialNumber" resultType="com.gyjiot.iot.model.ProductCode">
        select p.protocol_code as protocolCode,
               p.product_id as productId
          from
           iot_product p
           inner join iot_device d on p.product_id = d.product_id
           and d.serial_number = #{serialNumber,jdbcType=VARCHAR}
    </select>

    <select id="getProtocolByProductId" resultType="java.lang.String">
        select p.protocol_code from
        iot_product p
        where p.product_id = #{productId,jdbcType=BIGINT}
    </select>

    <select id="selectByTempleId" resultMap="ProductResult">
        select p.product_id,p.product_name,p.protocol_code,p.transport from iot_device_template t inner join
                                                                            iot_product p on t.product_id = p.product_id
        where t.template_id = #{templeId}
    </select>
</mapper>
