package com.bonus.core.common.thridutils.alipay;

import com.alipay.api.domain.EmployeeInfoDTO;
import com.bonus.core.allocation.mobile.constants.EnterpriseSignStatusEnum;
import com.bonus.core.allocation.mobile.model.EnterpriseCodeMetadataModel;
import com.bonus.core.allocation.mobile.service.EnterpriseCodeMetadataService;
import com.bonus.core.common.utils.SpringContextHolder;

import java.util.Optional;

public class AliEnterpriseCodeUtil {
   public static boolean isEnterpriseCodeEnabled() {
      EnterpriseCodeMetadataModel enterpriseCodeMetadataModel = ((EnterpriseCodeMetadataService)SpringContextHolder.getBean(EnterpriseCodeMetadataService.class)).queryMetadata();
      return EnterpriseSignStatusEnum.ACTIVATED.key().equalsIgnoreCase(enterpriseCodeMetadataModel.getActivate()) && !enterpriseCodeMetadataModel.getAccountId().equals("-1");
   }

   public static String findEnterpriseId() {
      EnterpriseCodeMetadataModel enterpriseCodeMetadataModel = ((EnterpriseCodeMetadataService)SpringContextHolder.getBean(EnterpriseCodeMetadataService.class)).queryMetadata();
      return enterpriseCodeMetadataModel.getEnterpriseId();
   }

   public static String findEnterpriseAccountId() {
      EnterpriseCodeMetadataModel enterpriseCodeMetadataModel = ((EnterpriseCodeMetadataService)SpringContextHolder.getBean(EnterpriseCodeMetadataService.class)).queryMetadata();
      return enterpriseCodeMetadataModel.getAccountId();
   }

   public static boolean isEmployeeJoined(String uid) {
      EmployeeInfoDTO employee = ((AliPayPersonUtil)SpringContextHolder.getBean(AliPayPersonUtil.class)).queryEmplyeeInfoWithUid(findEnterpriseId(), uid);
      return (Boolean)Optional.ofNullable(employee).map((e) -> {
         return e.getActivate().equalsIgnoreCase("activated");
      }).orElse(false);
   }
}
