package shop.alien.mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import shop.alien.entity.store.LifeGroupBuyCount;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import shop.alien.entity.store.vo.LifeGroupBuyCountDateVo;
import shop.alien.entity.store.vo.LifeGroupBuyCountVo;

import java.util.List;

/**
* @author youch
* @description 针对表【life_group_buy_count(团购数据统计)】的数据库操作Mapper
* @createDate 2025-09-04 15:21:58
* @Entity shop.alien.entity.store.LifeGroupBuyCount
*/
public interface LifeGroupBuyCountMapper extends BaseMapper<LifeGroupBuyCount> {

    void insertList(List<LifeGroupBuyCount> lifeGroupBuyCounts);

    @Select("select sum(count_num) countNum,count(0) personNum,count_type countType,count_date countDate from life_group_buy_count where store_id = #{storeId} and count_date = #{date} group by count_type")
    List<LifeGroupBuyCountVo> selectJysjListByStoreId(@Param("storeId") String storeId, @Param("date") String date);

    @Select("select count_date countDate, " +
            "(select count(0) from life_group_buy_count where count_type = '0' and count_date = countDate and store_id = #{storeId}) spfks, " +
            "ifnull((select sum(count_num) from life_group_buy_count where count_type = '0' and count_date = countDate and store_id = #{storeId}),0) spfwl, " +
            "ifnull((select sum(count_num) from life_group_buy_count where count_type = '2' and count_date = countDate and store_id = #{storeId}),0) sjtcs, " +
            "ifnull((select sum(count_num) from life_group_buy_count where count_type = '3' and count_date = countDate and store_id = #{storeId}),0) xjtcs, " +
            "(select count(0) from life_user_order where store_id = #{storeId} and status = '7' and finish_time LIKE CONCAT('%', countDate, '%')) xfrs, " +
            "(select count(0) from life_collect where store_id = #{storeId} and created_time LIKE CONCAT('%', countDate, '%') and business_type is null and delete_flag = 0) spscl, " +
            "(select count(0) from life_collect where store_id = #{storeId} and created_time LIKE CONCAT('%', countDate, '%') and business_type is null and delete_flag = 1) spqxscl " +
            "from life_group_buy_count where store_id = #{storeId} and count_date between #{startDate} and #{endDate} group by count_date ORDER BY count_date asc")
    List<LifeGroupBuyCountDateVo> selectSphxzbListByStoreId(@Param("storeId") String storeId, @Param("startDate") String startDate, @Param("endDate") String endDate);

    @Select("select id groupId,group_name groupName, " +
            "ifnull((select sum(count_num) from life_group_buy_count where count_type = '0' and group_id = groupId),0) spfwl, " +
            "(select count(DISTINCT(user_id)) from life_group_buy_count where count_type = '0' and group_id = groupId group by count_date) spfks, " +
            "(select count(0) from life_collect where coupon_id = groupId and business_type is null and delete_flag = 0) spscl, " +
            "(select count(DISTINCT(luo.user_id)) from life_user_order luo left join order_coupon_middle ocm on luo.id = ocm.order_id  where luo.coupon_type = 2 and luo.status = '7' and ocm.coupon_id = groupId) jgrs, " +
            "(select count(0) from order_coupon_middle ocm left join life_user_order luo on luo.id = ocm.order_id  where luo.coupon_type = 2 and luo.status = '7' and ocm.coupon_id = groupId) xfrs " +
            "from life_group_buy_main where store_id = #{storeId} order by spfwl desc")
    List<LifeGroupBuyCountDateVo> selectDpzbpmListByStoreId(@Param("storeId") String storeId);
}




