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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.*;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import com.bonus.canteen.core.common.config.sys.EnterpriseCodeProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AliPayPersonUtil {
   private static final Logger log = LoggerFactory.getLogger(AliPayPersonUtil.class);
   @Autowired
   private EnterpriseCodeProperties enterpriseCodeProperties;

//   public static void main(String[] args) {
//      (new AliPayPersonUtil()).deleteEmployeeInfo("2088520563624577", "2288057005728330");
//   }
//
//   public AlipayCommerceEcEmployeeAddResponse addEmployee(String enterpriseId, String employeeName, String userId) {
//      try {
//         AlipayClient alipayClient = new DefaultAlipayClient(this.getClientParams());
//         AlipayCommerceEcEmployeeAddRequest request = new AlipayCommerceEcEmployeeAddRequest();
//         AlipayCommerceEcEmployeeAddModel model = new AlipayCommerceEcEmployeeAddModel();
//         model.setEnterpriseId(enterpriseId);
//         model.setEmployeeName(employeeName);
//         model.setIdentityType("ALIPAY_USER_ID");
//         model.setIotCheckType("facepay_epc_agree_nores");
//         model.setIdentity(userId);
//         request.setBizModel(model);
//         AlipayCommerceEcEmployeeAddResponse response = (AlipayCommerceEcEmployeeAddResponse)alipayClient.execute(request);
//         if (response.isSuccess()) {
//            log.info("【企业码】添加员工成功,{}", JSONObject.toJSONString(response));
//            return response;
//         } else {
//            String var10002 = response.getMsg();
//            throw new LeException("【企业码】添加员工失败原因:" + var10002 + "，" + response.getSubMsg());
//         }
//      } catch (AlipayApiException var8) {
//         throw new RuntimeException(var8);
//      }
//   }
//
//   public QYMEmployeeInviteVO queryEmployeeInvite(String enterpriseId) {
//      try {
//         AlipayClient alipayClient = new DefaultAlipayClient(this.getClientParams());
//         AlipayCommerceEcEmployeeInviteQueryRequest request = new AlipayCommerceEcEmployeeInviteQueryRequest();
//         AlipayCommerceEcEmployeeInviteQueryModel model = new AlipayCommerceEcEmployeeInviteQueryModel();
//         model.setEnterpriseId(enterpriseId);
//         request.setBizModel(model);
//         AlipayCommerceEcEmployeeInviteQueryResponse response = (AlipayCommerceEcEmployeeInviteQueryResponse)alipayClient.execute(request);
//         if (response.isSuccess()) {
//            QYMEmployeeInviteVO qymEmployeeInviteVO = new QYMEmployeeInviteVO();
//            BeanUtil.copyProperties(response, qymEmployeeInviteVO, new String[0]);
//            return qymEmployeeInviteVO;
//         } else {
//            String var10002 = response.getMsg();
//            throw new LeException("【企业码】获取邀请链接失败原因:" + var10002 + "，" + response.getSubMsg());
//         }
//      } catch (AlipayApiException var7) {
//         throw new RuntimeException(var7);
//      }
//   }
//
//   public AlipayCommerceEcEmployeeInfoModifyResponse modifyEmployeeInfo(String enterpriseId, String employeeId, String employeeName) {
//      try {
//         AlipayClient alipayClient = new DefaultAlipayClient(this.getClientParams());
//         AlipayCommerceEcEmployeeInfoModifyRequest request = new AlipayCommerceEcEmployeeInfoModifyRequest();
//         AlipayCommerceEcEmployeeInfoModifyModel model = new AlipayCommerceEcEmployeeInfoModifyModel();
//         model.setEnterpriseId(enterpriseId);
//         model.setEmployeeId(employeeId);
//         model.setEmployeeName(employeeName);
//         request.setBizModel(model);
//         AlipayCommerceEcEmployeeInfoModifyResponse response = (AlipayCommerceEcEmployeeInfoModifyResponse)alipayClient.execute(request);
//         if (response.isSuccess()) {
//            System.out.println("调用成功");
//            return response;
//         } else {
//            String var10002 = response.getMsg();
//            throw new LeException("【企业码】员工信息修改失败原因:" + var10002 + "，" + response.getSubMsg());
//         }
//      } catch (AlipayApiException var8) {
//         throw new RuntimeException(var8);
//      }
//   }

//   public EmployeeInfoDTO queryEmployeeInfo(String enterpriseId, String employeeId) {
//      return this.queryEnterpriseEmployeeInfo(enterpriseId, employeeId, (String)null);
//   }

   public EmployeeInfoDTO queryEnterpriseEmployeeInfo(String enterpriseId, String employeeId, String userId) {
      try {
         AlipayClient alipayClient = new DefaultAlipayClient(this.getClientParams());
         AlipayCommerceEcEmployeeInfoQueryRequest request = new AlipayCommerceEcEmployeeInfoQueryRequest();
         JSONObject bizContent = new JSONObject();
         bizContent.put("enterprise_id", enterpriseId);
         if (ObjectUtil.isNotNull(employeeId)) {
            bizContent.put("employee_id", employeeId);
         }

         if (ObjectUtil.isNotNull(userId)) {
            bizContent.put("user_id", userId);
         }

         request.setBizContent(bizContent.toJSONString());
         AlipayCommerceEcEmployeeInfoQueryResponse response = (AlipayCommerceEcEmployeeInfoQueryResponse)alipayClient.execute(request);
         if (response.isSuccess()) {
            EmployeeInfoDTO employeeInfoDTO = new EmployeeInfoDTO();
            BeanUtil.copyProperties(response.getEmployeeInfo(), employeeInfoDTO, new String[0]);
            return employeeInfoDTO;
         } else {
            Logger var10000 = log;
            String var10001 = response.getMsg();
            var10000.error("【企业码】员工信息查询失败原因:" + var10001 + "，" + response.getSubMsg());
            return null;
         }
      } catch (AlipayApiException var9) {
         throw new RuntimeException(var9);
      }
   }

   public EmployeeInfoDTO queryEmplyeeInfoWithUid(String enterpriseId, String uid) {
      return this.queryEnterpriseEmployeeInfo(enterpriseId, (String)null, uid);
   }

//   public AlipayCommerceEcEmployeeDeleteResponse deleteEmployeeInfo(String enterpriseId, String employeeId) {
//      try {
//         AlipayClient alipayClient = new DefaultAlipayClient(this.getClientParams());
//         AlipayCommerceEcEmployeeDeleteRequest request = new AlipayCommerceEcEmployeeDeleteRequest();
//         AlipayCommerceEcEmployeeDeleteModel model = new AlipayCommerceEcEmployeeDeleteModel();
//         model.setEnterpriseId(enterpriseId);
//         model.setEmployeeId(employeeId);
//         request.setBizModel(model);
//         AlipayCommerceEcEmployeeDeleteResponse response = (AlipayCommerceEcEmployeeDeleteResponse)alipayClient.execute(request);
//         if (response.isSuccess()) {
//            System.out.println("调用成功");
//            return response;
//         } else {
//            String var10002 = response.getMsg();
//            throw new LeException("【企业码】员工信息删除失败原因:" + var10002 + "，" + response.getSubMsg());
//         }
//      } catch (AlipayApiException var7) {
//         throw new RuntimeException(var7);
//      }
//   }
//
//   public QYMEmployeeIdsVO queryEmployeeIds(String enterpriseId, String departmentId, Integer pageNum, Integer pageSize) {
//      try {
//         AlipayClient alipayClient = new DefaultAlipayClient(this.getClientParams());
//         AlipayCommerceEcEmployeeIdlistQueryRequest request = new AlipayCommerceEcEmployeeIdlistQueryRequest();
//         request.setBizContent("{  \"enterprise_id\":\"" + enterpriseId + "\",  \"department_id\":\"" + departmentId + "\",  \"page_num\":" + pageNum + ",  \"page_size\":" + pageSize + "}");
//         AlipayCommerceEcEmployeeIdlistQueryResponse response = (AlipayCommerceEcEmployeeIdlistQueryResponse)alipayClient.execute(request);
//         if (response.isSuccess()) {
//            QYMEmployeeIdsVO qymEmployeeIdsVO = new QYMEmployeeIdsVO();
//            BeanUtil.copyProperties(response, qymEmployeeIdsVO, new String[0]);
//            return qymEmployeeIdsVO;
//         } else {
//            String var10002 = response.getMsg();
//            throw new LeException("【企业码】查询员工id集合失败原因:" + var10002 + "，" + response.getSubMsg());
//         }
//      } catch (AlipayApiException var9) {
//         throw new RuntimeException(var9);
//      }
//   }
//
//   public void alipay(String authCode, String ordId) {
//      try {
//         AlipayClient alipayClient = new DefaultAlipayClient(this.getClientParams());
//         AlipayTradePayRequest request = new AlipayTradePayRequest();
//         JSONObject bizContent = new JSONObject();
//         bizContent.put("out_trade_no", ordId);
//         bizContent.put("total_amount", 0.01);
//         bizContent.put("subject", "支付测试");
//         bizContent.put("scene", "bar_code");
//         bizContent.put("auth_code", authCode);
//         JSONObject businessParams = new JSONObject();
//         businessParams.put("assignJointAccountId", "2088520561875205");
//         businessParams.put("enterprisePayAmount", "1");
//         bizContent.put("business_params", businessParams);
//         JSONObject extendParams = new JSONObject();
//         extendParams.put("pdSubBizScene", "enterprisePay");
//         bizContent.put("extend_params", extendParams);
//         JSONArray queryOptions = new JSONArray();
//         queryOptions.add("enterprise_pay_info");
//         bizContent.put("query_options", queryOptions);
//         request.setBizContent(bizContent.toString());
//         AlipayTradePayResponse response = (AlipayTradePayResponse)alipayClient.execute(request);
//         if (response.isSuccess()) {
//            System.out.println("调用成功");
//         } else {
//            System.out.println("调用失败");
//         }
//
//      } catch (AlipayApiException var10) {
//         throw new RuntimeException(var10);
//      }
//   }

   public AlipayConfig getClientParams() {
      AlipayConfig certParams = new AlipayConfig();
      certParams.setAppId(this.enterpriseCodeProperties.getAppId());
      certParams.setPrivateKey(this.enterpriseCodeProperties.getPrivateKey());
      certParams.setAlipayPublicKey(this.enterpriseCodeProperties.getAlipayPublicKey());
      return certParams;
   }
}
