package com.fs.pay.pay.service.impl;


import com.alibaba.fastjson.JSON;
import com.fs.common.utils.spring.SpringUtils;
import com.fs.huifuPay.sdk.opps.core.config.MerConfig;
import com.fs.pay.pay.config.PayConfig;
import com.fs.pay.pay.domain.*;
import com.fs.pay.pay.dto.*;
import com.fs.pay.pay.service.PayService;
import com.fs.store.domain.FsPayConfig;
import com.fs.system.domain.SysConfig;
import com.fs.system.mapper.SysConfigMapper;
import com.google.gson.Gson;
import org.json.JSONObject;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.Map;

import static com.fs.pay.pay.util.PayUtil.*;


@Service
public class PayApiServiceImpl implements PayService {

    @Override
    public CreateWxOrderResult createWxOrder(WxJspayDTO wxJspayDTO) {
        FsPayConfig payConfig = getPayConfig();
        wxJspayDTO.setAccount(payConfig.getYbAccount());
        wxJspayDTO.setAppId(payConfig.getAppId());
        wxJspayDTO.setNotifyUrl(payConfig.getYbNotifyUrl());
        Map<String, String> map = wxJspayDTO.toMap();
        String responseStr = endPayHttp(PayConfig.wxJspayUrl, map);
        if (isSgin(responseStr)){
            //请求失败101 或者验签失败
        }
        CreateWxOrderResult result = GsonFromJson(responseStr, CreateWxOrderResult.class);
        return result;


    }
    @Override
    public CreateZfbOrderResult createZfbOrder(ZfbJspayDTO zfbJspayDTO) {
        FsPayConfig payConfig = getPayConfig();
        zfbJspayDTO.setAccount(payConfig.getYbAccount());
        zfbJspayDTO.setNotifyUrl(payConfig.getYbNotifyUrl());
        Map<String, String> map = zfbJspayDTO.toMap();
        String responseStr = endPayHttp(PayConfig.zfbUnifiedorderUrl, map);
        if (isSgin(responseStr)){
            //请求失败101 或者验签失败
        }
        CreateZfbOrderResult result = GsonFromJson(responseStr, CreateZfbOrderResult.class);
        return result;
    }
    //获取订单信息
    @Override
    public OrderResult getOrder(OrderQueryDTO o) {
        FsPayConfig payConfig = getPayConfig();
        o.setIsNeedUpInfo("1");
        o.setAccount(payConfig.getYbAccount());
        Map<String, String> map = o.toMap();
        String responseStr = endPayHttp(PayConfig.orderQueryUrl, map);
            if (isSgin(responseStr)){
                //请求失败101 或者验签失败
            }
        OrderResult orderResponse = GsonFromJson(responseStr, OrderResult.class);
            if (orderResponse.getStatus().equals("100")){
                if (orderResponse.getState().equals("0")|| orderResponse.getState().equals("5")||orderResponse.getState().equals("6")  ){
                    String upInfo = new JSONObject(responseStr).getString("upInfo");
                    orderResponse.setBankOrderId(new JSONObject(upInfo).getString("bankOrderId"));
                    orderResponse.setBankTrxId(new JSONObject(upInfo).getString("bankTrxId"));
                }
            }

        return orderResponse;
    }

    @Override
    public RefundResult refund(RefundDTO refundDTO) {
        FsPayConfig payConfig = getPayConfig();
        refundDTO.setAccount(payConfig.getYbAccount());
        Map<String, String> map = refundDTO.toMap();
        String responseStr = endPayHttp(PayConfig.refundUrl, map);
        if (isSgin(responseStr)){
            //请求失败101 或者验签失败
        }
        RefundResult result = GsonFromJson(responseStr, RefundResult.class);
        return result;
    }

    //获取退款订单信息
    @Override
    public QueryRefundResult getRefundOrder(RefundOrderDTO o) {
        FsPayConfig payConfig = getPayConfig();
        o.setAccount(payConfig.getYbAccount());
        Map<String, String> map = o.toMap();
        String responseStr = endPayHttp(PayConfig.getRefundOrderUrl, map);
        if (isSgin(responseStr)){
            //请求失败101 或者验签失败
        }
        QueryRefundResult queryRefundResult = GsonFromJson(responseStr, QueryRefundResult.class);
        return queryRefundResult;
    }
    //获取退款信息list
    @Override
    public QueryRefundListResult getRefundOrderList(RefundOrderListDTO o) {
        FsPayConfig payConfig = getPayConfig();
        o.setAccount(payConfig.getYbAccount());
        Map<String, String> map = o.toMap();
        String responseStr = endPayHttp(PayConfig.getRefundOrderListUrl, map);

        if (isSgin(responseStr)){
            //请求失败101 或者验签失败
        }
        QueryRefundListResult queryRefundResult = GsonFromJson(responseStr, QueryRefundListResult.class);
        return queryRefundResult;
    }
    public FsPayConfig getPayConfig(){
        SysConfigMapper sysConfigMapper= SpringUtils.getBean(SysConfigMapper.class);
        SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey("store.pay");
        return  JSON.parseObject(sysConfig.getConfigValue(), FsPayConfig.class);
    }

}
