package net.yyc.common.ding.service;


import net.yyc.common.ding.config.DingConfig;
import net.yyc.common.ding.entity.*;
import net.yyc.common.ding.entity.*;
import net.yyc.common.ding.utils.HttpClientUtils;
import net.yyc.common.ding.utils.JsonUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.List;

@RequiredArgsConstructor
@Slf4j
public class DingService {

//	private final DingConfig config;

	/**
	 * 灵工信息认证
	 *
	 * @param
	 * @return
	 * @author zhangwt
	 * @since 1.0
	 * 示例：
	 * {
	 * "mobile": "15000998877",
	 * "name": "张三",
	 * "idType": "1",
	 * "idCard": "350198765623418765"
	 * }
	 */
	public EmployCheckResponse check(DingConfig config, List list) {

		EmployCheckRequest request = new EmployCheckRequest(config, list);
		log.info("人力家请求报文: {}", request.getBody());
		String json = HttpClientUtils.doPost(request);
		log.info("人力家返回数据: {}", json);
		EmployCheckResponse response = JsonUtils.deserializer(json, EmployCheckResponse.class);
		if (!response.isSuccess()) {
			log.error("灵工信息认证失败, code: {}, message: {}", response.getCode(), response.getMsg());
		}
		return response;
	}

	/**
	 * 子账户余额查询
	 *
	 * @param
	 * @return
	 * @author zhangwt
	 * @since 1.0
	 */
	public EmployAccountQueryResponse query(DingConfig config) {

		EmployAccountQueryRequest request = new EmployAccountQueryRequest(config);
		log.info("人力家请求报文: {}", request.getBody());
		String json = HttpClientUtils.doPost(request);
		log.info("人力家返回数据: {}", json);
		EmployAccountQueryResponse response = JsonUtils.deserializer(json, EmployAccountQueryResponse.class);
		if (!response.isSuccess()) {
			log.error("子账户余额查询失败, code: {}, message: {}", response.getCode(), response.getMsg());
		}
		return response;
	}


	/**
	 * 子账户余额查询
	 *
	 * @param subjectName   结算主体
	 * @param statementNo   结算单id
	 * @param statementName 结算单名称
	 * @param bizType       业务类型
	 * @param list
	 * @return 示例：
	 * {
	 * "name": "张三",
	 * "mobile": "15000998877",
	 * "idCard": "350198765623418765"
	 * "amount": "0.22"
	 * }
	 */
	// @Deprecated
	// public EmployPayResponse pay(DingConfig config,String subjectName,
	// 						   String statementNo,
	// 						   String statementName,
	// 						   Integer bizType,
	// 						   List list) {
	//
	// 	EmployPayRequest request = new EmployPayRequest(config, subjectName,
	// 			statementNo,
	// 			statementName,
	// 			bizType,
	// 			list);
	// 	log.info("人力家请求报文: {}",request.getBody());
	// 	String json = HttpClientUtils.doPost(request);
	// 	log.info("人力家返回数据: {}", json);
	// 	EmployPayResponse response = JsonUtils.deserializer(json, EmployPayResponse.class);
	// 	if (!response.isSuccess()) {
	// 		log.error("结算单推送失败, code: {}, message: {}", response.getCode(), response.getMsg());
	// 	}
	// 	return response;
	// }

	/**
	 * 子账户余额查询
	 *
	 * @param subjectName   结算主体
	 * @param statementNo   结算单id
	 * @param statementName 结算单名称
	 * @param bizType       业务类型
	 * @param list
	 * @return 示例：
	 * {
	 * "name": "张三",
	 * "mobile": "15000998877",
	 * "idCard": "350198765623418765"
	 * "amount": "0.22"
	 * }
	 */
	@Deprecated
	public EmployPayResponse payV2(DingConfig config, String subjectName,
                                   String statementNo,
                                   String statementName,
                                   Integer bizType,
                                   List list) {

		EmployPayRequestV2 request = new EmployPayRequestV2(config, subjectName,
				statementNo,
				statementName,
				bizType,
				list);
		log.info("人力家请求报文: {}", request.getBody());
		String json = HttpClientUtils.doPost(request);
		log.info("人力家返回数据: {}", json);
		EmployPayResponse response = JsonUtils.deserializer(json, EmployPayResponse.class);
		if (!response.isSuccess()) {
			log.error("结算单推送失败, code: {}, message: {}", response.getCode(), response.getMsg());
		}
		return response;
	}

	/**
	 * 结算单推送
	 *
	 * @param subjectName   结算主体
	 * @param statementNo   结算单id
	 * @param statementName 结算单名称
	 * @param bizType       业务类型
	 * @param list          自由职业者
	 * @param taskStartTime 任务开始时间
	 * @param taskEndTime   任务结束时间
	 * @return 示例：
	 * {
	 * "name": "张三",
	 * "mobile": "15000998877",
	 * "idCard": "350198765623418765"
	 * "amount": "0.22"
	 * }
	 */
	public EmployPayResponse payV3(DingConfig config, String subjectName,
								   String statementNo,
								   String statementName,
								   Integer bizType,
								   List list,
								   LocalDateTime taskStartTime,
								   LocalDateTime taskEndTime) {

		EmployPayRequestV3 request = new EmployPayRequestV3(
				config,
				subjectName,
				statementNo,
				statementName,
				bizType,
				list,
				taskStartTime,
				taskEndTime);
		log.info("人力家请求报文: {}", request.getBody());
		String json = HttpClientUtils.doPost(request);
		log.info("人力家返回数据: {}", json);
		EmployPayResponse response = JsonUtils.deserializer(json, EmployPayResponse.class);
		if (!response.isSuccess()) {
			log.error("结算单推送失败, code: {}, message: {}", response.getCode(), response.getMsg());
		}
		return response;
	}


	/**
	 * 结算单查询
	 *
	 * @param statementNo 结算单id
	 * @return 查询结果
	 */
	public static EmployFreelancesQueryResponse freelancesQuery(DingConfig config, String statementNo) {
		EmployFreelancesQueryRequest request = new EmployFreelancesQueryRequest(config, statementNo);

		log.info("人力家请求报文: {}", request.getBody());
		String json = HttpClientUtils.doPost(request);
		log.info("人力家返回数据: {}", json);
		EmployFreelancesQueryResponse response = JsonUtils.deserializer(json, EmployFreelancesQueryResponse.class);
		if (!response.isSuccess()) {
			log.error("结算单查询失败, code: {}, message: {}", response.getCode(), response.getMsg());
		}
		return response;
	}

}
