package com.yifu.cloud.plus.v1.ekp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.icbc.api.DefaultIcbcClient;
import com.icbc.api.IcbcApiException;
import com.icbc.api.IcbcConstants;
import com.icbc.api.UiIcbcClient;
import com.icbc.api.request.JftApiPayrollQueryDetailRequestV1;
import com.icbc.api.request.JftUiPayrollComfirmSubmitRequestV1;
import com.icbc.api.response.JftApiPayrollQueryDetailResponseV1;
import com.yifu.cloud.plus.v1.ekp.config.IcbcConfigProperties;
import com.yifu.cloud.plus.v1.ekp.entity.EkpSocialInfo;
import com.yifu.cloud.plus.v1.ekp.mapper.EkpSocialInfoMapper;
import com.yifu.cloud.plus.v1.ekp.service.IcbcTransactionFlowIssueService;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 聚富通到账通知交易流水查询
 *
 * @author huyc
 * @date 2024-05-23 11:21:56
 */
@Log4j2
@Service
@RequiredArgsConstructor
@EnableConfigurationProperties(IcbcConfigProperties.class)
public class IcbcTransactionFlowIssueServiceImpl extends ServiceImpl<EkpSocialInfoMapper, EkpSocialInfo> implements IcbcTransactionFlowIssueService {

	@Autowired
	private IcbcConfigProperties icbcConfigProperties;

	public R submitIcbcTransactionFlow() {

		UiIcbcClient client = new UiIcbcClient(icbcConfigProperties.getAppId(), IcbcConstants.SIGN_TYPE_RSA2, icbcConfigProperties.getAppPrivateKey(),
				IcbcConstants.CHARSET_UTF8);
		JftUiPayrollComfirmSubmitRequestV1 request = new JftUiPayrollComfirmSubmitRequestV1();
		request.setServiceUrl("https://apipcs3.dccnet.com.cn/ui/jft/ui/payroll/comfirmsubmit/V1");
		JftUiPayrollComfirmSubmitRequestV1.JjftUiPayrolComfirmSubmitRequestV1BizV1 bizContent =
				new JftUiPayrollComfirmSubmitRequestV1.JjftUiPayrolComfirmSubmitRequestV1BizV1();
		bizContent.setAppId(icbcConfigProperties.getAppId());
		//HR系统名称
		bizContent.setAppName("HRO");
		//企业编号
		bizContent.setOutVendorId(icbcConfigProperties.getCompanyNo());
		//操作类型：1-受理 2-审核
		bizContent.setOprType("1");
		bizContent.setTotalAmt("1000");
		bizContent.setTotalCount("1");
		bizContent.setAppSerialno("21900");
		bizContent.setLocalFilepath("D:/icbcFile/955888_21900_0_20240524.xls");
		bizContent.setFileMdcode(getMdCode("D:/icbcFile/955888_21900_0_20240524.xls"));
		bizContent.setFileType("1");
		bizContent.setNotifyUrl(null);
		bizContent.setAppRemark("备注");
		bizContent.setBusinessType(null);
		bizContent.setFileCheckSign(null);
		request.setBizContent(bizContent);
		try {
			String result = client.buildPostForm(request);
			if (Common.isNotNull(result)) {
				return R.ok();
			}
		} catch (IcbcApiException e) {
			e.printStackTrace();
			return R.failed();
		}
		return R.ok();
	}

	public R selectIcbcTransactionFlowInfo() {

		DefaultIcbcClient client = new DefaultIcbcClient(icbcConfigProperties.getAppId(), IcbcConstants.SIGN_TYPE_RSA2,
				icbcConfigProperties.getAppPrivateKey(), IcbcConstants.CHARSET_UTF8, IcbcConstants.FORMAT_JSON,
				icbcConfigProperties.getApigwPublicKey(), IcbcConstants.ENCRYPT_TYPE_AES, icbcConfigProperties.getAesKey(), null, null);
		JftApiPayrollQueryDetailRequestV1 request = new JftApiPayrollQueryDetailRequestV1();
		request.setServiceUrl("https://apipcs3.dccnet.com.cn/api/jft/api/payroll/querydetail/V1");
		JftApiPayrollQueryDetailRequestV1.JftApiPayrollQueryDetailRequestV1Biz bizContent = new
				JftApiPayrollQueryDetailRequestV1.JftApiPayrollQueryDetailRequestV1Biz();
		bizContent.setAppId(icbcConfigProperties.getAppId());
		bizContent.setOutVendorId(icbcConfigProperties.getCompanyNo());
		bizContent.setType("2");
		//批次号，appSerialno为空时，必输
		bizContent.setAppBatserialno("21900");
		//内部批次号，组成规则：批次号-顺序号，appBatserialno为空时必输
//		bizContent.setAppSerialno("230921");
		bizContent.setStartId("0");
		bizContent.setEndId("10");
		request.setBizContent(bizContent);
		try {
			JftApiPayrollQueryDetailResponseV1 responseV1 = client.execute(request);
			if (Common.isNotNull(responseV1)) {
				return R.ok();
			}
		} catch (IcbcApiException e) {
			e.printStackTrace();
			return R.failed();
		}
		return R.ok();
	}

	public String getMdCode(String filePath) {
		try {
			MessageDigest md5Digest = MessageDigest.getInstance("MD5");
			byte[] buffer = new byte[8192];

			try (FileInputStream fis = new FileInputStream(filePath)) {
				int bytesRead;
				while ((bytesRead = fis.read(buffer)) != -1) {
					md5Digest.update(buffer, 0, bytesRead);
				}
			}
			byte[] md5Bytes = md5Digest.digest();

			// Convert the byte to hex format
			StringBuilder result = new StringBuilder();
			for (byte md5Byte : md5Bytes) {
				result.append(Integer.toString((md5Byte & 0xff) + 0x100, 16).substring(1));
			}

			return result.toString();
		} catch (NoSuchAlgorithmException | IOException e) {
			//Handle the exception according to your requirements
			e.printStackTrace();
			return null;
		}
	}

}
