<?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.zehong.system.mapper.PalletDeviceBindingMapper">
    
    <resultMap type="PalletDeviceBinding" id="PalletDeviceBindingResult">
        <result property="palletDeviceBindingId"    column="f_pallet_device_binding_id"    />
        <result property="trayId"    column="f_tray_id"    />
        <result property="deviceCode"    column="f_device_code"    />
        <result property="row"    column="f_row"    />
        <result property="col"    column="f_col"    />
        <result property="index"    column="f_index"    />
        <result property="number"    column="f_number"    />
        <result property="bindingTime"    column="f_binding_time"    />
        <result property="unbindingTime"    column="f_unbinding_time"    />
        <result property="createTime"    column="f_create_time"    />
        <result property="status"    column="f_status"    />
        <result property="recordYear"    column="f_record_year"    />
        <result property="recordMonth"    column="f_record_month"    />
        <result property="recordDate"    column="f_record_date"    />
        <result property="recordHour"    column="f_record_hour"    />
        <result property="recordMinute"    column="f_record_minute"    />

        <result property="writeTimeStatus"    column="f_write_time_status"    />
        <result property="adjustmentZeroAd"    column="f_adjustment_zero_ad"    />
        <result property="zeroStatus"    column="f_zero_status"    />
        <result property="calibrationAd"    column="f_calibration_ad"    />
        <result property="calibrationAdStatus"    column="f_calibration_status"    />
        <result property="concentration"    column="f_concentration"    />

        <result property="runTimeStatus"    column="f_run_time_status"    />
        <result property="realTimeAd"    column="f_real_time_ad"    />
        <result property="realTimeStatus"    column="f_real_time_ad_status"    />

        <result property="calibrationConcentration"    column="f_calibration_concentration"    />
        <result property="calibrationConcentrationStatus"    column="f_calibration_concentration_status"    />

        <result property="writeSelfCheckStatus"    column="f_write_self_check_status"    />
        <result property="relayStatus"    column="f_relay_status"    />
        <result property="pulseStatus"    column="f_pulse_status"    />
        <result property="moduleStatus"    column="f_module_status"    />
        <result property="simCardStatus"    column="f_sim_card_status"    />
        <result property="networkStatus"    column="f_network_status"    />

    </resultMap>

    <sql id="selectPalletDeviceBindingVo">
        select palDeviceBinding.f_pallet_device_binding_id,
               palDeviceBinding.f_tray_id,
               trayInfo.f_tray_code as fTrayCode,
               palDeviceBinding.f_device_code,
               palDeviceBinding.f_row,
               palDeviceBinding.f_col,
               palDeviceBinding.f_index,
               palDeviceBinding.f_number,
               palDeviceBinding.f_binding_time,
               palDeviceBinding.f_unbinding_time,
               palDeviceBinding.f_create_time,
               palDeviceBinding.f_status,
               palDeviceBinding.f_record_year,
               palDeviceBinding.f_record_month,
               palDeviceBinding.f_record_date,
               palDeviceBinding.f_record_hour,
               palDeviceBinding.f_record_minute,
               palDeviceBinding.f_write_time_status,
               palDeviceBinding.f_adjustment_zero_ad,
               palDeviceBinding.f_zero_status,
               palDeviceBinding.f_calibration_ad,
               palDeviceBinding.f_calibration_status,
               palDeviceBinding.f_concentration,
               palDeviceBinding.f_run_time_status,
               palDeviceBinding.f_real_time_ad,
               palDeviceBinding.f_real_time_ad_status,
               palDeviceBinding.f_calibration_concentration,
               palDeviceBinding.f_calibration_concentration_status,
               palDeviceBinding.f_write_self_check_status,
               palDeviceBinding.f_relay_status,
               palDeviceBinding.f_pulse_status,
               palDeviceBinding.f_module_status,
               palDeviceBinding.f_sim_card_status,
               palDeviceBinding.f_network_status
        from t_pallet_device_binding palDeviceBinding
        left join t_tray_info trayInfo  on trayInfo.f_tray_id = palDeviceBinding.f_tray_id
    </sql>

    <select id="countErrorByTrayId" parameterType="Long" resultType="int">
        select count(1) from t_pallet_device_binding palDeviceBinding
        where palDeviceBinding.f_tray_id = #{trayId}
        and (palDeviceBinding.f_status = '0' or palDeviceBinding.f_status = '5')
    </select>
    <select id="countByTrayId" parameterType="Long" resultType="int">
        select count(1) from t_pallet_device_binding palDeviceBinding
        where palDeviceBinding.f_tray_id = #{trayId}
    </select>
    <select id="countDeviceByTrayId" parameterType="long" resultType="int">
        select count(1) from t_pallet_device_binding palDeviceBinding
        where palDeviceBinding.f_tray_id = #{trayId}
        and palDeviceBinding.f_device_code is not null
        and palDeviceBinding.f_device_code != ''
    </select>
    <select id="selectPalletDeviceBindingList" parameterType="PalletDeviceBinding" resultMap="PalletDeviceBindingResult">
        <include refid="selectPalletDeviceBindingVo"/>
        <where>  
            <if test="trayId != null "> and palDeviceBinding.f_tray_id = #{trayId}</if>
            <if test="deviceCode != null  and deviceCode != ''"> and palDeviceBinding.f_device_code like concat('%',#{deviceCode},'%') </if>
            <if test="fTrayCode != null  and fTrayCode != ''"> and trayInfo.f_tray_code like concat('%',#{fTrayCode},'%')</if>
            <if test="row != null "> and palDeviceBinding.f_row = #{row}</if>
            <if test="col != null "> and palDeviceBinding.f_col = #{col}</if>
            <if test="index != null "> and palDeviceBinding.f_index = #{index}</if>
            <if test="number != null "> and palDeviceBinding.f_number = #{number}</if>
            <if test="bindingTime != null "> and palDeviceBinding.f_binding_time = #{bindingTime}</if>
            <if test="unbindingTime != null "> and palDeviceBinding.f_unbinding_time = #{unbindingTime}</if>
            <if test="createTime != null "> and palDeviceBinding.f_create_time = #{createTime}</if>
            <if test="status != null and status != '' "> and palDeviceBinding.f_status = #{status}</if>
        </where>
        order by trayInfo.f_tray_code ,palDeviceBinding.f_index asc
    </select>
    <select id="getAllExcludeUnbindingTimeByTrayId" parameterType="long" resultMap="PalletDeviceBindingResult">
        <include refid="selectPalletDeviceBindingVo"/>
        where palDeviceBinding.f_tray_id = #{trayId}
        order by palDeviceBinding.f_index asc
    </select>
    <select id="listByTrayCode" parameterType="string" resultMap="PalletDeviceBindingResult">
        <include refid="selectPalletDeviceBindingVo"/>
        where trayInfo.f_tray_code = #{trayCode}
    </select>
    
    <select id="selectPalletDeviceBindingById" parameterType="Long" resultMap="PalletDeviceBindingResult">
        <include refid="selectPalletDeviceBindingVo"/>
        where palDeviceBinding.f_pallet_device_binding_id = #{palletDeviceBindingId}
    </select>
    <select id="selectByTrayIdAndIndex" resultMap="PalletDeviceBindingResult">
        select f_pallet_device_binding_id,
               f_tray_id,
               f_device_code,
               f_row,
               f_col,
               f_index,
               f_number,
               f_binding_time,
               f_unbinding_time,
               f_create_time,
               f_status,
               f_record_year,
               f_record_month,
               f_record_date,
               f_record_hour,
               f_record_minute ,
               f_write_time_status,
               f_adjustment_zero_ad,
               f_zero_status,
               f_calibration_ad,
               f_calibration_status,
               f_concentration,
               f_run_time_status,
               f_real_time_ad,
               f_real_time_ad_status,
               f_calibration_concentration,
               f_calibration_concentration_status,
               f_write_self_check_status,
               f_relay_status,
               f_pulse_status,
               f_module_status,
               f_sim_card_status,
               f_network_status
        from t_pallet_device_binding palDeviceBinding where palDeviceBinding.f_tray_id = (
                    SELECT
                        trayInfo.f_tray_id
                    FROM
                        t_tray_info trayInfo
                            LEFT JOIN t_storey_info storeyInfo ON trayInfo.f_storey_code = storeyInfo.f_storey_code
                    where storeyInfo.f_ip =  #{ip}
            ) and palDeviceBinding.f_index = #{index}

    </select>

    <select id="checkRepeat" parameterType="list" resultType="int">
        select count(1) from t_pallet_device_binding palDeviceBinding
        <if test="validDeviceCodes != null and validDeviceCodes.size() > 0">
            where palDeviceBinding.f_device_code in
            <foreach item="item" collection="validDeviceCodes" index="index"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="validDeviceCodes == null or validDeviceCodes.size() == 0">
            where 1=0
        </if>
    </select>
    <insert id="batchInsertPalletDeviceBinding" parameterType="list"  useGeneratedKeys="true" keyProperty="palletDeviceBindingId">
        insert into t_pallet_device_binding (f_tray_id, f_device_code,f_row,f_col,f_index,f_number,f_binding_time,
        f_create_time,f_status)
        values
        <foreach collection="palletDeviceBindingList" item="item" index="index" separator=",">
            (
            #{item.trayId}, #{item.deviceCode}, #{item.row}, #{item.col},#{item.index},#{item.number},
            #{item.bindingTime},#{item.createTime},#{item.status}
            )
        </foreach>
    </insert>
    <update id="resetAll" parameterType="long">
        update t_pallet_device_binding
        set f_device_code = null,
            f_status = null,
            f_binding_time = null,
            f_unbinding_time = null,
            f_status = null,
            f_record_year = null,
            f_record_month = null,
            f_record_date = null,
            f_record_hour = null,
            f_record_minute = null,
            f_write_time_status = null,
            f_adjustment_zero_ad = null,
            f_zero_status = null,
            f_calibration_ad = null,
            f_calibration_status = null,
            f_concentration = null,
            f_calibration_status = null,
            f_run_time_status = null,
            f_real_time_ad = null,
            f_real_time_ad_status = null,
            f_calibration_concentration = null,
            f_calibration_concentration_status = null,
            f_write_self_check_status = null,
            f_relay_status = null,
            f_pulse_status = null,
            f_module_status = null,
            f_sim_card_status = null,
            f_network_status = null
        where f_tray_id = #{trayId}
    </update>
    <insert id="insertPalletDeviceBinding" parameterType="PalletDeviceBinding" useGeneratedKeys="true" keyProperty="palletDeviceBindingId">
        insert into t_pallet_device_binding
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="trayId != null">f_tray_id,</if>
            <if test="deviceCode != null">f_device_code,</if>
            <if test="row != null">f_row,</if>
            <if test="col != null">f_col,</if>
            <if test="index != null">f_index,</if>
            <if test="number != null">f_number,</if>
            <if test="bindingTime != null">f_binding_time,</if>
            <if test="unbindingTime != null">f_unbinding_time,</if>
            <if test="createTime != null">f_create_time,</if>
            <if test="status != null">f_status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="trayId != null">#{trayId},</if>
            <if test="deviceCode != null">#{deviceCode},</if>
            <if test="row != null">#{row},</if>
            <if test="col != null">#{col},</if>
            <if test="index != null">#{index},</if>
            <if test="number != null">#{number},</if>
            <if test="bindingTime != null">#{bindingTime},</if>
            <if test="unbindingTime != null">#{unbindingTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="status != null">#{status},</if>
         </trim>
    </insert>

    <update id="unbindDevice" parameterType="long">
        update t_pallet_device_binding
        set f_device_code = null,
            f_status = null,
            f_unbinding_time = NOW(),
            f_record_year = null,
            f_record_month = null,
            f_record_date = null,
            f_record_hour = null,
            f_record_minute = null,
            f_write_time_status = null,
            f_adjustment_zero_ad = null,
            f_zero_status = null,
            f_calibration_ad = null,
            f_calibration_status = null,
            f_concentration = null,
            f_run_time_status = null,
            f_real_time_ad = null,
            f_real_time_ad_status = null,
            f_calibration_concentration = null,
            f_calibration_concentration_status = null,
            f_write_self_check_status = null,
            f_relay_status = null,
            f_pulse_status = null,
            f_module_status = null,
            f_sim_card_status = null,
            f_network_status = null
        where f_pallet_device_binding_id = #{palletDeviceBindingId}
    </update>
    <update id="updatePalletDeviceBinding" parameterType="PalletDeviceBinding">
        update t_pallet_device_binding
        <trim prefix="SET" suffixOverrides=",">
            <if test="trayId != null">f_tray_id = #{trayId},</if>
            <if test="deviceCode != null">f_device_code = #{deviceCode},</if>
            <if test="deviceCode == null">f_device_code = null,</if>
            <if test="row != null">f_row = #{row},</if>
            <if test="col != null">f_col = #{col},</if>
            <if test="index != null">f_index = #{index},</if>
            <if test="number != null">f_number = #{number},</if>
            <if test="bindingTime != null">f_binding_time = #{bindingTime},</if>
            <if test="unbindingTime != null">f_unbinding_time = #{unbindingTime},</if>
            <if test="createTime != null">f_create_time = #{createTime},</if>
            <if test="status != null">f_status = #{status},</if>

            <if test="recordYear != null">f_record_year = #{recordYear},</if>
            <if test="recordMonth != null">f_record_month = #{recordMonth},</if>
            <if test="recordDate != null">f_record_date = #{recordDate},</if>
            <if test="recordHour != null">f_record_hour = #{recordHour},</if>
            <if test="recordMinute != null">f_record_minute = #{recordMinute},</if>
            <if test="writeTimeStatus != null">f_write_time_status = #{writeTimeStatus},</if>
            <if test="adjustmentZeroAd != null">f_adjustment_zero_ad = #{adjustmentZeroAd},</if>
            <if test="zeroStatus != null">f_zero_status = #{zeroStatus},</if>
            <if test="calibrationAd != null">f_calibration_ad = #{calibrationAd},</if>
            <if test="concentration != null">f_concentration = #{concentration},</if>
            <if test="calibrationAdStatus != null">f_calibration_status = #{calibrationAdStatus},</if>
            <if test="runTimeStatus != null">f_run_time_status = #{runTimeStatus},</if>
            <if test="realTimeAd != null">f_real_time_ad = #{realTimeAd},</if>
            <if test="realTimeStatus != null">f_real_time_ad_status = #{realTimeStatus},</if>

            <if test="calibrationConcentration != null">f_calibration_concentration = #{calibrationConcentration},</if>
            <if test="calibrationConcentrationStatus != null">f_calibration_concentration_status = #{calibrationConcentrationStatus},</if>
            <if test="writeSelfCheckStatus != null">f_write_self_check_status = #{writeSelfCheckStatus},</if>
            <if test="relayStatus != null">f_relay_status = #{relayStatus},</if>
            <if test="pulseStatus != null">f_pulse_status = #{pulseStatus},</if>
            <if test="moduleStatus != null">f_module_status = #{moduleStatus},</if>
            <if test="simCardStatus != null">f_sim_card_status = #{simCardStatus},</if>
            <if test="networkStatus != null">f_network_status = #{networkStatus},</if>
        </trim>
        where f_pallet_device_binding_id = #{palletDeviceBindingId}
    </update>
    <update id="batchUpdateDeviceCode" parameterType="list">
        <foreach collection="palletDeviceBindingList" item="item"  index="index" separator=";">
            UPDATE t_pallet_device_binding
            <trim prefix="SET" suffixOverrides=",">
                <if test="item.deviceCode != null">f_device_code = #{item.deviceCode},</if>
                <if test="item.deviceCode == null">f_device_code = null,</if>
            </trim>
            WHERE f_pallet_device_binding_id = #{item.palletDeviceBindingId}
        </foreach>
    </update>
    <update id="batchUpdateAdAndStatus" parameterType="list">
        <foreach collection="palletDeviceBindingList" item="item"  index="index" separator=";">
            UPDATE t_pallet_device_binding
            <trim prefix="SET" suffixOverrides=",">
                <if test="item.adjustmentZeroAd != null">f_adjustment_zero_ad = #{item.adjustmentZeroAd},</if>
                <if test="item.zeroStatus == null">f_zero_status = #{item.zeroStatus},</if>
                <if test="item.calibrationAd != null">f_calibration_ad = #{item.calibrationAd},</if>
                <if test="item.calibrationAdStatus != null">f_calibration_status = #{item.calibrationAdStatus},</if>
                <if test="item.calibrationConcentration != null">f_calibration_concentration = #{item.calibrationConcentration},</if>
                <if test="item.calibrationConcentrationStatus != null">f_calibration_concentration_status = #{item.calibrationConcentrationStatus},</if>
            </trim>
            where f_pallet_device_binding_id = #{item.palletDeviceBindingId}
        </foreach>
    </update>
    <update id="batchUpdateDeviceCodeAndUnbindingTime" parameterType="list">
        <foreach collection="palletDeviceBindingList" item="item"  index="index" separator=";">
            UPDATE t_pallet_device_binding
            <trim prefix="SET" suffixOverrides=",">
                f_unbinding_time = null,
                f_status = null,
                f_binding_time = NOW(),
                f_record_year = null,
                f_record_month = null,
                f_record_date = null,
                f_record_hour = null,
                f_record_minute = null,
                f_write_time_status = null,
                f_adjustment_zero_ad = null,
                f_zero_status = null,
                f_calibration_ad = null,
                f_calibration_status = null,
                f_concentration = null,
                f_run_time_status = null,
                f_real_time_ad = null,
                f_real_time_ad_status = null,
                f_calibration_concentration = null,
                f_calibration_concentration_status = null,
                f_write_self_check_status = null,
                f_relay_status = null,
                f_pulse_status = null,
                f_module_status = null,
                f_sim_card_status = null,
                f_network_status = null
                <choose>
                    <when test="item.deviceCode != null">
                        , f_device_code = #{item.deviceCode}
                    </when>
                </choose>
            </trim>
            WHERE f_pallet_device_binding_id = #{item.palletDeviceBindingId}
        </foreach>
    </update>
    <update id="unbindAllDevice" parameterType="long">
        update t_pallet_device_binding
        set f_device_code = null,
            f_status = null,
            f_unbinding_time = NOW(),
            f_status = '1',
            f_record_year = null,
            f_record_month = null,
            f_record_date = null,
            f_record_hour = null,
            f_record_minute = null,
            f_write_time_status = null,
            f_adjustment_zero_ad = null,
            f_zero_status = null,
            f_calibration_ad = null,
            f_calibration_status = null,
            f_concentration = null,
            f_run_time_status = null,
            f_real_time_ad = null,
            f_real_time_ad_status = null,
            f_calibration_concentration = null,
            f_calibration_concentration_status = null,
            f_write_self_check_status = null,
            f_relay_status = null,
            f_pulse_status = null,
            f_module_status = null,
            f_sim_card_status = null,
            f_network_status = null
        where f_tray_id = #{trayId}
    </update>

    <delete id="deletePalletDeviceBindingById" parameterType="Long">
        delete from t_pallet_device_binding where f_pallet_device_binding_id = #{palletDeviceBindingId}
    </delete>
    <delete id="deleteAllByTrayId" parameterType="Long">
        delete from t_pallet_device_binding where f_tray_id = #{trayId}
    </delete>

    <delete id="deletePalletDeviceBindingByIds" parameterType="String">
        delete from t_pallet_device_binding where f_pallet_device_binding_id in 
        <foreach item="palletDeviceBindingId" collection="array" open="(" separator="," close=")">
            #{palletDeviceBindingId}
        </foreach>
    </delete>
</mapper>