package com.ruoyi.system.yyinterface;

import com.alibaba.fastjson.JSON;
import com.ruoyi.system.common.ReturnResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author jiuli
 * @Date 2024/1/16
 */
public class VoucherUtils {

    private static final Logger logger = LoggerFactory.getLogger(VoucherUtils.class);

    public static ReturnResult callVoucherQuery(Object obj, String url) {
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Content-Type", "application/json");
        ReturnResult result = null;
        try {
            String acctoken = TokenUtils.getAccessToken().getAccessToken();
            ResultVO resultVO = HttpUtil.doPost(TokenUtils.getDevUrl() + url + "?access_token=" + acctoken, JSON.toJSONString(obj), headerMap);
            Object res = resultVO.getData();
            result = JSON.parseObject(res.toString(), ReturnResult.class);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return result;
    }

    public static ReturnResult invokeByGet(String attribute, String str, String url) {
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Content-Type", "application/json");
        ReturnResult result = null;
        try {
            String acctoken = TokenUtils.getAccessToken().getAccessToken();
            ResultVO resultVO = HttpUtil.doGet(TokenUtils.getDevUrl() + url + "?access_token=" + acctoken + "&" + attribute + "=" + str, headerMap);
            Object res = resultVO.getData();
            result = JSON.parseObject(res.toString(), ReturnResult.class);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return result;
    }

    public static String yyyyMMDate(String str) {
        SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat output = new SimpleDateFormat("yyyy-MM");
        Date date;
        try {
            date = input.parse(str);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return output.format(date);
    }
}
