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

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.cache.FlowCache;
import org.springblade.core.log.exception.ServiceException;
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.mall.entity.MallInvitationCallbackVO;
import org.springblade.flow.shop.entity.InvitationCallbackData;
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.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

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

/**
 * @author July
 * @version 1.0.0
 * @ClassName MallInvitationFailCallback.java
 * @Description 商场入驻失败
 * @createTime 2021年09月22日 13:43:35
 */
@Component
@Slf4j
@AllArgsConstructor
public class MallInvitationFailCallback implements MallInvitationCallback {

	private FlowBusinessService flowBusinessService;
	private IMallService mallService;
	private IAuditService auditService;


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

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

		//获取对应的业务表对象
		Mall mall = mallInvitationCallbackVO.getMall();
		Audit audit = mallInvitationCallbackVO.getAudit();

		if(Objects.equals(audit.getAuditStatus(),ShopEnum.FAIL.name())){
			log.info("回调已处理，忽略！");
			return;
		}

		mall.setMerchantNo(invitationCallbackData.getMerchantNo());

		//获取流程任务,处理流程流传
		BladeFlow bladeFlow = new BladeFlow();
		bladeFlow.setProcessInstanceId(audit.getProcessInstanceId());
		bladeFlow.setAssignee(AuditCandidate.YEEPAY_PLATFORM.name());
		List<BladeFlow> records = flowBusinessService.selectTodoPage(new Page<BladeFlow>(), bladeFlow).getRecords();
		BladeFlow task= records.get(0);
		Assert.notNull(task,() -> {throw new ServiceException("商户入驻回调异常：找不到对应的流程任务");});
		task.setPass(Boolean.FALSE);
		task.setComment(invitationCallbackData.getAuditOpinion());
		task.getVariables().put("isPass",Boolean.FALSE);
		Assert.isTrue(flowBusinessService.completeTask(task),()->{ throw new ServiceException(" 商户入驻回调异常： 入驻申请驳回处理异常！ "); });


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

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