package org.springblade.flow.activity.listener;

import cn.hutool.core.convert.Convert;
import lombok.NoArgsConstructor;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
import org.springblade.common.enums.AppConstant;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.flow.activity.entity.ActivityEnum;
import org.springblade.ldt.activity.entity.Activity;
import org.springblade.ldt.activity.entity.JoinRecord;
import org.springblade.ldt.activity.service.IActivityService;
import org.springblade.ldt.activity.service.IJoinRecordService;
import org.springblade.ldt.mall.entity.Mall;
import org.springblade.ldt.mall.service.IMallService;
import org.springblade.ldt.shop.entity.Shop;
import org.springblade.ldt.shop.service.IShopService;
import org.springframework.util.Assert;

import java.util.Optional;

/**
 * @author: lianghanqiang
 * @description: 平台审核完毕处理
 * @since: 9/3/21 -- 10:53 AM
 */
@NoArgsConstructor
public class ActivityPlatformCompleteListener implements TaskListener {

	private IActivityService activityService;
	private IJoinRecordService joinRecordService;
	private IShopService shopService;
	private IMallService mallService;

	{
		activityService = SpringUtil.getBean(IActivityService.class);
		joinRecordService = SpringUtil.getBean(IJoinRecordService.class);
		shopService = SpringUtil.getBean(IShopService.class);
		mallService = SpringUtil.getBean(IMallService.class);
	}

	@Override
	public void notify(DelegateTask delegateTask) {

		long businessId = Convert.toLong(delegateTask.getVariable("businessId"));
		boolean pass = Convert.toBool(delegateTask.getVariable("pass"));

		Activity activity = activityService.getById(businessId);

		this.joinActivity(activity);

		activity.setAuditStatus(pass? ActivityEnum.PASS.name():ActivityEnum.FAIL.name());

		Assert.isTrue(activityService.saveOrUpdate(activity),()->{throw  new ServiceException("活动审核： 平台审核完毕处理异常");});
	}

	/**
	 * 参与活动
	 * @param activity
	 */
	private void joinActivity(Activity activity){
		JoinRecord joinRecord = new JoinRecord();
		joinRecord.setActivityId(activity.getId());
		joinRecord.setJoinId(activity.getLaunchId());
		joinRecord.setJoinType(activity.getLaunchType());
		if (activity.getLaunchType().equals(AppConstant.PLATFORM.SHOP.name())) {
			Shop shop = shopService.getById(activity.getLaunchId());
			joinRecord.setJoinName(Optional.ofNullable(shop).map(Shop::getName).orElse("暂无名称"));
			joinRecord.setJoinLogo(Optional.ofNullable(shop).map(Shop::getCover).orElse(""));
		} else if (activity.getLaunchType().equals(AppConstant.PLATFORM.MALL.name())) {
			Mall mall = mallService.getById(activity.getLaunchId());
			joinRecord.setJoinName(Optional.ofNullable(mall).map(Mall::getMallName).orElse("暂无名称"));
			joinRecord.setJoinLogo(Optional.ofNullable(mall).map(Mall::getLogo).orElse(""));
		}
		joinRecord.setDiscount(activity.getDiscount());
		joinRecordService.saveOrUpdate(joinRecord);
	}
}
