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

import cn.hutool.core.collection.CollUtil;
import com.bonus.canteen.core.order.domain.OrderCart;
import com.bonus.canteen.core.order.domain.param.OrderCartQueryParam;
import com.bonus.canteen.core.order.domain.param.OrderCartDelParam;
import com.bonus.canteen.core.order.domain.vo.OrderCartVO;
import com.bonus.canteen.core.order.service.IOrderCartService;
import com.bonus.canteen.core.order.utils.OrderCartParamChecker;
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.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 订单购物车Controller
 * 
 * @author ruoyi
 * @date 2025-04-14
 */
@Controller
@RequestMapping("/order_cart")
public class OrderCartController extends BaseController
{
    @Autowired
    private IOrderCartService iOrderCartService;

    /**
     * 查询订单购物车列表
     */
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult list(@RequestBody OrderCartQueryParam orderCartQueryParam)
    {
        OrderCartParamChecker.listCheck(orderCartQueryParam);
        List<OrderCartVO> list = iOrderCartService.selectOrderCartList(orderCartQueryParam);
        return AjaxResult.success(list);
    }

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

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

    /**
     * 修改订单购物车
     */
    @GetMapping("/edit/{cartId}")
    public OrderCart edit(@PathVariable("cartId") Long cartId)
    {
        return iOrderCartService.selectOrderCartByCartId(cartId);
    }

    /**
     * 修改保存订单购物车
     */
    @SysLog(title = "订单购物车", module = "订单-订单详情", businessType = OperaType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody OrderCart orderCart)
    {
        OrderCartParamChecker.editCheck(orderCart);
        iOrderCartService.updateOrderCart(orderCart);
        return AjaxResult.success();
    }

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