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

import cn.newfeifan.mall.module.product.controller.admin.spu.vo.ProductSpuUpdateStatusReqVO;
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 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.module.product.controller.admin.spuapply.vo.*;
import cn.newfeifan.mall.module.product.dal.dataobject.spuapply.SpuApplyDO;
import cn.newfeifan.mall.module.product.service.spuapply.SpuApplyService;

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

    @Resource
    private SpuApplyService spuApplyService;

    @PostMapping("/create")
    @Operation(summary = "创建商品spu申请")
    @PreAuthorize("@ss.hasPermission('product:spu:create')")
    public CommonResult<Long> createSpuApply(@Valid @RequestBody SpuApplySaveReqVO createReqVO) {
        return success(spuApplyService.createSpuApply(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品spu申请")
    @PreAuthorize("@ss.hasPermission('product:spu:update')")
    public CommonResult<Boolean> updateSpuApply(@Valid @RequestBody SpuApplySaveReqVO updateReqVO) {
        spuApplyService.updateSpuApply(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get-detail")
    @Operation(summary = "获得商品 spu 申请 明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:spu:query')")
    public CommonResult<SpuApplyRespVO> getSpuApply(@RequestParam("id") Long id) {
        return success(spuApplyService.getSpuApplyDetail(id));
    }
    @PutMapping("/update-status")
    @Operation(summary = "更新商品 SPUApply Status")
    @PreAuthorize("@ss.hasPermission('product:spu:update')")
    public CommonResult<Boolean> updateStatus(@Valid @RequestBody ProductSpuUpdateStatusReqVO updateReqVO) {
        spuApplyService.updateSpuStatus(updateReqVO);
        return success(true);
    }


    @GetMapping("/page")
    @Operation(summary = "获得商品spu申请分页")
    @PreAuthorize("@ss.hasPermission('product:spu:query')")
    public CommonResult<PageResult<SpuApplyRespVO>> getSpuApplyPage(@Valid SpuApplyPageReqVO pageReqVO) {
        PageResult<SpuApplyDO> pageResult = spuApplyService.getSpuApplyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SpuApplyRespVO.class));
    }

}