package org.springblade.yeePay.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yeepay.yop.sdk.service.common.response.YopResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.common.cache.PlatformCache;
import org.springblade.common.config.yeepay.OpenProduct;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.log.logger.BladeLogger;
import org.springblade.core.tool.api.R;
import org.springblade.yeePay.common.YeePayConst;
import org.springblade.yeePay.entity.ConfigAppDto;
import org.springblade.yeePay.entity.InitOrderDto;
import org.springblade.yeePay.entity.saas.*;
import org.springblade.yeePay.entity.saas.account.*;
import org.springblade.yeePay.entity.saas.changeservice.ProductFeeModifyDto;
import org.springblade.yeePay.entity.saas.changeservice.ProductFeeQueryDto;
import org.springblade.yeePay.entity.saas.receipt.BillTradeDayDownloadDto;
import org.springblade.yeePay.entity.saas.settlement.SettleBalanceQueryDto;
import org.springblade.yeePay.entity.saas.settlement.SettleSelfSettleApplyDto;
import org.springblade.yeePay.entity.saas.trade.TradeOrderQueryDto;
import org.springblade.yeePay.service.YeepayCommonService;
import org.springblade.yeePay.service.YeepaySaasService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author: lianghanqiang
 * @description: 易宝支付控制层
 * @since: 9/6/21 -- 2:18 PM
 */
@RestController
@RequestMapping("/yeepay")
@Api(tags = "易宝支付")
@AllArgsConstructor
public class YeepayController {
	private YeepaySaasService yeepaySaasService;
	private YeePayConst yeePayConst;
	private YeepayCommonService yeepayCommonService;
	private BladeLogger logger;

	@PostMapping("/invite")
	@ApiOperation("商户入驻")
	public R invite(@RequestBody InvitationDto invitationDto) {
		return R.data(yeepaySaasService.inviteMerchant(invitationDto));
	}

	@PostMapping("/inviteMicro")
	@ApiOperation("小微入驻")
	public R inviteMicro(@RequestBody MicroInvitationDto microInvitationDto) {
		return R.data(yeepaySaasService.inviteMicro(microInvitationDto));
	}

	@PostMapping("wechatApply")
	@ApiOperation("微信实名认证")
	public R wechatApply(@RequestBody WechatApplyDto wechatApplyDto) {
		return R.data(yeepaySaasService.wechatApply(wechatApplyDto));
	}

	@PostMapping("querywechatApply")
	@ApiOperation("微信实名认证查询")
	public R querywechatApply(WechatAuthDto wechatAuthDtow) {
		return R.data(yeepaySaasService.wechatAuthQuery(wechatAuthDtow));
	}

	@PostMapping("cancelWechatApply")
	@ApiOperation("撤销微信实名认证")
	public R cancelWechatApply(WechatAuthDto wechatAuthDtow) {
		return R.data(yeepaySaasService.wechatAuthCancel(wechatAuthDtow));
	}

	@PostMapping("queryShopStatus")
	@ApiOperation("商户开户意愿查询")
	public R queryShopStatus(AuthStateQueryDto authStateQueryDto) {
		return R.data(yeepaySaasService.authStateQuery(authStateQueryDto));
	}

	@PostMapping("queryInvitation")
	@ApiOperation("入驻查询")
	public R queryInvitation(String requestId) {
		return R.data(yeepaySaasService.queryInvitation(requestId));
	}

	@PostMapping("orderInit")
	@ApiOperation("聚合支付下单")
	public R orderInit(@RequestBody InitOrderDto initOrderDto) {
		return R.data(yeepaySaasService.aggPrepay(initOrderDto));
	}

	@PostMapping("payLinkOrder")
	@ApiOperation("聚合码支付下单")
	public R payLinkOrder(@RequestBody PayLinkOrderDto payLinkOrderDto) {
		return R.data(yeepaySaasService.payLinkOrder(payLinkOrderDto));
	}

	@PostMapping("configApp")
	@ApiOperation("配置小程序")
	public R configApp(@RequestBody ConfigAppDto configAppDto) {
		return R.data(yeepayCommonService.configApp(configAppDto));
	}

	@GetMapping("queryAppStatus")
	@ApiOperation("查询小程序配置信息")
	public R queryAppStatus(String appIdType, String parentMerchantNo, String merchantNo) {
		return R.data(yeepayCommonService.queryApp(parentMerchantNo, merchantNo, appIdType));
	}

	@PostMapping("/upload")
	@ApiOperation("上传文件")
	public R upload(MultipartFile file) {
		return R.data(yeepayCommonService.addFileToYeepay(file));
	}

	@GetMapping("/queryBalance")
	@ApiOperation("账户余额查询")
	public JSONObject queryBalance(String merchant) {
		return JSON.parseObject(yeepaySaasService.accountInfosQuery(merchant).getStringResult());
	}


	@PostMapping("/withdrawCardBind")
	@ApiOperation("银行卡绑卡")
	public R withdrawCardBind(@RequestBody WithdrawCardBindDto withdrawCardBindDto) {
		return R.data(yeepaySaasService.withdrawCardBind(withdrawCardBindDto));
	}

	@GetMapping("/withdrawCardQuery")
	@ApiOperation("提现卡查询")
	public R withdrawCardQuery(String merchantNo) {
		return R.data(yeepaySaasService.withdrawCardQuery(merchantNo));
	}

	@GetMapping("/settleBalanceQuery")
	@ApiOperation("待结算金额查询")
	public R settleBalanceQuery(SettleBalanceQueryDto settleBalanceQueryDto) {
		return R.data(yeepaySaasService.settleBalanceQuery(settleBalanceQueryDto));
	}

	@PostMapping("/withdraw")
	@ApiOperation("提现")
	public R withdraw(WithdrawOrderDto withdrawOrderDto) {
		return R.data(yeepaySaasService.withdrawOrder(withdrawOrderDto));
	}

	@PostMapping("/withdrawQuery")
	@ApiOperation("提现查询")
	public R withdrawQuery(WithdrawQueryDto withdrawQueryDto) {
		return R.data(yeepaySaasService.withdrawQuery(withdrawQueryDto));
	}

	@PostMapping("/settle")
	@ApiOperation("结算")
	public R settle(SettleSelfSettleApplyDto settleSelfSettleApplyDto) {
		return R.data(yeepaySaasService.settleSelfSettleApply(settleSelfSettleApplyDto));
	}

	@GetMapping("/feeQuery")
	@ApiOperation("产品费率查询")
	public JSONObject feeQuery(ProductFeeQueryDto productFeeQueryDto) {
		return JSON.parseObject(yeepaySaasService.productFeeQuery(productFeeQueryDto).getStringResult());
	}

	@GetMapping("/feeUpdate")
	@ApiOperation("产品费率修改")
	public JSONObject feeUpdate(ProductFeeModifyDto productFeeModifyDto) {
		return JSON.parseObject(yeepaySaasService.productFeeModify(productFeeModifyDto).getStringResult());
	}

	@PostMapping("/transfer")
	@ApiOperation("商户转账")
	public JSONObject transfer(TransferOrderDto transferOrderDto) {
		return JSON.parseObject(yeepaySaasService.transferOrder(transferOrderDto).getStringResult());
	}

	@PostMapping("/reCharge")
	@ApiOperation("充值")
	public JSONObject reCharge(OnlineBankRechargeOrderDto onlineBankRechargeOrderDto) {
		return JSON.parseObject(yeepaySaasService.onlineBankRechargeOrder(onlineBankRechargeOrderDto).getStringResult());
	}

	@PostMapping("/queryOrder")
	@ApiOperation("查询订单")
	@ApiLog
	public JSONObject queryOrder(TradeOrderQueryDto orderQueryDto) {
		return JSON.parseObject(yeepaySaasService.tradeOrderQuery(orderQueryDto).getStringResult());
	}

	@GetMapping("/bill/tradedaydownload")
	@ApiOperation("对账")
	public YopResponse tradedaydownload(BillTradeDayDownloadDto billTradeDayDownloadDto) {
		return yeepaySaasService.billTradeDayDownload(billTradeDayDownloadDto);
	}

	@GetMapping("/test")
	@ApiOperation("测试")
	@ApiLog
	public R test() {
		logger.info("log", "asdf");
		return R.success("s");
	}

	public static void main(String[] args) {
		List<InvitationDto.ProductInfo> productInfos = new ArrayList<>(32);
		Arrays.stream(OpenProduct.MICRO_PRODUCTS).forEach(item -> {
			InvitationDto.ProductInfo productInfo = PlatformCache.productInfo(item);
			productInfo.setProductName(null);
			productInfos.add(productInfo);
		});

	}
}
