package cn.newfeifan.mall.module.distri.controller.admin.integral;

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.newfeifan.mall.framework.common.pojo.PageParam;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.pojo.CommonResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;
import static cn.newfeifan.mall.framework.common.pojo.CommonResult.success;

import cn.newfeifan.mall.framework.excel.core.util.ExcelUtils;

import cn.newfeifan.mall.framework.operatelog.core.annotations.OperateLog;
import static cn.newfeifan.mall.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.newfeifan.mall.module.distri.controller.admin.integral.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.integral.IntegralDO;
import cn.newfeifan.mall.module.distri.service.integral.IntegralService;

@Tag(name = "管理后台 - 推荐用户积分")
@RestController
@RequestMapping("/distri/integral")
@Validated
public class IntegralController {

    @Resource
    private IntegralService integralService;

    @PostMapping("/create")
    @Operation(summary = "创建推荐用户积分")
    @PreAuthorize("@ss.hasPermission('distri:integral:create')")
    public CommonResult<Long> createIntegral(@Valid @RequestBody IntegralSaveReqVO createReqVO) {
        return success(integralService.createIntegral(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新推荐用户积分")
    @PreAuthorize("@ss.hasPermission('distri:integral:update')")
    public CommonResult<Boolean> updateIntegral(@Valid @RequestBody IntegralSaveReqVO updateReqVO) {
        integralService.updateIntegral(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除推荐用户积分")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distri:integral:delete')")
    public CommonResult<Boolean> deleteIntegral(@RequestParam("id") Long id) {
        integralService.deleteIntegral(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得推荐用户积分")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distri:integral:query')")
    public CommonResult<IntegralRespVO> getIntegral(@RequestParam("id") Long id) {
        IntegralDO integral = integralService.getIntegral(id);
        return success(BeanUtils.toBean(integral, IntegralRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得推荐用户积分分页")
    @PreAuthorize("@ss.hasPermission('distri:integral:query')")
    public CommonResult<PageResult<IntegralRespVO>> getIntegralPage(@Valid IntegralPageReqVO pageReqVO) {
        PageResult<IntegralDO> pageResult = integralService.getIntegralPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IntegralRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出推荐用户积分 Excel")
    @PreAuthorize("@ss.hasPermission('distri:integral:export')")
    @OperateLog(type = EXPORT)
    public void exportIntegralExcel(@Valid IntegralPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IntegralDO> list = integralService.getIntegralPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "推荐用户积分.xls", "数据", IntegralRespVO.class,
                        BeanUtils.toBean(list, IntegralRespVO.class));
    }

}