package org.springblade.flow.mall.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.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.flow.mall.task.MallInvitationTask;
import org.springblade.flow.shop.entity.ShopEnum;
import org.springblade.ldt.mall.entity.Mall;
import org.springblade.ldt.mall.service.IMallService;
import org.springblade.ldt.shop.entity.Audit;
import org.springblade.ldt.shop.service.IAuditService;
import org.springframework.util.Assert;

import java.util.Map;

/**
 * @author July
 * @version 1.0.0
 * @ClassName MallUserCompleteListener.java
 * @Description 用户重新提交后处理
 * @createTime 2021年09月22日 14:19:10
 */
@NoArgsConstructor
public class MallUserCompleteListener implements TaskListener {

	private final IAuditService auditService;
	private final IMallService mallService;

	{
		auditService = SpringUtil.getBean(IAuditService.class);
		mallService = SpringUtil.getBean(IMallService.class);
	}

	@Override
	public void notify(DelegateTask delegateTask) {
		String auditId =  ((Map<String,String>)delegateTask.getVariable("audit")).get("id");
		Long mallId = Convert.toLong(delegateTask.getVariable("mallId"));
		Mall mall = mallService.getById(mallId);
		Audit audit = auditService.getById(auditId);

		audit.setAuditStatus(ShopEnum.WAITING_INVITATION.name());
		mall.setAuditStatus(ShopEnum.WAITING_INVITATION.name());

		MallInvitationTask.apply(audit);

		Assert.isTrue(auditService.saveOrUpdate(audit), () -> {
			throw new ServiceException("商场审核：用户处理完毕异常");
		});
		Assert.isTrue(mallService.saveOrUpdate(mall), () -> {
			throw new ServiceException("商场审核：用户处理完毕异常");
		});

	}
}
