package com.zhongzheng.controller.base;

import java.util.List;
import java.util.Arrays;

import com.zhongzheng.common.utils.ServletUtils;
import com.zhongzheng.modules.base.bo.ShoppingCartAddBo;
import com.zhongzheng.modules.base.bo.ShoppingCartEditBo;
import com.zhongzheng.modules.base.bo.ShoppingCartQueryBo;
import com.zhongzheng.modules.base.service.IShoppingCartService;
import com.zhongzheng.modules.base.vo.ShoppingCartVo;
import com.zhongzheng.modules.user.entity.ClientLoginUser;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhongzheng.common.annotation.Log;
import com.zhongzheng.common.core.controller.BaseController;
import com.zhongzheng.common.core.domain.AjaxResult;
import com.zhongzheng.common.enums.BusinessType;
import com.zhongzheng.common.utils.poi.ExcelUtil;
import com.zhongzheng.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 购物车Controller
 *
 * @author hjl
 * @date 2021-12-09
 */
@Api(value = "购物车控制器", tags = {"购物车管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/base/cart")
public class ShoppingCartController extends BaseController {

    private final IShoppingCartService iShoppingCartService;

    private final com.zhongzheng.framework.web.service.WxTokenService WxTokenService;

    /**
     * 查询购物车列表
     */
    @ApiOperation("查询购物车列表")
    @GetMapping("/list")
    public TableDataInfo<ShoppingCartVo> list(ShoppingCartQueryBo bo) {
        ClientLoginUser loginUser = WxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        startPage();
        List<ShoppingCartVo> list = iShoppingCartService.selectList(bo);
        return getDataTable(list);
    }

    /**
     * 导出购物车列表
     */
   /* @ApiOperation("导出购物车列表")
    @PreAuthorize("@ss.hasPermi('system:cart:export')")
    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<ShoppingCartVo> export(ShoppingCartQueryBo bo) {
        List<ShoppingCartVo> list = iShoppingCartService.queryList(bo);
        ExcelUtil<ShoppingCartVo> util = new ExcelUtil<ShoppingCartVo>(ShoppingCartVo.class);
        return util.exportExcel(list, "购物车");
    }*/

    /**
     * 获取购物车详细信息
     */
   /* @ApiOperation("获取购物车详细信息")
    @PreAuthorize("@ss.hasPermi('system:cart:query')")
    @GetMapping("/{id}")
    public AjaxResult<ShoppingCartVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iShoppingCartService.queryById(id));
    }*/

    /**
     * 新增购物车
     */
    @ApiOperation("新增购物车")
    @PostMapping()
    public AjaxResult<Void> add(@RequestBody ShoppingCartAddBo bo) {
        ClientLoginUser loginUser = WxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        return toAjax(iShoppingCartService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改购物车
     */
    /*@ApiOperation("修改购物车")
    @PreAuthorize("@ss.hasPermi('system:cart:edit')")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult<Void> edit(@RequestBody ShoppingCartEditBo bo) {
        return toAjax(iShoppingCartService.updateByEditBo(bo) ? 1 : 0);
    }*/

    /**
     * 删除购物车
     */
    @ApiOperation("删除购物车")
    @PostMapping("/delete/{id}")
    public AjaxResult<Void> remove(@PathVariable Long id) {
        ClientLoginUser loginUser = WxTokenService.getLoginUser(ServletUtils.getRequest());
        return toAjax(iShoppingCartService.deleteById(id, loginUser.getUser().getUserId()) ? 1 : 0);
    }
}
