package com.fs.ybPay.service.impl;

import com.fs.common.utils.spring.SpringUtils;
import com.fs.his.domain.FsPayConfig;
import com.fs.system.domain.SysConfig;
import com.fs.system.mapper.SysConfigMapper;
import com.fs.tzBankPay.form.GetTokenForm;
import com.fs.ybPay.config.PayConfig;
import com.fs.ybPay.domain.*;
import com.fs.ybPay.dto.*;
import com.fs.ybPay.service.IPayService;
import com.google.gson.Gson;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.logging.Logger;

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


@Service
public class PayServiceImpl implements IPayService {
    org.slf4j.Logger logger= LoggerFactory.getLogger(getClass());
    @Override
    public CreateWxOrderResult createWxOrder(WxJspayDTO wxJspayDTO) {
        SysConfigMapper sysConfigMapper= SpringUtils.getBean(SysConfigMapper.class);
        SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey("his.pay");
        FsPayConfig fsPayConfig = new Gson().fromJson(sysConfig.getConfigValue(), FsPayConfig.class);
        String account = fsPayConfig.getYbAccount();
        wxJspayDTO.setAccount(account);
        wxJspayDTO.setAppId(fsPayConfig.getAppId());
        wxJspayDTO.setNotifyUrl(fsPayConfig.getYbNotifyUrl());
        wxJspayDTO.setIsMinipg("1");
        wxJspayDTO.setStoreid("0");
        Map<String, String> map = wxJspayDTO.toMap();
        String responseStr = endPayHttp(PayConfig.wxJspayUrl, map,fsPayConfig.getYbKey());
        CreateWxOrderResult result = GsonFromJson(responseStr, CreateWxOrderResult.class);
        return result;


    }

    //获取订单信息
    @Override
    public OrderResult getOrder(OrderQueryDTO o) {
        o.setIsNeedUpInfo("1");
        SysConfigMapper sysConfigMapper= SpringUtils.getBean(SysConfigMapper.class);
        SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey("his.pay");
        FsPayConfig fsPayConfig = new Gson().fromJson(sysConfig.getConfigValue(), FsPayConfig.class);
        String account = fsPayConfig.getYbAccount();
        o.setAccount(account);
        Map<String, String> map = o.toMap();
        String responseStr = endPayHttp(PayConfig.orderQueryUrl, map,fsPayConfig.getYbKey());


        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 CreateZfbOrderResult createZfbOrder(ZfbJspayDTO zfbJspayDTO) {
        SysConfigMapper sysConfigMapper= SpringUtils.getBean(SysConfigMapper.class);
        SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey("his.pay");
        FsPayConfig fsPayConfig = new Gson().fromJson(sysConfig.getConfigValue(), FsPayConfig.class);
        String account = fsPayConfig.getYbAccount();
        zfbJspayDTO.setAccount(account);
        zfbJspayDTO.setNotifyUrl(fsPayConfig.getYbNotifyUrl());
        Map<String, String> map = zfbJspayDTO.toMap();
        String responseStr = endPayHttp(PayConfig.zfbUnifiedorderUrl, map,fsPayConfig.getYbKey());
        CreateZfbOrderResult result = GsonFromJson(responseStr, CreateZfbOrderResult.class);
        return result;
    }
    @Override
    public RefundResult refund(RefundDTO refundDTO) {
        SysConfigMapper sysConfigMapper= SpringUtils.getBean(SysConfigMapper.class);
        SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey("his.pay");
        FsPayConfig fsPayConfig = new Gson().fromJson(sysConfig.getConfigValue(), FsPayConfig.class);
        String account = fsPayConfig.getYbAccount();
        refundDTO.setAccount(account);
        Map<String, String> map = refundDTO.toMap();
        String responseStr = endPayHttp(PayConfig.refundUrl, map,fsPayConfig.getYbKey());
        logger.info("易宝退款:"+refundDTO.getUpOrderId()+responseStr);
        RefundResult result = GsonFromJson(responseStr, RefundResult.class);
        return result;
    }

    //获取退款订单信息
    @Override
    public QueryRefundResult getRefundOrder(RefundOrderDTO o) {
        SysConfigMapper sysConfigMapper= SpringUtils.getBean(SysConfigMapper.class);
        SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey("his.pay");
        FsPayConfig fsPayConfig = new Gson().fromJson(sysConfig.getConfigValue(), FsPayConfig.class);
        String account = fsPayConfig.getYbAccount();
        o.setAccount(account);
        Map<String, String> map = o.toMap();
        String responseStr = endPayHttp(PayConfig.getRefundOrderUrl, map,fsPayConfig.getYbKey());
        System.out.println(responseStr);
        QueryRefundResult queryRefundResult = GsonFromJson(responseStr, QueryRefundResult.class);
        return queryRefundResult;
    }
    //获取退款信息list
    @Override
    public QueryRefundListResult getRefundOrderList(RefundOrderListDTO o) {
        SysConfigMapper sysConfigMapper= SpringUtils.getBean(SysConfigMapper.class);
        SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey("his.pay");
        FsPayConfig fsPayConfig = new Gson().fromJson(sysConfig.getConfigValue(), FsPayConfig.class);
        String account = fsPayConfig.getYbAccount();
        o.setAccount(account);
        Map<String, String> map = o.toMap();
        String responseStr = endPayHttp(PayConfig.getRefundOrderListUrl, map,fsPayConfig.getYbKey());
        QueryRefundListResult queryRefundResult = GsonFromJson(responseStr, QueryRefundListResult.class);
        return queryRefundResult;
    }


}
