package com.cweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.channel.ccb.CCBTradeService;
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.BsPlatform;
import com.hfkj.channel.lakala.LaKaLaTradeService;
import com.hfkj.model.ResponseData;
import com.hfkj.service.BsPlatformService;
import com.hfkj.sysenum.PlatformTypeEnum;
import com.hfkj.sysenum.StoreDiscountActivityTypeEnum;
import com.hfkj.sysenum.UserDiscountTypeEnum;
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 BsPlatformService platformService;

    @Resource
    private LaKaLaTradeService laKaLaTradeService;

    @Resource
    private WeiXinPayService weiXinPayService;

    @Resource
    private CCBTradeService ccbTradeService;

    @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, "");
            }

            // 查询当前支付
            List<BsPlatform> platformList = platformService.getPlatformList();
            for (BsPlatform platform : platformList) {

                if (platform.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());
                    }
                }


                if (platform.getPlatformType().equals(PlatformTypeEnum.type3.getNumber())) {
                    try {
                        Map<String, Object> order = ccbTradeService.b2cPay(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, "");
            }

            // return ResponseMsgUtil.success(weiXinPayService.preorder(body.getString("outTradeNo")));
            // 查询当前支付
            List<BsPlatform> platformList = platformService.getPlatformList();
            for (BsPlatform platform : platformList) {

                if (platform.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());
                    }
                }

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

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


}
