package com.yzmoney.common.service.impl;

import com.yzmoney.common.constant.PropertyUtils;
import com.yzmoney.common.service.SaveQrcodeLogService;
import com.yzmoney.common.util.HttpClientUtil;
import com.yzmoney.common.util.JDKStackTrace;
import com.yzmoney.common.util.JsonUtils;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 保存二维码信息
 *
 */
@Service
public class SaveQrcodeLogServiceImpl implements SaveQrcodeLogService{
    private static Logger logger = Logger.getLogger(SaveQrcodeLogServiceImpl.class);

    private String saveQrcodeInfoUrl = PropertyUtils.getValue("add_qrcode_info","");

    private boolean sendHttp(String url,Map param){
        logger.info("远程保存二维码信息开始，param:" + JsonUtils.toJson(param)+",sendHttp--url："+ url);
        if(StringUtils.isNotEmpty(url)){
            try {
                String result = HttpClientUtil.get(url, param, null);
                if(result!=null){
                    JSONObject object = JSONObject.fromObject(result);
                    if(object != null && object.getInt("code") == 0){
                        logger.info("远程保存二维码信息成功，param:" + JsonUtils.toJson(param)+",sendHttp--url："+ url + ",result："+result);
                        return true;
                    }else{
                        logger.info("远程保存二维码信息失败，param:" + JsonUtils.toJson(param)+",sendHttp--url："+ url + ",result："+result);
                    }
                }
            } catch (Exception e) {
                logger.info("远程保存二维码信息异常，param:" + JsonUtils.toJson(param)+",sendHttp--url："+ url + JDKStackTrace.getJDKStrack(e));
                return false;
            }
        }
        return false;
    }

    @Override
    public void saveQrcodeInfo(final Map param) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                sendHttp(saveQrcodeInfoUrl,param);
            }
        }).start();
    }
}
