<?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">


    <select id="selectLedgerbyEpcForOutStore" resultType="string">
        select rifd_code from ledger_rfid where rifd_code = #{epc}
                                            and is_scrap = 1
                                            and location_type = '1'
        limit 1
    </select>
    <select id="selectLedgerbyEpcForInStore" resultType="string">
        select rifd_code from ledger_rfid where rifd_code = #{epc} limit 1
    </select>
    <insert id="submintInsertRecordIn">
        INSERT ignore into 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 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 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 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>
    <update id="updataLedgerFoyOut">
        UPDATE ledger_rfid
        SET
        location_type = '2',
        location_code = #{outStoreDto.areaCode},
        update_time = now()
        WHERE rifd_code =#{epc}
    </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 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 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 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>

    <insert id="insertInspectionInfo">
        INSERT INTO record_inspection_info (inspection_id, epc_code, location_code, inspection_state,
        inspection_remark)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{id}, #{item.epcCode},#{item.locationCode},#{item.state}, #{item.remark})
        </foreach>


    </insert>

    <select id="fpOutSelect" 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 = 2
    </select>

    <insert id="insertWasreOutRecord">
        INSERT INTO record_waste_out (epc_code, location_code, create_by) VALUES
        <foreach collection="list" item="item" separator=",">
            ( #{item.epcCode},#{item.locationCode},#{user})
        </foreach>
    </insert>

    <update id="updataLedgerForWasreOut">
        UPDATE ledger_rfid
        SET location_type = null,
        location_code = null,
        is_scrap = '3',
        update_time = now()
        <where>
            rifd_code in
            <foreach item="item" collection="list" open="(" separator="," close=")">
                #{item.epcCode}
            </foreach>

        </where>
    </update>

    <resultMap id="checkTaskResultMap" type="com.ruoyi.api.domain.CheckTaskInfo">
        <result column="index" property="index"/>
        <result column="location_code" property="locationCode"/>
        <result column="plane_position" property="locationCode"/>
        <result column="store_name" property="areaName"/>
        <result column="area_name" property="areaName"/>
        <result column="task_state" property="taskState"/>
        <result column="objid" property="id"/>
        <result column="area_id" property="areaId"/>
        <result column="inspection_id" property="inspectionId"/>

    </resultMap>
    <select id="selectLocationByAreaUser" resultMap="checkTaskResultMap">
        select bs.store_id as objid ,
                bs.store_name,
                location_code
        from base_location bl
                 left join base_store bs on bl.store_id = bs.store_id
                 right join  base_area_user bau on bau.area_id = bl.store_id
        where bl.stote_tag = 2 and bau.user_id=#{user}
    </select>
    <insert id="insertCheckTask">
        INSERT INTO record_inspection_task (inspection_id, area_id,area_name, plane_position) VALUES
        <foreach collection="list" item="data" separator=",">
            (#{id}, #{data.id}, #{data.areaName}, #{data.locationCode})
        </foreach>
    </insert>


    <select id="inspectionSelectTask" resultMap="checkTaskResultMap">
        select  rit.objid,
                area_id,
                area_name,
                plane_position,
                row_number() over (order by rit.objid) as 'index',
                task_state,
                rit.inspection_id
        from record_inspection_task rit
                 right join  record_inspection ri on rit.inspection_id = ri.inspection_id
        where task_state = '待完成'
          and inspection_user = #{user}
    </select>

    <select id="inspectionSelectLocation" resultType="com.ruoyi.api.domain.StockDto">
        select row_number() over (order by location_code) as 'index',
               rifd_code                                  as epcCode,
               location_code                              as locationCode,
               create_time                                as createTime,
               '未检'                                       as state
        from ledger_rfid
        where is_scrap = 1
          and location_code = #{code}
    </select>
    <!--完成任务-->
    <update id="updataInspectionTaskByObjid">
        UPDATE record_inspection_task SET task_state = '完成' WHERE objid = #{tableId};
    </update>

    <insert id="insertInspetionPictrue">
        INSERT INTO record_inspection_img (inspection_id, task_id, img_path, create_by)
         VALUES (#{inspetionId},#{tableId}, #{pathName}, #{user});
    </insert>

    <insert id="insertBfImg">
        INSERT INTO bg_wheel_chocks.record_invalidated_img (invalidated_id, epc, img_path, create_by)
        VALUES (#{objid}, #{epc},#{pathName},#{user});
    </insert>
</mapper>