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

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.activityvoucheruser.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.activityvoucheruser.ActivityVoucherUserDO;
import cn.iocoder.yudao.module.member.service.activityvoucheruser.ActivityVoucherUserService;

@Tag(name = "管理后台 - 活动用户拥有代金券")
@RestController
@RequestMapping("/member/activity-voucher-user")
@Validated
public class ActivityVoucherUserController {

    @Resource
    private ActivityVoucherUserService activityVoucherUserService;

    @PostMapping("/create")
    @Operation(summary = "创建活动用户拥有代金券")
    @PreAuthorize("@ss.hasPermission('member:activity-voucher-user:create')")
    public CommonResult<Long> createActivityVoucherUser(@Valid @RequestBody ActivityVoucherUserSaveReqVO createReqVO) {
        return success(activityVoucherUserService.createActivityVoucherUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新活动用户拥有代金券")
    @PreAuthorize("@ss.hasPermission('member:activity-voucher-user:update')")
    public CommonResult<Boolean> updateActivityVoucherUser(@Valid @RequestBody ActivityVoucherUserSaveReqVO updateReqVO) {
        activityVoucherUserService.updateActivityVoucherUser(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得活动用户拥有代金券")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:activity-voucher-user:query')")
    public CommonResult<ActivityVoucherUserRespVO> getActivityVoucherUser(@RequestParam("id") Long id) {
        ActivityVoucherUserDO activityVoucherUser = activityVoucherUserService.getActivityVoucherUser(id);
        return success(BeanUtils.toBean(activityVoucherUser, ActivityVoucherUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得活动用户拥有代金券分页")
    @PreAuthorize("@ss.hasPermission('member:activity-voucher-user:query')")
    public CommonResult<PageResult<ActivityVoucherUserRespVO>> getActivityVoucherUserPage(@Valid ActivityVoucherUserPageReqVO pageReqVO) {
        PageResult<ActivityVoucherUserDO> pageResult = activityVoucherUserService.getActivityVoucherUserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ActivityVoucherUserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出活动用户拥有代金券 Excel")
    @PreAuthorize("@ss.hasPermission('member:activity-voucher-user:export')")
    @OperateLog(type = EXPORT)
    public void exportActivityVoucherUserExcel(@Valid ActivityVoucherUserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ActivityVoucherUserDO> list = activityVoucherUserService.getActivityVoucherUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "活动用户拥有代金券.xls", "数据", ActivityVoucherUserRespVO.class,
                        BeanUtils.toBean(list, ActivityVoucherUserRespVO.class));
    }

}