package cn.iocoder.yudao.module.member.controller.admin.storeshoppingcart;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.storeshoppingcart.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.storeshoppingcart.StoreShoppingCartDO;
import cn.iocoder.yudao.module.member.service.storeshoppingcart.StoreShoppingCartService;

@Tag(name = "管理后台 - 小程序商品购物车")
@RestController
@RequestMapping("/member/store-shopping-cart")
@Validated
public class StoreShoppingCartController {

    @Resource
    private StoreShoppingCartService storeShoppingCartService;

    @PostMapping("/create")
    @Operation(summary = "创建小程序商品购物车")
    @PreAuthorize("@ss.hasPermission('member:store-shopping-cart:create')")
    public CommonResult<Long> createStoreShoppingCart(@Valid @RequestBody StoreShoppingCartSaveReqVO createReqVO) {
        return success(storeShoppingCartService.createStoreShoppingCart(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新小程序商品购物车")
    @PreAuthorize("@ss.hasPermission('member:store-shopping-cart:update')")
    public CommonResult<Boolean> updateStoreShoppingCart(@Valid @RequestBody StoreShoppingCartSaveReqVO updateReqVO) {
        storeShoppingCartService.updateStoreShoppingCart(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除小程序商品购物车")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:store-shopping-cart:delete')")
    public CommonResult<Boolean> deleteStoreShoppingCart(@RequestParam("id") Long id) {
        storeShoppingCartService.deleteStoreShoppingCart(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得小程序商品购物车")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:store-shopping-cart:query')")
    public CommonResult<StoreShoppingCartRespVO> getStoreShoppingCart(@RequestParam("id") Long id) {
        StoreShoppingCartDO storeShoppingCart = storeShoppingCartService.getStoreShoppingCart(id);
        return success(BeanUtils.toBean(storeShoppingCart, StoreShoppingCartRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得小程序商品购物车分页")
    @PreAuthorize("@ss.hasPermission('member:store-shopping-cart:query')")
    public CommonResult<PageResult<StoreShoppingCartRespVO>> getStoreShoppingCartPage(@Valid StoreShoppingCartPageReqVO pageReqVO) {
        PageResult<StoreShoppingCartDO> pageResult = storeShoppingCartService.getStoreShoppingCartPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StoreShoppingCartRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出小程序商品购物车 Excel")
    @PreAuthorize("@ss.hasPermission('member:store-shopping-cart:export')")
    @OperateLog(type = EXPORT)
    public void exportStoreShoppingCartExcel(@Valid StoreShoppingCartPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StoreShoppingCartDO> list = storeShoppingCartService.getStoreShoppingCartPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "小程序商品购物车.xls", "数据", StoreShoppingCartRespVO.class,
                        BeanUtils.toBean(list, StoreShoppingCartRespVO.class));
    }

}