package org.springblade.gateway.login_gateway.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.sun.xml.bind.v2.TODO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.common.utils.BeanPropertyUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.gateway.login_gateway.service.LoginService;
import org.springblade.sing.active.entity.ActiveProductRecord;
import org.springblade.sing.active.service.IActiveProductRecordService;
import org.springblade.sing.heat.entity.UserHeat;
import org.springblade.sing.heat.service.IUserHeatService;
import org.springblade.sing.point.entity.UserPufaPoint;
import org.springblade.sing.point.service.IUserPufaPointService;
import org.springblade.sing.user.entity.LoginUser;
import org.springblade.sing.user.service.ILoginUserService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;


/**
 * @author cy-computer
 */
@RestController
@RequestMapping("/user/login")
@Api(value = "用户登录控制器", tags = "登录接口")
@AllArgsConstructor
public class LoginController {

	private final LoginService loginService;
	private final ILoginUserService loginUserService;
	private final IUserPufaPointService userPufaPointService;
	private final IUserHeatService userHeatService;
	private final IActiveProductRecordService activeProductRecordService;

	/**
	 * 登陆校验
	 * 判断是否为移动用户
	 */
	@GetMapping("/isCMCC")
	public R<Boolean> isCMCC(@ApiParam(value = "手机号",required = true) @RequestParam String phone){
//		loginService.isCMCC(phone)
		return R.data(false);
	}

	/**
	 * 保存用户的手机号
	 */
	@PostMapping("/savePhone")
	public R<Boolean> savePhone(@RequestParam String phone,@RequestParam Long userId){
		List<LoginUser> list = loginUserService.list(Condition.getQueryWrapper(new LoginUser()).lambda().eq(LoginUser::getPhone, phone));
		Assert.isTrue(CollUtil.isEmpty(list), "该手机号已被绑定");
		LoginUser loginUser = loginUserService.getById(userId);
		Assert.notNull(loginUser, "找不到该用户");
		loginUser.setPhone(phone);
		return R.status(loginUserService.updateById(loginUser));
	}

	/**
	 * 用户登录
	 */
	@PostMapping("/login")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "用户登录", notes = "传入loginUser")
	public R login(@Valid @RequestBody LoginUser loginUser) {
		synchronized (loginUser.getOpenid().intern()){
			List<LoginUser> users = loginUserService.list(Condition.getQueryWrapper(new LoginUser()).lambda().eq(LoginUser::getOpenid, loginUser.getOpenid()).orderByDesc(LoginUser::getCreateTime));
			if(CollUtil.isEmpty(users)){
				LoginUser user = new LoginUser();
				user.setAvatar(loginUser.getAvatar());
				user.setNickName(loginUser.getNickName());
				user.setOpenid(loginUser.getOpenid());
				user.setSex(loginUser.getSex());
				loginUserService.save(user);
				return R.data(user);
			}
			return R.data(users.get(0));
		}
	}

	/**
	 * h5登录
	 */
	@PostMapping("/h5Login")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "h5登录", notes = "传入loginUser")
	public R h5Login(@RequestParam String phone,@RequestParam String code) {
		//TODO 验证码验证码
		if (ObjectUtil.notEqual("123456", code)) {
			return R.fail("验证码错误");
		}
		synchronized (phone.intern()){
			List<LoginUser> users = loginUserService.list(Condition.getQueryWrapper(new LoginUser()).lambda().eq(LoginUser::getPhone, phone).orderByDesc(LoginUser::getCreateTime));
			if(CollUtil.isEmpty(users)){
				LoginUser user = new LoginUser();
				user.setPhone(phone);
				loginUserService.save(user);
				return R.data(user);
			}
			return R.data(users.get(0));
		}
	}




	/**
	 * 查询用户积分
	 */
	@PostMapping("/userPufaPoint")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "查询用户普法积分")
	public R<UserPufaPoint> userPufaPoint(@ApiParam(value = "用户手机号码",required = true) @RequestParam String phone) {
		return R.data(userPufaPointService.getOne(
			Wrappers.<UserPufaPoint>lambdaQuery()
				.eq(UserPufaPoint::getPhone,phone)));
	}

	/**
	 * 查询用户热力值
	 */
	@PostMapping("/userHeatValue")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "查询用户热力值")
	public R<UserHeat> userHeatValue(@ApiParam(value = "用户手机号码",required = true) @RequestParam String phone) {
		return R.data(userHeatService.getOne(
			Wrappers.<UserHeat>lambdaQuery()
				.eq(UserHeat::getPhone,phone)));
	}

	/**
	 * 查询参赛用户票数
	 */
	@PostMapping("/userVoteCount")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "查询参赛用户票数")
	public R<Long> userVoteCount(@ApiParam(value = "用户手机号码",required = true) @RequestParam String phone) {
		Map<String, Object> map = activeProductRecordService.getMap(Wrappers.<ActiveProductRecord>query().select(new StringBuilder("ifnull(sum(")
				.append(BeanPropertyUtil.getFieldNameToUnder(ActiveProductRecord::getVoteCount))
				.append("),0) as count")
				.toString())
			.lambda()
			.eq(ActiveProductRecord::getPhone, phone));
		return R.data(MapUtil.getLong(map,"count"));
	}

	/**
	 * 查询用户热力值和普法积分
	 */
	@GetMapping("/userHeatValueAndPufaPoint")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "查询用户热力值和普法积分")
	public R<Map<String,?>> userHeatValueAndPufaPoint(@ApiParam(value = "用户手机号码",required = true) @RequestParam String phone) {
		Map<String, Object> hashMap = new HashMap<>();
		hashMap.put("userHeatValue", Optional.ofNullable(userHeatValue(phone).getData()).map(UserHeat::getHeatValue).orElse(BigDecimal.ZERO));
		hashMap.put("userPufaPoint", Optional.ofNullable(userPufaPoint(phone).getData()).map(UserPufaPoint::getPufaPoint).orElse(BigDecimal.ZERO));
		hashMap.put("userVoteCount",Optional.ofNullable(userVoteCount(phone).getData()).orElse(0L));
		return R.data(hashMap);
	}
}
