package org.springblade.flow.shop.task.callback;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.springblade.common.cache.FlowCache;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.flow.business.constant.AuditCandidate;
import org.springblade.flow.business.service.FlowBusinessService;
import org.springblade.flow.core.entity.BladeFlow;
import org.springblade.flow.core.utils.TaskUtil;
import org.springblade.flow.shop.entity.InvitationCallbackData;
import org.springblade.flow.shop.entity.InvitationCallbackVO;
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.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Objects;


/**
 * @author: lianghanqiang
 * @description: 商户入驻失败
 * @since: 9/6/21 -- 6:49 PM
 */
@Component
@AllArgsConstructor
public class ShopInvitationFailCallback implements ShopInvitationCallback {

	private  FlowBusinessService flowBusinessService;
	private  IShopService shopService;
	private  IAuditService auditService;


	@Override
	@Transactional
	public  void handle(InvitationCallbackData invitationCallbackData) {

		//获取缓存的流程信息
		InvitationCallbackVO invitationCallbackVO = FlowCache.getAudit(invitationCallbackData.getRequestNo());
		Assert.notNull(invitationCallbackVO,() -> {throw new ServiceException("商户入驻回调异常：找不到对应的流程信息");});

		//获取对应的业务表对象
		Audit audit = invitationCallbackVO.getAudit();
		Shop shop = invitationCallbackVO.getShop();

		if(Objects.equals(audit.getAuditStatus(),ShopEnum.FAIL.name())){
			return;
		}

		shop.setMerchantNo(invitationCallbackData.getMerchantNo());

		//获取流程任务
		BladeFlow bladeFlow = new BladeFlow();
		bladeFlow.setProcessInstanceId(audit.getProcessInstanceId());
		bladeFlow.setAssignee(AuditCandidate.YEEPAY_PLATFORM.name());
//		bladeFlow.getVariables().put("requestNo",invitationCallbackData.getRequestNo());
		List<BladeFlow> records = flowBusinessService.selectTodoPage(new Page<BladeFlow>(), bladeFlow).getRecords();
		BladeFlow task= records.get(0);
		Assert.notNull(task,() -> {throw new ServiceException("商户入驻回调异常：找不到对应的流程任务");});

		task.setComment(invitationCallbackData.getAuditOpinion());
		task.setPass(Boolean.FALSE);
		task.getVariables().put("approve",Boolean.FALSE);
	 	Assert.isTrue(flowBusinessService.completeTask(task),()->{ throw new ServiceException(" 商户入驻回调异常： 入驻申请驳回处理异常！ "); });

	 	//修改审核状态
	 	audit.setAuditStatus(ShopEnum.FAIL.name());
	 	shop.setAuditStatus(ShopEnum.FAIL.name());

		Assert.isTrue(auditService.saveOrUpdate(audit),()->{ throw new ServiceException(" 商户入驻回调异常： 入驻申请驳回处理异常！ "); });
		Assert.isTrue(shopService.saveOrUpdate(shop),()->{ throw new ServiceException(" 商户入驻回调异常： 入驻申请驳回处理异常！ "); });
	}
}
