package com.fs.tzBankPay.TzBankService.TzBankServiceImpl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
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.TzBankService.TzBankService;
import com.fs.tzBankPay.util.TzBankSendUtil;
import com.fs.tzBankPay.config.TzBankConfig;
import com.fs.tzBankPay.doman.*;
import com.fs.common.core.redis.RedisCache;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TzBankServiceImpl implements TzBankService {
    TzBankSendUtil  tzBankSendUtil =new TzBankSendUtil();
    Logger logger= LoggerFactory.getLogger(getClass());
    @Autowired
    RedisCache redisCache;
    public TzBankResult<PayCreateOrderResult> createOrder(PayCreateOrder order){
        SysConfigMapper sysConfigMapper= SpringUtils.getBean(SysConfigMapper.class);
        SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey("his.pay");
        FsPayConfig fsPayConfig = new Gson().fromJson(sysConfig.getConfigValue(), FsPayConfig.class);
        String platMerCstNo = fsPayConfig.getTzPlatMerCstNo();
        order.setOrderSource("02"); // 场景号
        order.setCurrency("CNY"); // 币种
        order.setTransType("2001"); // 交易类型
        order.setTradeMerCstNo(platMerCstNo); // 交易商户号
        order.setPlatMerCstNo(platMerCstNo); // 平台商户号
        order.setNeedLedger("00"); // 是否需要分账
        // 获取当前时间戳
        Instant currentTimestamp = Instant.now();
        // 增加一个小时
        Instant oneHourLater = currentTimestamp.plusSeconds(3600);
        order.setExpiredTime(oneHourLater.toEpochMilli()+""); // 订单过期时间
        List<String> payType = Arrays.asList(PayType.微信小程序支付.getCode(),PayType.支付宝条码支付.getCode());
        if(order.getPayType()==null){
            order.setPayType(payType);
        }
        order.setPayNotifyUrl(fsPayConfig.getTzPayDecrypt());
        order.setPayReturnUrl(TzBankConfig.payReturnUrl);
        String send = tzBankSendUtil.send(order,TzBankConfig.payCreateOrder);
        TzBankResult<PayCreateOrderResult> messageInfo = new Gson().fromJson(send, new TypeToken<TzBankResult<PayCreateOrderResult>>() {}.getType());
        if (messageInfo.getRetCode()!="-1"){
            String param="{\"orderId\":"+order.getOrderId()+",\"orderType\":"+order.getOrderType()+"}";
            String url2=TzBankConfig.wakeupCashier+"?businessCstNo="+order.getBusinessCstNo()+"&platMerCstNo="+messageInfo.getBody().getPlatMerCstNo()+"&orderNoList="+messageInfo.getBody().getOrderFlowNo()+"&returnUrl=miniProgram/pages_order/tzWeixinPay&openId="+order.getOpenId()+"&appId="+fsPayConfig.getAppId()+"&extraData="+param;
            messageInfo.getBody().setUrl(url2);
        }
        return messageInfo;
    }
    //查询订单
    public TzBankResult<QueryOrderResult> queryOrder(payQueryOrder payQueryOrder) {
        SysConfigMapper sysConfigMapper= SpringUtils.getBean(SysConfigMapper.class);
        SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey("his.pay");
        FsPayConfig fsPayConfig = new Gson().fromJson(sysConfig.getConfigValue(), FsPayConfig.class);
        String platMerCstNo = fsPayConfig.getTzPlatMerCstNo();
        payQueryOrder.setPlatMerCstNo(platMerCstNo);
        String send = tzBankSendUtil.send(payQueryOrder,TzBankConfig.payQueryOrder);
        TzBankResult<QueryOrderResult> messageInfo = new Gson().fromJson(send, new TypeToken<TzBankResult<QueryOrderResult>>() {}.getType());
        return messageInfo;
    }




    //退款
    public TzBankResult<RefundResult> refund(RefundParam tzBankResult) {
        tzBankResult.setTrxSendTime(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
        // 平台商户号
        SysConfigMapper sysConfigMapper= SpringUtils.getBean(SysConfigMapper.class);
        SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey("his.pay");
        FsPayConfig fsPayConfig = new Gson().fromJson(sysConfig.getConfigValue(), FsPayConfig.class);
        String platMerCstNo = fsPayConfig.getTzPlatMerCstNo();
        tzBankResult.setPlatMerCstNo(platMerCstNo);
        tzBankResult.setNotifyURL(fsPayConfig.getTzRefundDecrypt());
        String send = tzBankSendUtil.send(tzBankResult,TzBankConfig.payRefund);
        logger.info("台州银行退款："+send);
        TzBankResult<RefundResult> messageInfo = new Gson().fromJson(send, new TypeToken<TzBankResult<RefundResult>>() {}.getType());
        return messageInfo;
    }
    //查询退款
    public TzBankResult<QueryRefundResult> queryRefundOrder(QueryRefundOrderParam r) {
        r.setTrxSendTime(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())); // 示例时间格式，根据实际情况调整
        SysConfigMapper sysConfigMapper= SpringUtils.getBean(SysConfigMapper.class);
        SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey("his.pay");
        FsPayConfig fsPayConfig = new Gson().fromJson(sysConfig.getConfigValue(), FsPayConfig.class);
        String platMerCstNo = fsPayConfig.getTzPlatMerCstNo();
        r.setPlatMerCstNo(platMerCstNo);
        String send = tzBankSendUtil.send(r,TzBankConfig.payRefundQuery);
        TzBankResult<QueryRefundResult> messageInfo = new Gson().fromJson(send, new TypeToken<TzBankResult<QueryRefundResult>>() {}.getType());
        return messageInfo;
    }

    @Override
    public TzBankResult<OrderShareResult> orderShare(OrderShareParam orderShareParam) {
        orderShareParam.setTrxSendTime(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
        // 平台商户号
        SysConfigMapper sysConfigMapper= SpringUtils.getBean(SysConfigMapper.class);
        SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey("his.pay");
        FsPayConfig fsPayConfig = new Gson().fromJson(sysConfig.getConfigValue(), FsPayConfig.class);
        String platMerCstNo = fsPayConfig.getTzPlatMerCstNo();
        orderShareParam.setPlatMerCstNo(platMerCstNo);
        orderShareParam.setNotifyURL(fsPayConfig.getTzOrderShareDecrypt());
        orderShareParam.setPlatMerCstName("成都温江芸医汇互联网医院有限公司");
        String send = tzBankSendUtil.send(orderShareParam,TzBankConfig.orderShare);
        TzBankResult<OrderShareResult> messageInfo = new Gson().fromJson(send, new TypeToken<TzBankResult<OrderShareResult>>() {}.getType());
        return messageInfo;
    }

    @Override
    public TzBankResult<orderShareQueryResult> orderShareQuery(orderShareQueryParam orderShareParam) {
        SysConfigMapper sysConfigMapper= SpringUtils.getBean(SysConfigMapper.class);
        SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey("his.pay");
        FsPayConfig fsPayConfig = new Gson().fromJson(sysConfig.getConfigValue(), FsPayConfig.class);
        orderShareParam.setPlatMerCstNo(fsPayConfig.getTzPlatMerCstNo());
        orderShareParam.setTrxSendTime(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
        String send = tzBankSendUtil.send(orderShareParam,TzBankConfig.orderShareQuery);
        TzBankResult<orderShareQueryResult> messageInfo = new Gson().fromJson(send, new TypeToken<TzBankResult<orderShareQueryResult>>() {}.getType());
        return messageInfo;
    }
}
