package com.bonus.canteen.core.order.controller;

import java.util.List;
import java.util.Objects;

import cn.hutool.core.collection.CollUtil;
import com.bonus.canteen.core.order.domain.OrderShoppingCart;
import com.bonus.canteen.core.order.domain.param.OrderShoppingCartQueryParam;
import com.bonus.canteen.core.order.domain.param.ShoppingCartDelParam;
import com.bonus.canteen.core.order.domain.vo.OrderShoppingCartVO;
import com.bonus.canteen.core.order.service.IOrderShoppingCartService;
import com.bonus.canteen.core.order.utils.ShoppingCartParamChecker;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.BusinessType;
import com.bonus.common.log.enums.OperaType;
import com.bonus.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

/**
 * 订单购物车Controller
 * 
 * @author ruoyi
 * @date 2025-04-14
 */
@Controller
@RequestMapping("/shopping_cart")
public class OrderShoppingCartController extends BaseController
{
    @Autowired
    private IOrderShoppingCartService orderShoppingCartService;

    /**
     * 查询订单购物车列表
     */
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult list(@RequestBody OrderShoppingCartQueryParam orderShoppingCart)
    {
        ShoppingCartParamChecker.listCheck(orderShoppingCart);
        List<OrderShoppingCartVO> list = orderShoppingCartService.selectOrderShoppingCartList(orderShoppingCart);
        return AjaxResult.success(list);
    }

    /**
     * 导出订单购物车列表
     */
    @SysLog(title = "订单购物车", module = "订单-订单详情", businessType = OperaType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public void export(OrderShoppingCartQueryParam orderShoppingCart, HttpServletResponse response)
    {
        List<OrderShoppingCartVO> list = orderShoppingCartService.selectOrderShoppingCartList(orderShoppingCart);
        ExcelUtil<OrderShoppingCartVO> util = new ExcelUtil<OrderShoppingCartVO>(OrderShoppingCartVO.class);
        util.exportExcel(response, list, "订单购物车数据");
    }

    /**
     * 新增保存订单购物车
     */
    @SysLog(title = "订单购物车", module = "订单-订单详情", businessType = OperaType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody OrderShoppingCart orderShoppingCart)
    {
        ShoppingCartParamChecker.addCheck(orderShoppingCart);
        return toAjax(orderShoppingCartService.insertOrderShoppingCart(orderShoppingCart));
    }

    /**
     * 修改订单购物车
     */
    @GetMapping("/edit/{shoppingCartId}")
    public OrderShoppingCart edit(@PathVariable("shoppingCartId") Long shoppingCartId)
    {
        return orderShoppingCartService.selectOrderShoppingCartByShoppingCartId(shoppingCartId);
    }

    /**
     * 修改保存订单购物车
     */
    @SysLog(title = "订单购物车", module = "订单-订单详情", businessType = OperaType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody OrderShoppingCart orderShoppingCart)
    {
        ShoppingCartParamChecker.editCheck(orderShoppingCart);
        return toAjax(orderShoppingCartService.updateOrderShoppingCart(orderShoppingCart));
    }

    /**
     * 删除订单购物车
     */
    @SysLog(title = "订单购物车删除", module = "订单-订单详情", businessType = OperaType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(@RequestBody ShoppingCartDelParam param)
    {
        if(Objects.isNull(param) || CollUtil.isEmpty(param.getShoppingCartIds())) {
            throw new ServiceException("购物车餐品ID为空");
        }
        return toAjax(orderShoppingCartService.deleteOrderShoppingCartByShoppingCartIds(param.getShoppingCartIds()));
    }
}
