package com.yeejoin.amos.maintenance.feign;

import java.util.List;

import com.yeejoin.amos.maintenance.business.util.CommonResponse;
import com.yeejoin.amos.maintenance.business.util.CommonResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.yeejoin.amos.maintenance.business.param.PushMsgParam;

@Service("pushFeignServer")
public class PushFeignServer {
	 @Autowired
	    private RestTemplate restTemplate;
	
	    @Value("${Push.fegin.name}")
	    private String RPushFeginName;
	    private static String sendMessage = "/api/user/sendMessage";
	    
	    public  String geturls(String url){
	    	return "http://"+RPushFeginName+url;
	    }
	    public  HttpHeaders getHeader(String toke,String product,String appKey){
	    	HttpHeaders headers = new HttpHeaders();
		    headers.setContentType(MediaType.APPLICATION_JSON);
		    headers.set("Content-Type", "application/json");  
		    headers.set("token", toke);
		    headers.set("product",product);
		    headers.set("appKey", appKey);
		    return headers;
	    } 
	    
	    public CommonResponse sendMessage(String toke, String product, String appKey, List<PushMsgParam> pushMsgParam){
			 try {
				    HttpEntity httpEntity = new HttpEntity<>(pushMsgParam, getHeader( toke, product, appKey));
				    CommonResponse commonResponse1 =  restTemplate.postForObject(geturls(sendMessage),httpEntity, CommonResponse.class);
					return commonResponse1;
					} catch (Exception e) {
					e.printStackTrace();	 
					 return CommonResponseUtil.failure("发送失败");
					 
					}
		}
}
