package com.unis.base.app.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.unis.base.app.service.PushService;
import com.unis.cbl.common.utils.HttpRequestUtil;
import com.unis.common.utils.JsonUtils;
import com.unis.common.utils.ResponseWrapper;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class PushServiceImpl implements PushService {

    @Autowired
    private Environment env;

    @ApiOperation(value = "往库级推送信息")
    public String push(JSONObject obj, String flag) {
        String sendResult = "";
        log.info("url：{} 推送信息：{}",flag, obj);
        if(null != obj) {
            try{
                String str= URLEncoder.encode(JsonUtils.serialize(obj),"UTF-8");
                Map<String, Object> noticeMap = new HashMap<String, Object>();
                noticeMap.put("datas", str);
                String sendUrl = env.getProperty(flag);;
                if(StringUtils.isNotEmpty(sendUrl)) {
                    sendResult = HttpRequestUtil.doFormPost(sendUrl, noticeMap);
                    log.info("库级系统:{} |  返回结果 | res：{}",sendUrl,sendResult);
                }
            }  catch (Exception e) {
                e.printStackTrace();
                sendResult = e.getMessage();
            }
        }
        return sendResult;
    }
}
