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

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.spuapplylog.vo.*;
import cn.newfeifan.mall.module.product.dal.dataobject.spuapplylog.SpuApplyLogDO;
import cn.newfeifan.mall.module.product.service.spuapplylog.SpuApplyLogService;

@Tag(name = "管理后台 - 商品审核记录")
@RestController
@RequestMapping("/product/spu-apply-log")
@Validated
public class SpuApplyLogController {

    @Resource
    private SpuApplyLogService spuApplyLogService;

    @PostMapping("/create")
    @Operation(summary = "创建商品审核记录")
    @PreAuthorize("@ss.hasPermission('product:spu-apply-log:create')")
    public CommonResult<Long> createSpuApplyLog(@Valid @RequestBody SpuApplyLogSaveReqVO createReqVO) {
        return success(spuApplyLogService.createSpuApplyLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品审核记录")
    @PreAuthorize("@ss.hasPermission('product:spu-apply-log:update')")
    public CommonResult<Boolean> updateSpuApplyLog(@Valid @RequestBody SpuApplyLogSaveReqVO updateReqVO) {
        spuApplyLogService.updateSpuApplyLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品审核记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:spu-apply-log:delete')")
    public CommonResult<Boolean> deleteSpuApplyLog(@RequestParam("id") Long id) {
        spuApplyLogService.deleteSpuApplyLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品审核记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:spu-apply-log:query')")
    public CommonResult<SpuApplyLogRespVO> getSpuApplyLog(@RequestParam("id") Long id) {
        SpuApplyLogDO spuApplyLog = spuApplyLogService.getSpuApplyLog(id);
        return success(BeanUtils.toBean(spuApplyLog, SpuApplyLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品审核记录分页")
    @PreAuthorize("@ss.hasPermission('product:spu:query')")
    public CommonResult<PageResult<SpuApplyLogRespVO>> getSpuApplyLogPage(@Valid SpuApplyLogPageReqVO pageReqVO) {
        PageResult<SpuApplyLogDO> pageResult = spuApplyLogService.getSpuApplyLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SpuApplyLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商品审核记录 Excel")
    @PreAuthorize("@ss.hasPermission('product:spu-apply-log:export')")
    @OperateLog(type = EXPORT)
    public void exportSpuApplyLogExcel(@Valid SpuApplyLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SpuApplyLogDO> list = spuApplyLogService.getSpuApplyLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商品审核记录.xls", "数据", SpuApplyLogRespVO.class,
                        BeanUtils.toBean(list, SpuApplyLogRespVO.class));
    }

}