<?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.warehouse.mapper.InRecordMapper">
    <resultMap type="InRecord" id="InRecordResult">
        <id property="id" column="id"/>
        <result property="inNo" column="in_no"/>
        <result property="areaId" column="area_id"/>
        <result property="productId" column="product_id"/>
        <result property="inType" column="in_type"/>
        <result property="inNum" column="in_num"/>
        <result property="inStatus" column="in_status"/>
        <result property="operator" column="operator"/>
        <result property="inOperator" column="in_operator"/>
        <result property="confirmTime" column="confirm_time"/>
        <result property="remark" column="remark"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="areaCode" column="area_code"/>
        <result property="areaName" column="area_name"/>
        <result property="productCode" column="product_code"/>
        <result property="productName" column="product_name"/>
        <result property="brand" column="brand"/>
        <result property="category" column="category"/>
        <result property="spec" column="spec"/>
        <result property="unit" column="unit"/>
    </resultMap>

    <sql id="selectInRecordVo">
        select ir.id, ir.in_no, ir.area_id, ir.product_id, ir.in_type, ir.in_num, ir.in_status,
               ir.operator, ir.in_operator, ir.confirm_time, ir.remark, ir.create_time, ir.update_time,
               wa.area_code, wa.area_name, pi.product_code, pi.product_name, pi.brand, pi.category, pi.spec, pi.unit
        from in_record ir
        left join warehouse_area wa on wa.id = ir.area_id
        left join product_info pi on pi.id = ir.product_id
    </sql>

    <select id="selectInRecordList" parameterType="InRecord" resultMap="InRecordResult">
        <include refid="selectInRecordVo"/>
        <where>
            <if test="inNo != null and inNo != ''">and ir.in_no like concat('%', #{inNo}, '%')</if>
            <if test="areaId != null">and ir.area_id = #{areaId}</if>
            <if test="productId != null">and ir.product_id = #{productId}</if>
            <if test="areaName != null and areaName != ''">and wa.area_name like concat('%', #{areaName}, '%')</if>
            <if test="productName != null and productName != ''">and pi.product_name like concat('%', #{productName}, '%')</if>
            <if test="brand != null and brand != ''">and pi.brand like concat('%', #{brand}, '%')</if>
            <if test="category != null and category != ''">and pi.category like concat('%', #{category}, '%')</if>
            <if test="spec != null and spec != ''">and pi.spec like concat('%', #{spec}, '%')</if>
            <if test="inType != null">and ir.in_type = #{inType}</if>
            <if test="inStatus != null">and ir.in_status = #{inStatus}</if>
            <if test="inOperator != null and inOperator != ''">and ir.in_operator like concat('%', #{inOperator}, '%')</if>
        </where>
        order by ir.id desc
    </select>

    <select id="selectInRecordById" parameterType="Long" resultMap="InRecordResult">
        <include refid="selectInRecordVo"/> where ir.id = #{id}
    </select>

    <select id="selectInRecordByIdForUpdate" parameterType="Long" resultMap="InRecordResult">
        select id, in_no, area_id, product_id, in_type, in_num, in_status, operator, in_operator, confirm_time, remark, create_time, update_time
        from in_record where id = #{id} for update
    </select>

    <select id="checkInNoUnique" parameterType="String" resultMap="InRecordResult">
        <include refid="selectInRecordVo"/> where ir.in_no = #{inNo} limit 1
    </select>

    <insert id="insertInRecord" parameterType="InRecord" useGeneratedKeys="true" keyProperty="id">
        insert into in_record(in_no, area_id, product_id, in_type, in_num, in_status, operator, in_operator, remark, create_time)
        values(#{inNo}, #{areaId}, #{productId}, #{inType}, #{inNum}, #{inStatus}, #{operator}, #{inOperator}, #{remark}, sysdate())
    </insert>

    <update id="updateInRecord" parameterType="InRecord">
        update in_record
        <set>
            <if test="inNo != null and inNo != ''">in_no = #{inNo},</if>
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="productId != null">product_id = #{productId},</if>
            <if test="inType != null">in_type = #{inType},</if>
            <if test="inNum != null">in_num = #{inNum},</if>
            <if test="operator != null">operator = #{operator},</if>
            <if test="inOperator != null">in_operator = #{inOperator},</if>
            <if test="remark != null">remark = #{remark},</if>
            update_time = sysdate()
        </set>
        where id = #{id} and in_status = 0
    </update>

    <update id="confirmInRecord" parameterType="InRecord">
        update in_record
        set in_status = #{inStatus}, operator = #{operator}, confirm_time = #{confirmTime}, update_time = sysdate()
        where id = #{id} and in_status = 0
    </update>

    <delete id="deleteInRecordByIds" parameterType="Long">
        delete from in_record where in_status = 0 and id in
        <foreach collection="array" item="id" open="(" separator="," close=")">#{id}</foreach>
    </delete>
</mapper>
