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

import cn.newfeifan.mall.sale.controller.admin.merchantapply.vo.MerchantApplyPageReqVO;
import cn.newfeifan.mall.sale.controller.admin.merchantapply.vo.MerchantApplyRespVO;
import cn.newfeifan.mall.sale.controller.admin.merchantapply.vo.MerchantApplySaveReqVO;
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.sale.dal.dataobject.merchantapply.MerchantApplyDO;
import cn.newfeifan.mall.sale.service.merchantapply.MerchantApplyService;

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

    @Resource
    private MerchantApplyService merchantApplyService;

    @PostMapping("/create")
    @Operation(summary = "创建商户申请")
    @PreAuthorize("@ss.hasPermission('sale:merchant-apply:create')")
    public CommonResult<Long> createMerchantApply(@Valid @RequestBody MerchantApplySaveReqVO createReqVO) {
        return success(merchantApplyService.createMerchantApply(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商户申请")
    @PreAuthorize("@ss.hasPermission('sale:merchant-apply:update')")
    public CommonResult<Boolean> updateMerchantApply(@Valid @RequestBody MerchantApplySaveReqVO updateReqVO) {
        merchantApplyService.updateMerchantApply(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商户申请")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sale:merchant-apply:delete')")
    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")
    @PreAuthorize("@ss.hasPermission('sale:merchant-apply:query')")
    public CommonResult<MerchantApplyRespVO> getMerchantApply(@RequestParam("id") Long id) {
        MerchantApplyDO merchantApply = merchantApplyService.getMerchantApply(id);
        return success(BeanUtils.toBean(merchantApply, MerchantApplyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商户申请分页")
    @PreAuthorize("@ss.hasPermission('sale:merchant-apply:query')")
    public CommonResult<PageResult<MerchantApplyRespVO>> getMerchantApplyPage(@Valid MerchantApplyPageReqVO pageReqVO) {
        PageResult<MerchantApplyDO> pageResult = merchantApplyService.getMerchantApplyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MerchantApplyRespVO.class));
    }

    @GetMapping("/getMerchantApplyCount")
    @Operation(summary = "获得商户申请待审核数量")
    @PreAuthorize("@ss.hasPermission('sale:merchant-apply:query')")
    public CommonResult<Integer> getMerchantApplyCount() {
        return success(merchantApplyService.selectMerchantApplyCount());
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商户申请 Excel")
    @PreAuthorize("@ss.hasPermission('sale:merchant-apply:export')")
    @OperateLog(type = EXPORT)
    public void exportMerchantApplyExcel(@Valid MerchantApplyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MerchantApplyDO> list = merchantApplyService.getMerchantApplyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商户申请.xls", "数据", MerchantApplyRespVO.class,
                        BeanUtils.toBean(list, MerchantApplyRespVO.class));
    }

}