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;

/**
 * @author: lianghanqiang
 * @description:
 * @since: 9/3/21 -- 9:22 AM
 */
@NoArgsConstructor
public class ShopMallCompleteListener implements TaskListener {

	private IAuditService auditService;
	private IShopService shopService;

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

	@Override
	public void notify(DelegateTask delegateTask) {

		boolean isPass = Convert.toBool(delegateTask.getVariable("pass"));
		Shop shop = shopService.getById( Convert.toLong(delegateTask.getVariable("shopId")));

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

		audit.setAuditStatus(isPass? ShopEnum.WAITING_PLATFORM.name():ShopEnum.FAIL.name());
		shop.setAuditStatus(isPass? ShopEnum.WAITING_PLATFORM.name():ShopEnum.FAIL.name());

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

	}
}
