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

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.activityawards.vo.ActivityAwardsPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.activityawards.vo.ActivityAwardsRespVO;
import cn.iocoder.yudao.module.member.controller.admin.activityawards.vo.ActivityAwardsSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.activityawards.ActivityAwardsDO;
import cn.iocoder.yudao.module.member.service.activityawards.ActivityAwardsService;
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")
@Validated
public class ActivityAwardsController {

    @Resource
    private ActivityAwardsService activityAwardsService;

    @PostMapping("/create")
    @Operation(summary = "创建活动奖项")
    @PreAuthorize("@ss.hasPermission('member:activity-awards:create')")
    public CommonResult<Long> createActivityAwards(@Valid @RequestBody ActivityAwardsSaveReqVO createReqVO) {
        return success(activityAwardsService.createActivityAwards(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新活动奖项")
    @PreAuthorize("@ss.hasPermission('member:activity-awards:update')")
    public CommonResult<Boolean> updateActivityAwards(@Valid @RequestBody ActivityAwardsSaveReqVO updateReqVO) {
        activityAwardsService.updateActivityAwards(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得活动奖项")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:activity-awards:query')")
    public CommonResult<ActivityAwardsRespVO> getActivityAwards(@RequestParam("id") Long id) {
        ActivityAwardsDO activityAwards = activityAwardsService.getActivityAwards(id);
        return success(BeanUtils.toBean(activityAwards, ActivityAwardsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得活动奖项分页")
    @PreAuthorize("@ss.hasPermission('member:activity-awards:query')")
    public CommonResult<PageResult<ActivityAwardsRespVO>> getActivityAwardsPage(@Valid ActivityAwardsPageReqVO pageReqVO) {
        PageResult<ActivityAwardsDO> pageResult = activityAwardsService.getActivityAwardsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ActivityAwardsRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得活动奖项列表")
    public CommonResult<List<ActivityAwardsRespVO>> getActivityAwardsList(@Valid ActivityAwardsPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ActivityAwardsDO> list = activityAwardsService.getActivityAwardsPage(pageReqVO).getList();
        return success(BeanUtils.toBean(list, ActivityAwardsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出活动奖项 Excel")
    @PreAuthorize("@ss.hasPermission('member:activity-awards:export')")
    @OperateLog(type = EXPORT)
    public void exportActivityAwardsExcel(@Valid ActivityAwardsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ActivityAwardsDO> list = activityAwardsService.getActivityAwardsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "活动奖项.xls", "数据", ActivityAwardsRespVO.class,
                        BeanUtils.toBean(list, ActivityAwardsRespVO.class));
    }

}