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

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.wxprofitsharing.vo.*;
import cn.iocoder.yudao.module.pay.dal.dataobject.wxprofitsharing.WxProfitsharingDO;
import cn.iocoder.yudao.module.pay.service.wxprofitsharing.WxProfitsharingService;

@Tag(name = "管理后台 - 微信分账记录")
@RestController
@RequestMapping("/pay/wx-profitsharing")
@Validated
public class WxProfitsharingController {

    @Resource
    private WxProfitsharingService wxProfitsharingService;

    @PostMapping("/create")
    @Operation(summary = "创建微信分账记录")
    @PreAuthorize("@ss.hasPermission('pay:wx-profitsharing:create')")
    public CommonResult<Long> createWxProfitsharing(@Valid @RequestBody WxProfitsharingSaveReqVO createReqVO) {
        return success(wxProfitsharingService.createWxProfitsharing(createReqVO));
    }

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

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

    @GetMapping("/get")
    @Operation(summary = "获得微信分账记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pay:wx-profitsharing:query')")
    public CommonResult<WxProfitsharingRespVO> getWxProfitsharing(@RequestParam("id") Long id) {
        WxProfitsharingDO wxProfitsharing = wxProfitsharingService.getWxProfitsharing(id);
        return success(BeanUtils.toBean(wxProfitsharing, WxProfitsharingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得微信分账记录分页")
    @PreAuthorize("@ss.hasPermission('pay:wx-profitsharing:query')")
    public CommonResult<PageResult<WxProfitsharingRespVO>> getWxProfitsharingPage(@Valid WxProfitsharingPageReqVO pageReqVO) {
        PageResult<WxProfitsharingDO> pageResult = wxProfitsharingService.getWxProfitsharingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WxProfitsharingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出微信分账记录 Excel")
    @PreAuthorize("@ss.hasPermission('pay:wx-profitsharing:export')")
    @OperateLog(type = EXPORT)
    public void exportWxProfitsharingExcel(@Valid WxProfitsharingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WxProfitsharingDO> list = wxProfitsharingService.getWxProfitsharingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "微信分账记录.xls", "数据", WxProfitsharingRespVO.class,
                        BeanUtils.toBean(list, WxProfitsharingRespVO.class));
    }

}