package co.yixiang.yshop.module.distributor.controller.admin.distributorwithdrawalmethod;

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.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.common.pojo.CommonResult;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;

import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;

import co.yixiang.yshop.framework.operatelog.core.annotations.OperateLog;

import static co.yixiang.yshop.framework.operatelog.core.enums.OperateTypeEnum.*;

import co.yixiang.yshop.module.distributor.controller.admin.distributorwithdrawalmethod.vo.*;
import co.yixiang.yshop.module.distributor.dal.dataobject.distributorwithdrawalmethod.DistributorWithdrawalMethodDO;
import co.yixiang.yshop.module.distributor.convert.distributorwithdrawalmethod.DistributorWithdrawalMethodConvert;
import co.yixiang.yshop.module.distributor.service.distributorwithdrawalmethod.DistributorWithdrawalMethodService;

@Tag(name = "管理后台 - 分销商提现方式")
@RestController
@RequestMapping("/distributor/withdrawal-method")
@Validated
public class DistributorWithdrawalMethodController {

    @Resource
    private DistributorWithdrawalMethodService withdrawalMethodService;

    @PostMapping("/create")
    @Operation(summary = "创建分销商提现方式")
    @PreAuthorize("@ss.hasPermission('distributor:withdrawal-method:create')")
    public CommonResult<Long> createWithdrawalMethod(@Valid @RequestBody DistributorWithdrawalMethodCreateReqVO createReqVO) {
        return success(withdrawalMethodService.createWithdrawalMethod(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分销商提现方式")
    @PreAuthorize("@ss.hasPermission('distributor:withdrawal-method:update')")
    public CommonResult<Boolean> updateWithdrawalMethod(@Valid @RequestBody DistributorWithdrawalMethodUpdateReqVO updateReqVO) {
        withdrawalMethodService.updateWithdrawalMethod(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分销商提现方式")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distributor:withdrawal-method:delete')")
    public CommonResult<Boolean> deleteWithdrawalMethod(@RequestParam("id") Long id) {
        withdrawalMethodService.deleteWithdrawalMethod(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得分销商提现方式")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distributor:withdrawal-method:query')")
    public CommonResult<DistributorWithdrawalMethodRespVO> getWithdrawalMethod(@RequestParam("id") Long id) {
        DistributorWithdrawalMethodDO withdrawalMethod = withdrawalMethodService.getWithdrawalMethod(id);
        return success(DistributorWithdrawalMethodConvert.INSTANCE.convert(withdrawalMethod));
    }

    @GetMapping("/list")
    @Operation(summary = "获得分销商提现方式列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('distributor:withdrawal-method:query')")
    public CommonResult<List<DistributorWithdrawalMethodRespVO>> getWithdrawalMethodList(@RequestParam("ids") Collection<Long> ids) {
        List<DistributorWithdrawalMethodDO> list = withdrawalMethodService.getWithdrawalMethodList(ids);
        return success(DistributorWithdrawalMethodConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得分销商提现方式分页")
    @PreAuthorize("@ss.hasPermission('distributor:withdrawal-method:query')")
    public CommonResult<PageResult<DistributorWithdrawalMethodRespVO>> getWithdrawalMethodPage(@Valid DistributorWithdrawalMethodPageReqVO pageVO) {
        PageResult<DistributorWithdrawalMethodDO> pageResult = withdrawalMethodService.getWithdrawalMethodPage(pageVO);
        return success(DistributorWithdrawalMethodConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分销商提现方式 Excel")
    @PreAuthorize("@ss.hasPermission('distributor:withdrawal-method:export')")
    @OperateLog(type = EXPORT)
    public void exportWithdrawalMethodExcel(@Valid DistributorWithdrawalMethodExportReqVO exportReqVO,
                                            HttpServletResponse response) throws IOException {
        List<DistributorWithdrawalMethodDO> list = withdrawalMethodService.getWithdrawalMethodList(exportReqVO);
        // 导出 Excel
        List<DistributorWithdrawalMethodExcelVO> datas = DistributorWithdrawalMethodConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "分销商提现方式.xls", "数据", DistributorWithdrawalMethodExcelVO.class, datas);
    }

}
