package cn.newfeifan.mall.module.sale.controller.admin.merchantapply;

import cn.newfeifan.mall.framework.security.core.annotations.PreAuthenticated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
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.sale.controller.admin.merchantapply.vo.*;
import cn.newfeifan.mall.module.sale.dal.dataobject.merchantapply.MerchantApplyDO;
import cn.newfeifan.mall.module.sale.service.merchantapply.MerchantApplyService;

@Tag(name = "管理后台 - 商户申请")
@RestController
@RequestMapping("/sale/merchant-apply")
@Validated
public class MerchantApplyController {

    @Resource
    private MerchantApplyService merchantApplyService;

    @PostMapping("/create")
    @Operation(summary = "创建商户申请")
    @PreAuthenticated
    public CommonResult<Long> createMerchantApply(@Valid @RequestBody MerchantApplySaveReqVO createReqVO) {
        return success(merchantApplyService.createMerchantApply(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商户申请")
    @PreAuthenticated
    public CommonResult<Boolean> updateMerchantApply(@Valid @RequestBody MerchantApplySaveReqVO updateReqVO) {
        merchantApplyService.updateMerchantApply(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商户申请")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthenticated
    public CommonResult<Boolean> deleteMerchantApply(@RequestParam("id") Long id) {
        merchantApplyService.deleteMerchantApply(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商户申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthenticated
    public CommonResult<MerchantApplyRespVO> getMerchantApply(@RequestParam("id") Long id) {
        MerchantApplyDO merchantApply = merchantApplyService.getMerchantApply(id);
        return success(BeanUtils.toBean(merchantApply, MerchantApplyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商户申请分页")
    @PreAuthenticated
    public CommonResult<PageResult<MerchantApplyRespVO>> getMerchantApplyPage(@Valid MerchantApplyPageReqVO pageReqVO) {
        PageResult<MerchantApplyDO> pageResult = merchantApplyService.getMerchantApplyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MerchantApplyRespVO.class));
    }
}