<?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.ruoyi.business.mapper.HwDeviceModeMapper">
    
    <resultMap type="HwDeviceMode" id="HwDeviceModeResult">
        <result property="deviceModeId"    column="device_mode_id"    />
        <result property="deviceModeName"    column="device_mode_name"    />
        <result property="tenantId"    column="tenant_id"    />
        <result property="sceneId"    column="scene_id"    />
        <result property="languageCode"    column="language_code"    />
        <result property="gpsFlag"    column="gps_flag"    />
        <result property="deviceModeStatus"    column="device_mode_status"    />
        <result property="commonFlag"    column="common_flag"    />
        <result property="modeClassfication"    column="mode_classfication"    />
        <result property="deviceModePic"    column="device_mode_pic"    />
        <result property="dataVerifyLevel"    column="data_verify_level"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="deviceModeField"    column="device_mode_field"    />
        <result property="sceneName"    column="scene_name"    />
        <result property="tenantName"    column="tenant_name"    />
    </resultMap>

    <resultMap id="HwDeviceModeHwDeviceModeFunctionResult" type="HwDeviceMode" extends="HwDeviceModeResult">
        <collection property="hwDeviceModeFunctionList" notNullColumn="sub_mode_function_id" javaType="java.util.List" resultMap="HwDeviceModeFunctionResult" />
    </resultMap>

    <resultMap type="HwDeviceModeFunction" id="HwDeviceModeFunctionResult">
        <result property="modeFunctionId"    column="sub_mode_function_id"    />
        <result property="deviceModeId"    column="sub_device_mode_id"    />
        <result property="functionMode"    column="sub_function_mode"    />
        <result property="coordinate"    column="sub_coordinate"    />
        <result property="functionName"    column="sub_function_name"    />
        <result property="functionIdentifier"    column="sub_function_identifier"    />
        <result property="functionType"    column="sub_function_type"    />
        <result property="dataType"    column="sub_data_type"    />
        <result property="dataDefinition"    column="sub_data_definition"    />
        <result property="functionFormula"    column="sub_function_formula"    />
        <result property="propertyUnit"    column="sub_property_unit"    />
        <result property="displayFlag"    column="sub_display_flag"    />
        <result property="rwFlag"    column="sub_rw_flag"    />
        <result property="invokeMethod"    column="sub_invoke_method"    />
        <result property="eventType"    column="sub_event_type"    />
        <result property="remark"    column="sub_remark"    />
        <result property="acquisitionFormula"    column="sub_acquisition_formula"    />
        <result property="orderFlag"    column="sub_order_flag"    />
        <result property="deviceRegister"    column="sub_device_register"    />
        <result property="propertyStep"    column="sub_property_step"    />
        <result property="propertyField"    column="sub_property_field"    />
    </resultMap>

    <resultMap type="HwDeviceModeParameter" id="HwDeviceModeParameterResult">
        <result property="modeParameterId"    column="mode_parameter_id"    />
        <result property="modeFunctionId"    column="mode_function_id"    />
        <result property="releatedParameterId"    column="releated_parameter_id"    />
        <result property="parameterType"    column="parameter_type"    />
        <result property="parameterName"    column="parameter_name"    />
        <result property="parameterIdentifier"    column="parameter_identifier"    />
        <result property="dataType"    column="data_type"    />
        <result property="dataDefinition"    column="data_definition"    />
        <result property="propertyUnit"    column="property_unit"    />
        <result property="propertyStep"    column="property_step"    />
    </resultMap>


    <sql id="selectHwDeviceModeVo">
        select device_mode_id, device_mode_name, tenant_id, scene_id, language_code, gps_flag, device_mode_status, common_flag, mode_classfication, device_mode_pic, data_verify_level, remark, create_by, create_time, update_by, update_time, device_mode_field from hw_device_mode hdm
    </sql>

    <sql id="selectHwDeviceModeParameterVo">
        select mode_parameter_id, mode_function_id, releated_parameter_id, parameter_type, parameter_name, parameter_identifier, data_type, data_definition, property_unit, property_step from hw_device_mode_parameter
    </sql>

    <select id="selectHwDeviceModeList" parameterType="HwDeviceMode" resultMap="HwDeviceModeResult">
        <include refid="selectHwDeviceModeVo"/>
        <where>  
            <if test="deviceModeName != null  and deviceModeName != ''"> and device_mode_name like concat('%', #{deviceModeName}, '%')</if>
            <if test="tenantId != null "> and tenant_id = #{tenantId}</if>
            <if test="sceneId != null "> and scene_id = #{sceneId}</if>
            <if test="languageCode != null  and languageCode != ''"> and language_code = #{languageCode}</if>
            <if test="gpsFlag != null  and gpsFlag != ''"> and gps_flag = #{gpsFlag}</if>
            <if test="deviceModeStatus != null  and deviceModeStatus != ''"> and device_mode_status = #{deviceModeStatus}</if>
            <if test="commonFlag != null  and commonFlag != ''"> and common_flag = #{commonFlag}</if>
            <if test="modeClassfication != null "> and mode_classfication = #{modeClassfication}</if>
            <if test="deviceModePic != null  and deviceModePic != ''"> and device_mode_pic = #{deviceModePic}</if>
            <if test="dataVerifyLevel != null  and dataVerifyLevel != ''"> and data_verify_level = #{dataVerifyLevel}</if>
            <if test="deviceModeField != null  and deviceModeField != ''"> and device_mode_field = #{deviceModeField}</if>
            <!-- 租户数据范围过滤 -->
            ${params.tenantDataScope}
        </where>
        order by device_mode_id desc
    </select>
    
    <select id="selectHwDeviceModeByDeviceModeId" parameterType="Long" resultMap="HwDeviceModeHwDeviceModeFunctionResult">
        select a.device_mode_id, a.device_mode_name, a.tenant_id, a.scene_id, a.language_code, a.gps_flag, a.device_mode_status, a.common_flag, a.mode_classfication, a.device_mode_pic, a.data_verify_level, a.remark, a.create_by, a.create_time, a.update_by, a.update_time, a.device_mode_field,
 b.mode_function_id as sub_mode_function_id, b.device_mode_id as sub_device_mode_id, b.function_mode as sub_function_mode, b.coordinate as sub_coordinate, b.function_name as sub_function_name, b.function_identifier as sub_function_identifier, b.function_type as sub_function_type, b.data_type as sub_data_type, b.data_definition as sub_data_definition, b.function_formula as sub_function_formula, b.property_unit as sub_property_unit, b.display_flag as sub_display_flag, b.rw_flag as sub_rw_flag, b.invoke_method as sub_invoke_method, b.event_type as sub_event_type, b.remark as sub_remark, b.acquisition_formula as sub_acquisition_formula, b.order_flag as sub_order_flag, b.device_register as sub_device_register, b.property_step as sub_property_step, b.property_field as sub_property_field
        from hw_device_mode a
        left join hw_device_mode_function b on b.device_mode_id = a.device_mode_id
        where a.device_mode_id = #{deviceModeId}
    </select>
        
    <insert id="insertHwDeviceMode" parameterType="HwDeviceMode" useGeneratedKeys="true" keyProperty="deviceModeId">
        insert into hw_device_mode
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceModeName != null and deviceModeName != ''">device_mode_name,</if>
            <if test="tenantId != null">tenant_id,</if>
            <if test="sceneId != null">scene_id,</if>
            <if test="languageCode != null">language_code,</if>
            <if test="gpsFlag != null and gpsFlag != ''">gps_flag,</if>
            <if test="deviceModeStatus != null and deviceModeStatus != ''">device_mode_status,</if>
            <if test="commonFlag != null and commonFlag != ''">common_flag,</if>
            <if test="modeClassfication != null">mode_classfication,</if>
            <if test="deviceModePic != null">device_mode_pic,</if>
            <if test="dataVerifyLevel != null">data_verify_level,</if>
            <if test="remark != null">remark,</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="deviceModeField != null">device_mode_field,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceModeName != null and deviceModeName != ''">#{deviceModeName},</if>
            <if test="tenantId != null">#{tenantId},</if>
            <if test="sceneId != null">#{sceneId},</if>
            <if test="languageCode != null">#{languageCode},</if>
            <if test="gpsFlag != null and gpsFlag != ''">#{gpsFlag},</if>
            <if test="deviceModeStatus != null and deviceModeStatus != ''">#{deviceModeStatus},</if>
            <if test="commonFlag != null and commonFlag != ''">#{commonFlag},</if>
            <if test="modeClassfication != null">#{modeClassfication},</if>
            <if test="deviceModePic != null">#{deviceModePic},</if>
            <if test="dataVerifyLevel != null">#{dataVerifyLevel},</if>
            <if test="remark != null">#{remark},</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="deviceModeField != null">#{deviceModeField},</if>
         </trim>
    </insert>

    <update id="updateHwDeviceMode" parameterType="HwDeviceMode">
        update hw_device_mode
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceModeName != null and deviceModeName != ''">device_mode_name = #{deviceModeName},</if>
            <if test="tenantId != null">tenant_id = #{tenantId},</if>
            <if test="sceneId != null">scene_id = #{sceneId},</if>
            <if test="languageCode != null">language_code = #{languageCode},</if>
            <if test="gpsFlag != null and gpsFlag != ''">gps_flag = #{gpsFlag},</if>
            <if test="deviceModeStatus != null and deviceModeStatus != ''">device_mode_status = #{deviceModeStatus},</if>
            <if test="commonFlag != null and commonFlag != ''">common_flag = #{commonFlag},</if>
            <if test="modeClassfication != null">mode_classfication = #{modeClassfication},</if>
            <if test="deviceModePic != null">device_mode_pic = #{deviceModePic},</if>
            <if test="dataVerifyLevel != null">data_verify_level = #{dataVerifyLevel},</if>
            <if test="remark != null">remark = #{remark},</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="deviceModeField != null">device_mode_field = #{deviceModeField},</if>
        </trim>
        where device_mode_id = #{deviceModeId}
    </update>

    <delete id="deleteHwDeviceModeByDeviceModeId" parameterType="Long">
        delete from hw_device_mode where device_mode_id = #{deviceModeId}
    </delete>

    <delete id="deleteHwDeviceModeByDeviceModeIds" parameterType="String">
        delete from hw_device_mode where device_mode_id in 
        <foreach item="deviceModeId" collection="array" open="(" separator="," close=")">
            #{deviceModeId}
        </foreach>
    </delete>
    
    <delete id="deleteHwDeviceModeFunctionByDeviceModeIds" parameterType="String">
        delete from hw_device_mode_function where device_mode_id in 
        <foreach item="deviceModeId" collection="array" open="(" separator="," close=")">
            #{deviceModeId}
        </foreach>
    </delete>

    <delete id="deleteHwDeviceModeFunctionByDeviceModeId" parameterType="Long">
        delete from hw_device_mode_function where device_mode_id = #{deviceModeId}
    </delete>

    <insert id="batchHwDeviceModeFunction" useGeneratedKeys="true" keyProperty="modeFunctionId">
        insert into hw_device_mode_function( mode_function_id, device_mode_id, function_mode, coordinate, function_name, function_identifier, function_type, data_type, data_definition, function_formula, property_unit, display_flag, rw_flag, invoke_method, event_type, remark, acquisition_formula, order_flag, device_register, property_step, property_field) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.modeFunctionId}, #{item.deviceModeId}, #{item.functionMode}, #{item.coordinate}, #{item.functionName}, #{item.functionIdentifier}, #{item.functionType}, #{item.dataType}, #{item.dataDefinition}, #{item.functionFormula}, #{item.propertyUnit}, #{item.displayFlag}, #{item.rwFlag}, #{item.invokeMethod}, #{item.eventType}, #{item.remark}, #{item.acquisitionFormula}, #{item.orderFlag}, #{item.deviceRegister}, #{item.propertyStep}, #{item.propertyField})
        </foreach>
    </insert>

    <insert id="insertHwDeviceModeFunction" parameterType="HwDeviceModeFunction" useGeneratedKeys="true" keyProperty="modeFunctionId">
        insert into hw_device_mode_function
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceModeId != null">device_mode_id,</if>
            <if test="functionMode != null and functionMode != ''">function_mode,</if>
            <if test="coordinate != null">coordinate,</if>
            <if test="functionName != null and functionName != ''">function_name,</if>
            <if test="functionIdentifier != null and functionIdentifier != ''">function_identifier,</if>
            <if test="functionType != null">function_type,</if>
            <if test="dataType != null">data_type,</if>
            <if test="dataDefinition != null">data_definition,</if>
            <if test="functionFormula != null">function_formula,</if>
            <if test="propertyUnit != null">property_unit,</if>
            <if test="displayFlag != null">display_flag,</if>
            <if test="rwFlag != null">rw_flag,</if>
            <if test="invokeMethod != null">invoke_method,</if>
            <if test="eventType != null">event_type,</if>
            <if test="remark != null">remark,</if>
            <if test="acquisitionFormula != null">acquisition_formula,</if>
            <if test="orderFlag != null">order_flag,</if>
            <if test="deviceRegister != null">device_register,</if>
            <if test="propertyStep != null">property_step,</if>
            <if test="propertyField != null">property_field,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceModeId != null">#{deviceModeId},</if>
            <if test="functionMode != null and functionMode != ''">#{functionMode},</if>
            <if test="coordinate != null">#{coordinate},</if>
            <if test="functionName != null and functionName != ''">#{functionName},</if>
            <if test="functionIdentifier != null and functionIdentifier != ''">#{functionIdentifier},</if>
            <if test="functionType != null">#{functionType},</if>
            <if test="dataType != null">#{dataType},</if>
            <if test="dataDefinition != null">#{dataDefinition},</if>
            <if test="functionFormula != null">#{functionFormula},</if>
            <if test="propertyUnit != null">#{propertyUnit},</if>
            <if test="displayFlag != null">#{displayFlag},</if>
            <if test="rwFlag != null">#{rwFlag},</if>
            <if test="invokeMethod != null">#{invokeMethod},</if>
            <if test="eventType != null">#{eventType},</if>
            <if test="remark != null">#{remark},</if>
            <if test="acquisitionFormula != null">#{acquisitionFormula},</if>
            <if test="orderFlag != null">#{orderFlag},</if>
            <if test="deviceRegister != null">#{deviceRegister},</if>
            <if test="propertyStep != null">#{propertyStep},</if>
            <if test="propertyField != null">#{propertyField},</if>
        </trim>
    </insert>

    <update id="updateHwDeviceModeFunction" parameterType="HwDeviceModeFunction">
        update hw_device_mode_function
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceModeId != null">device_mode_id = #{deviceModeId},</if>
            <if test="functionMode != null and functionMode != ''">function_mode = #{functionMode},</if>
            <if test="coordinate != null">coordinate = #{coordinate},</if>
            <if test="functionName != null and functionName != ''">function_name = #{functionName},</if>
            <if test="functionIdentifier != null and functionIdentifier != ''">function_identifier = #{functionIdentifier},</if>
            <if test="functionType != null">function_type = #{functionType},</if>
            <if test="dataType != null">data_type = #{dataType},</if>
            <if test="dataDefinition != null">data_definition = #{dataDefinition},</if>
            <if test="functionFormula != null">function_formula = #{functionFormula},</if>
            <if test="propertyUnit != null">property_unit = #{propertyUnit},</if>
            <if test="displayFlag != null">display_flag = #{displayFlag},</if>
            <if test="rwFlag != null">rw_flag = #{rwFlag},</if>
            <if test="invokeMethod != null">invoke_method = #{invokeMethod},</if>
            <if test="eventType != null">event_type = #{eventType},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="acquisitionFormula != null">acquisition_formula = #{acquisitionFormula},</if>
            <if test="orderFlag != null">order_flag = #{orderFlag},</if>
            <if test="deviceRegister != null">device_register = #{deviceRegister},</if>
            <if test="propertyStep != null">property_step = #{propertyStep},</if>
            <if test="propertyField != null">property_field = #{propertyField},</if>
        </trim>
        where mode_function_id = #{modeFunctionId}
    </update>

    <delete id="deleteHwDeviceModeFunctionByModeFunctionId" parameterType="Long">
        delete from hw_device_mode_function where mode_function_id = #{modeFunctionId}
    </delete>


    <insert id="batchHwDeviceModeParameters">
        insert into hw_device_mode_parameter( mode_function_id, parameter_type, parameter_name, parameter_identifier, data_type, data_definition,  property_unit, property_step) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.modeFunctionId}, #{item.parameterType},  #{item.parameterName}, #{item.parameterIdentifier}, #{item.dataType}, #{item.dataDefinition}, #{item.propertyUnit}, #{item.propertyStep})
        </foreach>
    </insert>

    <delete id="deleteHwDeviceModeParameterByModeFunctionId" parameterType="Long">
        delete from hw_device_mode_parameter where mode_function_id = #{modeFunctionId}
    </delete>

    <select id="selectDeviceModeParametersByModeFunctionId" parameterType="Long" resultMap="HwDeviceModeParameterResult">
        <include refid="selectHwDeviceModeParameterVo"/>
        where mode_function_id = #{modeFunctionId}
    </select>


    <select id="selectHwDeviceModeJoinList" parameterType="HwDeviceMode" resultMap="HwDeviceModeResult">
        select hdm.device_mode_id,hdm.device_mode_name,hdm.gps_flag,hdm.mode_classfication,
        hs.scene_name,ht.tenant_name,hdm.tenant_id,hdm.scene_id
        from hw_device_mode hdm
        left join hw_scene hs on hdm.scene_id = hs.scene_id
        left join hw_tenant ht on hdm.tenant_id=ht.tenant_id
        <where>
            <if test="deviceModeName != null  and deviceModeName != ''"> and hdm.device_mode_name like concat('%', #{deviceModeName}, '%')</if>
            <if test="tenantId != null "> and hdm.tenant_id = #{tenantId}</if>
            <if test="sceneId != null "> and hdm.scene_id = #{sceneId}</if>
            <if test="languageCode != null  and languageCode != ''"> and hdm.language_code = #{languageCode}</if>
            <if test="gpsFlag != null  and gpsFlag != ''"> and hdm.gps_flag = #{gpsFlag}</if>
            <if test="deviceModeStatus != null  and deviceModeStatus != ''"> and hdm.device_mode_status = #{deviceModeStatus}</if>
            <if test="commonFlag != null  and commonFlag != ''"> and hdm.common_flag = #{commonFlag}</if>
            <if test="modeClassfication != null "> and hdm.mode_classfication = #{modeClassfication}</if>
            <if test="deviceModePic != null  and deviceModePic != ''"> and hdm.device_mode_pic = #{deviceModePic}</if>
            <if test="dataVerifyLevel != null  and dataVerifyLevel != ''"> and hdm.data_verify_level = #{dataVerifyLevel}</if>
            <if test="deviceModeField != null  and deviceModeField != ''"> and hdm.device_mode_field = #{deviceModeField}</if>
            <!-- 租户数据范围过滤 -->
            ${params.tenantDataScope}
        </where>
    </select>


</mapper>