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

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.member.controller.admin.activityvoucher.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.activityvoucher.ActivityVoucherDO;
import cn.iocoder.yudao.module.member.service.activityvoucher.ActivityVoucherService;

@Tag(name = "管理后台 - 活动代金券")
@RestController
@RequestMapping("/member/activity-voucher")
@Validated
public class ActivityVoucherController {

    @Resource
    private ActivityVoucherService activityVoucherService;

    @PostMapping("/create")
    @Operation(summary = "创建活动代金券")
    @PreAuthorize("@ss.hasPermission('member:activity-voucher:create')")
    public CommonResult<Long> createActivityVoucher(@Valid @RequestBody ActivityVoucherSaveReqVO createReqVO) {
        return success(activityVoucherService.createActivityVoucher(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新活动代金券")
    @PreAuthorize("@ss.hasPermission('member:activity-voucher:update')")
    public CommonResult<Boolean> updateActivityVoucher(@Valid @RequestBody ActivityVoucherSaveReqVO updateReqVO) {
        activityVoucherService.updateActivityVoucher(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得活动代金券")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:activity-voucher:query')")
    public CommonResult<ActivityVoucherRespVO> getActivityVoucher(@RequestParam("id") Long id) {
        ActivityVoucherDO activityVoucher = activityVoucherService.getActivityVoucher(id);
        return success(BeanUtils.toBean(activityVoucher, ActivityVoucherRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得活动代金券分页")
    @PreAuthorize("@ss.hasPermission('member:activity-voucher:query')")
    public CommonResult<PageResult<ActivityVoucherRespVO>> getActivityVoucherPage(@Valid ActivityVoucherPageReqVO pageReqVO) {
        PageResult<ActivityVoucherDO> pageResult = activityVoucherService.getActivityVoucherPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ActivityVoucherRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出活动代金券 Excel")
    @PreAuthorize("@ss.hasPermission('member:activity-voucher:export')")
    @OperateLog(type = EXPORT)
    public void exportActivityVoucherExcel(@Valid ActivityVoucherPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ActivityVoucherDO> list = activityVoucherService.getActivityVoucherPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "活动代金券.xls", "数据", ActivityVoucherRespVO.class,
                        BeanUtils.toBean(list, ActivityVoucherRespVO.class));
    }

}