package com.tourism.webadmin.back.dao;

import com.tourism.common.core.base.dao.BaseDaoMapper;
import com.tourism.webadmin.back.model.TourProjectGroupPurchase;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.*;

/**
 * 拼团设置主表数据操作访问接口。
 *
 * @author 吃饭睡觉
 * @date 2024-09-06
 */
public interface TourProjectGroupPurchaseMapper extends BaseDaoMapper<TourProjectGroupPurchase> {

    /**
     * 批量插入对象列表。
     *
     * @param tourProjectGroupPurchaseList 新增对象列表。
     */
    void insertList(List<TourProjectGroupPurchase> tourProjectGroupPurchaseList);

    /**
     * 获取过滤后的对象列表。
     *
     * @param tourProjectGroupPurchaseFilter 主表过滤对象。
     * @param orderBy 排序字符串，order by从句的参数。
     * @return 对象列表。
     */
    List<TourProjectGroupPurchase> getTourProjectGroupPurchaseList(
            @Param("tourProjectGroupPurchaseFilter") TourProjectGroupPurchase tourProjectGroupPurchaseFilter, @Param("orderBy") String orderBy);


    /**
     * 获取各个项目最低价格的团购
     */
    @Select({
            "<script>",
            "SELECT tpgp.* FROM tour_project_group_purchase AS tpgp ",
            "LEFT JOIN tour_tourism_project AS ttp ON tpgp.project_id = ttp.id ",
                "WHERE(tpgp.project_id, tpgp.adult_price) IN (",
                "SELECT project_id, min(adult_price) FROM tour_project_group_purchase ",
                "WHERE project_id IN",
                "<foreach collection='projectIds' item='id' open='(' separator=',' close=')'>",
                    "#{id}",
                "</foreach>",
                "AND end_time >= #{endTime}",
                "GROUP BY project_id)",
            "</script>"
    })
    List<TourProjectGroupPurchase> getLowestPriceGroupPurchaseList(@Param("projectIds") List<Long> projectIds, @Param("endTime") LocalDate endTime);

}
