package org.springblade.flow.shop.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.shop.entity.ShopEnum;
import org.springblade.ldt.shop.entity.Audit;
import org.springblade.ldt.shop.entity.Shop;
import org.springblade.ldt.shop.service.IAuditService;
import org.springblade.ldt.shop.service.IShopService;
import org.springframework.util.Assert;

import java.util.Objects;

/**
 * @author: lianghanqiang
 * @description: 用户重新提交后处理
 * @since: 9/2/21 -- 7:51 PM
 */
@NoArgsConstructor
public class ShopUserCompleteListener implements TaskListener {

	private IAuditService auditService;
	private IShopService shopService;

	{
		auditService = SpringUtil.getBean(IAuditService.class);
		shopService = SpringUtil.getBean(IShopService.class);
	}

	@Override
	public void notify(DelegateTask delegateTask) {

		Long auditId = Convert.toLong(delegateTask.getVariable("auditId"));
		Long shopId = Convert.toLong(delegateTask.getVariable("shopId"));
		Shop shop = shopService.getById(shopId);
		Audit audit = auditService.getById(auditId);

		boolean toPlatform = Objects.isNull(shop.getMallId());

		audit.setAuditStatus(toPlatform? ShopEnum.WAITING_PLATFORM.name(): ShopEnum.WAITING_MALL.name());
		shop.setAuditStatus(toPlatform? ShopEnum.WAITING_PLATFORM.name(): ShopEnum.WAITING_MALL.name());


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

	}
}
