package cn.newfeifan.mall.module.sale.controller.app.merchantapplylog;

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

@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 = "获得商户审核记录分页")
    public CommonResult<PageResult<MerchantApplyLogRespVO>> getMerchantApplyLogPage(@Valid MerchantApplyLogPageReqVO pageReqVO) {
        PageResult<MerchantApplyLogDO> pageResult = merchantApplyLogService.getMerchantApplyLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MerchantApplyLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商户审核记录 Excel")
    @PreAuthorize("@ss.hasPermission('sale:merchant-apply-log:export')")
    @OperateLog(type = EXPORT)
    public void exportMerchantApplyLogExcel(@Valid MerchantApplyLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MerchantApplyLogDO> list = merchantApplyLogService.getMerchantApplyLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商户审核记录.xls", "数据", MerchantApplyLogRespVO.class,
                        BeanUtils.toBean(list, MerchantApplyLogRespVO.class));
    }

}