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

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.springblade.common.cache.FlowCache;
import org.springblade.common.enums.AppConstant;
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.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.util.Assert;

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

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


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

	@Override
	public void handle(InvitationCallbackData invitationCallbackData) {
		//获取缓存的流程信息
		InvitationCallbackVO invitationCallbackVO = FlowCache.getAudit(invitationCallbackData.getRequestNo());

		Assert.notNull(invitationCallbackVO,() -> {throw new ServiceException("商户入驻回调异常：找不到对应的流程信息");});

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

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

		shop.setMerchantNo(invitationCallbackData.getMerchantNo());
		Boolean isMicro = Objects.equals(audit.getTypeOfEnterprise(), AppConstant.ENTERPRISE_TYPE.MINICRO.name());
		shopService.saveOrUpdate(shop);

		//获取流程任务
		BladeFlow bladeFlow = new BladeFlow();
		bladeFlow.setAssignee(isMicro? AuditCandidate.YEEPAY_PLATFORM.name():AuditCandidate.$INITIATOR.name());
		bladeFlow.setProcessInstanceId(audit.getProcessInstanceId());
		List<BladeFlow> records = flowBusinessService.selectTodoPage(new Page<BladeFlow>(), bladeFlow).getRecords();
		BladeFlow task= records.get(0);

		task.setPass(Boolean.TRUE);
		task.getVariables().put("approve",Boolean.TRUE);
		task.getVariables().put("isMicro",isMicro);
		Assert.notNull(task,() -> {throw new ServiceException("商户入驻回调异常：找不到对应的流程任务");});


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

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

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

		FlowCache.removeAudit(invitationCallbackData.getRequestNo());
	}
}
