package cn.newfeifan.mall.sale.controller.admin.merchantapplylog;

import cn.newfeifan.mall.sale.controller.admin.merchantapplylog.vo.MerchantApplyLogPageReqVO;
import cn.newfeifan.mall.sale.controller.admin.merchantapplylog.vo.MerchantApplyLogRespVO;
import cn.newfeifan.mall.sale.controller.admin.merchantapplylog.vo.MerchantApplyLogSaveReqVO;
import cn.newfeifan.mall.sale.service.merchantapplylog.MerchantApplyLogService;
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.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.sale.dal.dataobject.merchantapplylog.MerchantApplyLogDO;

@Tag(name = "管理后台 - 商户审核记录")
@RestController
@RequestMapping("/sale/merchant-apply-log")
@Validated
public class MerchantApplyLogController {

    @Resource
    private MerchantApplyLogService merchantApplyLogService;

    @PostMapping("/create")
    @Operation(summary = "创建商户审核记录")
    @PreAuthorize("@ss.hasPermission('sale:merchant-apply-log:create')")
    public CommonResult<Long> createMerchantApplyLog(@Valid @RequestBody MerchantApplyLogSaveReqVO createReqVO) {
        return success(merchantApplyLogService.createMerchantApplyLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商户审核记录")
    @PreAuthorize("@ss.hasPermission('sale:merchant-apply-log:update')")
    public CommonResult<Boolean> updateMerchantApplyLog(@Valid @RequestBody MerchantApplyLogSaveReqVO updateReqVO) {
        merchantApplyLogService.updateMerchantApplyLog(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得商户审核记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sale:merchant-apply-log:query')")
    public CommonResult<MerchantApplyLogRespVO> getMerchantApplyLog(@RequestParam("id") Long id) {
        MerchantApplyLogDO merchantApplyLog = merchantApplyLogService.getMerchantApplyLog(id);
        return success(BeanUtils.toBean(merchantApplyLog, MerchantApplyLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商户审核记录分页")
    @PreAuthorize("@ss.hasPermission('sale:merchant-apply-log:query')")
    public CommonResult<PageResult<MerchantApplyLogRespVO>> getMerchantApplyLogPage(@Valid MerchantApplyLogPageReqVO pageReqVO) {

        if(pageReqVO.getMerchantApplyId() == null) {
            pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        }

        PageResult<MerchantApplyLogRespVO> pageResult = merchantApplyLogService.getMerchantApplyLogPage(pageReqVO);
        return success(pageResult);
    }

}