package cn.iocoder.yudao.module.system.controller.admin.tenantpoints;

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.*;
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.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.tenantpoints.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.tenantpoints.TenantPointsDO;
import cn.iocoder.yudao.module.system.service.tenantpoints.TenantPointsService;

@Tag(name = "管理后台 - 租户积分记录")
@RestController
@RequestMapping("/system/tenant-points")
@Validated
public class TenantPointsController {

    @Resource
    private TenantPointsService tenantPointsService;

    @PostMapping("/create")
    @Operation(summary = "创建租户积分记录")
    @PreAuthorize("@ss.hasPermission('system:tenant-points:create')")
    public CommonResult<Long> createTenantPoints(@Valid @RequestBody TenantPointsSaveReqVO createReqVO) {
        return success(tenantPointsService.createTenantPoints(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新租户积分记录")
    @PreAuthorize("@ss.hasPermission('system:tenant-points:update')")
    public CommonResult<Boolean> updateTenantPoints(@Valid @RequestBody TenantPointsSaveReqVO updateReqVO) {
        tenantPointsService.updateTenantPoints(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除租户积分记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:tenant-points:delete')")
    public CommonResult<Boolean> deleteTenantPoints(@RequestParam("id") Long id) {
        tenantPointsService.deleteTenantPoints(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除租户积分记录")
                @PreAuthorize("@ss.hasPermission('system:tenant-points:delete')")
    public CommonResult<Boolean> deleteTenantPointsList(@RequestParam("ids") List<Long> ids) {
        tenantPointsService.deleteTenantPointsListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得租户积分记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:tenant-points:query')")
    public CommonResult<TenantPointsRespVO> getTenantPoints(@RequestParam("id") Long id) {
        TenantPointsDO tenantPoints = tenantPointsService.getTenantPoints(id);
        return success(BeanUtils.toBean(tenantPoints, TenantPointsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得租户积分记录分页")
    @PreAuthorize("@ss.hasPermission('system:tenant-points:query')")
    public CommonResult<PageResult<TenantPointsPageRespVO>> getTenantPointsPage(@Valid TenantPointsPageReqVO pageReqVO) {
        PageResult<TenantPointsPageRespVO> tenantPointsPage = tenantPointsService.getTenantPointsPage(pageReqVO);
        return success(tenantPointsPage);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出租户积分记录 Excel")
    @PreAuthorize("@ss.hasPermission('system:tenant-points:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTenantPointsExcel(@Valid TenantPointsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);

        List<TenantPointsPageRespVO> list = tenantPointsService.getTenantPointsPage(pageReqVO).getList();

        // 导出 Excel
        ExcelUtils.write(response, "租户积分记录.xls", "数据", TenantPointsPageRespVO.class,
                        BeanUtils.toBean(list, TenantPointsPageRespVO.class));
    }

    @GetMapping("/transaction-history/page")
    @Operation(summary = "获得租户积分记录分页")
    @Parameter(name = "tenantId", description = "租户 Id")
    @PreAuthorize("@ss.hasPermission('system:tenant-balance:query')")
    public CommonResult<PageResult<TenantPointsDO>> getTenantTransactionHistoryPointsPage(PageParam pageReqVO,
                                                                        @RequestParam("tenantId") Long tenantId) {
        return success(tenantPointsService.getTenantTransactionHistoryPointsPage(pageReqVO, tenantId));
    }

}