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

import cn.hutool.core.convert.Convert;
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.util.Assert;

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

/**
 * @author July
 * @version 1.0.0
 * @ClassName MallInvitationSuccessCallback.java
 * @Description 商场入驻成功
 * @createTime 2021年09月22日 11:40:00
 */
@Component
@AllArgsConstructor
@Slf4j
public class MallInvitationSuccessCallback implements MallInvitationCallback {

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

	@Override
	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.PASS.name())){
			log.info("回调已处理，忽略！");
			return;
		}

		mall.setMerchantNo(invitationCallbackData.getMerchantNo());

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

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

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

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

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