/*
 *      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.activity.wrapper;

import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.modules.guosen.activity.entity.Activity;
import org.springblade.modules.guosen.activity.vo.ActivityVO;
import org.springblade.modules.guosen.activityPresent.entity.ActivityPresent;
import org.springblade.modules.guosen.activityPresent.service.IActivityPresentService;
import org.springblade.modules.guosen.activityTag.entity.ActivityTag;
import org.springblade.modules.guosen.activityTag.service.IActivityTagService;
import org.springblade.modules.guosen.helpRecord.entity.HelpRecord;
import org.springblade.modules.guosen.helpRecord.service.IHelpRecordService;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * 包装类,返回视图层所需的字段
 *
 * @author BladeX
 * @since 2021-06-09
 */
public class ActivityWrapper extends BaseEntityWrapper<Activity, ActivityVO>  {

	private IActivityTagService activityTagService;
	private IActivityPresentService activityPresentService;
	private IHelpRecordService helpRecordService;


	public ActivityWrapper(IActivityTagService activityTagService, IActivityPresentService activityPresentService, IHelpRecordService helpRecordService) {
		this.activityTagService = activityTagService;
		this.activityPresentService = activityPresentService;
		this.helpRecordService = helpRecordService;
	}

	public ActivityWrapper(IActivityTagService activityTagService, IActivityPresentService activityPresentService) {
		this.activityTagService = activityTagService;
		this.activityPresentService = activityPresentService;
	}

	public static ActivityWrapper build(IActivityTagService activityTagService, IActivityPresentService activityPresentService, IHelpRecordService helpRecordService) {
		return new ActivityWrapper(activityTagService,activityPresentService,helpRecordService);

	}

	public static ActivityWrapper build(IActivityTagService activityTagService, IActivityPresentService activityPresentService) {
		return new ActivityWrapper(activityTagService,activityPresentService);
	}

	@Override
	public ActivityVO entityVO(Activity activity) {
		ActivityVO activityVO = Objects.requireNonNull(BeanUtil.copy(activity, ActivityVO.class));
		Long id = activityVO.getId();
		if (activityTagService != null) {
			List<Long> tagIds = activityTagService.list(Condition.getQueryWrapper(new ActivityTag()).lambda()
				.eq(ActivityTag::getActivityId, id)).stream().map(ActivityTag::getTagId).collect(toList());
			activityVO.setTagIds(tagIds);
		}

		if (activityPresentService != null) {
			List<Long> presentIds = activityPresentService.list(Condition.getQueryWrapper(new ActivityPresent()).lambda()
				.eq(ActivityPresent::getActivityId, id)).stream().map(ActivityPresent::getPresentId).collect(toList());
			activityVO.setPresentIds(presentIds);
		}

		if (helpRecordService != null) {
			List<HelpRecord> helpRecords = helpRecordService.list(Condition.getQueryWrapper(new HelpRecord()).lambda()
				.eq(HelpRecord::getActivityId, id));
			//查看打榜数量
			long count = helpRecords.stream().filter(item -> item.getType() == 0 || item.getType() == 2).count();
			activityVO.setHitNum(count);
			//查看打赏数量
			long count1 = helpRecords.stream().filter(item -> item.getType() == 1 || item.getType() == 3).count();
			activityVO.setRewardNum(count1);
			//礼物总值
			List<HelpRecord> cashList = helpRecords.stream().filter(item ->item.getTotalCash()!=null && !BigDecimal.ZERO.equals(item.getTotalCash())).collect(toList());
			BigDecimal totalCash = BigDecimal.ZERO;
			for (HelpRecord record : cashList) {
				BigDecimal cash = record.getTotalCash();
				totalCash = cash.add(totalCash);
			}
			activityVO.setTotalCash(totalCash);
		}


		return activityVO;
	}

}
