<?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.StockCheckRecordMapper">
    <resultMap type="StockCheckRecord" id="StockCheckRecordResult">
        <id property="id" column="id"/>
        <result property="checkNo" column="check_no"/>
        <result property="areaId" column="area_id"/>
        <result property="productId" column="product_id"/>
        <result property="bookNum" column="book_num"/>
        <result property="realNum" column="real_num"/>
        <result property="diffNum" column="diff_num"/>
        <result property="checkStatus" column="check_status"/>
        <result property="checkUser" column="check_user"/>
        <result property="confirmUser" column="confirm_user"/>
        <result property="confirmTime" column="confirm_time"/>
        <result property="reason" column="reason"/>
        <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="selectStockCheckRecordVo">
        select scr.id, scr.check_no, scr.area_id, scr.product_id, scr.book_num, scr.real_num, scr.diff_num,
               scr.check_status, scr.check_user, scr.confirm_user, scr.confirm_time, scr.reason, scr.create_time, scr.update_time,
               wa.area_code, wa.area_name, pi.product_code, pi.product_name, pi.brand, pi.category, pi.spec, pi.unit
        from stock_check_record scr
        left join warehouse_area wa on wa.id = scr.area_id
        left join product_info pi on pi.id = scr.product_id
    </sql>

    <select id="selectStockCheckRecordList" parameterType="StockCheckRecord" resultMap="StockCheckRecordResult">
        <include refid="selectStockCheckRecordVo"/>
        <where>
            <if test="areaId != null">and scr.area_id = #{areaId}</if>
            <if test="productId != null">and scr.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="checkStatus != null">and scr.check_status = #{checkStatus}</if>
        </where>
        order by scr.id desc
    </select>

    <select id="selectStockCheckRecordById" parameterType="Long" resultMap="StockCheckRecordResult">
        <include refid="selectStockCheckRecordVo"/> where scr.id = #{id}
    </select>

    <select id="selectStockCheckRecordByIdForUpdate" parameterType="Long" resultMap="StockCheckRecordResult">
        select id, check_no, area_id, product_id, book_num, real_num, diff_num, check_status,
               check_user, confirm_user, confirm_time, reason, create_time, update_time
        from stock_check_record where id = #{id} for update
    </select>

    <select id="checkCheckNoUnique" parameterType="String" resultMap="StockCheckRecordResult">
        <include refid="selectStockCheckRecordVo"/> where scr.check_no = #{checkNo} limit 1
    </select>

    <insert id="insertStockCheckRecord" parameterType="StockCheckRecord" useGeneratedKeys="true" keyProperty="id">
        insert into stock_check_record(check_no, area_id, product_id, book_num, real_num, diff_num, check_status, check_user, reason, create_time)
        values(#{checkNo}, #{areaId}, #{productId}, #{bookNum}, #{realNum}, #{diffNum}, #{checkStatus}, #{checkUser}, #{reason}, sysdate())
    </insert>

    <update id="updateStockCheckRecord" parameterType="StockCheckRecord">
        update stock_check_record
        <set>
            <if test="realNum != null">real_num = #{realNum},</if>
            <if test="diffNum != null">diff_num = #{diffNum},</if>
            <if test="reason != null">reason = #{reason},</if>
            update_time = sysdate()
        </set>
        where id = #{id} and check_status = 0
    </update>

    <update id="confirmStockCheckRecord" parameterType="StockCheckRecord">
        update stock_check_record
        set check_status = #{checkStatus}, confirm_user = #{confirmUser}, confirm_time = #{confirmTime}, update_time = sysdate()
        where id = #{id} and check_status = 0
    </update>

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