<?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.manage.mapper.LedgerRfidMapper">
    
    <resultMap type="com.ruoyi.manage.domain.LedgerRfid" id="LedgerRfidResult">
        <result property="objid"    column="objid"    />
        <result property="manufacturerId"    column="manufacturer_id"    />
        <result property="rifdCode"    column="rifd_code"    />
        <result property="batchCode"    column="batch_code"    />
        <result property="locationType"    column="location_type"    />
        <result property="locationCode"    column="location_code"    />
        <result property="isScrap"    column="is_scrap"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createTime"    column="create_time"    />
        <result property="dictLabel"    column="dict_label"    />
        <association property="baseManufacturer" javaType="BaseManufacturer" resultMap="com.ruoyi.manage.mapper.BaseManufacturerMapper.BaseManufacturerResult"/>
    </resultMap>

    <sql id="selectLedgerRfidVo">
        select objid,
               lr.manufacturer_id,
               rifd_code,
               batch_code,
               location_type,
               location_code,
               is_scrap,
               lr.update_time,
               lr.create_time,
               bm.manufacturer_code,
               bm.manufacturer_name
        from ledger_rfid lr
                 left join base_manufacturer bm on lr.manufacturer_id = bm.manufacturer_id
    </sql>

    <select id="selectLedgerRfidList" parameterType="LedgerRfid" resultMap="LedgerRfidResult">
        <include refid="selectLedgerRfidVo"/>
        <where>  
            <if test="manufacturerId != null "> and lr.manufacturer_id = #{manufacturerId}</if>
            <if test="rifdCode != null  and rifdCode != ''"> and rifd_code = #{rifdCode}</if>
            <if test="batchCode != null  and batchCode != ''"> and batch_code = #{batchCode}</if>
            <if test="locationType != null  and locationType != ''"> and location_type = #{locationType}</if>
            <if test="locationCode != null  and locationCode != ''"> and location_code = #{locationCode}</if>
            <if test="isScrap != null  and isScrap != ''"> and is_scrap = #{isScrap}</if>
        </where>
    </select>
    
    <select id="selectLedgerRfidByObjid" parameterType="Long" resultMap="LedgerRfidResult">
        <include refid="selectLedgerRfidVo"/>
        where objid = #{objid}
    </select>
        
    <insert id="insertLedgerRfid" parameterType="LedgerRfid" useGeneratedKeys="true" keyProperty="objid">
        insert into ledger_rfid
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="manufacturerId != null">manufacturer_id,</if>
            <if test="rifdCode != null">rifd_code,</if>
            <if test="batchCode != null">batch_code,</if>
            <if test="locationType != null">location_type,</if>
            <if test="locationCode != null">location_code,</if>
            <if test="isScrap != null">is_scrap,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="manufacturerId != null">#{manufacturerId},</if>
            <if test="rifdCode != null">#{rifdCode},</if>
            <if test="batchCode != null">#{batchCode},</if>
            <if test="locationType != null">#{locationType},</if>
            <if test="locationCode != null">#{locationCode},</if>
            <if test="isScrap != null">#{isScrap},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateLedgerRfid" parameterType="LedgerRfid">
        update ledger_rfid
        <trim prefix="SET" suffixOverrides=",">
            <if test="manufacturerId != null">manufacturer_id = #{manufacturerId},</if>
            <if test="rifdCode != null">rifd_code = #{rifdCode},</if>
            <if test="batchCode != null">batch_code = #{batchCode},</if>
            <if test="locationType != null">location_type = #{locationType},</if>
            <if test="locationCode != null">location_code = #{locationCode},</if>
            <if test="isScrap != null">is_scrap = #{isScrap},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where objid = #{objid}
    </update>

    <delete id="deleteLedgerRfidByObjid" parameterType="Long">
        delete from ledger_rfid where objid = #{objid}
    </delete>

    <delete id="deleteLedgerRfidByObjids" parameterType="String">
        delete from ledger_rfid where objid in 
        <foreach item="objid" collection="array" open="(" separator="," close=")">
            #{objid}
        </foreach>
    </delete>

    <select id="countWasteNumber" resultType="int">
        select count(objid) from ledger_rfid where location_type=1 and is_scrap=2
    </select>

    <select id="countRfid" resultType="com.ruoyi.manage.domain.ResultBeen">
        select bl.location_code as name , count(rifd_code) as value
        from base_location bl
                 left join ledger_rfid lr on lr.location_code = bl.location_code
        where stote_tag = 2
          and is_enable = 'Y'

        group by bl.location_code
    </select>

</mapper>