package org.springblade.flow.shop.task;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.IdUtil;
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.cache.PlatformCache;
import org.springblade.common.config.entity.PlatformConfig;
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.SpringUtil;
import org.springblade.flow.shop.entity.InvitationCallbackData;
import org.springblade.flow.shop.entity.InvitationCallbackVO;
import org.springblade.flow.shop.task.callback.ShopInvitationFailCallback;
import org.springblade.flow.utils.InvitationBuilder;
import org.springblade.flow.utils.MicroInvitationBuilder;
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.springblade.yeePay.common.YeePayConst;
import org.springblade.yeePay.common.YeepayApiConstant;
import org.springblade.yeePay.entity.saas.InvitationDto;
import org.springblade.yeePay.entity.saas.MicroInvitationDto;
import org.springblade.yeePay.service.YeepayCommonService;
import org.springblade.yeePay.service.YeepaySaasService;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

/**
 * @author: lianghanqiang
 * @description: 商户入驻申请
 * @since: 9/7/21 -- 11:10 AM
 */
public class InvitationTask implements JavaDelegate {


	private IShopService shopService;
	private IAuditService auditService;
	private YeepaySaasService yeepaySaasService;
	private YeepayCommonService yeepayCommonService;
	private YeePayConst yeePayConst;

	{
		shopService = SpringUtil.getBean(IShopService.class);
		auditService = SpringUtil.getBean(IAuditService.class);
		yeepaySaasService = SpringUtil.getBean(YeepaySaasService.class);
		yeepayCommonService = SpringUtil.getBean(YeepayCommonService.class);
		yeePayConst = SpringUtil.getBean(YeePayConst.class);
	}

	@Override
	public void execute(DelegateExecution delegateExecution) {

		Long auditId = Convert.toLong(delegateExecution.getVariable("auditId"));
		Audit audit = auditService.getById(auditId);
		Shop shop = shopService.getById(audit.getEntityId());

		//流程变量
		Map<String, Object> variables = delegateExecution.getVariables();
		YopResponse yopResponse;
		if(Objects.equals(audit.getTypeOfEnterprise(),AppConstant.ENTERPRISE_TYPE.MINICRO.name())){
			yopResponse = microInvitation(audit,shop,variables);
		}else {
			yopResponse = merchantInvitation(audit,shop,variables);
		}


		String stringResult = yopResponse.getStringResult();
		JSONObject resObj = JSON.parseObject(stringResult);

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

	}


	/**
	 * 商户进件
	 * */
	private YopResponse merchantInvitation(Audit audit, Shop shop, Map<String, Object> variables){
		//封装进件信息
		InvitationDto invitationDto = InvitationBuilder.build(audit);
		invitationDto.setNotifyUrl(CallbackServeUrl.CALL_BACK_SERVE_URL + "/payment/invitation/callback/" + AppConstant.InvitationType.SHOP_INVITATION);
		invitationDto.setBusinessRole(YeepayApiConstant.businessRole.SETTLED_MERCHANT.name());
		audit.setRequestNo(invitationDto.getRequestNo());

		auditService.updateById(audit);

		//缓存入驻申请的数据，方便后续回调使用
		FlowCache.putAudit(InvitationCallbackVO.builder()
				.audit(audit)
				.shop(shop)
				.variables(variables)
				.build(),
			invitationDto.getRequestNo());


		//向易宝平台提交入驻申请
		return yeepaySaasService.inviteMerchant(invitationDto);
	}


	/**
	 * 	小微商户进件
	 * */
	private YopResponse microInvitation(Audit audit, Shop shop, Map<String, Object> variables){

			//封装进件信息
			MicroInvitationDto microInvitationDto = MicroInvitationBuilder.build(audit);
			microInvitationDto.setNotifyUrl(CallbackServeUrl.CALL_BACK_SERVE_URL + "/payment/invitation/callback/" + AppConstant.InvitationType.SHOP_INVITATION);
			microInvitationDto.setBusinessRole(YeepayApiConstant.businessRole.SETTLED_MERCHANT.name());
			audit.setRequestNo(microInvitationDto.getRequestNo());

			auditService.updateById(audit);

			//缓存入驻申请的数据，方便后续回调使用
			FlowCache.putAudit(InvitationCallbackVO.builder()
					.audit(audit)
					.shop(shop)
					.variables(variables)
					.build(),
				microInvitationDto.getRequestNo());


			//向易宝平台提交入驻申请
			return yeepaySaasService.inviteMicro(microInvitationDto);
	}
}
