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

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.storegoods.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.storegoods.StoreGoodsDO;
import cn.iocoder.yudao.module.member.service.storegoods.StoreGoodsService;

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

    @Resource
    private StoreGoodsService storeGoodsService;

    @PostMapping("/create")
    @Operation(summary = "创建商品")
    @PreAuthorize("@ss.hasPermission('member:store-goods:create')")
    public CommonResult<Integer> createStoreGoods(@Valid @RequestBody StoreGoodsSaveReqVO createReqVO) {
        return success(storeGoodsService.createStoreGoods(createReqVO));
    }

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

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

    @GetMapping("/get")
    @Operation(summary = "获得商品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:store-goods:query')")
    public CommonResult<StoreGoodsRespVO> getStoreGoods(@RequestParam("id") Integer id) {
        StoreGoodsDO storeGoods = storeGoodsService.getStoreGoods(id);
        return success(BeanUtils.toBean(storeGoods, StoreGoodsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品分页")
    @PreAuthorize("@ss.hasPermission('member:store-goods:query')")
    public CommonResult<PageResult<StoreGoodsRespVO>> getStoreGoodsPage(@Valid StoreGoodsPageReqVO pageReqVO) {
        PageResult<StoreGoodsDO> pageResult = storeGoodsService.getStoreGoodsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StoreGoodsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商品 Excel")
    @PreAuthorize("@ss.hasPermission('member:store-goods:export')")
    @OperateLog(type = EXPORT)
    public void exportStoreGoodsExcel(@Valid StoreGoodsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StoreGoodsDO> list = storeGoodsService.getStoreGoodsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商品.xls", "数据", StoreGoodsRespVO.class,
                        BeanUtils.toBean(list, StoreGoodsRespVO.class));
    }

}