package com.xsy.sshs.casepush.service.impl;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.ParseException;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xsy.sshs.casepush.pojo.CasePushBean;
import com.xsy.sshs.wechat.utils.IPUtil;
import com.xsy.sshs.wechatuser.dao.IUserDao;
import com.xsy.sshs.wechatuser.pojo.WechatUser;
import net.sf.json.JSONException;
public class Newpush {
private static Logger logger = Logger.getLogger(Newpush.class);
@Autowired
private IUserDao iUserDao;
@Value("${courtPath}")
private String courtPath;
@Value("${APPID}")
private String APPID;
@Value("${APPSECRET}")
private String APPSECRET;
// @Scheduled(cron="0 30 7 * * ?")
@Scheduled( cron="0/5 * * * * ?")
public void foreshowPush() {
SimpleDateFormat sdfin = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long start1 = System.currentTimeMillis();
logger.debug("推送方法:" + start1+ ",日期:" + sdfin.format(new Date()));
for (int pageNumber = 1;pageNumber<Integer.MAX_VALUE; pageNumber++) {
try {
String previewCase = replaceStr(IPUtil.getPreviewCase(courtPath + "/weixin/previewCase?pageNumber=" + pageNumber));
logger.debug("当前for调用次数:"+pageNumber+"----------------------------------------------");
List caseList = getCasePush(previewCase);
if (caseList.size() < 400) {
logger.debug("****************无推送开播消息**************************");
break;
}
logger.debug("**************************************************");
for (int i=0;i<caseList.size();i++) {
WechatUser weChatUser = new WechatUser();
weChatUser.setBelongCourt(caseList.get(i).getCourtCode());
List wehcrat = iUserDao.findUserList(weChatUser);
logger.debug("当前用户数量:"+wehcrat.size());
if (wehcrat.size() <= 0) {
logger.debug("当前CourtCode为:" + caseList.get(i).getCourtCode());
}
if (wehcrat.size() > 0) {
for (int j = 0; j < wehcrat.size(); j++) {
// 消息模板推送
try {
// Date date = new Date(new Long(casePush.getBeginTime()));
// if (isSameDate(new Date(), date)) {
// SendOrderPaySuccessMsg.send_casePush_message(null,
// APPID, APPSECRET,
// wehcrat.get(i).getWechatUuid(), casePush);
// }
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long start = System.currentTimeMillis();
logger.debug("当前推送次数:"+j);
logger.debug("推送前的信息:" + "法院code:" + caseList.get(i).getCourtCode() + ",被推送用户:"
+ wehcrat.get(j).getWechatName() + sdf.format(start));
// try {
// SendOrderPaySuccessMsg.send_casePush_message(null, APPID, APPSECRET,
// wehcrat.get(j).getWechatUuid(), caseList.get(i));
// } catch (java.text.ParseException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
// }
} catch (ParseException e) {
e.printStackTrace();
}
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static List<CasePushBean> getCasePush(String jsonStr) throws ParseException, IOException {
List<CasePushBean> list = new ArrayList<CasePushBean>();
try {
JSONObject jsonObj1 = JSON.parseObject(jsonStr);
JSONObject jsonObj2 = jsonObj1.getJSONObject("data");
JSONArray jsonArray = jsonObj2.getJSONArray("caseList");
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
CasePushBean casePush = new CasePushBean();
casePush.setCaseId(jsonObject.getString("caseId"));
casePush.setCourtCode(jsonObject.getString("courtCode"));
casePush.setCaseNo(jsonObject.getString("caseNo"));
casePush.setLiveId(jsonObject.getString("liveId"));
casePush.setVideoId(jsonObject.getString("videoId"));
casePush.setTitle(jsonObject.getString("title"));
casePush.setBeginTime(jsonObject.getString("beginTime"));
casePush.setLiveState(jsonObject.getString("liveState"));
casePush.setDescription(jsonObject.getString("description"));
casePush.setVideoThumbnail(jsonObject.getString("videoThumbnail"));
casePush.setThumbnail(jsonObject.getString("thumbnail"));
casePush.setDefaultImg(jsonObject.getString("defaultImg"));
casePush.setRoomId(jsonObject.getString("roomId"));
casePush.setRoomName(jsonObject.getString("roomName"));
casePush.setCourtName(jsonObject.getString("courtName"));
casePush.setJudge(jsonObject.getString("judge"));
list.add(casePush);
}
} catch (JSONException e) {
}
logger.debug("取出结果开始+++++++++++++++++++++++++++++++++");
System.out.println("取出来的所有接口数据"+JSON.toJSONString(list));
logger.debug("取出结果结束---------------------------------");
return list;
}
private boolean isSameDate(Date date1, Date date2) {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
boolean isSameYear = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR);
boolean isSameMonth = isSameYear && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
boolean isSameDate = isSameMonth && cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH);
return isSameDate;
}
private String replaceStr(String str) {
String dest = "";
if (str != null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(str);
dest = m.replaceAll("");
}
dest.replace("\\", "");
return dest;
}
}