package org.springblade.flow.mall.task;

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yeepay.yop.sdk.service.common.response.YopResponse;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.springblade.common.cache.FlowCache;
import org.springblade.common.constant.CallbackServeUrl;
import org.springblade.common.enums.AppConstant;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.flow.mall.entity.MallInvitationCallbackVO;
import org.springblade.flow.mall.task.callback.MallInvitationFailCallback;
import org.springblade.flow.shop.entity.InvitationCallbackData;
import org.springblade.flow.utils.InvitationBuilder;
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.yeePay.common.YeePayConst;
import org.springblade.yeePay.common.YeepayApiConstant;
import org.springblade.yeePay.entity.saas.InvitationDto;
import org.springblade.yeePay.service.YeepaySaasService;

import java.rmi.server.ServerCloneException;
import java.util.Map;
import java.util.Objects;

/**
 * @author July
 * @version 1.0.0
 * @ClassName MallInvitationTask.java
 * @Description 商场入驻申请
 * @createTime 2021年09月22日 11:32:00
 */
public class MallInvitationTask  {

	private static final IMallService mallService;
	private static final IAuditService auditService;
	private static final YeePayConst yeePayConst;
	private static YeepaySaasService yeepaySaasService;
	private static MallInvitationFailCallback mallInvitationFailCallback;

	static {
		mallService = SpringUtil.getBean(IMallService.class);
		auditService = SpringUtil.getBean(IAuditService.class);
		yeePayConst = SpringUtil.getBean(YeePayConst.class);
		yeepaySaasService = SpringUtil.getBean(YeepaySaasService.class);
		mallInvitationFailCallback = SpringUtil.getBean(MallInvitationFailCallback.class);
	}


	public static boolean apply(Audit audit) throws ServiceException {

		//封装进件信息
		InvitationDto invitationDto = InvitationBuilder.build(audit);
		invitationDto.setNotifyUrl(CallbackServeUrl.CALL_BACK_SERVE_URL+"/payment/invitation/callback/"+ AppConstant.InvitationType.MALL_INVITATION);
		invitationDto.setParentMerchantNo(yeePayConst.getPlatformServiceNo());
		invitationDto.setBusinessRole(YeepayApiConstant.businessRole.SETTLED_MERCHANT.name());
		invitationDto.getMerchantCorporationInfo().setLegalName(audit.getLegalPerson());
		audit.setRequestNo(invitationDto.getRequestNo());


		//todo：向易宝入驻
		YopResponse yopResponse = yeepaySaasService.inviteMerchant(invitationDto);
		String stringResult = yopResponse.getStringResult();
		JSONObject resObj = JSON.parseObject(stringResult);

		if(! (Objects.equals(resObj.getString("returnCode"),"NIG00000"))){
			throw new ServiceException(resObj.getString("returnMsg"));
		}
		return true;
	}
}
