<?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.BizStockOutboundMapper">

    <resultMap id="BizStockOutboundResult" type="com.ruoyi.web.modules.stock.domain.BizStockOutbound">
        <id property="outboundId" column="outbound_id"/>
        <result property="outboundNo" column="outbound_no"/>
        <result property="shopId" column="shop_id"/>
        <result property="outboundType" column="outbound_type"/>
        <result property="refOrderNo" column="ref_order_no"/>
        <result property="remark" column="remark"/>
        <result property="operatorId" column="operator_id"/>
        <result property="operatorName" column="operator_name"/>
        <result property="outboundTime" column="outbound_time"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

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

    <select id="selectByRefOrderNo" resultMap="BizStockOutboundResult">
        select * from biz_stock_outbound where ref_order_no = #{refOrderNo} limit 1
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="outboundId">
        insert into biz_stock_outbound (outbound_no, shop_id, outbound_type, ref_order_no, remark,
                                        operator_id, operator_name, outbound_time, create_by, create_time)
        values (#{outboundNo}, #{shopId}, #{outboundType}, #{refOrderNo}, #{remark},
                #{operatorId}, #{operatorName}, #{outboundTime}, #{createBy}, #{createTime})
    </insert>

    <select id="selectList" resultType="com.ruoyi.web.modules.stock.vo.StockOutboundListRowVO">
        select b.outbound_id as outboundId,
               b.outbound_no as outboundNo,
               b.outbound_type as outboundType,
               b.outbound_time as outboundTime,
               b.remark as remark,
               b.operator_name as operatorName
        from biz_stock_outbound b
        where b.shop_id = #{shopId}
        <if test="query.keyword != null and query.keyword != ''">
            and (
                b.outbound_no like concat('%', #{query.keyword}, '%')
                or b.ref_order_no like concat('%', #{query.keyword}, '%')
                or exists (
                    select 1 from biz_stock_outbound_item i
                    where i.outbound_id = b.outbound_id
                      and i.goods_name like concat('%', #{query.keyword}, '%')
                )
            )
        </if>
        <if test="query.outboundType != null and query.outboundType != ''">
            and b.outbound_type = #{query.outboundType}
        </if>
        <if test="query.operatorId != null">
            and b.operator_id = #{query.operatorId}
        </if>
        <if test="query.beginTime != null and query.beginTime != ''">
            and b.outbound_time &gt;= concat(#{query.beginTime}, ' 00:00:00')
        </if>
        <if test="query.endTime != null and query.endTime != ''">
            and b.outbound_time &lt;= concat(#{query.endTime}, ' 23:59:59')
        </if>
        order by b.outbound_time desc, b.outbound_id desc
    </select>

    <select id="selectDetail" resultType="com.ruoyi.web.modules.stock.vo.StockOutboundDetailVO">
        select outbound_id as outboundId,
               outbound_no as outboundNo,
               outbound_type as outboundType,
               ref_order_no as refOrderNo,
               outbound_time as outboundTime,
               remark as remark,
               operator_id as operatorId,
               operator_name as operatorName
        from biz_stock_outbound
        where shop_id = #{shopId} and outbound_id = #{outboundId}
    </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_outbound
        where shop_id = #{shopId}
        order by operator_name
    </select>
</mapper>
