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

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.activityuser.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.activityuser.ActivityUserDO;
import cn.iocoder.yudao.module.member.service.activityuser.ActivityUserService;

@Tag(name = "管理后台 - 活动参加人员")
@RestController
@RequestMapping("/member/activity-user")
@Validated
public class ActivityUserController {

    @Resource
    private ActivityUserService activityUserService;

    @PostMapping("/create")
    @Operation(summary = "创建活动参加人员")
    @PreAuthorize("@ss.hasPermission('member:activity-user:create')")
    public CommonResult<Long> createActivityUser(@Valid @RequestBody ActivityUserSaveReqVO createReqVO) {
        return success(activityUserService.createActivityUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新活动参加人员")
    @PreAuthorize("@ss.hasPermission('member:activity-user:update')")
    public CommonResult<Boolean> updateActivityUser(@Valid @RequestBody ActivityUserSaveReqVO updateReqVO) {
        activityUserService.updateActivityUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除活动参加人员")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:activity-user:delete')")
    public CommonResult<Boolean> deleteActivityUser(@RequestParam("id") Long id) {
        activityUserService.deleteActivityUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得活动参加人员")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:activity-user:query')")
    public CommonResult<ActivityUserRespVO> getActivityUser(@RequestParam("id") Long id) {
        ActivityUserDO activityUser = activityUserService.getActivityUser(id);
        return success(BeanUtils.toBean(activityUser, ActivityUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得活动参加人员分页")
    @PreAuthorize("@ss.hasPermission('member:activity-user:query')")
    public CommonResult<PageResult<ActivityUserRespVO>> getActivityUserPage(@Valid ActivityUserPageReqVO pageReqVO) {
        PageResult<ActivityUserDO> pageResult = activityUserService.getActivityUserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ActivityUserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出活动参加人员 Excel")
    @PreAuthorize("@ss.hasPermission('member:activity-user:export')")
    @OperateLog(type = EXPORT)
    public void exportActivityUserExcel(@Valid ActivityUserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ActivityUserDO> list = activityUserService.getActivityUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "活动参加人员.xls", "数据", ActivityUserRespVO.class,
                        BeanUtils.toBean(list, ActivityUserRespVO.class));
    }

}