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

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.activitypaytable.vo.ActivityPayTablePageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.activitypaytable.vo.ActivityPayTableRespVO;
import cn.iocoder.yudao.module.member.controller.admin.activitypaytable.vo.ActivityPayTableSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.activitypaytable.ActivityPayTableDO;
import cn.iocoder.yudao.module.member.service.activitypaytable.ActivityPayTableService;
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-table")
@Validated
public class ActivityPayTableController {

    @Resource
    private ActivityPayTableService activityPayTableService;

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

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

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

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

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

    @GetMapping("/groupPage")
    @Operation(summary = "获得活动付款分页")
    public CommonResult<PageResult<ActivityPayTableRespVO>>  getActivityPayTableGroupPage(@Valid ActivityPayTablePageReqVO pageReqVO) {
        PageResult<ActivityPayTableRespVO> pageResult = activityPayTableService. getActivityPayTableGroupPage(pageReqVO);
        return success(pageResult);
    }

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

}