
package com.tmerclub.cloud.user.controller.platform;

import com.tmerclub.cloud.api.coupon.constant.PutOnStatus;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.user.dto.UserAdminDTO;
import com.tmerclub.cloud.user.dto.UserRechargeDTO;
import com.tmerclub.cloud.user.model.UserRecharge;
import com.tmerclub.cloud.user.service.UserBalanceLogService;
import com.tmerclub.cloud.user.service.UserRechargeService;
import com.tmerclub.cloud.user.vo.UserBalanceLogVO;
import com.tmerclub.cloud.user.vo.UserRechargeVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 余额充值级别表
 *
 * @author tmerclub
 * @date 2021-04-27 15:51:36
 */
@RestController("platformUserRechargeController")
@RequestMapping("/p/userRecharge")
@Tag(name = "platform-余额充值级别表")
public class UserRechargeController {

    @Autowired
    private UserRechargeService userRechargeService;

    @Autowired
    private UserBalanceLogService userBalanceLogService;


    @GetMapping("/list")
    @Operation(summary = "余额充值列表数据", description = "余额充值列表数据")
    public ServerResponseEntity<List<UserRechargeVO>> getUserBalancePage() {
        List<UserRechargeVO> list = userRechargeService.list();
        return ServerResponseEntity.success(userRechargeService.list());
    }

    @GetMapping("/page")
    @Operation(summary = "获取余额充值级别表列表", description = "分页获取余额充值级别表列表")
    public ServerResponseEntity<PageVO<UserRecharge>> page(@Valid PageDTO pageDTO) {
        PageVO<UserRecharge> userRechargePage = userRechargeService.page(pageDTO);
        return ServerResponseEntity.success(userRechargePage);
    }

    @GetMapping("/info")
    @Operation(summary = "获取余额充值级别表", description = "根据rechargeId获取余额充值级别表")
    public ServerResponseEntity<UserRechargeVO> getByRechargeId(@RequestParam Long rechargeId) {
        UserRechargeVO userRechargeVO = userRechargeService.getRechargeByRechargeId(rechargeId, PutOnStatus.PUT_ON.value());
        return ServerResponseEntity.success(userRechargeVO);
    }

    @PostMapping
    @Operation(summary = "保存余额充值级别表", description = "保存余额充值级别表")
    public ServerResponseEntity<Void> save(@Valid @RequestBody UserRechargeDTO userRechargeDTO) {
        userRechargeService.saveRecharge(userRechargeDTO);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新余额充值级别表", description = "更新余额充值级别表")
    public ServerResponseEntity<Void> update(@Valid @RequestBody UserRechargeDTO userRechargeDTO) {
        userRechargeService.updateByRechargeId(userRechargeDTO);
        userRechargeService.removeCacheByRechargeId(userRechargeDTO.getRechargeId());
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除余额充值级别表", description = "根据余额充值级别表id删除余额充值级别表")
    public ServerResponseEntity<Void> delete(@RequestParam Long rechargeId) {
        userRechargeService.deleteById(rechargeId);
        userRechargeService.removeCacheByRechargeId(rechargeId);
        return ServerResponseEntity.success();
    }

    @PutMapping("/batchUpdateUserBalance")
    @Operation(summary = "平台批量修改会员余额", description = "平台批量修改会员余额")
    public ServerResponseEntity<Boolean> batchUpdateUserBalance(@RequestBody @Valid UserAdminDTO userAdminDTO) {
        return ServerResponseEntity.success(userRechargeService.batchUpdateUserBalance(userAdminDTO));
    }

    @GetMapping("/getPageByUserId")
    @Operation(summary = "分页查询某个用户的余额明细", description = "分页查询某个用户的余额明细")
    public ServerResponseEntity<PageVO<UserBalanceLogVO>> getPageByUserId(@Valid PageDTO pageDTO, Long userId) {
        PageVO<UserBalanceLogVO> res = userBalanceLogService.getPageByUserId(pageDTO, userId);
        return ServerResponseEntity.success(res);
    }

}
