<?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.web.modules.stock.mapper.BizStockInboundMapper">

    <resultMap id="BizStockInboundResult" type="com.ruoyi.web.modules.stock.domain.BizStockInbound">
        <id property="inboundId" column="inbound_id"/>
        <result property="inboundNo" column="inbound_no"/>
        <result property="shopId" column="shop_id"/>
        <result property="inboundType" column="inbound_type"/>
        <result property="remark" column="remark"/>
        <result property="operatorId" column="operator_id"/>
        <result property="operatorName" column="operator_name"/>
        <result property="inboundTime" column="inbound_time"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <select id="selectMaxInboundNoByPrefix" resultType="java.lang.String">
        select max(inbound_no) from biz_stock_inbound where inbound_no like concat(#{prefix}, '%')
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="inboundId">
        insert into biz_stock_inbound (inbound_no, shop_id, inbound_type, remark, operator_id, operator_name,
                                       inbound_time, create_by, create_time)
        values (#{inboundNo}, #{shopId}, #{inboundType}, #{remark}, #{operatorId}, #{operatorName},
                #{inboundTime}, #{createBy}, #{createTime})
    </insert>

    <select id="selectByIdAndShop" resultMap="BizStockInboundResult">
        select * from biz_stock_inbound
        where inbound_id = #{inboundId} and shop_id = #{shopId}
    </select>

    <select id="selectList" resultType="com.ruoyi.web.modules.stock.vo.StockInboundListRowVO">
        select b.inbound_id as inboundId,
               b.inbound_no as inboundNo,
               b.inbound_type as inboundType,
               b.inbound_time as inboundTime,
               b.remark as remark,
               b.operator_name as operatorName
        from biz_stock_inbound b
        where b.shop_id = #{shopId}
        <if test="query.keyword != null and query.keyword != ''">
            and (
                b.inbound_no like concat('%', #{query.keyword}, '%')
                or exists (
                    select 1 from biz_stock_inbound_item i
                    where i.inbound_id = b.inbound_id
                      and i.goods_name like concat('%', #{query.keyword}, '%')
                )
            )
        </if>
        <if test="query.inboundType != null and query.inboundType != ''">
            and b.inbound_type = #{query.inboundType}
        </if>
        <if test="query.operatorId != null">
            and b.operator_id = #{query.operatorId}
        </if>
        <if test="query.beginTime != null and query.beginTime != ''">
            and b.inbound_time &gt;= concat(#{query.beginTime}, ' 00:00:00')
        </if>
        <if test="query.endTime != null and query.endTime != ''">
            and b.inbound_time &lt;= concat(#{query.endTime}, ' 23:59:59')
        </if>
        order by b.inbound_time desc, b.inbound_id desc
    </select>

    <select id="selectDetail" resultType="com.ruoyi.web.modules.stock.vo.StockInboundDetailVO">
        select inbound_id as inboundId,
               inbound_no as inboundNo,
               inbound_type as inboundType,
               inbound_time as inboundTime,
               remark as remark,
               operator_id as operatorId,
               operator_name as operatorName
        from biz_stock_inbound
        where shop_id = #{shopId} and inbound_id = #{inboundId}
    </select>

    <select id="selectDistinctOperators" resultType="com.ruoyi.web.modules.stock.vo.StockOperatorVO">
        select distinct operator_id as operatorId, operator_name as operatorName
        from biz_stock_inbound
        where shop_id = #{shopId}
        order by operator_name
    </select>
</mapper>
