<?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.voyagestockdistribute.VoyageStockDistributeNewMapper">

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

    <select id="selectPage" resultType="com.yc.ship.module.product.controller.admin.voyagestockdistributeNew.vo.VoyageStockDistributeNewPageRespVO">
        SELECT
            t1.voyage_id,
            t3.NAME voyage_name,
            t1.ship_id,
            t2.NAME ship_name,
            sum( num ) num,
            sum( room_model_num ) room_model_num,
        sum(ifnull(book_num,0)) book_num,
            t1.enable_share
        FROM
            product_voyage_stock_distribute_new t1
                LEFT JOIN resource_ship t2 ON t1.ship_id = t2.id
                LEFT JOIN product_voyage t3 ON t1.voyage_id = t3.id
        WHERE
            t1.deleted = 0 and t2.deleted = 0 and t3.deleted = 0
          <if test="params.shipId != null">
            AND t1.ship_id = #{params.shipId}
          </if>
          <if test="params.voyageName != null and params.voyageName != ''">
              AND t3.NAME LIKE CONCAT('%', #{params.voyageName}, '%')
          </if>
          AND t1.object_id = #{params.objectId}
          AND t1.type = #{params.type}
        GROUP BY
            voyage_id
        order by
            t3.start_time asc
    </select>
    <select id="selectTotalDistributeNum" resultType="java.math.BigDecimal">
        select sum(num) from product_voyage_stock_distribute_new
        where deleted = 0 and voyage_id = #{voyageId} and type = 1
    </select>
    <select id="selectGroupList"
            resultType="com.yc.ship.module.product.controller.admin.voyagestockdistributeNew.vo.VoyageStockDistributeNewPageRespVO">
        SELECT
        t1.voyage_id,
        sum( num ) num,
        sum( room_model_num ) room_model_num,
        sum(ifnull(book_num,0)) book_num,
        t1.enable_share
        FROM
        product_voyage_stock_distribute_new t1
        WHERE
        t1.deleted = 0
        <if test="voyageIds != null and voyageIds.size() > 0">
            AND t1.voyage_id in
            <foreach item="item" collection="voyageIds" separator="," open="(" close=")" index="index">
                #{item}
            </foreach>
        </if>
        AND t1.object_id = #{params.objectId}
        AND t1.type = #{params.type}
        GROUP BY
        voyage_id
    </select>
</mapper>