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

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.storesalegoods.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.storesalegoods.StoreSaleGoodsDO;
import cn.iocoder.yudao.module.member.service.storesalegoods.StoreSaleGoodsService;

@Tag(name = "管理后台 - 售卖商品")
@RestController
@RequestMapping("/member/store-sale-goods")
@Validated
public class StoreSaleGoodsController {

    @Resource
    private StoreSaleGoodsService storeSaleGoodsService;

    @PostMapping("/create")
    @Operation(summary = "创建售卖商品")
    @PreAuthorize("@ss.hasPermission('member:store-sale-goods:create')")
    public CommonResult<Long> createStoreSaleGoods(@Valid @RequestBody StoreSaleGoodsSaveReqVO createReqVO) {
        return success(storeSaleGoodsService.createStoreSaleGoods(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新售卖商品")
    @PreAuthorize("@ss.hasPermission('member:store-sale-goods:update')")
    public CommonResult<Boolean> updateStoreSaleGoods(@Valid @RequestBody StoreSaleGoodsSaveReqVO updateReqVO) {
        storeSaleGoodsService.updateStoreSaleGoods(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除售卖商品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:store-sale-goods:delete')")
    public CommonResult<Boolean> deleteStoreSaleGoods(@RequestParam("id") Long id) {
        storeSaleGoodsService.deleteStoreSaleGoods(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得售卖商品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:store-sale-goods:query')")
    public CommonResult<StoreSaleGoodsRespVO> getStoreSaleGoods(@RequestParam("id") Long id) {
        StoreSaleGoodsDO storeSaleGoods = storeSaleGoodsService.getStoreSaleGoods(id);
        return success(BeanUtils.toBean(storeSaleGoods, StoreSaleGoodsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得售卖商品分页")
    @PreAuthorize("@ss.hasPermission('member:store-sale-goods:query')")
    public CommonResult<PageResult<StoreSaleGoodsRespVO>> getStoreSaleGoodsPage(@Valid StoreSaleGoodsPageReqVO pageReqVO) {
        PageResult<StoreSaleGoodsDO> pageResult = storeSaleGoodsService.getStoreSaleGoodsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StoreSaleGoodsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出售卖商品 Excel")
    @PreAuthorize("@ss.hasPermission('member:store-sale-goods:export')")
    @OperateLog(type = EXPORT)
    public void exportStoreSaleGoodsExcel(@Valid StoreSaleGoodsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StoreSaleGoodsDO> list = storeSaleGoodsService.getStoreSaleGoodsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "售卖商品.xls", "数据", StoreSaleGoodsRespVO.class,
                        BeanUtils.toBean(list, StoreSaleGoodsRespVO.class));
    }

}