HzRemoteManager.java 4.25 KB
package cn.com.glsx.manager;

import cn.com.glsx.config.HzProperty;
import cn.com.glsx.config.SystemProperty;
import cn.com.glsx.dj.insure.model.InsureOrder;
import cn.com.glsx.framework.core.util.DateUtils;
import cn.com.glsx.util.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.entity.ContentType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

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


/**
 * @author zhoudan
 */
@Service
public class HzRemoteManager {

    private final static Logger LOGGER = LoggerFactory.getLogger(HzRemoteManager.class);

    @Autowired
    private HzProperty hzProperty;
    @Autowired
    private Environment env;
    @Autowired
    private SystemProperty systemProperty;

    public void insureResultNotice(InsureOrder order) {
//        String url = "http://iov.szhaizhen.com/anon/carboxvin/syncCarInsuranceStatus.dev";
        String url = env.getProperty("insure_result_hz_notice");
        JSONObject requestBody = this.getRequestBody(order);
        LOGGER.info("调用海圳远程通知api参数:{}",requestBody);
        JSONObject response = null;
        //尝试三次回调,解决连接超时问题
        int tryCount = systemProperty.getReinvokeTimes();
        int i = 0;
        while(i++ < tryCount){
            try{
                response = HttpUtil.postJSON(url,requestBody, ContentType.APPLICATION_JSON,null);
                LOGGER.info("海圳远程通知api返回结果:{}",response);
            }catch(Exception e){
                LOGGER.error("序列号:{},回调失败:{}", order.getSeqNo(), e.getMessage(), e);
            }
            if(response!=null && response.getString("success").equals("true")){
                break;
            }
            LOGGER.info("{}海圳第{}次回调,延时时间:{}", order.getSeqNo(), i, 2 * i * systemProperty.getReinvokeSleepTime());
            try {
                Thread.sleep(2 * i * systemProperty.getReinvokeSleepTime());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private JSONObject getRequestBody(InsureOrder order) {
        JSONObject requestBody = new JSONObject();
        requestBody.put("boxImei",order.getSubDeviceSn());
        if("1".equals(order.getInsuranceStatus())){
            requestBody.put("serviceStatus", "已生效");
        }else if("6".equals(order.getInsuranceStatus())){
            requestBody.put("serviceStatus", "已取消");
        }
        requestBody.put("serviceType", "保险");
        requestBody.put("serviceName", "盗抢险");
        requestBody.put("serviceCompany", "中国人保");
        requestBody.put("thirdId", order.getFullInsuranceNo());
        requestBody.put("effectiveTime", DateUtils.formatDate(order.getEffectDate(),DateUtils.DEFAULT_DATE_FORMAT));
        requestBody.put("periodUnit", 2);
        requestBody.put("periodValue", order.getInsurePeriod());
        requestBody.put("serviceUrl", order.getPdfUrl());
        return requestBody;
    }

    public static void main(String[] args) {
        HzRemoteManager manager = new HzRemoteManager();
        System.out.println(new Long(System.currentTimeMillis()).toString().substring(0,10));
        Map<String,String> params = new HashMap<String,String>(16);
        JSONObject requestBody = new JSONObject();
        params.put("boxImei", "1780124097");
        params.put("serviceType", "保险");
        params.put("serviceName", "盗抢险");
        params.put("serviceCompany", "中国人保");
        requestBody.put("serviceStatus", "已生效");
        requestBody.put("effectiveTime", "2020-02-26");
        requestBody.put("periodUnit", 2);
        requestBody.put("periodValue", 36);
        requestBody.put("serviceUrl", "");
        requestBody.put("thirdId", "111111111111");
    }

    public Map<String,String> getBaseParams() {
        Map<String,String> params = new HashMap<String,String>(8);
        params.put("serviceType", hzProperty.getServiceType());
        params.put("serviceName", hzProperty.getServiceName());
        params.put("serviceCompany", hzProperty.getServiceCompany());
        return params;
    }
}