package com.cweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.channel.ccb.CCBTradeService;
import com.hfkj.channel.tianque.service.TianQueTradeService;
import com.hfkj.channel.weixin.WeiXinPayService;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsMerPlatformNo;
import com.hfkj.entity.BsPlatform;
import com.hfkj.channel.lakala.LaKaLaTradeService;
import com.hfkj.entity.BsTradeOrder;
import com.hfkj.model.ResponseData;
import com.hfkj.service.BsMerPlatformNoService;
import com.hfkj.service.BsPlatformService;
import com.hfkj.service.BsTradeOrderService;
import com.hfkj.sysenum.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Controller
@Api(value = "支付管理")
@RequestMapping(value = "/pay")
public class PayController {

    private static Logger log = LoggerFactory.getLogger(PayController.class);
    @Resource
    private BsTradeOrderService tradeOrderService;
    @Resource
    private LaKaLaTradeService laKaLaTradeService;
    @Resource
    private TianQueTradeService tianQueTradeService;
    @Resource
    private WeiXinPayService weiXinPayService;
    @Resource
    private CCBTradeService ccbTradeService;
    @Resource
    private BsMerPlatformNoService merPlatformNoService;

    @RequestMapping(value="/microPay",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "交易【被扫】")
    public ResponseData microPay(@RequestBody JSONObject body) {
        try {

            if (body == null
                    || StringUtils.isBlank(body.getString("outTradeNo"))
                    || StringUtils.isBlank(body.getString("authCode"))) {
                log.error("configStore error!","参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 查询交易
            BsTradeOrder trade = tradeOrderService.getOrderByOutTradeNo(body.getString("outTradeNo"));
            if (trade == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到交易订单");
            }
            if (!trade.getStatus().equals(TradeOrderStatusEnum.status1.getNumber())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "订单不处于待支付状态");
            }
            // 查询交易渠道
            BsMerPlatformNo merPlatform = merPlatformNoService.getCurrentPlatform(trade.getMerId());
            if (merPlatform == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知交易渠道");
            }
            if (merPlatform.getPlatformType().equals(PlatformTypeEnum.type1.getNumber())) {
                try {
                    Map<String, Object> order = laKaLaTradeService.microPay(body.getString("outTradeNo"), body.getString("authCode"));
                    if (StringUtils.isNotBlank(MapUtils.getString(order, "order"))) {
                        return ResponseMsgUtil.success(order);
                    }
                } catch (Exception e) {
                    log.error("拉卡拉支付失败", e.getMessage());
                }
            }
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "支付失败");

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/jsapiPay",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "交易【JSAPI】")
    public ResponseData jsapiPay(@RequestBody JSONObject body) {
        try {
            if (body == null || StringUtils.isBlank(body.getString("outTradeNo"))) {
                log.error("configStore error!","参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 查询交易
            BsTradeOrder trade = tradeOrderService.getOrderByOutTradeNo(body.getString("outTradeNo"));
            if (trade == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到交易订单");
            }
            if (!trade.getStatus().equals(TradeOrderStatusEnum.status1.getNumber())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "订单不处于待支付状态");
            }
            // 查询交易渠道
            BsMerPlatformNo merPlatform = merPlatformNoService.getCurrentPlatform(trade.getMerId());
            if (merPlatform == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知交易渠道");
            }
            if (merPlatform.getPlatformType().equals(PlatformTypeEnum.type1.getNumber())) {
                try {
                    Map<String, Object> order = laKaLaTradeService.preorder(body.getString("outTradeNo"));
                    if (StringUtils.isNotBlank(MapUtils.getString(order, "order"))) {
                        return ResponseMsgUtil.success(order);
                    }
                } catch (Exception e) {
                    log.error("拉卡拉支付失败", e.getMessage());
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, e.getMessage());
                }
            } else if (merPlatform.getPlatformType().equals(PlatformTypeEnum.type5.getNumber())) {
                try {
                    Map<String, Object> order = tianQueTradeService.preorder(body.getString("outTradeNo"));
                    if (StringUtils.isNotBlank(MapUtils.getString(order, "order"))) {
                        return ResponseMsgUtil.success(order);
                    }
                } catch (Exception e) {
                    log.error("随行付支付失败", e.getMessage());
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, e.getMessage());
                }
            }

            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "支付失败");

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }


}
