package org.springblade.yeePay.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springblade.common.annotation.AccessLimit;
import org.springblade.core.tool.api.R;
import org.springblade.yeePay.entity.*;
import org.springblade.yeePay.service.YeePayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/yeePay")
@Api(tags = "易宝支付模块")
@Validated
public class YeePayController {

	@Autowired
	YeePayService yeePayService;

	/**
	 * 统一下单接口
	 * @param initOrderDto 下单信息
	 */
	@PostMapping("initOrder")
	@ApiOperation("统一下单接口")
	public R initOrder(@RequestBody @Valid InitOrderDto initOrderDto){
		return yeePayService.initOrder(initOrderDto);
	}

	/**
	 * 公众号配置接口
	 * @param ConfigAppDto 公众号配置信息
	 */
	@PostMapping("configApp")
	@ApiOperation("公众号配置接口")
	public R configApp(@RequestBody @Valid ConfigAppDto ConfigAppDto){
		return yeePayService.configApp(ConfigAppDto);
	}

	/**
	 * 查询公众号配置接口
	 */
	@GetMapping("queryApp")
	@ApiOperation("查询公众号配置接口")
	public R configApp(@RequestParam(required = false) String appIdType){
		return yeePayService.queryApp(appIdType);
	}

	/**
	 *	代付：交易到银行卡(单笔出款)
	 * @param payOrderDto 交易信息
	 * */
	@PostMapping("transferToCard")
	@ApiOperation("代付：交易到银行卡(单笔出款)")
	public R transferToCard(@RequestBody @Valid PayOrderDto payOrderDto, String parentMerchantNo){
		return yeePayService.payOrder(payOrderDto, parentMerchantNo);
	}

	/**
	 *	代付：转账到银行卡(批量出款)  ---采购商批量付款专用
	 * @param batchTransferDto 转账信息
	 * */
	@PostMapping("batchTransfer")
	@ApiOperation("代付：转账到银行卡(批量出款)")
	public R batchTransfer(@RequestBody @Valid BatchTransferDto batchTransferDto){
		return yeePayService.batchTransferToCard(batchTransferDto);
	}


	/**
	 * 	转账订单查询
	 * @param requestNo 易宝订单号
	 * */
	@GetMapping("queryTransferOrder")
	@ApiOperation("转账订单查询")
	public R queryTransferOrder(@RequestParam @ApiParam(value = "请求订单号",required = true) String requestNo ){
		return yeePayService.queryTransferOrder(requestNo);
	}

	/**
	 * 支付回调接口（支付结果通知）
	 */
	@PostMapping("notify")
	@ApiOperation("支付回调接口（支付结果通知）")
	public R orderNotify(@RequestParam(required = false) Map<String,Object> params){
		yeePayService.orderNotify(params);
		return R.success("操作成功！");
	}

	/**
	 * 转账回调接口（转账结果通知）
	 */
	@PostMapping("/transfer/notify")
	@ApiOperation("转账回调接口（转账结果通知）")
	public R transferNotify(@RequestParam(required = false) Map<String,Object> params){
		yeePayService.transferNotify(params);
		return R.success("操作成功！");
	}


	/**
	 * 转账回调接口（转账结果通知 批量）
	 */
	@PostMapping("/batchTransfer/notify")
	@ApiOperation("转账回调接口（转账结果通知 批量）")
	public R batchTransferNotify(@RequestParam(required = false) Map<String,Object> params){
		yeePayService.batchTransferNotify(params);
		return R.success("操作成功！");
	}

	/**
	 * 账户余额查询
	 */
	@GetMapping("queryBalance")
	@ApiOperation("账户余额查询")
	public R queryBalance(@RequestParam(required = true) String merchantNo){
		return yeePayService.queryBalance(merchantNo);
	}


	/**
	 *	网关：充值
	 * @param payGateWayDto 充值信息
	 * */
	@PostMapping("/payByGateway")
	@ApiOperation("网关：充值")
	@AccessLimit(second = 2, maxCount = 1)
	public R payByGateway(@RequestBody @Valid PayGateWayDto payGateWayDto){
		return yeePayService.payByGateWay(payGateWayDto);
	}

	/**
	 *	分润回调
	 * @param params 分润信息
	 * */
	@PostMapping("/payByBonus/notify")
	@ApiOperation("分润回调")
	public R payByBonus(@RequestParam(required = false) Map<String,Object> params){
		yeePayService.bonusNotify(params);
		return R.success("操作成功！");
	}

	/**
	 * 网关充值回调接口（充值结果通知）
	 */
	@PostMapping("/gateway/notify")
	@ApiOperation("网关充值回调接口（充值结果通知）")
	public R gatewayNotify(@RequestParam(required = false) Map<String,Object> params){
		yeePayService.gatewayNotify(params);
		return R.success("操作成功！");
	}


	//------------------------------------------2.0版本--------------------------------------------//

	/**
	 *	商户进件回调
	 * @param params
	 * */
	@PostMapping("/merchant/register/notify")
	@ApiOperation("商户进件回调")
	public R merchantRegisterNotify(@RequestParam(required = false) Map<String,Object> params){
		yeePayService.merchantRegisterNotify(params);
		return R.success("操作成功！");
	}

	/**
	 *	小微用户进件回调
	 * @param params
	 * */
	@PostMapping("/user/register/notify")
	@ApiOperation("商户进件回调")
	public R userRegisterNotify(@RequestParam(required = false) Map<String,Object> params){
		yeePayService.userRegisterNotify(params);
		return R.success("操作成功！");
	}

	/**
	 * B2B网银充值回调（充值回调）
	 */
	@PostMapping("/recharge/order/notify")
	public R rechargeNotify(@RequestParam(required = false) Map<String,Object> params){
		yeePayService.rechargeOrderNotify(params);
		return R.success("操作成功！");
	}

	/**
	 * 付款交易回调（代付回调）
	 */
	@PostMapping("/transfer/order/notify")
	public R transferOrderNotify(@RequestParam(required = false) Map<String,Object> params){
		yeePayService.transferOrderNotify(params);
		return R.success("操作成功！");
	}

	/**
	 * 付款交易回调（代付回调）
	 */
	@PostMapping("/pay/order/notify")
	public R payOrderNotify(@RequestParam(required = false) Map<String,Object> params){
		yeePayService.payOrderNotify(params);
		return R.success("操作成功！");
	}

	/**
	 * 提现交易回调（提现回调）
	 */
	@PostMapping("/withdraw/order/notify")
	public R withdrawOrderNotify(@RequestParam(required = false) Map<String,Object> params){
		yeePayService.withdrawOrderNotify(params);
		return R.success("操作成功！");
	}
}
