package org.springblade.flow.shop.listener;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.IdUtil;
import lombok.NoArgsConstructor;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
import org.springblade.common.cache.FlowCache;
import org.springblade.common.cache.PlatformCache;
import org.springblade.common.config.entity.PlatformConfig;
import org.springblade.common.enums.AppConstant;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.flow.shop.entity.InvitationCallbackVO;
import org.springblade.flow.shop.entity.ShopEnum;
import org.springblade.flow.shop.task.callback.ShopInvitationFailCallback;
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.springblade.yeePay.common.YeePayConst;
import org.springblade.yeePay.common.YeepayApiConstant;
import org.springblade.yeePay.entity.saas.InvitationDto;
import org.springblade.yeePay.service.YeepayCommonService;
import org.springblade.yeePay.service.YeepaySaasService;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.Map;

/**
 * @author: lianghanqiang
 * @description: 平台审核后处理
 * @since: 9/2/21 -- 7:40 PM
 */
@NoArgsConstructor
public class ShopPlatformCompleteListener implements TaskListener {

	private IShopService shopService;
	private IAuditService auditService;


	{
		shopService = SpringUtil.getBean(IShopService.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("shopId"));
		Audit audit = auditService.getById(auditId);
		Shop shop = shopService.getById(shopId);


		//修改审核状态
		shop.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(shopService.saveOrUpdate(shop),()->{throw  new ServiceException("商户审核：平台审核完毕异常");});

	}

}
