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.core.sms.model.SmsData;
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.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.springblade.modules.resource.utils.SmsUtil;
import org.springblade.wx.utils.UrlTransform;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author July
 * @version 1.0.0
 * @ClassName MallInvitationSignCallback.java
 * @Description 商场入驻待签约
 * @createTime 2021年09月22日 13:54:04
 */
@Component
@Slf4j
@AllArgsConstructor
public class MallInvitationSignCallback implements MallInvitationCallback {

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

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

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


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


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

		mall.setMerchantNo(invitationCallbackData.getMerchantNo());

		//处理流程流传
		BladeFlow bladeFlow = new BladeFlow();
		bladeFlow.setAssignee(AuditCandidate.YEEPAY_PLATFORM.name());
		bladeFlow.setProcessInstanceId(audit.getProcessInstanceId());
		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.TRUE);
		task.getVariables().put("isPass",Boolean.TRUE);
		task.getVariables().put("signUrl",invitationCallbackData.getAgreementSignUrl());
		task.getVariables().put("requestNo",invitationCallbackData.getRequestNo());
		Assert.isTrue(flowBusinessService.completeTask(task),()->{ throw new ServiceException(" 商户入驻回调异常： 入驻申请签约处理异常！ "); });



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

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