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

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.wxprofitsharinginfo.vo.*;
import cn.iocoder.yudao.module.pay.dal.dataobject.wxprofitsharinginfo.WxProfitsharingInfoDO;
import cn.iocoder.yudao.module.pay.service.wxprofitsharinginfo.WxProfitsharingInfoService;

@Tag(name = "管理后台 - 微信分账明细")
@RestController
@RequestMapping("/pay/wx-profitsharing-info")
@Validated
public class WxProfitsharingInfoController {

    @Resource
    private WxProfitsharingInfoService wxProfitsharingInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建微信分账明细")
    @PreAuthorize("@ss.hasPermission('pay:wx-profitsharing-info:create')")
    public CommonResult<Long> createWxProfitsharingInfo(@Valid @RequestBody WxProfitsharingInfoSaveReqVO createReqVO) {
        return success(wxProfitsharingInfoService.createWxProfitsharingInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新微信分账明细")
    @PreAuthorize("@ss.hasPermission('pay:wx-profitsharing-info:update')")
    public CommonResult<Boolean> updateWxProfitsharingInfo(@Valid @RequestBody WxProfitsharingInfoSaveReqVO updateReqVO) {
        wxProfitsharingInfoService.updateWxProfitsharingInfo(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得微信分账明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pay:wx-profitsharing-info:query')")
    public CommonResult<WxProfitsharingInfoRespVO> getWxProfitsharingInfo(@RequestParam("id") Long id) {
        WxProfitsharingInfoDO wxProfitsharingInfo = wxProfitsharingInfoService.getWxProfitsharingInfo(id);
        return success(BeanUtils.toBean(wxProfitsharingInfo, WxProfitsharingInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得微信分账明细分页")
    @PreAuthorize("@ss.hasPermission('pay:wx-profitsharing-info:query')")
    public CommonResult<PageResult<WxProfitsharingInfoRespVO>> getWxProfitsharingInfoPage(@Valid WxProfitsharingInfoPageReqVO pageReqVO) {
        PageResult<WxProfitsharingInfoDO> pageResult = wxProfitsharingInfoService.getWxProfitsharingInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WxProfitsharingInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出微信分账明细 Excel")
    @PreAuthorize("@ss.hasPermission('pay:wx-profitsharing-info:export')")
    @OperateLog(type = EXPORT)
    public void exportWxProfitsharingInfoExcel(@Valid WxProfitsharingInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WxProfitsharingInfoDO> list = wxProfitsharingInfoService.getWxProfitsharingInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "微信分账明细.xls", "数据", WxProfitsharingInfoRespVO.class,
                        BeanUtils.toBean(list, WxProfitsharingInfoRespVO.class));
    }

}