package com.bonus.canteen.core.order.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.bonus.canteen.core.menu.enums.MenuRecipeSortEnum;
import com.bonus.canteen.core.menu.service.IMenuRecipeService;
import com.bonus.canteen.core.menu.vo.MenuRecipeDishesVO;
import com.bonus.canteen.core.order.domain.MenuDishCheckDTO;
import com.bonus.canteen.core.order.domain.OrderShoppingCart;
import com.bonus.canteen.core.order.domain.param.OrderShoppingCartQueryParam;
import com.bonus.canteen.core.order.domain.vo.OrderShoppingCartVO;
import com.bonus.canteen.core.order.mapper.OrderShoppingCartMapper;
import com.bonus.canteen.core.order.module.MenuModule;
import com.bonus.canteen.core.order.service.IOrderShoppingCartService;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.houqin.utils.id.Id;
import com.bonus.common.security.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 订单购物车Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-14
 */
@Service
public class OrderShoppingCartServiceImpl implements IOrderShoppingCartService
{
    @Autowired
    private OrderShoppingCartMapper orderShoppingCartMapper;
    @Autowired
    private MenuModule menuModule;

    /**
     * 查询订单购物车
     * 
     * @param shoppingCartId 订单购物车主键
     * @return 订单购物车
     */
    @Override
    public OrderShoppingCart selectOrderShoppingCartByShoppingCartId(Long shoppingCartId)
    {
        return orderShoppingCartMapper.selectOrderShoppingCartByShoppingCartId(shoppingCartId);
    }

    /**
     * 查询订单购物车列表
     * 
     * @param orderShoppingCart 订单购物车
     * @return 订单购物车
     */
    @Override
    public List<OrderShoppingCartVO> selectOrderShoppingCartList(OrderShoppingCartQueryParam orderShoppingCart) {
        OrderShoppingCart orderShoppingCartQuery = new OrderShoppingCart();
        orderShoppingCartQuery.setCanteenId(orderShoppingCart.getCanteenId());
        orderShoppingCartQuery.setStallId(orderShoppingCart.getStallId());
        orderShoppingCartQuery.setOrderType(orderShoppingCart.getOrderType());
        orderShoppingCartQuery.setUserId(orderShoppingCart.getUserId());
        List<OrderShoppingCart> orderShoppingCartList = orderShoppingCartMapper.selectOrderShoppingCartList(orderShoppingCartQuery);
        if (CollUtil.isEmpty(orderShoppingCartList)) {
            return new ArrayList<>();
        }

        MenuDishCheckDTO menuDishCheckDTO = new MenuDishCheckDTO();
        menuDishCheckDTO.setCanteenId(orderShoppingCart.getCanteenId());
        menuDishCheckDTO.setStallId(orderShoppingCart.getStallId());
        menuDishCheckDTO.setApplyType(MenuRecipeSortEnum.MOBILE_RESERVE.getKey().toString());

        Map<String, List<OrderShoppingCart>> groupedByOrderDate = orderShoppingCartList.stream()
                .collect(Collectors.groupingBy(cart -> DateUtil.format(cart.getOrderDate(), "yyyy-MM-dd")));

        List<OrderShoppingCartVO> orderShoppingCartVOList = new ArrayList<>();

        for (Map.Entry<String, List<OrderShoppingCart>> entry : groupedByOrderDate.entrySet()) {
            String orderDateStr = entry.getKey();
            List<OrderShoppingCart> carts = entry.getValue();
            String filteredOrderDate = orderShoppingCart.getMenuDateList().stream().filter(orderDate -> orderDate.equals(orderDateStr))
                    .findFirst().orElse(null);

            menuDishCheckDTO.setApplyDate(orderDateStr);
            Map<Integer, List<MenuRecipeDishesVO>> menuRecipeDishMap = menuModule.getMenuRecipeDish(menuDishCheckDTO);

            for (OrderShoppingCart shoppingCart : carts) {
                OrderShoppingCartVO shoppingCartVO = convertToOrderShoppingCartVO(shoppingCart, menuRecipeDishMap);
                if(filteredOrderDate == null) {
                    shoppingCartVO.setIsValid(0);
                }
                orderShoppingCartVOList.add(shoppingCartVO);
            }
        }

        return orderShoppingCartVOList;
    }

    private OrderShoppingCartVO convertToOrderShoppingCartVO(OrderShoppingCart shoppingCart,
                                                             Map<Integer, List<MenuRecipeDishesVO>> menuRecipeDishMap) {
        OrderShoppingCartVO shoppingCartVO = new OrderShoppingCartVO();
        BeanUtils.copyProperties(shoppingCart, shoppingCartVO);
        shoppingCartVO.setIsValid(0);

        List<MenuRecipeDishesVO> menuRecipeDishes = menuRecipeDishMap.get(shoppingCart.getMealtimeType());
        if (CollUtil.isNotEmpty(menuRecipeDishes)) {
            for (MenuRecipeDishesVO menuRecipeDishesVO : menuRecipeDishes) {
                if (shoppingCart.getGoodsId().toString().equals(menuRecipeDishesVO.getDishesId())) {
                    shoppingCartVO.setIsValid(1);
                    break;
                }
            }
        }

        return shoppingCartVO;
    }


    /**
     * 新增订单购物车
     * 
     * @param orderShoppingCart 订单购物车
     * @return 结果
     */
    @Override
    public int insertOrderShoppingCart(OrderShoppingCart orderShoppingCart)
    {
        if(Objects.nonNull(orderShoppingCart.getShoppingCartId())) {
            return updateOrderShoppingCart(orderShoppingCart);
        }else {
            orderShoppingCart.setShoppingCartId(Id.next());
            orderShoppingCart.setCreateBy(SecurityUtils.getUsername());
            orderShoppingCart.setUpdateBy(SecurityUtils.getUsername());
            return orderShoppingCartMapper.insertOrderShoppingCart(orderShoppingCart);
        }
    }

    /**
     * 修改订单购物车
     * 
     * @param orderShoppingCart 订单购物车
     * @return 结果
     */
    @Override
    public int updateOrderShoppingCart(OrderShoppingCart orderShoppingCart)
    {
        orderShoppingCart.setUpdateTime(DateUtils.getNowDate());
        orderShoppingCart.setUpdateTime(DateUtils.getNowDate());
        return orderShoppingCartMapper.updateOrderShoppingCart(orderShoppingCart);
    }

    /**
     * 批量删除订单购物车
     * 
     * @param shoppingCartIds 需要删除的订单购物车主键
     * @return 结果
     */
    @Override
    public int deleteOrderShoppingCartByShoppingCartIds(List<Long> shoppingCartIds)
    {
        return orderShoppingCartMapper.deleteOrderShoppingCartByShoppingCartIds(shoppingCartIds);
    }

    /**
     * 删除订单购物车信息
     * 
     * @param shoppingCartId 订单购物车主键
     * @return 结果
     */
    @Override
    public int deleteOrderShoppingCartByShoppingCartId(Long shoppingCartId)
    {
        return orderShoppingCartMapper.deleteOrderShoppingCartByShoppingCartId(shoppingCartId);
    }
}
