package org.springblade.gateway.shop_gateway.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.enums.ResCode;
import org.springblade.common.utils.OtpUtils;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tenant.annotation.TenantIgnore;
import org.springblade.core.tool.api.R;
import org.springblade.gateway.shop_gateway.entity.vo.BalanceAccountInfoVO;
import org.springblade.gateway.shop_gateway.service.IAppShopService;
import org.springblade.ldt.shop.entity.Shop;
import org.springblade.ldt.shop.service.IShopService;
import org.springblade.payment.handle.Trade;
import org.springblade.payment.handle.entity.Order;
import org.springblade.gateway.shop_gateway.entity.dto.CodeOrder;
import org.springblade.ldt.user.entity.LoginUser;
import org.springblade.ldt.user.service.ILoginUserService;
import org.springblade.yeePay.common.YeePayConst;
import org.springblade.yeePay.common.YeepayApiConstant;
import org.springblade.yeePay.entity.saas.settlement.SettleBalanceQueryDto;
import org.springblade.yeePay.service.YeepaySaasService;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;

/**
 * @author: lianghanqiang
 * @description:
 * @since: 8/25/21 -- 6:23 PM
 */
@RestController
@RequestMapping("/shopTrade")
@Api(tags = "小B端交易模块")
@AllArgsConstructor
public class ShopTradeController {

	private ILoginUserService loginUserService;
	private Trade trade;
	private IAppShopService appShopService;

	@ApiOperation("授权码下单")
	@PostMapping("initOrderByCode")
	@TenantIgnore
	public R initOrderByCode(@RequestBody CodeOrder codeOrder){

		//解析校验授权码
		Map<String, String> decodeParams = OtpUtils.decodeParams(codeOrder.getAuthCode());

		Assert.isTrue(OtpUtils.validate(decodeParams.get(OtpUtils.PARAMS_ID),decodeParams.get(OtpUtils.PARAMS_SECRET)),
			()->{ throw new ServiceException(ResCode.SECRET_VALIDATE_ERROR); });

		LoginUser loginUser = loginUserService.getById(decodeParams.get(OtpUtils.PARAMS_ID));
		Assert.notNull(loginUser,() -> {throw  new ServiceException(ResCode.USER_NOT_FOUNT);});


		//处理订单
		Order order = Order.builder()
			.appId(codeOrder.getAppId())
			.billsTitle(codeOrder.getBillsTitle())
			.loginUser(loginUser)
			.money(codeOrder.getMoney())
			.expireTime(codeOrder.getExpireTime())
			.secret(codeOrder.getSecret())
			.mallId(codeOrder.getMallId())
			.shopId(codeOrder.getShopId())
			.openId(codeOrder.getOpenId())
			.build();


		try {
			trade.tradeForScanPay(order,codeOrder.getChannelId());
		} catch (Exception e) {
			e.printStackTrace();
			throw  new ServiceException(ResCode.TRADE_ERROR);
		}

		return R.success("交易成功！");

	}

	@ApiOperation("商户余额账户查询")
	@GetMapping("/balanceQuery")
	public R balanceQuery(Long shopId){
		BalanceAccountInfoVO balanceAccountInfoVO=appShopService.queryBalance(shopId);
		return R.data(balanceAccountInfoVO);
	}

	public static void main(String[] args) {
		System.out.println(OtpUtils.validate("1453916897006411778","402800"));
	}

}
