package cn.newfeifan.mall.module.distri.controller.admin.applicationforwithdrawalchannel;

import cn.newfeifan.mall.module.distri.service.applicationforwithdrawal.ApplicationForWithdrawalService;
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.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.distri.controller.admin.applicationforwithdrawalchannel.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.applicationforwithdrawalchannel.ApplicationForWithdrawalChannelDO;
import cn.newfeifan.mall.module.distri.service.applicationforwithdrawalchannel.ApplicationForWithdrawalChannelService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 提现渠道记录")
@RestController
@RequestMapping("/distri/application-for-withdrawal-channel")
@Validated
public class ApplicationForWithdrawalChannelController {

    @Resource
    private ApplicationForWithdrawalChannelService applicationForWithdrawalChannelService;

    @Resource
    private ApplicationForWithdrawalService applicationForWithdrawalService;

    @PostMapping("/create")
    @Operation(summary = "创建提现渠道记录")
    @PreAuthorize("@ss.hasPermission('distri:application-for-withdrawal-channel:create')")
    public CommonResult<Long> createApplicationForWithdrawalChannel(@Valid @RequestBody ApplicationForWithdrawalChannelSaveReqVO createReqVO) {
        return success(applicationForWithdrawalChannelService.createApplicationForWithdrawalChannel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新提现渠道记录")
    @PreAuthorize("@ss.hasPermission('distri:application-for-withdrawal-channel:update')")
    public CommonResult<Boolean> updateApplicationForWithdrawalChannel(@Valid @RequestBody ApplicationForWithdrawalChannelSaveReqVO updateReqVO) {
        applicationForWithdrawalChannelService.updateApplicationForWithdrawalChannel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除提现渠道记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distri:application-for-withdrawal-channel:delete')")
    public CommonResult<Boolean> deleteApplicationForWithdrawalChannel(@RequestParam("id") Long id) {
        applicationForWithdrawalChannelService.deleteApplicationForWithdrawalChannel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得提现渠道记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distri:application-for-withdrawal-channel:query')")
    public CommonResult<ApplicationForWithdrawalChannelRespVO> getApplicationForWithdrawalChannel(@RequestParam("id") Long id) {
        ApplicationForWithdrawalChannelDO applicationForWithdrawalChannel = applicationForWithdrawalChannelService.getApplicationForWithdrawalChannel(id);
        return success(BeanUtils.toBean(applicationForWithdrawalChannel, ApplicationForWithdrawalChannelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得提现渠道记录分页")
    @PreAuthorize("@ss.hasPermission('distri:application-for-withdrawal-channel:query')")
    public CommonResult<PageResult<ApplicationForWithdrawalChannelRespVO>> getApplicationForWithdrawalChannelPage(@Valid ApplicationForWithdrawalChannelPageReqVO pageReqVO) {
        PageResult<ApplicationForWithdrawalChannelDO> pageResult = applicationForWithdrawalChannelService.getApplicationForWithdrawalChannelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ApplicationForWithdrawalChannelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出提现渠道记录 Excel")
    @PreAuthorize("@ss.hasPermission('distri:application-for-withdrawal-channel:export')")
    @OperateLog(type = EXPORT)
    public void exportApplicationForWithdrawalChannelExcel(@RequestParam("id") Long id,
              HttpServletResponse response) throws IOException {
        List<ApplicationForWithdrawalChannelExcelRespVO> list = applicationForWithdrawalService.getApplicationForWithdrawalChannelPage(id);

        // 修改该记录的导出状态
        applicationForWithdrawalChannelService.updateApplicationForWithdrawalChannel(id);

        // 导出 Excel
        ExcelUtils.write(response, "提现记录.xls", "数据", ApplicationForWithdrawalChannelExcelRespVO.class, list);
    }
//    @GetMapping("/export-gzhx")
//    @Operation(summary = "导出广州华夏项目成绩数据 Excel")
//    @PreAuthorize("@ss.hasPermission('distri:application-for-withdrawal-channel:export')")
//    @OperateLog(type = EXPORT)
//    public void exportGZHXExcel(@RequestParam("id") Long id,
//              HttpServletResponse response) throws IOException {
//        List<GZHXExcelResp> list = applicationForWithdrawalService.exportGZHXExcel();
//        // 导出 Excel
//        ExcelUtils.write(response, "提现记录.xls", "数据", GZHXExcelResp.class, list);
//    }

    @PostMapping("/import")
    @Operation(summary = "导入提现明细， excel表")
    @PreAuthorize("@ss.hasPermission('distri:application-for-withdrawal-channel:import')")
    public CommonResult<List<ApplicationForWithdrawalChannelImportExcelVO>> importExcel(@RequestParam("file") MultipartFile file, @RequestParam("id") Long id) throws Exception{

        List<ApplicationForWithdrawalChannelImportExcelVO> list = ExcelUtils.read(file, ApplicationForWithdrawalChannelImportExcelVO.class);

        return success(applicationForWithdrawalChannelService.importExcel(list,id));
    }

}