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

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.storegoodstype.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.storegoodstype.StoreGoodsTypeDO;
import cn.iocoder.yudao.module.member.service.storegoodstype.StoreGoodsTypeService;

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

    @Resource
    private StoreGoodsTypeService storeGoodsTypeService;

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

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

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

    @GetMapping("/get")
    @Operation(summary = "获得商品类别")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:store-goods-type:query')")
    public CommonResult<StoreGoodsTypeRespVO> getStoreGoodsType(@RequestParam("id") Integer id) {
        StoreGoodsTypeDO storeGoodsType = storeGoodsTypeService.getStoreGoodsType(id);
        return success(BeanUtils.toBean(storeGoodsType, StoreGoodsTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品类别分页")
    @PreAuthorize("@ss.hasPermission('member:store-goods-type:query')")
    public CommonResult<PageResult<StoreGoodsTypeRespVO>> getStoreGoodsTypePage(@Valid StoreGoodsTypePageReqVO pageReqVO) {
        PageResult<StoreGoodsTypeDO> pageResult = storeGoodsTypeService.getStoreGoodsTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StoreGoodsTypeRespVO.class));
    }

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

}