package cn.iocoder.yudao.module.pay.controller.admin.wxprofitsharingreturn;

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 cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.pay.controller.admin.wxprofitsharingreturn.vo.*;
import cn.iocoder.yudao.module.pay.dal.dataobject.wxprofitsharingreturn.WxProfitsharingReturnDO;
import cn.iocoder.yudao.module.pay.service.wxprofitsharingreturn.WxProfitsharingReturnService;

@Tag(name = "管理后台 - 微信分账回退")
@RestController
@RequestMapping("/pay/wx-profitsharing-return")
@Validated
public class WxProfitsharingReturnController {

    @Resource
    private WxProfitsharingReturnService wxProfitsharingReturnService;

    @PostMapping("/create")
    @Operation(summary = "创建微信分账回退")
    @PreAuthorize("@ss.hasPermission('pay:wx-profitsharing-return:create')")
    public CommonResult<Long> createWxProfitsharingReturn(@Valid @RequestBody WxProfitsharingReturnSaveReqVO createReqVO) {
        return success(wxProfitsharingReturnService.createWxProfitsharingReturn(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新微信分账回退")
    @PreAuthorize("@ss.hasPermission('pay:wx-profitsharing-return:update')")
    public CommonResult<Boolean> updateWxProfitsharingReturn(@Valid @RequestBody WxProfitsharingReturnSaveReqVO updateReqVO) {
        wxProfitsharingReturnService.updateWxProfitsharingReturn(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除微信分账回退")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pay:wx-profitsharing-return:delete')")
    public CommonResult<Boolean> deleteWxProfitsharingReturn(@RequestParam("id") Long id) {
        wxProfitsharingReturnService.deleteWxProfitsharingReturn(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得微信分账回退")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pay:wx-profitsharing-return:query')")
    public CommonResult<WxProfitsharingReturnRespVO> getWxProfitsharingReturn(@RequestParam("id") Long id) {
        WxProfitsharingReturnDO wxProfitsharingReturn = wxProfitsharingReturnService.getWxProfitsharingReturn(id);
        return success(BeanUtils.toBean(wxProfitsharingReturn, WxProfitsharingReturnRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得微信分账回退分页")
    @PreAuthorize("@ss.hasPermission('pay:wx-profitsharing-return:query')")
    public CommonResult<PageResult<WxProfitsharingReturnRespVO>> getWxProfitsharingReturnPage(@Valid WxProfitsharingReturnPageReqVO pageReqVO) {
        PageResult<WxProfitsharingReturnDO> pageResult = wxProfitsharingReturnService.getWxProfitsharingReturnPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WxProfitsharingReturnRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出微信分账回退 Excel")
    @PreAuthorize("@ss.hasPermission('pay:wx-profitsharing-return:export')")
    @OperateLog(type = EXPORT)
    public void exportWxProfitsharingReturnExcel(@Valid WxProfitsharingReturnPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WxProfitsharingReturnDO> list = wxProfitsharingReturnService.getWxProfitsharingReturnPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "微信分账回退.xls", "数据", WxProfitsharingReturnRespVO.class,
                        BeanUtils.toBean(list, WxProfitsharingReturnRespVO.class));
    }

}