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

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.activityawardsuser.vo.ActivityAwardsUserPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.activityawardsuser.vo.ActivityAwardsUserRespVO;
import cn.iocoder.yudao.module.member.controller.admin.activityawardsuser.vo.ActivityAwardsUserSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.activityawardsuser.ActivityAwardsUserDO;
import cn.iocoder.yudao.module.member.service.activityawardsuser.ActivityAwardsUserService;
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-awards-user")
@Validated
public class ActivityAwardsUserController {

    @Resource
    private ActivityAwardsUserService activityAwardsUserService;

    @PostMapping("/create")
    @Operation(summary = "创建活动中奖人员")
    @PreAuthorize("@ss.hasPermission('member:activity-awards-user:create')")
    public CommonResult<Long> createActivityAwardsUser(@Valid @RequestBody ActivityAwardsUserSaveReqVO createReqVO) {
        return success(activityAwardsUserService.createActivityAwardsUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新活动中奖人员")
    @PreAuthorize("@ss.hasPermission('member:activity-awards-user:update')")
    public CommonResult<Boolean> updateActivityAwardsUser(@Valid @RequestBody ActivityAwardsUserSaveReqVO updateReqVO) {
        activityAwardsUserService.updateActivityAwardsUser(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得活动中奖人员")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:activity-awards-user:query')")
    public CommonResult<ActivityAwardsUserRespVO> getActivityAwardsUser(@RequestParam("id") Long id) {
        ActivityAwardsUserDO activityAwardsUser = activityAwardsUserService.getActivityAwardsUser(id);
        return success(BeanUtils.toBean(activityAwardsUser, ActivityAwardsUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得活动中奖人员分页")
    @PreAuthorize("@ss.hasPermission('member:activity-awards-user:query')")
    public CommonResult<PageResult<ActivityAwardsUserRespVO>> getActivityAwardsUserPage(@Valid ActivityAwardsUserPageReqVO pageReqVO) {
        PageResult<ActivityAwardsUserDO> pageResult = activityAwardsUserService.getActivityAwardsUserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ActivityAwardsUserRespVO.class));
    }

    @GetMapping("/JoinPage")
    @Operation(summary = "获得活动中奖人员分页")
    public CommonResult<PageResult<ActivityAwardsUserRespVO>> getActivityAwardsUserJoinPage(@Valid ActivityAwardsUserPageReqVO pageReqVO) {
        return success(activityAwardsUserService.getActivityAwardsUserJoinPage(pageReqVO));
    }



    @GetMapping("/export-excel")
    @Operation(summary = "导出活动中奖人员 Excel")
    @PreAuthorize("@ss.hasPermission('member:activity-awards-user:export')")
    @OperateLog(type = EXPORT)
    public void exportActivityAwardsUserExcel(@Valid ActivityAwardsUserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ActivityAwardsUserDO> list = activityAwardsUserService.getActivityAwardsUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "活动中奖人员.xls", "数据", ActivityAwardsUserRespVO.class,
                        BeanUtils.toBean(list, ActivityAwardsUserRespVO.class));
    }

}