package org.springblade.activity.task;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springblade.activity.dto.vo.ExchangeRecordVO;
import org.springblade.activity.dto.vo.ParticipateRecordVO;
import org.springblade.activity.entity.ActivityMemberExchangeRecord;
import org.springblade.activity.entity.ActivityParticipateRecord;
import org.springblade.activity.entity.BladeActivity;
import org.springblade.activity.service.IActivityMemberExchangeRecordService;
import org.springblade.activity.service.IActivityParticipateRecordService;
import org.springblade.activity.service.IBladeActivityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

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

@Component
@Slf4j
public class ActivityRecordTiming {


	@Autowired
	private IBladeActivityService bladeActivityService;

	@Autowired
	private IActivityMemberExchangeRecordService activityMemberExchangeRecordService;

	@Autowired
	private IActivityParticipateRecordService activityParticipateRecordService;


	/**
	 * 自动更新账号登录状态
	 * cron = "0 0 0 * * ?" 00:00
	 * cron = "0 * * * * ? *" 每分钟
	 * cron = "0 0 * * * ? *"  每小时
	 */
	@Scheduled(cron = "0 5 0 * * ?")
	public void activityRecordTiming() {
		log.info("自动获取录入活动的各项记录开始 ====== ");
		try {
			List<BladeActivity> list = bladeActivityService.list(new LambdaQueryWrapper<BladeActivity>()
				.eq(BladeActivity::getIsDeleted, 0)
				.isNotNull(BladeActivity::getActivityUuid)
			);
			log.info("需要同步的活动数 === {}", list.size());
			if (list != null && !list.isEmpty()) {
				for (BladeActivity bladeActivity : list) {
					//活动会员兑奖记录 同步
					sysExchangeRecord(bladeActivity.getActivityUuid());
					//活动参与记录 同步
					sysParticipateRecord(bladeActivity.getActivityUuid());
				}
			} else {
				log.info("没有需要同步的活动");
			}
			log.info("自动获取录入活动的各项记录结束 ====== ");
		} catch (Exception e) {
			log.info("自动获取录入活动的各项记录异常 ====== ");
			e.printStackTrace();
		}
	}


	@Value("${activity.exchangeRecordUrl}")
	private String exchangeRecordUrl;

	@Value("${activity.participateRecordUrl}")
	private String participateRecordUrl;

	/**
	 * 活动会员兑奖记录
	 */
	public void sysExchangeRecord(String activityId) {
		try {
			HashMap<String, Object> map = new HashMap<>();
			map.put("activityId", activityId);
//			String s = HttpUtils.doGetHttp(exchangeRecordUrl, null, map);
			String s = HttpUtil.get(exchangeRecordUrl, map);
			JSONObject jsonObject = JSON.parseObject(s);
			if (jsonObject != null && !jsonObject.isEmpty() && jsonObject.getString("code").equals("10000")) {
				List<ExchangeRecordVO> recordList = JSON.parseArray(jsonObject.getString("data"), ExchangeRecordVO.class);
				if (recordList != null && !recordList.isEmpty()) {
					activityMemberExchangeRecordService.remove(new LambdaQueryWrapper<ActivityMemberExchangeRecord>()
						.eq(ActivityMemberExchangeRecord::getActivityUuid, activityId)
					);
					List<ActivityMemberExchangeRecord> collect = recordList.stream()
						.map(obj -> {
							ActivityMemberExchangeRecord entity = new ActivityMemberExchangeRecord();
							BeanUtils.copyProperties(obj, entity);
							entity.setCreateTime(new Date());
							return entity;
						}).collect(Collectors.toList());
					boolean act = activityMemberExchangeRecordService.saveBatch(collect);
					if (act) {
						log.info("同步活动会员兑奖记录成功");
					} else {
						log.info("同步活动会员兑奖记录失败");
					}
				} else {
					log.info("同步失败,活动会员兑奖记录为空");
				}
			} else if (jsonObject != null && !jsonObject.isEmpty() && jsonObject.getString("code").equals("500")) {
				log.info(jsonObject.getString("message"));
			} else {
				log.info("同步活动会员兑奖记录异常");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 活动参与记录
	 */
	public void sysParticipateRecord(String activityId) {
		try {
			HashMap<String, Object> map = new HashMap<>();
			map.put("activityId", activityId);
//			String s = HttpUtils.doGetHttp(participateRecordUrl, null, map);
			String s = HttpUtil.get(participateRecordUrl, map);
			JSONObject jsonObject = JSON.parseObject(s);
			if (jsonObject != null && !jsonObject.isEmpty() && jsonObject.getString("code").equals("10000")) {
				List<ParticipateRecordVO> recordList = JSON.parseArray(jsonObject.getString("data"), ParticipateRecordVO.class);
				if (recordList != null && !recordList.isEmpty()) {
					activityParticipateRecordService.remove(new LambdaQueryWrapper<ActivityParticipateRecord>()
						.eq(ActivityParticipateRecord::getActivityUuid, activityId)
					);
					List<ActivityParticipateRecord> collect = recordList.stream()
						.map(obj -> {
							ActivityParticipateRecord entity = new ActivityParticipateRecord();
							BeanUtils.copyProperties(obj, entity);
							entity.setCreateTime(new Date());
							return entity;
						}).collect(Collectors.toList());
					boolean act = activityParticipateRecordService.saveBatch(collect);
					if (act) {
						log.info("同步活动参与记录成功");
					} else {
						log.info("同步活动参与记录失败");
					}
				} else {
					log.info("同步失败,活动参与记录为空");
				}
			} else if (jsonObject != null && !jsonObject.isEmpty() && jsonObject.getString("code").equals("500")) {
				log.info(jsonObject.getString("message"));
			} else {
				log.info("同步活动参与记录异常");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


}
