package com.yifu.cloud.plus.v1.yifu.social.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yifu.cloud.plus.v1.yifu.common.core.exception.CheckedException;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: hgw
 * @Date: 2024-12-6 17:01:23
 * @Description:
 * @return: 税友配置
 **/
@Configuration
@Data
@Slf4j
public class SocialFriendConfig {

	// appKey
	@Value("${socialFriend.appKey}")
	private String appKey;
	// appSecret
	@Value("${socialFriend.appSecret}")
	private String appSecret;
	// url前缀
	@Value("${socialFriend.urlPre}")
	private String urlPre;
	// url推
	@Value("${socialFriend.urlPush}")
	private String urlPush;
	// url拉结果数据
	@Value("${socialFriend.urlGetInfo}")
	private String urlGetInfo;

	/**
	 * @Description: 4：推送任务接口
	 * @Author: hgw
	 * @Date: 2024-5-8 15:07:45
	 * @return: java.lang.String
	 **/
	public String getFourAppAdd(RestTemplate restTemplate) {
		String addId = "";
		String appAddUrl = urlPre + urlPush;
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		headers.add("accept", MediaType.TEXT_PLAIN.toString());
		headers.add("appKey", appKey);
		headers.add("appSecret", appSecret);
		
		JSONObject json = new JSONObject();
		json.put("bizNo", "e5150440add340158e6b258bf8c60e7c");
		json.put("qymc", "安徽测试企业");
		json.put("nsrsbh", "91340100634561365L");
		json.put("areaid", "340100");
		JSONArray ywblzhlbArr = new JSONArray();
		JSONObject jsonYwblzhlbYgs = new JSONObject();
		jsonYwblzhlbYgs.put("mm", "Aa123456");
		jsonYwblzhlbYgs.put("blxm", "社保");
		jsonYwblzhlbYgs.put("dwbh", "123456");
		jsonYwblzhlbYgs.put("zh", "1234567");
		jsonYwblzhlbYgs.put("mmlx", "4");
		JSONObject jsonYwblzhlbYsd = new JSONObject();
		jsonYwblzhlbYsd.put("mm", "Aa123456");
		jsonYwblzhlbYsd.put("blxm", "医保");
		jsonYwblzhlbYsd.put("dwbh", "123456");
		jsonYwblzhlbYsd.put("zh", "1234567");
		jsonYwblzhlbYsd.put("mmlx", "4");
		ywblzhlbArr.add(jsonYwblzhlbYgs);
		ywblzhlbArr.add(jsonYwblzhlbYsd);
		json.put("ywblzhlb", ywblzhlbArr);
		JSONArray cbrymdArr = new JSONArray();
		JSONObject cbrymdOne = new JSONObject();
		cbrymdOne.put("csrq", "2009-08-21");
		cbrymdOne.put("gj", "中国");
		cbrymdOne.put("whcd", "大学本科");
		cbrymdOne.put("sjhm", "10000340997");
		cbrymdOne.put("xb", "男");
		cbrymdOne.put("hkszdxzq", "浙江省|杭州市|滨江区");
		cbrymdOne.put("mz", "汉族");
		cbrymdOne.put("cjgzrq", "2017-06-14");
		JSONArray blxmlbArr = new JSONArray();
		JSONObject blxmlbOne = new JSONObject();
		String[] strArr = {"企业职工养老"};
		blxmlbOne.put("xzlb", strArr);
		blxmlbOne.put("blxm", "社保");
		blxmlbOne.put("bccbrq", "2024-05-30");
		blxmlbOne.put("sbgz", "4000");
		JSONObject blxmlbTwo = new JSONObject();
		String[] strArrTwo = {"生育保险"};
		blxmlbTwo.put("xzlb", strArrTwo);
		blxmlbTwo.put("blxm", "医保");
		blxmlbTwo.put("bccbrq", "2024-04-30");
		blxmlbTwo.put("sbgz", "4000");
		blxmlbArr.add(blxmlbOne);
		blxmlbArr.add(blxmlbTwo);
		cbrymdOne.put("blxmlb", blxmlbArr);
		cbrymdOne.put("jzdz", "滨江区南环路3738号");
		cbrymdOne.put("xm", "测试金辰");
		cbrymdOne.put("zzlx", "居民身份证");
		cbrymdOne.put("zzhm", "828888200908213819");
		cbrymdOne.put("hkxz", "外地居民户口");
		JSONObject tszdxx = new JSONObject();
		tszdxx.put("dwjyqssj", "2028-08-07");
		tszdxx.put("ygxs", "订立固定期限劳动合同");
		tszdxx.put("gzsjzd", "标准工作时间制度");
		tszdxx.put("jydjsj", "2023-08-01");
		tszdxx.put("zygz", "专业技术人员");
		tszdxx.put("ldhtqsqk", "初签");
		tszdxx.put("grsf", "企业管理人员");
		cbrymdOne.put("tszdxx", tszdxx);
		cbrymdOne.put("jzdzxzq", "浙江省|杭州市|滨江区");
		cbrymdOne.put("hkszddz", "滨江区南环路3738号");
		JSONObject jyxx = new JSONObject();
		jyxx.put("ldhtqsrq", "2023-08-20");
		jyxx.put("ldhtjzrq", "2030-12-30");
		jyxx.put("ldhtlx", "固定期限劳动合同");
		cbrymdOne.put("jyxx", jyxx);
		cbrymdArr.add(cbrymdOne);
		json.put("cbrymd", cbrymdArr);
		HttpEntity<String> formEntity = new HttpEntity<>(json.toString(), headers);
		String dataResultList = restTemplate.postForObject(appAddUrl, formEntity, String.class);
		// {"head":{"code":"00000000","description":"成功","msg":"成功","time":"2024-12-06 17:10:26","status":"Y"}
		// ,"body":{"requestId":"85440b327d71466abcd9c5c81d5bb172"}}

		if (Common.isNotNull(dataResultList)) {
			JSONObject dataObject = JSON.parseObject(dataResultList);
			String head = dataObject.getString("head");
			if (Common.isNotNull(head)) {
				JSONObject headObject = JSON.parseObject(head);
				String status = headObject.getString("status");
				if ("Y".equals(status)) {
					String body = dataObject.getString("body");
					JSONObject bodyObject = JSON.parseObject(body);
					addId = bodyObject.getString("requestId");
					System.out.println(addId);
				} else {
					System.out.println("失败");
				}
			}
		}
		return addId;
	}

	/**
	 * @param: fileKey ： resultFile 查看文件； resultAnnex 查看附件
	 * @Description: 5：任务查询接口
	 * @Author: hgw
	 * @Date: 2024-5-8 15:07:45
	 * @return: java.lang.String
	 **/
	public R<String> getFiveJob(RestTemplate restTemplate, String addId, String fileKey) {
		if (Common.isEmpty(addId)) {
			return null;
		}
		String resultFile = null;
		String moduleDetailUrl = urlPre + urlGetInfo;
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
		headers.setContentType(type);
		headers.add("accept", MediaType.TEXT_PLAIN.toString());
		headers.add("appKey", appKey);
		headers.add("appSecret", appSecret);
		HttpEntity<String> formEntity = new HttpEntity<>(headers);
		ResponseEntity response = restTemplate.exchange(moduleDetailUrl, HttpMethod.GET, formEntity, String.class);
		if (Common.isEmpty(response) || Common.isEmpty(response.getBody())) {
			throw new CheckedException("获取税友【任务查询接口】失败");
		}
		int status = response.getStatusCodeValue();
		if (status != 200) {
			throw new CheckedException("获取税友【任务查询接口】失败，status="+status);
		}
		String body = (String) response.getBody();
		JSONObject bodyObject = JSON.parseObject(body);
		JSONObject dataObject = (JSONObject) bodyObject.get("data");
		String statusStr = dataObject.getString("status");
		if (!"完成".equals(statusStr)) {
			return R.failed(statusStr);
		}
		resultFile = dataObject.getString(fileKey);
		if (Common.isEmpty(resultFile)) {
			return R.failed(resultFile);
		}
		return R.ok(resultFile);
	}

}
