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.service.IActivityService;
import org.springblade.ldt.shop.entity.Shop;
import org.springblade.ldt.shop.service.IShopService;
import org.springframework.util.Assert;

import java.util.Objects;

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

	private IActivityService activityService;
	private IShopService shopService;

	{
		activityService = SpringUtil.getBean(IActivityService.class);
		shopService = SpringUtil.getBean(IShopService.class);
	}

	@Override
	public void notify(DelegateTask delegateTask) {

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

		Activity activity = activityService.getById(businessId);

		boolean isMall = Objects.equals(activity.getLaunchType(), AppConstant.PLATFORM.MALL);

		if(isMall){
			activity.setAuditStatus(ActivityEnum.WAITING_PLATFORM.name());
		}else {
			Shop shop = shopService.getById(activity.getLaunchId());
			activity.setAuditStatus(Objects.isNull(shop.getMallId())?
				ActivityEnum.WAITING_PLATFORM.name():
				ActivityEnum.WAITING_MALL.name());
		}

		Assert.isTrue(activityService.saveOrUpdate(activity),()->{throw  new ServiceException("活动审核： 用户重新提交完毕处理异常");});
	}
}
