/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.guosen.userActivityStatistical.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.DigestUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.guosen.activity.service.IActivityService;
import org.springblade.modules.guosen.loginUser.entity.LoginUser;
import org.springblade.modules.guosen.loginUser.service.ILoginUserService;
import org.springblade.modules.guosen.rank.service.RankService;
import org.springblade.modules.guosen.userActivityStatistical.dto.RankQueryDTO;
import org.springblade.modules.guosen.userActivityStatistical.entity.UserActivityStatistical;
import org.springblade.modules.guosen.userActivityStatistical.service.IUserActivityStatisticalService;
import org.springblade.modules.guosen.userActivityStatistical.vo.UserActivityStatisticalVO;
import org.springblade.modules.guosen.userActivityStatistical.wrapper.UserActivityStatisticalWrapper;
import org.springblade.modules.guosen.userBank.entity.UserBank;
import org.springblade.modules.guosen.withdrawRecords.entity.WithdrawRecords;
import org.springblade.modules.system.service.ILogUsualService;
import org.springblade.modules.system.service.IParamService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 *  控制器
 *
 * @author BladeX
 * @since 2021-06-20
 */
@RestController
@AllArgsConstructor
@RequestMapping("cyzh-userActivityStatistical/useractivitystatistical")
@Api(value = "", tags = "接口")
public class UserActivityStatisticalController extends BladeController {

	private final IUserActivityStatisticalService userActivityStatisticalService;

	@Resource
	private RankService rankService;
	@Resource
	private IActivityService activityService;



	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入userActivityStatistical")
	public R<UserActivityStatisticalVO> detail(UserActivityStatistical userActivityStatistical) {
		UserActivityStatistical detail = userActivityStatisticalService.getOne(Condition.getQueryWrapper(userActivityStatistical));
		return R.data(UserActivityStatisticalWrapper.build(rankService).entityVO(detail));
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入userActivityStatistical")
	public R<IPage<UserActivityStatisticalVO>> list(UserActivityStatistical userActivityStatistical, Query query) {
		IPage<UserActivityStatistical> pages = userActivityStatisticalService.page(Condition.getPage(query), Condition.getQueryWrapper(userActivityStatistical));
		return R.data(UserActivityStatisticalWrapper.build(rankService).pageVO(pages));
	}

	/**
	 * 获取排名
	 */
	@GetMapping("/getRankList")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入userActivityStatistical")
	public R getRankList(RankQueryDTO rankQueryDTO, Query query) {
		Assert.notNull(rankQueryDTO.getActivityId(), "活动id不能为空");
		if (rankQueryDTO.getType() == null) {
			//默认查询选手排名
			rankQueryDTO.setType(0);
		}
		IPage rank = userActivityStatisticalService.getRank(rankQueryDTO, query);
		return R.data(rank);
	}

	/**
	 * 获取我每个活动的奖金情况，小程序奖金兑换用
	 */
	@GetMapping("/list2")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入userActivityStatistical")
	public R<IPage<UserActivityStatisticalVO>> list2(UserActivityStatistical userActivityStatistical, Query query) {
		IPage<UserActivityStatistical> pages = userActivityStatisticalService.page(Condition.getPage(query), Condition.getQueryWrapper(userActivityStatistical));
		return R.data(UserActivityStatisticalWrapper.build(activityService).pageVO(pages));
	}


	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入userActivityStatistical")
	public R<IPage<UserActivityStatisticalVO>> page(UserActivityStatisticalVO userActivityStatistical, Query query) {
		IPage<UserActivityStatisticalVO> pages = userActivityStatisticalService.selectUserActivityStatisticalPage(Condition.getPage(query), userActivityStatistical);
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入userActivityStatistical")
	public R save(@Valid @RequestBody UserActivityStatistical userActivityStatistical) {
		return R.status(userActivityStatisticalService.save(userActivityStatistical));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入userActivityStatistical")
	public R update(@Valid @RequestBody UserActivityStatistical userActivityStatistical) {
		return R.status(userActivityStatisticalService.updateById(userActivityStatistical));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入userActivityStatistical")
	public R submit(@Valid @RequestBody UserActivityStatistical userActivityStatistical) {
		return R.status(userActivityStatisticalService.saveOrUpdate(userActivityStatistical));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(userActivityStatisticalService.deleteLogic(Func.toLongList(ids)));
	}
	/**
	 * 提现
	 * [{id,userId,activityId,totalCash}]
	 * totalCash用来接收要兑换的现金
	 */
	@PostMapping("/withdraw")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "提现", notes = "活动id")
	public R withdraw(@RequestBody List<UserActivityStatistical> userActivityStatisticalList,@RequestParam String withdrawPassword) {

		return userActivityStatisticalService.withdraw(userActivityStatisticalList,withdrawPassword);
	}

	/**
	 * 获取我的奖金
	 */
	@GetMapping("/getBonus")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "获取我的奖金", notes = "userId")
	public R getBonus(Long userId) {
		return R.data(userActivityStatisticalService.getBonus(userId));
	}

	/**
	 * 获取用户的可用积分和可用热力值
	 */
	@GetMapping("/getUsableHotAndPoints")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "获取用户的可用积分和可用热力值", notes = "userId")
	public R getUsableHotAndPoints(Long userId,Long activityId) {
		return R.data(userActivityStatisticalService.getUsableHotAndPoints(userId,activityId));
	}



}
