/*
 *      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.loginUser.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
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 lombok.NonNull;
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.Func;
import org.springblade.modules.guosen.activity.entity.Activity;
import org.springblade.modules.guosen.activity.service.IActivityService;
import org.springblade.modules.guosen.loginUser.dto.LoginDTO;
import org.springblade.modules.guosen.loginUser.dto.LoginUserDTO;
import org.springblade.modules.guosen.loginUser.entity.LoginUser;
import org.springblade.modules.guosen.loginUser.service.ILoginUserService;
import org.springblade.modules.guosen.loginUser.vo.LoginUserVO;
import org.springblade.modules.guosen.loginUser.wrapper.LoginUserWrapper;
import org.springblade.modules.guosen.rank.service.RankService;
import org.springblade.modules.guosen.userActivityStatistical.entity.UserActivityStatistical;
import org.springblade.modules.guosen.userActivityStatistical.service.IUserActivityStatisticalService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 *  控制器
 *
 * @author BladeX
 * @since 2021-06-16
 */
@RestController
@AllArgsConstructor
@RequestMapping("cyzh-loginUser/loginuser")
@Api(value = "", tags = "接口")
public class LoginUserController extends BladeController {

	private final ILoginUserService loginUserService;
	@Resource
	private RankService rankService;
	@Resource
	private IUserActivityStatisticalService userActivityStatisticalService;
	@Resource
	private IActivityService activityService;


	/**
	 * 获取用户的openid
	 */
	@GetMapping("/getOpenIdByUserId")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "", notes = "传入loginUser")
	public R getOpenIdByUserId(Long  userId) {
		LoginUser loginUser = loginUserService.getById(userId);
		String openid = loginUser.getOpenid();
		return R.data(openid);
	}
	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入loginUser")
	public R<LoginUserVO> detail(LoginUser loginUser) {
		LoginUser detail = loginUserService.getOne(Condition.getQueryWrapper(loginUser));
		return R.data(LoginUserWrapper.build().entityVO(detail));
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入loginUser")
	public R<IPage<LoginUserVO>> list(@RequestParam Map<String,Object> loginUser, Query query) {
		IPage<LoginUser> pages = loginUserService.page(Condition.getPage(query), Condition.getQueryWrapper(loginUser,LoginUser.class));
		return R.data(LoginUserWrapper.build().pageVO(pages));
	}


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

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

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入loginUser")
	public R update(@Valid @RequestBody LoginUser loginUser) {
		Long userId = loginUser.getId();
		LoginUser user = loginUserService.getById(userId);
		if (user != null && user.getTotalHot() !=null &&loginUser.getTotalHot().compareTo(user.getTotalHot()) != 0) {
			//修改热力值
			List<Activity> list = activityService.list();
			Long activityId = list.get(0).getId();
			rankService.zSetRemove(activityId.toString(),userId.toString(),0);
			rankService.addScore(activityId.toString(),userId.toString(), Convert.toDouble(loginUser.getTotalHot()),0);
			UserActivityStatistical serviceOne = userActivityStatisticalService.getOne(Condition.getQueryWrapper(new UserActivityStatistical())
				.lambda().eq(UserActivityStatistical::getActivityId, activityId)
				.eq(UserActivityStatistical::getUserId, userId));
			serviceOne.setTotalHotValue(loginUser.getTotalHot());
			userActivityStatisticalService.updateById(serviceOne);
		}
		return R.status(loginUserService.updateById(loginUser));
	}

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


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		List<Long> idList = Func.toLongList(ids);
		return R.status(loginUserService.delete(idList));
	}

	/**
	 * 登录
	 */
	@PostMapping("/login")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "登录", notes = "传入loginUser")
	public R login(@RequestBody LoginDTO loginDTO) {
		return R.data(loginUserService.login(loginDTO));
	}

	/**
	 * 获取用户信息，小程序我的作品里的数据
	 */
	@GetMapping("/getLoginWebVO")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "userId")
	public R getLoginWebVO(Long userId) {
		return R.data(loginUserService.getLoginWebVO(userId));
	}


	/**
	 * 设置用户提现密码 --废弃（提现密码绑定卡）
	 */
	@PostMapping("/setWithdrawPassword")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "设置用户提现密码", notes = "设置用户提现密码")
	public R setWithdrawPassword(@RequestParam Long userId,@RequestParam String password,@RequestParam String msgCode) {
		LoginUser loginUser = loginUserService.getById(userId);
		Assert.notNull(loginUser,"用户不存在");
		return R.data(loginUserService.getLoginWebVO(userId));
	}

	/**
	 * 用户标签
	 */
	@PostMapping("/bindTag")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "用户标签", notes = "用户标签")
	public R bindTag(@RequestBody LoginUser loginUser) {
		Assert.notNull(loginUser.getId(),"用户id为空");
		LoginUser isExist = loginUserService.getById(loginUser.getId());
		Assert.notNull(isExist,"用户不存在");
		isExist.setLimitTag(loginUser.getLimitTag());
		return R.data(loginUserService.updateById(isExist));
	}
}
