<?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.yc.ship.module.product.dal.mysql.voyagestock.VoyageStockMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <select id="selectNoShelfdNumVoyageIdsByVoyageIds" resultType="java.lang.Long">
        select voyage_id
        from product_voyage_stock
        where deleted = 0 and ship_id = #{shipId} and voyage_id not in (select voyage_id from product_voyage_stock where shelfed_num != 0)
         and voyage_id in
         <foreach item="item" collection="voyageIds" open="(" separator="," close=")">
            #{item}
         </foreach>
    </select>
    <select id="selectTotal"
            resultType="com.yc.ship.module.product.controller.admin.voyagestock.vo.VoyageStockTotalVO">
        select sum(t1.total_num) total_num, sum(t1.can_sell_num) can_sale_num, sum(t1.book_num) book_num from product_voyage_stock t1 LEFT JOIN product_voyage t2 on t1.voyage_id = t2.id
        where 1=1 and t1.deleted = 0 and t2.deleted = 0
              <if test="pageReqVO.shipId != null">
                  and t1.ship_id = #{pageReqVO.shipId}
              </if>
              <if test="pageReqVO.voyageIds != null">
                  and t1.voyage_id in
                  <foreach item="item" collection="pageReqVO.voyageIds" open="(" separator="," close=")">
                      #{item}
                  </foreach>
              </if>
          <if test="pageReqVO.voyageTime != null">
              and t2.start_time BETWEEN #{pageReqVO.voyageTime[0]} and #{pageReqVO.voyageTime[1]}
          </if>
    </select>
</mapper>