package com.cyksj.service.cps.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.common.util.SmsUtil;
import com.cyksj.common.util.StringUtil;
import com.cyksj.mapper.manage.distribute.DistributeWaitingSendPointsMapper;
import com.cyksj.mapper.manage.distribute.UserDistributeMapper;
import com.cyksj.model.entity.User;
import com.cyksj.model.views.BrokenLineCpsUserWMoneyView;
import com.cyksj.model.views.CpsUserWMoneyStatisticsDataView;
import com.cyksj.redis.RedisService;
import com.cyksj.service.cps.CpsManageService;
import com.cyksj.service.mail.MailService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/*
 *项目名: netflix
 *文件名: CpsManageServiceImpl
 *创建者: JavaZou
 *创建时间:2023/5/12 11:18
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class CpsManageServiceImpl implements CpsManageService {

	private final MailService mailService;

	private final RedisService redisService;

	private final DistributeWaitingSendPointsMapper waitingSendPointsMapper;

	private final UserDistributeMapper userDistributeMapper;

	@Override
	public void getLoginCode(Integer type, String login, Integer cid) {
		if (type > 2) {
			throw BusinessRuntimeException.getInstance("");
		}
		if (cid == null) {
			cid = 86;
		}
		//是否是渠道分销用户
		String phone = null;
		String email = null;
		if (type == 1) phone = login;
		else if (type == 2) email = login;
		User user = userDistributeMapper.getUserDistributeDetail(phone, email);
		if (user == null) throw BusinessRuntimeException.getInstance("只支持渠道分销用户登录");
		String CACHE_LOGIN_KEY = RedisService.key.CPS_BACK_LOGIN_KEY.getName() + login;
		Object o = redisService.get(CACHE_LOGIN_KEY);
		Long expire = redisService.getExpire(CACHE_LOGIN_KEY);
		if (o != null && expire >= 3 * 60 - 10) {
			throw BusinessRuntimeException.getInstance("获取验证码过于频繁");
		}
		String code = StringUtil.getRandomCodeStr(6);
		if (type == 1) {
			if (StrUtil.isEmpty(login) || (86 == cid && !Validator.isMobile(phone))) {
				throw BusinessRuntimeException.getInstance("请输入正确的手机号");
			}
			SmsUtil.sendTencent(cid, login, code);
		} else if (type == 2) {
			if (!Validator.isEmail(login)) {
				throw BusinessRuntimeException.getInstance("请输入正确的邮箱");
			}
			mailService.sendLoginCodeEmailToPeople(code, login);
		}
		redisService.set(CACHE_LOGIN_KEY, code, RedisService.key.CPS_BACK_LOGIN_KEY.getTimeout());
	}

	@Override
	public User login(Integer type, String login, String code) {
		String phone = null;
		String email = null;
		if (type == null) {
			type = 1;
		}
		if (type > 2) {
			throw BusinessRuntimeException.getInstance("");
		}
		if (type == 1) phone = login;
		if (type == 2) email = login;
		if (StrUtil.isEmpty(code)) {
			throw BusinessRuntimeException.getInstance("请输入验证码");
		}
		String CACHE_LOGIN_KEY = RedisService.key.CPS_BACK_LOGIN_KEY.getName();
		if (type == 1) {
			if (StrUtil.isEmpty(phone)) {
				throw BusinessRuntimeException.getInstance("请输入正确的手机号");
			}
			CACHE_LOGIN_KEY += phone;
		} else if (type == 2) {
			if (!Validator.isEmail(email)) {
				throw BusinessRuntimeException.getInstance("请输入正确的邮箱");
			}
			CACHE_LOGIN_KEY += email;
		}
		String cache_code = (String) redisService.get(CACHE_LOGIN_KEY);
		if (StrUtil.isBlank(cache_code) || !cache_code.equals(code)) {
			throw BusinessRuntimeException.getInstance("验证码错误");
		}
		User user = userDistributeMapper.getUserDistributeDetail(phone, email);
		if (user == null) throw BusinessRuntimeException.getInstance("用户不存在");
		redisService.del(CACHE_LOGIN_KEY);
		return user;
	}

	@Override
	public List<CpsUserWMoneyStatisticsDataView> getWMoneyStatisticsDataView(Long cpsUserId, DateTime first, DateTime end) {
		boolean today = isToday(first, end);
		return waitingSendPointsMapper.getWMoneyStatisticsDataView(cpsUserId, first, end, today);
	}

	@Override
	public BrokenLineCpsUserWMoneyView getWMoneyStatisticsDate(Long cpsUserId, DateTime first, DateTime end) {
		boolean today = isToday(first, end);
		return waitingSendPointsMapper.getWMoneyStatisticsDate(cpsUserId, first, end, today);
	}

	public boolean isToday(DateTime first, DateTime end) {
		boolean today = false;
		if (first != null && end != null) {
			if (DateUtil.beginOfDay(DateTime.now()).equals(first) && (DateUtil.beginOfDay(DateUtil.offsetDay(DateTime.now(), 1)).equals(end) || DateUtil.endOfDay(DateTime.now()).equals(end))) {
				today = true;
			}
		}
		return today;
	}
}
