package cn.iocoder.yudao.module.member.controller.admin.activitypay;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.member.controller.admin.activitypay.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.activitypay.ActivityPayDO;
import cn.iocoder.yudao.module.member.service.activitypay.ActivityPayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 活动付款")
@RestController
@RequestMapping("/member/activity-pay")
@Validated
public class ActivityPayController {

    @Resource
    private ActivityPayService activityPayService;

    @PostMapping("/create")
    @Operation(summary = "创建活动付款")
    @PreAuthorize("@ss.hasPermission('member:activity-pay:create')")
    public CommonResult<Long> createActivityPay(@Valid @RequestBody ActivityPaySaveReqVO createReqVO) {
        return success(activityPayService.createActivityPay(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新活动付款")
    @PreAuthorize("@ss.hasPermission('member:activity-pay:update')")
    public CommonResult<Boolean> updateActivityPay(@Valid @RequestBody ActivityPaySaveReqVO updateReqVO) {
        activityPayService.updateActivityPay(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除活动付款")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:activity-pay:delete')")
    public CommonResult<Boolean> deleteActivityPay(@RequestParam("id") Long id) {
        activityPayService.deleteActivityPay(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得活动付款")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:activity-pay:query')")
    public CommonResult<ActivityPayRespVO> getActivityPay(@RequestParam("id") Long id) {
        ActivityPayDO activityPay = activityPayService.getActivityPay(id);
        return success(BeanUtils.toBean(activityPay, ActivityPayRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得活动付款分页")
    @PreAuthorize("@ss.hasPermission('member:activity-pay:query')")
    public CommonResult<PageResult<ActivityPayRespVO>> getActivityPayPage(@Valid ActivityPayPageReqVO pageReqVO) {
        PageResult<ActivityPayDO> pageResult = activityPayService.getActivityPayPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ActivityPayRespVO.class));
    }

    @GetMapping("/pageNew")
    @Operation(summary = "获得活动付款分页")
    @PreAuthorize("@ss.hasPermission('member:activity-pay:query')")
    public CommonResult<PageResult<ActivityPayPageRVO>> getActivityPayPageNew(@Valid ActivityPayPageVO pageReqVO) {
        PageResult<ActivityPayPageRVO> pageResult = activityPayService.getPage(pageReqVO);
        return success(pageResult);
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出活动付款 Excel")
    @PreAuthorize("@ss.hasPermission('member:activity-pay:export')")
    @OperateLog(type = EXPORT)
    public void exportActivityPayExcel(@Valid ActivityPayPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ActivityPayDO> list = activityPayService.getActivityPayPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "活动付款.xls", "数据", ActivityPayRespVO.class,
                        BeanUtils.toBean(list, ActivityPayRespVO.class));
    }

    @GetMapping("/export-excelNew")
    @Operation(summary = "导出活动付款 Excel")
    @PreAuthorize("@ss.hasPermission('member:activity-pay:export')")
    @OperateLog(type = EXPORT)
    public void exportActivityPayExcelNew(@Valid ActivityPayPageVO pageReqVO,
                                       HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ActivityPayPageRVO> list = activityPayService.getPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "活动付款.xls", "数据", ActivityPayPageRVO.class,
                list);
    }

}