package com.newvideo.controller.parser; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.newvideo.domain.Config; import com.newvideo.domain.DetailSystem; import com.newvideo.service.imp.ConfigService; import com.newvideo.service.imp.SystemService; import com.newvideo.service.imp.zhibo.ZhiBoServcie; import com.newvideo.util.JsonUtil; import com.newvideo.util.NumberUtil; import com.newvideo.util.StringUtil; import com.newvideo.util.zhibo.ZhiBoUtil; import com.newvideo.zhibo.entity.Live; import com.newvideo.zhibo.entity.LiveType; import com.newvideo.zhibo.entity.ZhiBoContent; import net.sf.json.JSONArray; import net.sf.json.JSONObject; @Controller public class ZhiBoParser { @Resource private ZhiBoServcie zhiBoServcie; @Resource private SystemService systemService; @Resource private ConfigService configService; public void getTop(HttpServletRequest request, PrintWriter out) { String method = request.getParameter("Method"); String device = request.getParameter("Device"); String system = request.getParameter("System"); String sign = request.getParameter("Sign"); String platform = request.getParameter("Platform"); String version = request.getParameter("Version"); String packageName = request.getParameter("Package"); if (StringUtil.isNullOrEmpty(method)) { out.print(JsonUtil.loadFalseJson("请上传Method")); return; } if (StringUtil.isNullOrEmpty(device)) { out.print(JsonUtil.loadFalseJson("请上传Device")); return; } if (StringUtil.isNullOrEmpty(system)) { out.print(JsonUtil.loadFalseJson("请上传System")); return; } if (StringUtil.isNullOrEmpty(sign)) { out.print(JsonUtil.loadFalseJson("请上传Sign")); return; } if (StringUtil.isNullOrEmpty(platform)) { out.print(JsonUtil.loadFalseJson("请上传Platform")); return; } List list = zhiBoServcie.getTop10List(); JSONObject data = new JSONObject(); JSONArray array = new JSONArray(); for (ZhiBoContent zc : list) { array.add(StringUtil.outPutResultJson(zc)); } data.put("data", array); data.put("count", list.size()); out.print(JsonUtil.loadTrueJson(data.toString())); } public void getNewList(HttpServletRequest request, PrintWriter out) { String method = request.getParameter("Method"); String device = request.getParameter("Device"); String system = request.getParameter("System"); String sign = request.getParameter("Sign"); String platform = request.getParameter("Platform"); String version = request.getParameter("Version"); String packageName = request.getParameter("Package"); String page = request.getParameter("Page"); if (StringUtil.isNullOrEmpty(method)) { out.print(JsonUtil.loadFalseJson("请上传Method")); return; } if (StringUtil.isNullOrEmpty(device)) { out.print(JsonUtil.loadFalseJson("请上传Device")); return; } if (StringUtil.isNullOrEmpty(system)) { out.print(JsonUtil.loadFalseJson("请上传System")); return; } if (StringUtil.isNullOrEmpty(sign)) { out.print(JsonUtil.loadFalseJson("请上传Sign")); return; } if (StringUtil.isNullOrEmpty(platform)) { out.print(JsonUtil.loadFalseJson("请上传Platform")); return; } if (StringUtil.isNullOrEmpty(page)) { out.print(JsonUtil.loadFalseJson("请上传page")); return; } List list = zhiBoServcie.getNewList(Integer.parseInt(page)); long count = zhiBoServcie.getNewListCount(); JSONObject data = new JSONObject(); JSONArray array = new JSONArray(); for (ZhiBoContent zc : list) { array.add(StringUtil.outPutResultJson(zc)); } data.put("data", array); data.put("count", count); out.print(JsonUtil.loadTrueJson(data.toString())); } public void addStatistics(HttpServletRequest request, PrintWriter out) { String method = request.getParameter("Method"); String device = request.getParameter("Device"); String system = request.getParameter("System"); String sign = request.getParameter("Sign"); String platform = request.getParameter("Platform"); String version = request.getParameter("Version"); String packageName = request.getParameter("Package"); String type = request.getParameter("Type"); String roomId = request.getParameter("RoomId"); if (StringUtil.isNullOrEmpty(method)) { out.print(JsonUtil.loadFalseJson("请上传Method")); return; } if (StringUtil.isNullOrEmpty(device)) { out.print(JsonUtil.loadFalseJson("请上传Device")); return; } if (StringUtil.isNullOrEmpty(system)) { out.print(JsonUtil.loadFalseJson("请上传System")); return; } if (StringUtil.isNullOrEmpty(sign)) { out.print(JsonUtil.loadFalseJson("请上传Sign")); return; } if (StringUtil.isNullOrEmpty(platform)) { out.print(JsonUtil.loadFalseJson("请上传Platform")); return; } if (StringUtil.isNullOrEmpty(type)) { out.print(JsonUtil.loadFalseJson("请上传Type")); return; } if (StringUtil.isNullOrEmpty(roomId)) { out.print(JsonUtil.loadFalseJson("请上传RoomId")); return; } zhiBoServcie.addStatistics(roomId, Integer.parseInt(type)); out.print(JsonUtil.loadTrueJson("")); } public void getHotLive(HttpServletRequest request, PrintWriter out){ boolean b = basicVerify(request,out); if(!b){ return; } String platform = request.getParameter("Platform"); List list = ZhiBoUtil.getHotLive(platform); JSONObject data = new JSONObject(); Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create(); data.put("data", gson.toJson(list)); data.put("PageSize", list.size()); out.print(JsonUtil.loadTrueJson(data.toString())); } public void getAllLiveType(HttpServletRequest request, PrintWriter out){ boolean b = basicVerify(request,out); if(!b){ return; } String packageName = request.getParameter("Package"); String platform = request.getParameter("Platform"); DetailSystem detailSystem = systemService.getDetailSystemByPackage(packageName); List typelist = new ArrayList(); String value=""; if("IOS".equalsIgnoreCase(platform)){ Config config = configService.getConfigByKey("open_huajiao_IOS"); value = config.getValue(); }else{ Config config = configService.getConfigByKey("open_huajiao_Android"); value = config.getValue(); } if("是".equals(value)){ typelist = zhiBoServcie.getAllLiveType(detailSystem.getId()); } Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create(); JSONObject data = new JSONObject(); data.put("data", gson.toJson(typelist)); data.put("PageSize", typelist.size()); out.print(JsonUtil.loadTrueJson(data.toString())); } public void getLiveList(HttpServletRequest request, PrintWriter out){ boolean bv = basicVerify(request,out); if(!bv){ return; } String platform = request.getParameter("Platform"); String page = request.getParameter("Page"); boolean b = NumberUtil.isNumeric(page); if(!b){ out.print(JsonUtil.loadFalseJson("page必须为正整数")); return; } List list = ZhiBoUtil.getLiveList(Integer.valueOf(page),platform); if(list.size() > 0 && list.size() %2 != 0){ list.remove(list.size()-1); } Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create(); JSONObject data = new JSONObject(); data.put("PageSize", list.size()); data.put("data", gson.toJson(list)); out.print(JsonUtil.loadTrueJson(data.toString())); } public void getLiveListByType(HttpServletRequest request, PrintWriter out) { boolean bb = basicVerify(request,out); if(!bb){ return; } String typeid = request.getParameter("Type"); String page = request.getParameter("Page"); boolean b = NumberUtil.isNumeric(typeid); if(!b){ out.print(JsonUtil.loadFalseJson("type必须为正整数")); return; } b = NumberUtil.isNumeric(page); if(!b){ out.print(JsonUtil.loadFalseJson("page必须为正整数")); return; } LiveType liveType = zhiBoServcie.getLiveType(typeid); if(liveType == null){ out.print(JsonUtil.loadFalseJson("不存在该类型")); return; } String tag = liveType.getTag(); List list = ZhiBoUtil.getHuaJiaoList(Integer.parseInt(page),tag); if(list.size() > 0 && (list.size()%2 !=0)){ list.remove(list.size()-1); } JSONObject data = new JSONObject(); Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create(); data.put("PageSize", list.size()); data.put("data", gson.toJson(list)); out.print(JsonUtil.loadTrueJson(data.toString())); } private boolean basicVerify(HttpServletRequest request, PrintWriter out) { String method = request.getParameter("Method"); String device = request.getParameter("Device"); String system = request.getParameter("System"); String sign = request.getParameter("Sign"); String platform = request.getParameter("Platform"); String version = request.getParameter("Version"); String packageName = request.getParameter("Package"); if (StringUtil.isNullOrEmpty(method)) { out.print(JsonUtil.loadFalseJson("请上传Method")); return false; } if (StringUtil.isNullOrEmpty(device)) { out.print(JsonUtil.loadFalseJson("请上传Device")); return false; } if (StringUtil.isNullOrEmpty(system)) { out.print(JsonUtil.loadFalseJson("请上传System")); return false; } if (StringUtil.isNullOrEmpty(sign)) { out.print(JsonUtil.loadFalseJson("请上传Sign")); return false; } if (StringUtil.isNullOrEmpty(platform)) { out.print(JsonUtil.loadFalseJson("请上传Platform")); return false; } return true; } }