package com.cyksj.service.user.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyksj.common.util.Jsons;
import com.cyksj.mapper.UserActivityMapper;
import com.cyksj.mapper.UserActivityRankRobotMapper;
import com.cyksj.mapper.UserMapper;
import com.cyksj.mapper.UserStudentCertRecordMapper;
import com.cyksj.model.entity.*;
import com.cyksj.service.mange.coupon.CouponCommonService;
import com.cyksj.service.user.UserActivityService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目名: yhlxj
 * 文件名: UserActivityServiceImpl
 * 创建者: JavaZou
 * 创建时间:2024/4/16 12:16
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class UserActivityServiceImpl extends ServiceImpl<UserActivityMapper, UserActivity> implements UserActivityService {
	private final CouponCommonService couponCommonService;

	private final UserMapper userMapper;

	private final UserStudentCertRecordMapper userStudentCertRecordMapper;

	private final UserActivityRankRobotMapper UserActivityRankRobotMapper;
	@Override
	public void sendStudentCertActivity(User user, String dsCode, String school) throws Exception {
		UserActivity activity = getById(1);
		Long sendUserId = user.getId();
		if (StrUtil.isNotBlank(dsCode)) {
			log.info("用户code:{}进行大学生身份认证成功..", dsCode);
			User toUser = userMapper.selectOne(Wrappers.lambdaQuery(User.class).eq(User::getShowId, dsCode).last("limit 1"));
			if (toUser != null) {
				sendUserId = toUser.getId();
			}
		}
		if (StrUtil.isNotBlank(activity.getCouponIds())) {
			//发送优惠券
			List<Long> couponIds = Jsons.parseList(activity.getCouponIds(), Long.class);
			log.info("uid:{}大学生认证成功,发放优惠券:{}", user.getId(), couponIds);
			for (Long couponId : couponIds) {
				couponCommonService.sendCouponToUser(sendUserId, couponId, 0l, 0l, 0l, CouponUser.Channel.student_cert);
			}
		}
		try {
			UserStudentCertRecord record = new UserStudentCertRecord();
			//记录发送用户
			record.setUserId(sendUserId);
			record.setSchool(school);
			userStudentCertRecordMapper.insert(record);
		} catch (DuplicateKeyException e) {
		}
	}

	@Override
	public void saveOrUpdateActivity(UserActivity userActivity) throws Exception {
		checkActivity(userActivity);
		if (userActivity.getType() == 1) {
			userActivity.setRewardInfo(Jsons.toJson(userActivity.getRewardInfoList()));
		}
		saveOrUpdate(userActivity);
		if (userActivity.getType() == 1) {
			List<UserActivityRankRobot> rankRoot = userActivity.getRankRobots();
			List<Long> collect = null;
			if (rankRoot != null) {
				collect = rankRoot.stream().filter(e -> e.getId() != null).map(UserActivityRankRobot::getId).collect(Collectors.toList());
			}
			UserActivityRankRobotMapper.delete(Wrappers.lambdaQuery(UserActivityRankRobot.class)
					.eq(UserActivityRankRobot::getAcId, userActivity.getId())
					.notIn(CollUtil.isNotEmpty(collect), UserActivityRankRobot::getId, collect));

			if (CollUtil.isNotEmpty(rankRoot)) {
				rankRoot.forEach(root -> {
					root.setAcId(userActivity.getId());
					if (root.getId() != null) {
						UserActivityRankRobotMapper.updateById(root);
					} else {
						UserActivityRankRobotMapper.insert(root);
					}
				});
			}
		}
	}


	public void checkActivity(UserActivity userActivity) throws Exception {
		String couponIds = userActivity.getCouponIds();
		if (StrUtil.isNotBlank(couponIds)) {
			List<Long> couponidsList = Jsons.parseList(couponIds, Long.class);
			userActivity.setCouponIds(Jsons.toJson(couponidsList));
		}
	}
}
