/***文档注释***********************************************
 * 作者               :                   易鑫
 * 创建日期      :                   2016.03.12
 * 描述               :                  公告阅读人信息
 * 注意事项      :                   无
 * 遗留BUG :                      无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
 ***********************************************************/

package market.Slm.Notice.impl;

import java.util.List;

import market.Slm.Notice.mapper.SlmNoticeReaderMapper;
import market.Slm.Notice.service.SlmNoticeReaderService;
import market.Slm.Notice.vo.SlmNoticeReader;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
/*
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
//*/
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;

@RemoteService(path="SlmNoticeReaderService")
public class SlmNoticeReaderServiceImpl extends BaseServiceImpl<SlmNoticeReader,Object> implements SlmNoticeReaderService {

	//逻辑方法 查询
	public List<SlmNoticeReader> doQuery(SlmNoticeReader reader) {
		List<SlmNoticeReader> list=this.getMapper(SlmNoticeReaderMapper.class).find(reader, 0, 0);
		return list;
	}

	//60秒推送一次消息
	//全平台推送
	//@Scheduled(cron="0/60 * * * * ? ")
	/*
	private void doPush(){
		//获取未推送消息的数据进行推送
		SlmNoticeReader snr=new SlmNoticeReader();
		snr.setValid_flag("0");
		snr.setIs_notify("0");
		//【注意doQueryPush里面的push_id这个需要在Slm_Customer_Person_Info表中配置极光推送的推送id，具体跟app王鹏飞沟通】
		List<SlmNoticeReader> lsr=this.getMapper(SlmNoticeReaderMapper.class).doQueryPush(snr);
		
		String sNoticeNo="",sPersonNo="";
		if(lsr!=null&&lsr.size()>0){
			Collection<String> list = new LinkedList<String>();
			String strNoticeNo=lsr.get(0).getNotice_no();
			for(int i=0;i<lsr.size();i++){
				sNoticeNo+="'"+lsr.get(i).getNotice_no()+"',";
				sPersonNo+="'"+lsr.get(i).getPerson_no()+"',";
				//最后一条数据了，或者不是同一个通知了
				if(!strNoticeNo.equals(lsr.get(i).getNotice_no()) ||(i==lsr.size()-1)){
					if(list.size()>0){
						Map<String, String> extras = new HashMap<String, String>();
						int m=(i==0)?1:i;
						extras.put("notice_no", lsr.get(m-1).getNotice_no());
						//创建JPushClient
						JPushClient jpushClient = new JPushClient(PropertiesUtils.getProperties().getProperty("MasterSecret"), PropertiesUtils.getProperties().getProperty("AppKey"));
						//创建option
						PushPayload payload = PushPayload.newBuilder()
								.setPlatform(Platform.all())  //所有平台的用户
								.setAudience(Audience.registrationId(list))//registrationId指定用户
								.setNotification(Notification.newBuilder()
										.addPlatformNotification(IosNotification.newBuilder()
												.setAlert("收到一条新通知")
												.setBadge(+1)
												.setSound("happy")
												.addExtras(extras)
												.build())
												.addPlatformNotification(AndroidNotification.newBuilder()
														.addExtras(extras)
														.setAlert("收到一条新通知")
														.build())
														.build())
														.setOptions(Options.newBuilder().setApnsProduction(true).build())//指定开发环境
														.setMessage(Message.newBuilder().setMsgContent(lsr.get(m-1).getNotice_title()).addExtras(extras).build())//自定义信息
														.build();
						try {
							PushResult pu = jpushClient.sendPush(payload);
						} catch (APIConnectionException e) {
							e.printStackTrace();
						} catch (APIRequestException e) {
							e.printStackTrace();
						}
					}
					list = new LinkedList<String>();
				}
				if(lsr.get(i).getPush_id()!=null&&!"".equals(lsr.get(i).getPush_id())){
					list.add(lsr.get(i).getPush_id());
				}
			}
			if(!"".equals(sNoticeNo)&&!"".equals(sPersonNo)){
				this.getMapper(SlmNoticeReaderMapper.class).doUpdateStatic(sNoticeNo.substring(0,sNoticeNo.length()-1),sPersonNo.substring(0,sPersonNo.length()-1));
			}
		}
	}
	//*/
}