package com.fs.app.controller.live;


import com.fs.app.annotation.Login;
import com.fs.app.controller.AppBaseController;
import com.fs.common.annotation.Log;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.live.domain.LiveCart;
import com.fs.live.service.ILiveCartService;
import com.fs.live.vo.LiveCartVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车Controller
 *
 * @author fs
 * @date 2025-07-08
 */
@RestController
@RequestMapping("/live/liveCart")
@Api(tags = "购物车管理")
public class LiveCartController extends AppBaseController
{
    @Autowired
    private ILiveCartService liveCartService;

    /**
     * 查询购物车列表
     */
    @GetMapping("/list")
    @ApiOperation("查询购物车列表")
    @Login
    public R list(LiveCart liveCart)
    {
        PageHelper.startPage(liveCart.getPage(), liveCart.getPageSize());
        try {
            liveCart.setUserId(String.valueOf(getUserId()));
            List<LiveCartVo> list = liveCartService.selectLiveCartListVo(liveCart);
            PageInfo<LiveCartVo>  result = new PageInfo<>(list);
            return R.ok().put("data", result);
        } catch (Exception e) {
            return R.error("操作异常");
        }
    }

    /**
     * 导出购物车列表
     */
    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation("导出购物车列表")
    @Login
    public AjaxResult export(LiveCart liveCart)
    {
        List<LiveCart> list = liveCartService.selectLiveCartList(liveCart);
        ExcelUtil<LiveCart> util = new ExcelUtil<LiveCart>(LiveCart.class);
        return util.exportExcel(list, "购物车数据");
    }

    /**
     * 获取购物车详细信息
     */
    @GetMapping(value = "/{cardId}")
    @ApiOperation("获取购物车详细信息")
    @ApiImplicitParam(name = "cardId", value = "购物车ID", required = true, dataType = "Long", paramType = "path")
    @Login
    public AjaxResult getInfo(@PathVariable("cardId") Long cardId)
    {
        return AjaxResult.success(liveCartService.selectLiveCartByCartId(cardId));
    }

    /**
     * 新增购物车
     */
    @PostMapping
    @ApiOperation("新增购物车")
    @Login
    @RepeatSubmit
    public R add(@RequestBody @ApiParam("购物车信息") LiveCart liveCart)
    {
        liveCart.setUserId(String.valueOf(getUserId()));
        return R.ok().put("data",liveCartService.insertLiveCart(liveCart));
    }

    /**
     * 修改购物车
     */
    @PostMapping("/update")
    @ApiOperation("修改购物车")
    @Login
    @RepeatSubmit
    public R edit(@RequestBody @ApiParam("购物车信息") LiveCart liveCart)
    {
        liveCart.setUserId(String.valueOf(getUserId()));
        return R.ok().put("data",liveCartService.updateLiveCart(liveCart));
    }

    /**
     * 删除购物车
     */
	@PostMapping("/delete")
    @ApiOperation("删除购物车")
    @ApiImplicitParam(name = "cardIds", value = "购物车ID数组", required = true, dataType = "Long", paramType = "path")
    @Login
    public R remove(@RequestBody Long[] cardIds)
    {
        return R.ok().put("data",liveCartService.deleteByCartIds(cardIds, Long.valueOf(getUserId())));
    }

    @GetMapping("/checked")
    @ApiOperation("查询购物车列表")
    @Login
    public R getChecked(LiveCart liveCart)
    {
        PageHelper.startPage(liveCart.getPage(), liveCart.getPageSize());
        try {
            liveCart.setUserId(String.valueOf(getUserId()));
            List<LiveCartVo> list = liveCartService.getChecked(liveCart);
            PageInfo<LiveCartVo>  result = new PageInfo<>(list);
            return R.ok().put("data", result);
        } catch (Exception e) {
            return R.error("操作异常");
        }
    }



}
