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

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.groupcount.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.groupcount.GroupCountDO;
import cn.iocoder.yudao.module.member.service.groupcount.GroupCountService;

@Tag(name = "管理后台 - 用户分组充值统计")
@RestController
@RequestMapping("/member/group-count")
@Validated
public class GroupCountController {

    @Resource
    private GroupCountService groupCountService;

    @PostMapping("/create")
    @Operation(summary = "创建用户分组充值统计")
    @PreAuthorize("@ss.hasPermission('member:group-count:create')")
    public CommonResult<Long> createGroupCount(@Valid @RequestBody GroupCountSaveReqVO createReqVO) {
        return success(groupCountService.createGroupCount(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户分组充值统计")
    @PreAuthorize("@ss.hasPermission('member:group-count:update')")
    public CommonResult<Boolean> updateGroupCount(@Valid @RequestBody GroupCountSaveReqVO updateReqVO) {
        groupCountService.updateGroupCount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户分组充值统计")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:group-count:delete')")
    public CommonResult<Boolean> deleteGroupCount(@RequestParam("id") Long id) {
        groupCountService.deleteGroupCount(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户分组充值统计")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:group-count:query')")
    public CommonResult<GroupCountRespVO> getGroupCount(@RequestParam("id") Long id) {
        GroupCountDO groupCount = groupCountService.getGroupCount(id);
        return success(BeanUtils.toBean(groupCount, GroupCountRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户分组充值统计分页")
    @PreAuthorize("@ss.hasPermission('member:group-count:query')")
    public CommonResult<PageResult<GroupCountRespVO>> getGroupCountPage(@Valid GroupCountPageReqVO pageReqVO) {
        PageResult<GroupCountDO> pageResult = groupCountService.getGroupCountPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GroupCountRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户分组充值统计 Excel")
    @PreAuthorize("@ss.hasPermission('member:group-count:export')")
    @OperateLog(type = EXPORT)
    public void exportGroupCountExcel(@Valid GroupCountPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GroupCountDO> list = groupCountService.getGroupCountPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户分组充值统计.xls", "数据", GroupCountRespVO.class,
                        BeanUtils.toBean(list, GroupCountRespVO.class));
    }

}