package org.springblade.flow.mall.listener;

import cn.hutool.core.convert.Convert;
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.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;

/**
 * @author July
 * @version 1.0.0
 * @ClassName MallPlatformCompleteListener.java
 * @Description 平台审核后处理
 * @createTime 2021年09月22日 14:12:00
 */
public class MallPlatformCompleteListener implements TaskListener {

	private final IMallService mallService;
	private final IAuditService auditService;

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


	@Override
	public void notify(DelegateTask delegateTask) {
		boolean isPass = Convert.toBool(delegateTask.getVariable("pass"));
		Long auditId = Convert.toLong(delegateTask.getVariable("auditId"));
		Long shopId = Convert.toLong(delegateTask.getVariable("mallId"));
		Audit audit = auditService.getById(auditId);
		Mall mall = mallService.getById(shopId);

		//修改审核状态
		mall.setAuditStatus(isPass ? ShopEnum.WAITING_INVITATION.name() : ShopEnum.FAIL.name());
		audit.setAuditStatus(isPass ? ShopEnum.WAITING_INVITATION.name() : ShopEnum.FAIL.name());

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