package com.saye.hospitalgd.service.impl;

import com.saye.hospitalgd.commons.string.StringDUtil;
import com.saye.hospitalgd.mapper.TranscationsMapper;
import com.saye.hospitalgd.service.TranscationsService;
import com.saye.hospitalgd.service.system.ServiceParamsService;
import com.saye.hospitalgd.util.RefundUtil;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： Mr.zs
 * @date： 2023/10/8
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@Service
@Slf4j
public class TranscationsServiceImp implements TranscationsService {


    @Autowired
    TranscationsMapper transcationsMapper;


    @Autowired
    ServiceParamsService serviceParamsService;

    @Override
    public List<HashMap<Object, Object>> findTradeRecords(HashMap<Object, Object> map) {
        List<HashMap<Object, Object>> tempList = transcationsMapper.findTradeRecords(map);
        List<HashMap<Object, Object>> hashMaps = new ArrayList<>();
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        for (HashMap<Object, Object> ohm : tempList) {
            LocalDateTime o = (LocalDateTime) ohm.get("TIME_STAMP");
            ohm.put("TIME_STAMP", o.format(fmt));
            hashMaps.add(ohm);
        }
        return hashMaps;

    }

    @Override
    public List<HashMap<Object, Object>> findActionableTrading(HashMap<Object, Object> map) {
        List<HashMap<Object, Object>> tempList = transcationsMapper.findActionableTrading(map);
        List<HashMap<Object, Object>> hashMaps = new ArrayList<>();
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        for (HashMap<Object, Object> ohm : tempList) {
            LocalDateTime o = (LocalDateTime) ohm.get("TIME_STAMP");
            ohm.put("TIME_STAMP", o.format(fmt));
            hashMaps.add(ohm);
        }
        return hashMaps;


    }

    @Override
    public HashMap<Object, Object> findActionableTradingByJylsh(HashMap<Object, Object> map) {
        log.info("jylsh is :" + map.get("jylsh"));
        return transcationsMapper.findActionableTradingByJylsh(map);
    }

    @Override
    public HashMap<String, String> refundTrade(List<HashMap<Object, Object>> list) throws DocumentException {
        HashMap<Object, Object> refundObj = list.get(0);
        //调用支付接口退费接口
        List<HashMap<Object, Object>> palm_hospitalList = serviceParamsService.findParamValByParamCode("jfjk_wsdl");
        String palm_hospital = StringDUtil.changeNullToEmpty(palm_hospitalList.get(0).get("PARAM_VAL"));
        return new RefundUtil().refundTrade(refundObj, palm_hospital);
    }
}
