package com.qunzhixinxi.hnqz.admin.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.qunzhixinxi.hnqz.admin.entity.dto.CancellationScorePackageDTO;
import com.qunzhixinxi.hnqz.admin.entity.dto.EncryptedInput;
import com.qunzhixinxi.hnqz.admin.entity.dto.ExecSettlementDTO;
import com.qunzhixinxi.hnqz.admin.entity.dto.PickUpScorePackageDTO;
import com.qunzhixinxi.hnqz.admin.entity.dto.ScorePackageDTO;
import com.qunzhixinxi.hnqz.admin.entity.dto.ScorePackageSettleNoteDTO;
import com.qunzhixinxi.hnqz.admin.entity.dto.SettlementDTO;
import com.qunzhixinxi.hnqz.admin.entity.dto.TaxHelperAckBindingCardDTO;
import com.qunzhixinxi.hnqz.admin.entity.dto.TaxHelperAddMemberDTO;
import com.qunzhixinxi.hnqz.admin.entity.dto.TaxHelperAgreementSignatureDTO;
import com.qunzhixinxi.hnqz.admin.entity.dto.TaxHelperBindingCardCodeDTO;
import com.qunzhixinxi.hnqz.admin.entity.dto.TaxHelperTokenDTO;
import com.qunzhixinxi.hnqz.admin.entity.input.TaskSettleBatch;
import com.qunzhixinxi.hnqz.admin.service.PartyAService;
import com.qunzhixinxi.hnqz.common.core.util.R;
import com.qunzhixinxi.hnqz.common.security.annotation.Inner;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.json.Json;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * {@code PartyAController} 甲方控制层
 * <p>
 * 用于甲方接入
 * 1.采用非对称加密和对称加密，不需要登录token；
 * 2.改控制下的所有方法，需要放行，安全性有加密保证；
 *
 * @author Hengchen.Sun
 * @version 1.0.0
 * @date 2021-05-17
 * @since ver.1.0.0
 */
@Inner(value = false)
@RestController
@RequestMapping(value = "/party-a", produces = MediaType.APPLICATION_JSON_VALUE)
@AllArgsConstructor
@Slf4j
public class PartyAController {

	/**
	 * 甲方服务
	 */
	private final PartyAService partyAService;

	/**
	 * 税邦云增员
	 *
	 * @param input 增员DTO
	 * @return 增员操作结果
	 */
	@PostMapping(value = "/tax-helper/member")
	public R<?> taxHelperAddMember(@RequestBody EncryptedInput input) {

		// 获取解密的文档
		String decodeStr = input.getEncryptedInfo();

		// 实际业务处理
		TaxHelperAddMemberDTO taxHelperAddMemberDTO = JSONUtil.toBean(decodeStr, TaxHelperAddMemberDTO.class);
		Map<String, String> result = partyAService.taxHelperAddMember(taxHelperAddMemberDTO);

		if ("2000".equals(result.get("code"))) {
			return R.ok(result);
		} else {
			return R.failed(result);
		}
	}

	// /**
	//  * 用户协议签署
	//  * @param input 加密信息
	//  * @return 签署结果
	//  */
	// @PostMapping(value = "/tax-helper/agreement-signature")
	// public R<?> taxHelperAgreementSignature(@RequestBody EncryptedInput input){
	//
	// 	// 获取解密的文档
	// 	String decodeStr = input.getEncryptedInfo();
	//
	// 	// 实际业务处理
	// 	TaxHelperAgreementSignatureDTO taxHelperAgreementSignatureDTO = JSONUtil.toBean(decodeStr, TaxHelperAgreementSignatureDTO.class);
	// 	Map<String, String> result = partyAService.userAgreementAuthorization(taxHelperAgreementSignatureDTO);
	//
	// 	if ("2000".equals(result.get("code"))) {
	// 		return R.ok(result);
	// 	} else {
	// 		return R.failed(result);
	// 	}
	// }

	/**
	 * 获取税邦云h5认证token
	 *
	 * @param input 获取信息 加密信息输入
	 * @return 结果
	 */
	@PostMapping(value = "/tax-helper/h5-token")
	public R<?> taxHelperH5Token(@RequestBody EncryptedInput input) {

		// 获取解密的文档
		String decodeStr = input.getEncryptedInfo();

		// 实际业务处理
		TaxHelperTokenDTO taxHelperTokenDTO = JSONUtil.toBean(decodeStr, TaxHelperTokenDTO.class);
		Map<String, String> result = partyAService.taxHelperH5Token(taxHelperTokenDTO);

		if ("2000".equals(result.get("code"))) {
			return R.ok(result);
		} else {
			return R.failed(result);
		}
	}

	/**
	 * 税邦云获取用户绑卡验证码
	 *
	 * @param input 加密信息输入
	 * @return 结果
	 */
	@PostMapping(value = "/tax-helper/binding-card-code")
	public R<?> taxHelperBindingCardVerification(@RequestBody EncryptedInput input) {

		// 获取解密的文档
		String decodeStr = input.getEncryptedInfo();

		// 实际业务处理
		TaxHelperBindingCardCodeDTO taxHelperBindingCardCodeDTO = JSONUtil.toBean(decodeStr, TaxHelperBindingCardCodeDTO.class);
		Map<String, String> result = partyAService.taxHelperBindingCardVerification(taxHelperBindingCardCodeDTO);

		if ("2000".equals(result.get("code"))) {
			return R.ok(result);
		} else {
			return R.failed(result);
		}
	}

	/**
	 * 税邦云绑卡确认
	 *
	 * @param input 加密信息输入
	 * @return 结果
	 */
	@PostMapping(value = "/tax-helper/ack-binding-card")
	public R<?> taxHelperAckBindingCard(@RequestBody EncryptedInput input) {
		// 获取解密的文档
		String decodeStr = input.getEncryptedInfo();

		// 实际业务处理
		TaxHelperAckBindingCardDTO taxHelperAckBindingCardDTO = JSONUtil.toBean(decodeStr, TaxHelperAckBindingCardDTO.class);
		Map<String, String> result = partyAService.taxHelperAckBindingCard(taxHelperAckBindingCardDTO);

		if ("2000".equals(result.get("code"))) {
			return R.ok(result);
		} else {
			return R.failed(result);
		}
	}

	/**
	 * 创建积分包
	 *
	 * @param input 加密信息输入
	 * @return 结果
	 */
	@PostMapping("/score-package")
	public R<?> createScorePackage(@RequestBody EncryptedInput input) {
		// 获取解密的文档
		String decodeStr = input.getEncryptedInfo();

		// 实际业务处理
		ScorePackageDTO scorePackageDTO = JSONUtil.toBean(decodeStr, ScorePackageDTO.class);
		Map<String, String> result = partyAService.createScorePackage(scorePackageDTO);

		if ("2000".equals(result.get("code"))) {
			return R.ok(result);
		} else {
			return R.failed(result);
		}
	}

	/**
	 * 领取积分包
	 *
	 * @param input 加密信息输入
	 * @return 结果
	 */
	@PutMapping(value = "/score-package/pick-up")
	public R<?> pickUpScorePackage(@RequestBody EncryptedInput input) {
		// 获取解密文档
		String decodeStr = input.getEncryptedInfo();

		// 实际业务处理
		PickUpScorePackageDTO pickUpScorePackageDTO = JSONUtil.toBean(decodeStr, PickUpScorePackageDTO.class);
		Map<String, String> result = partyAService.pickUpScorePackage(pickUpScorePackageDTO);

		if ("2000".equals(result.get("code"))) {
			return R.ok(result);
		} else {
			return R.failed(result);
		}
	}

	/**
	 * 关闭积分包
	 *
	 * @param input 加密信息输入
	 * @return 结果
	 */
	@DeleteMapping("/score-package/cancellation")
	public R<?> cancelScorePackage(@RequestBody EncryptedInput input) {
		// 获取解密文档
		String decodeStr = input.getEncryptedInfo();

		// 实际业务处理
		CancellationScorePackageDTO cancellationScorePackageDTO = JSONUtil.toBean(decodeStr, CancellationScorePackageDTO.class);
		Map<String, String> result = partyAService.cancelScorePackage(cancellationScorePackageDTO);

		if ("2000".equals(result.get("code"))) {
			return R.ok(result);
		} else {
			return R.failed(result);
		}
	}

	/**
	 * 上传证据链
	 *
	 * @param input 加密信息输入
	 * @return 结果
	 */
	@PostMapping("/score-package/evidence")
	public R<?> submitEvidence(@RequestBody EncryptedInput input) {
		// 获取解密文档
		String decodeStr = input.getEncryptedInfo();

		// 实际业务处理
		TaskSettleBatch taskSettleBatch = JSONUtil.toBean(decodeStr, TaskSettleBatch.class);
		Map<String, String> result = partyAService.submitEvidence(taskSettleBatch);

		if ("2000".equals(result.get("code"))) {
			return R.ok(result);
		} else {
			return R.failed(result);
		}
	}

	/**
	 * 预结算
	 *
	 * @param input 加密信息输入
	 * @return 结果
	 */
	@PostMapping(value = "/settlement-note")
	public R<?> submitSettlement(@RequestBody EncryptedInput input) {
		// 获取解密文档
		String decodeStr = input.getEncryptedInfo();

		// 实际业务处理
		ScorePackageSettleNoteDTO scorePackageSettleNoteDTO = JSONUtil.toBean(decodeStr, ScorePackageSettleNoteDTO.class);
		Map<String, String> result = partyAService.submitSettlement(scorePackageSettleNoteDTO);

		if ("2000".equals(result.get("code"))) {
			return R.ok(result);
		} else {
			return R.failed(result);
		}
	}


	/**
	 * 结算
	 * @param input 加密信息输入
	 * @return 结果
	 */
	@PostMapping(value = "/settlement")
	public R<?> doSettlement(@RequestBody EncryptedInput input){
		// 获取解密文档
		String decodeStr = input.getEncryptedInfo();

		// 实际业务处理
		JSONArray jsonArray = JSONUtil.parseArray(decodeStr);
		List<ExecSettlementDTO> execSettlementDTOList =
				jsonArray.stream().map(json -> JSONUtil.toBean(JSONUtil.parseObj(json),ExecSettlementDTO.class))
						.collect(Collectors.toList());
		Map<String, String> result = partyAService.doSettlement(execSettlementDTOList);

		if ("2000".equals(result.get("code"))) {
			return R.ok(result);
		} else {
			return R.failed(result);
		}
	}


}
