<?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.OutRecordMapper">
    <resultMap type="OutRecord" id="OutRecordResult">
        <id property="id" column="id"/>
        <result property="outNo" column="out_no"/>
        <result property="areaId" column="area_id"/>
        <result property="productId" column="product_id"/>
        <result property="outType" column="out_type"/>
        <result property="outNum" column="out_num"/>
        <result property="sourceNo" column="source_no"/>
        <result property="operator" column="operator"/>
        <result property="outOperator" column="out_operator"/>
        <result property="outTime" column="out_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="selectOutRecordVo">
        select orr.id, orr.out_no, orr.area_id, orr.product_id, orr.out_type, orr.out_num,
               orr.source_no, orr.operator, orr.out_operator, orr.out_time, orr.remark, orr.create_time, orr.update_time,
               wa.area_code, wa.area_name, pi.product_code, pi.product_name, pi.brand, pi.category, pi.spec, pi.unit
        from out_record orr
        left join warehouse_area wa on wa.id = orr.area_id
        left join product_info pi on pi.id = orr.product_id
    </sql>

    <select id="selectOutRecordList" parameterType="OutRecord" resultMap="OutRecordResult">
        <include refid="selectOutRecordVo"/>
        <where>
            <if test="outNo != null and outNo != ''">and orr.out_no like concat('%', #{outNo}, '%')</if>
            <if test="areaId != null">and orr.area_id = #{areaId}</if>
            <if test="productId != null">and orr.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="outType != null">and orr.out_type = #{outType}</if>
            <if test="outOperator != null and outOperator != ''">and orr.out_operator like concat('%', #{outOperator}, '%')</if>
            <if test="sourceNo != null and sourceNo != ''">and orr.source_no like concat('%', #{sourceNo}, '%')</if>
        </where>
        order by orr.id desc
    </select>

    <select id="selectOutRecordById" parameterType="Long" resultMap="OutRecordResult">
        <include refid="selectOutRecordVo"/> where orr.id = #{id}
    </select>

    <select id="checkOutNoUnique" parameterType="String" resultMap="OutRecordResult">
        <include refid="selectOutRecordVo"/> where orr.out_no = #{outNo} limit 1
    </select>

    <insert id="insertOutRecord" parameterType="OutRecord" useGeneratedKeys="true" keyProperty="id">
        insert into out_record(out_no, area_id, product_id, out_type, out_num, source_no, operator, out_operator, out_time, remark, create_time)
        values(#{outNo}, #{areaId}, #{productId}, #{outType}, #{outNum}, #{sourceNo}, #{operator}, #{outOperator}, #{outTime}, #{remark}, sysdate())
    </insert>
</mapper>
