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

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 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.module.distri.controller.admin.integraltopuplog.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.integraltopuplog.IntegralTopUpLogDO;
import cn.newfeifan.mall.module.distri.service.integraltopuplog.IntegralTopUpLogService;

@Tag(name = "管理后台 - 平台积分充值记录")
@RestController
@RequestMapping("/distri/consumption-top-up-log")
@Validated
public class IntegralTopUpLogController {

    @Resource
    private IntegralTopUpLogService integralTopUpLogService;

    @PostMapping("/create")
    @Operation(summary = "创建平台积分充值记录")
    @PreAuthorize("@ss.hasPermission('distri:consumption-top-up-log:create')")
    public CommonResult<Long> createConsumptionTopUpLog(@Valid @RequestBody IntegralTopUpLogSaveReqVO createReqVO) {
        return success(integralTopUpLogService.createConsumptionTopUpLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新平台积分充值记录")
    @PreAuthorize("@ss.hasPermission('distri:consumption-top-up-log:update')")
    public CommonResult<Boolean> updateConsumptionTopUpLog(@Valid @RequestBody IntegralTopUpLogSaveReqVO updateReqVO) {
        integralTopUpLogService.updateConsumptionTopUpLog(updateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得平台积分充值记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distri:consumption-top-up-log:query')")
    public CommonResult<IntegralTopUpLogRespVO> getConsumptionTopUpLog(@RequestParam("id") Long id) {
        IntegralTopUpLogDO consumptionTopUpLog = integralTopUpLogService.getConsumptionTopUpLog(id);
        return success(BeanUtils.toBean(consumptionTopUpLog, IntegralTopUpLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得平台积分充值记录分页")
    @PreAuthorize("@ss.hasPermission('distri:consumption-top-up-log:query')")
    public CommonResult<PageResult<IntegralTopUpLogRespVO>> getConsumptionTopUpLogPage(@Valid IntegralTopUpLogPageReqVO pageReqVO) {
        PageResult<IntegralTopUpLogDO> pageResult = integralTopUpLogService.getConsumptionTopUpLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IntegralTopUpLogRespVO.class));
    }

    @GetMapping("/isFirstConsumption")
    @Operation(summary = "是否是第一次充值积分")
    @PreAuthorize("@ss.hasPermission('distri:consumption-top-up-log:query')")
    public CommonResult<IntegralInfoVO> isFirstConsumption(@RequestParam("userId") Long userId) {
        return success(integralTopUpLogService.isFirstConsumption(userId));
    }

    @GetMapping("/ptTopUpWithUserTopUpList")
    @Operation(summary = "获取平台充值和用户充值记录")
    @PreAuthorize("@ss.hasPermission('distri:consumption-top-up-log:ptTopUpWithUserTopUpList')")
    public CommonResult<PtTopUpWithUserTopUpRespVO> ptTopUpWithUserTopUpList() {
        return success(integralTopUpLogService.ptTopUpWithUserTopUpList());
    }
}