package org.jeecg.modules.oroqen.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.oroqen.entity.OroqenCart;
import org.jeecg.modules.oroqen.service.IOroqenCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 购物车
 * @Author: jeecg-boot
 * @Date: 2025-01-08
 * @Version: V1.0
 */
@Tag(name = "购物车", description = "购物车")
@RestController
@RequestMapping("/oroqen/cart")
@Slf4j
public class OroqenCartController extends JeecgController<OroqenCart, IOroqenCartService> {

    @Autowired
    private IOroqenCartService oroqenCartService;

    /**
     * 分页列表查询
     */
    @AutoLog(value = "购物车-分页列表查询")
    @Operation(summary = "购物车-分页列表查询", description = "购物车-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<OroqenCart>> queryPageList(OroqenCart oroqenCart,
                                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                   HttpServletRequest req) {
        QueryWrapper<OroqenCart> queryWrapper = QueryGenerator.initQueryWrapper(oroqenCart, req.getParameterMap());
        Page<OroqenCart> page = new Page<OroqenCart>(pageNo, pageSize);
        IPage<OroqenCart> pageList = oroqenCartService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 根据用户ID查询购物车
     */
    @AutoLog(value = "购物车-根据用户ID查询")
    @Operation(summary = "购物车-根据用户ID查询", description = "购物车-根据用户ID查询")
    @GetMapping(value = "/user/{userId}")
    public Result<List<OroqenCart>> getByUserId(@PathVariable String userId) {
        List<OroqenCart> list = oroqenCartService.getByUserId(userId);
        return Result.OK(list);
    }

    /**
     * 添加到购物车
     */
    @AutoLog(value = "购物车-添加商品")
    @Operation(summary = "购物车-添加商品", description = "购物车-添加商品")
    @PostMapping(value = "/add")
    public Result<String> addToCart(@RequestParam String userId,
                                    @RequestParam String productId,
                                    @RequestParam Integer quantity) {
        oroqenCartService.addToCart(userId, productId, quantity);
        return Result.OK("添加到购物车成功！");
    }

    /**
     * 更新购物车商品数量
     */
    @AutoLog(value = "购物车-更新数量")
    @Operation(summary = "购物车-更新数量", description = "购物车-更新数量")
    @PostMapping(value = "/update")
    public Result<String> updateQuantity(@RequestParam String userId,
                                         @RequestParam String productId,
                                         @RequestParam Integer quantity) {
        oroqenCartService.updateQuantity(userId, productId, quantity);
        return Result.OK("更新数量成功！");
    }

    /**
     * 从购物车移除商品
     */
    @AutoLog(value = "购物车-移除商品")
    @Operation(summary = "购物车-移除商品", description = "购物车-移除商品")
    @PostMapping(value = "/remove")
    public Result<String> removeFromCart(@RequestParam String userId,
                                         @RequestParam String productId) {
        oroqenCartService.removeFromCart(userId, productId);
        return Result.OK("移除商品成功！");
    }

    /**
     * 清空购物车
     */
    @AutoLog(value = "购物车-清空购物车")
    @Operation(summary = "购物车-清空购物车", description = "购物车-清空购物车")
    @PostMapping(value = "/clear/{userId}")
    public Result<String> clearCart(@PathVariable String userId) {
        oroqenCartService.clearCart(userId);
        return Result.OK("清空购物车成功！");
    }

    /**
     * 编辑
     */
    @AutoLog(value = "购物车-编辑")
    @Operation(summary = "购物车-编辑", description = "购物车-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody OroqenCart oroqenCart) {
        oroqenCartService.updateById(oroqenCart);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     */
    @AutoLog(value = "购物车-通过id删除")
    @Operation(summary = "购物车-通过id删除", description = "购物车-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        oroqenCartService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     */
    @AutoLog(value = "购物车-批量删除")
    @Operation(summary = "购物车-批量删除", description = "购物车-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.oroqenCartService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     */
    @AutoLog(value = "购物车-通过id查询")
    @Operation(summary = "购物车-通过id查询", description = "购物车-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<OroqenCart> queryById(@RequestParam(name = "id", required = true) String id) {
        OroqenCart oroqenCart = oroqenCartService.getById(id);
        if (oroqenCart == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(oroqenCart);
    }

    /**
     * 导出excel
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, OroqenCart oroqenCart) {
        return super.exportXls(request, oroqenCart, OroqenCart.class, "购物车");
    }

    /**
     * 通过excel导入数据
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, OroqenCart.class);
    }
}