package cn.newfeifan.mall.module.product.controller.admin.skuapply;

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.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.newfeifan.mall.framework.common.pojo.PageParam;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.pojo.CommonResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;
import static cn.newfeifan.mall.framework.common.pojo.CommonResult.success;

import cn.newfeifan.mall.framework.excel.core.util.ExcelUtils;

import cn.newfeifan.mall.framework.operatelog.core.annotations.OperateLog;
import static cn.newfeifan.mall.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.newfeifan.mall.module.product.controller.admin.skuapply.vo.*;
import cn.newfeifan.mall.module.product.dal.dataobject.skuapply.SkuApplyDO;
import cn.newfeifan.mall.module.product.service.skuapply.SkuApplyService;

@Tag(name = "管理后台 - 商品sku申请")
@RestController
@RequestMapping("/product/sku-apply")
@Validated
public class SkuApplyController {

    @Resource
    private SkuApplyService skuApplyService;

    @PostMapping("/create")
    @Operation(summary = "创建商品sku申请")
    @PreAuthorize("@ss.hasPermission('product:sku-apply:create')")
    public CommonResult<Long> createSkuApply(@Valid @RequestBody SkuApplySaveReqVO createReqVO) {
        return success(skuApplyService.createSkuApply(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品sku申请")
    @PreAuthorize("@ss.hasPermission('product:sku-apply:update')")
    public CommonResult<Boolean> updateSkuApply(@Valid @RequestBody SkuApplySaveReqVO updateReqVO) {
        skuApplyService.updateSkuApply(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品sku申请")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:sku-apply:delete')")
    public CommonResult<Boolean> deleteSkuApply(@RequestParam("id") Long id) {
        skuApplyService.deleteSkuApply(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品sku申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:sku-apply:query')")
    public CommonResult<SkuApplyRespVO> getSkuApply(@RequestParam("id") Long id) {
        SkuApplyDO skuApply = skuApplyService.getSkuApply(id);
        return success(BeanUtils.toBean(skuApply, SkuApplyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品sku申请分页")
    @PreAuthorize("@ss.hasPermission('product:sku-apply:query')")
    public CommonResult<PageResult<SkuApplyRespVO>> getSkuApplyPage(@Valid SkuApplyPageReqVO pageReqVO) {
        PageResult<SkuApplyDO> pageResult = skuApplyService.getSkuApplyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SkuApplyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商品sku申请 Excel")
    @PreAuthorize("@ss.hasPermission('product:sku-apply:export')")
    @OperateLog(type = EXPORT)
    public void exportSkuApplyExcel(@Valid SkuApplyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SkuApplyDO> list = skuApplyService.getSkuApplyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商品sku申请.xls", "数据", SkuApplyRespVO.class,
                        BeanUtils.toBean(list, SkuApplyRespVO.class));
    }

}