<?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.api.mapper.ApiMapper">

    <insert id="submintInsertRecordIn">
        INSERT ignore into bg_wheel_chocks.record_in (epc_code, batch_code, location_code,manufacturer_id,
        manufacturer_Name,
        create_by,create_time)
        VALUES
        <foreach collection="inStore.epclist" item="item" separator=",">
            (#{item},#{inStore.binchCode},#{inStore.selectLocationText},#{inStore.manufacturerId},#{inStore.selectManuText},#{inStore.submitUser},now())
        </foreach>
    </insert>
    <insert id="submintInsertLedger">
        INSERT ignore into bg_wheel_chocks.ledger_rfid (manufacturer_id, rifd_code, batch_code, location_code)
        VALUES
        <foreach collection="inStore.epclist" item="item" separator=",">
            (#{inStore.manufacturerId},#{item},#{inStore.binchCode},#{inStore.selectLocationText})
        </foreach>
    </insert>

    <!--    -->

    <insert id="submitInsertRecordOut">
        INSERT INTO bg_wheel_chocks.record_out (epc_code, location_code, use_user, create_by) VALUES
        <foreach collection="outStoreDto.epcList" item="epc" separator=",">
            (#{epc}, #{outStoreDto.areaCode}, #{outStoreDto.useUser}, #{outStoreDto.submitUser})
        </foreach>
    </insert>

    <update id="updataLedger">
        UPDATE bg_wheel_chocks.ledger_rfid
        SET
        location_type = '2',
        location_code = #{outStoreDto.areaCode},
        update_time = now()
        WHERE rifd_code in
        <foreach collection="outStoreDto.epcList" item="epc" separator="," open="(" close=")">
            #{epc}
        </foreach>
    </update>

    <select id="storeCheckSelect" resultType="com.ruoyi.api.domain.StockDto">
        select row_number() over (order by location_code) as 'index',
               rifd_code                                  as epcCode,
               location_code                              as locationCode,
               'N'                                        as state
        from ledger_rfid
        where location_type = 1
          and is_scrap = 1
    </select>

    <insert id="insertCheckInfo">
        INSERT INTO bg_wheel_chocks.record_store_check_info (check_id, epc_code, location_code, check_state)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{id}, #{item.epcCode},#{item.locationCode},#{item.state})
        </foreach>
    </insert>

    <select id="xjSelect" resultType="com.ruoyi.api.domain.StockDto">
        select row_number() over (order by lf.location_code) as 'index',
               rifd_code                                  as epcCode,
               lf.location_code                           as locationCode,
               lf.create_time                             as createTime,
               '未检'                                     as state
        from ledger_rfid lf
                 left join base_location bl on lf.location_code = bl.location_code
                 left join base_area_user bau on bl.store_id = bau.area_id
        where user_id=#{user}
          and is_scrap = 1
    </select>

    <select id="bfSelect" resultMap="com.ruoyi.manage.mapper.LedgerRfidMapper.LedgerRfidResult">
        select objid,
               manufacturer_id,
               rifd_code,
               batch_code,
               location_type,
               location_code,
               dict_label
        from ledger_rfid lf
                 left join sys_dict_data sdd on sdd.dict_value = is_scrap and sdd.dict_type = 'bf_y_n'
        where rifd_code =#{epc}
    </select>
    <update id="updataLedgerForBf" >
        UPDATE bg_wheel_chocks.ledger_rfid
        SET is_scrap = #{state},
            update_time=now()
        WHERE rifd_code =#{epc}
    </update>

    <select id="fpInSelect" resultMap="com.ruoyi.manage.mapper.LedgerRfidMapper.LedgerRfidResult">
        select rifd_code,
               batch_code,
               sdd1.dict_label as location_type,
               location_code,
               sdd. dict_label,
               ri.create_time
        from ledger_rfid lf
                 left join sys_dict_data sdd on sdd.dict_value = is_scrap and sdd.dict_type = 'bf_y_n'
                 left join sys_dict_data sdd1 on sdd1.dict_value = location_type and sdd1.dict_type = 'store_tag'
                 left join bg_wheel_chocks.record_invalidated ri on lf.rifd_code = ri.epc
        where rifd_code = #{epc} limit 1
    </select>
    <insert id="fpInSubmit" >
        INSERT INTO record_waste_in (epc, location_code, bf_time, in_code, create_by)
        VALUES (#{epc}, #{localtionCode},#{bf},#{subCode},#{user});

    </insert>
    <update id="updataLedgerIn">
        UPDATE ledger_rfid
        SET location_type   = '1',
            location_code   = #{code},
            update_time     = now()
        WHERE   rifd_code       =#{epc}
    </update>
</mapper>