package org.springblade.gateway.point_gateway.controller;

import cn.hutool.json.JSONUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.bcel.verifier.statics.LONG_Upper;
import org.springblade.common.utils.LogisticsQuery;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.log.logger.BladeLogger;
import org.springblade.core.tool.api.R;
import org.springblade.gateway.point_gateway.service.PointExchangeService;
import org.springblade.sing.yeepay.entity.YeepayOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;

/**
 * @author cy-computer
 */
@RestController
@RequestMapping("app/point")
@Api(value = "积分兑换", tags = "积分兑换接口")
@AllArgsConstructor
@Slf4j
public class PointExchangeController {

	private PointExchangeService pointExchangeService;
	private LogisticsQuery logisticsQuery;
	private final BladeLogger bladeLogger;

	/**
	 * 积分兑换商品
	 * @param type
	 * @param phone
	 * @param num
	 * @param goodsId
	 * @param addressId
	 * @return
	 */
	@PostMapping("/exchange")
	@ApiOperation("积分兑换")
	public R<Object> exchange(@ApiParam(value = "兑换类型",required = true) @RequestParam Integer type,
							   @ApiParam(value = "手机号",required = true) @RequestParam String phone,
							   @ApiParam(value = "openId",required = true) @RequestParam String openId,
							   @ApiParam(value = "活动id",required = true) @RequestParam Long activeId,
							   @ApiParam(value = "兑换数量",required = true) @RequestParam Integer num,
							   @ApiParam(value = "商品ID",required = true) @RequestParam Long goodsId,
							   @ApiParam(value = "收获地址ID",required = false) @RequestParam Long addressId,
							   BigDecimal point) throws Exception {
		YeepayOrder yeepayOrder = new YeepayOrder(){{
			setNum(num);
			setPhone(phone);
			setPointGoodsId(goodsId);
			setActiveId(activeId);
			setAddressId(addressId);
			setOpenId(openId);
			setUsePoint(point);
		}};
//		if (type == 1){
//			pointExchangeService.pointExchange(yeepayOrder);
//			return R.data(yeepayOrder);
//		}else if (type == 0){
//			return R.data(pointExchangeService.cashExchange(yeepayOrder));
//		}else if (type == 2){
//			if (point != null){
//				return R.data(pointExchangeService.hybridExchange(yeepayOrder));
//			}
//		}

		return R.data(pointExchangeService.merge(yeepayOrder,type));
	}

	/**
	 * 现金支付回调地址
	 */
	@ApiLog("现金支付回调地址")
	@RequestMapping(value ="/callbackUrl",method = {RequestMethod.GET, RequestMethod.POST})
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "现金支付回调地址")
	public String callbackUrl(HttpServletRequest request) {
		bladeLogger.info("道具订单支付回调", request.getParameterMap().toString());
		log.info("道具订单支付回调:{}", request.getParameterMap().toString());

		//处理现金道具支付回调
		pointExchangeService.goodsHandlerCasePropsOrder(request);
		return "SUCCESS";
	}


	@PostMapping("/queryLogistics")
	@ApiOperation("插叙物流接口")
	public R queryLogistics(@RequestParam String shipperCode,@RequestParam String logisticCode) throws Exception {
		String jsonStr = logisticsQuery.orderOnlineByJson(shipperCode, logisticCode);
		return R.data(JSONUtil.parseObj(jsonStr));
	}
}
