package com.cnd3b.restcontroller.customer.live; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.cnd3b.common.Controller; import com.cnd3b.common.data.Row; import com.cnd3b.common.data.Rows; import com.cnd3b.common.data.SQLFactory; import com.cnd3b.common.data.db.DataPool; import com.cnd3b.utility.polyv.Polyv; import p2.common.parse.A; import p2.pao.PaoRemote; import p2.pao.PaoSetRemote; import p2.util.P2Exception; import java.util.HashMap; public class live extends Controller { public live(JSONObject content) { super(content); } /** * 获取频道分类列表 * * @return */ public String getLiveCategory() { Polyv polyv = new Polyv(); HashMap map = polyv.listCategory(); JSONArray array = new JSONArray(); for (int id : map.keySet()) { JSONObject object = new JSONObject(); object.put("categoryId", id); object.put("categoryName", map.get(id)); array.add(object); } return getSucReturnObject().setData(array).toString(); } /** * 直播列表查询 * * @return */ public String queryChannelList() { /** *排序条件设置 */ String[] sortfield = {"t1.createdate desc"}; String sort = getSort(sortfield, "t1.createdate desc"); /** * 过滤条件设置 */ StringBuffer where = new StringBuffer(" 1=1 "); if (content.containsKey("where")) { JSONObject whereObject = content.getJSONObject("where"); if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) { where.append(" and("); where.append("t1.channelname like'%").append(whereObject.getString("condition")).append("%' "); where.append("or t2.fagentname like'%").append(whereObject.getString("condition")).append("%' "); where.append(")"); } if (whereObject.containsKey("categoryid") && !"".equals(whereObject.getString("categoryid"))) { where.append(" and("); where.append("t1.categoryid ='").append(whereObject.getString("categoryid")).append("' "); where.append(")"); } } SQLFactory costhead = new SQLFactory(this, "直播列表查询", pageSize, pageNumber, sort); costhead.addParameter("siteid", siteid); costhead.addParameter("tagentsid", tagentsid); costhead.addParameter_SQL("where", where); Rows rows = dbConnect.runSqlQuery(costhead.getSQL()); return getSucReturnObject().setDataByPaging(rows, sortfield).setFinalDo(DataPool.defaultdatalife, 1); } /** * 直播详情查询 * * @return */ public String queryChannelMain() { long tliveid = content.getLong("tliveid"); SQLFactory sqlFactory = new SQLFactory(this, "直播详情查询"); sqlFactory.addParameter("siteid", siteid); sqlFactory.addParameter("tliveid", tliveid); Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL()); return getSucReturnObject().setData(rows).toString(); } /** * 展会直播详情 * * @return */ public String getLiveInfo() throws P2Exception { SQLFactory sqlFactory = new SQLFactory(this, "展会直播详情"); sqlFactory.addParameter("siteid", siteid); sqlFactory.addParameter("tagentsid", tagentsid); Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL()); if (rows.isEmpty()) { PaoSetRemote paoSetRemote = getP2ServerSystemPaoSet("tliveapply", "siteid = '" + siteid + "' AND fissecret = 0 AND fstatus = '申请'"); if (paoSetRemote.isEmpty()) { return getErrReturnObject().setErrCode(3).setErrMsg("无直播间信息").toString(); } else { return getErrReturnObject().setErrCode(2).setErrMsg("直播申请中").toString(); } } else { return getSucReturnObject().setData(rows).toString(); } } /** * 直播数据统计(频道) * * @return */ public String getLiveDataCount() { String channelid = content.getString("channelid"); JSONObject resultObject = new JSONObject(); //观看次数(次) double viewCounts = getCount("观看次数", "num", channelid); resultObject.put("viewCounts", String.format("%.2f", viewCounts)); //观看时长(分钟) double viewDuration = getCount("观看时长", "num", channelid); resultObject.put("viewDuration", String.format("%.2f", viewDuration)); //观看人数(人) double viewers = getCount("观看人数", "num", channelid); resultObject.put("viewers", String.format("%.2f", viewers)); //人均观看次数(次) resultObject.put("viewCountsAvg", String.format("%.2f", viewers == 0.0 ? 0 : viewCounts / viewers)); //人均观看时长(分钟) resultObject.put("viewDurationAvg", String.format("%.2f", viewers == 0.0 ? 0 : viewDuration / viewers)); return getSucReturnObject().setData(resultObject).toString(); } /** * 直播数据统计(场次) * * @return * @throws P2Exception */ public String getLiveDataCountForSession() throws P2Exception { String sessionid = content.getString("sessionid"); double livepv = 0; double liveuv = 0; double duration = 0; PaoSetRemote paoSetRemote = getP2ServerSystemPaoSet("tlive_sessiondata", "sessionid = '" + sessionid + "'"); if (!paoSetRemote.isEmpty()) { PaoRemote paoRemote = paoSetRemote.getPao(0); //人数 livepv = paoRemote.getDouble("livepv"); //人次 liveuv = paoRemote.getDouble("liveuv"); //时长 duration = paoRemote.getDouble("duration"); } JSONObject resultObject = new JSONObject(); resultObject.put("livepv", String.format("%.2f", livepv)); resultObject.put("liveuv", String.format("%.2f", liveuv)); resultObject.put("duration", String.format("%.2f", duration)); resultObject.put("liveuvAvg", String.format("%.2f", livepv == 0 ? 0 : liveuv / livepv)); resultObject.put("durationAvg", String.format("%.2f", livepv == 0 ? 0 : duration / livepv)); return getSucReturnObject().setData(resultObject).toString(); } /** * 直播用户观看列表 * * @return */ public String getLiveUserList() { String channelid = content.getString("channelid"); String sessionid = content.getString("sessionid"); SQLFactory sqlFactory = new SQLFactory(this, "直播用户观看列表"); sqlFactory.addParameter("channelid", channelid); sqlFactory.addParameter("sessionid", sessionid); Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL()); return getSucReturnObject().setData(rows).toString(); } /** * 直播场次列表查询 * * @return */ public String getLiveSessionList() { String channelid = content.getString("channelid"); SQLFactory sqlFactory = new SQLFactory(this, "直播场次列表查询", pageSize, pageNumber, "createdate"); sqlFactory.addParameter("channelid", channelid); Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL()); return getSucReturnObject().setDataByPaging(rows).preloading(1).toString(); } /** * 私域直播详情 * * @return */ public String getSYLiveInfo() throws P2Exception { SQLFactory sqlFactory = new SQLFactory(this, "私域直播详情"); sqlFactory.addParameter("siteid", siteid); sqlFactory.addParameter("tagentsid", tagentsid); Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL()); if (rows.isEmpty()) { PaoSetRemote paoSetRemote = getP2ServerSystemPaoSet("tliveapply", "siteid = '" + siteid + "' and tagentsid='" + tagentsid + "' AND fissecret = 1 AND fstatus = '申请'"); if (paoSetRemote.isEmpty()) { return getErrReturnObject().setErrCode(3).setErrMsg("无直播间信息").toString(); } else { return getErrReturnObject().setErrCode(2).setErrMsg("直播申请中").toString(); } } else { return getSucReturnObject().setData(rows).toString(); } } /** * 私域直播数据统计(频道) * * @return */ public String getSYLiveDataCount() { String channelid = content.getString("channelid"); JSONObject resultObject = new JSONObject(); //观看次数(次) double viewCounts = getCount("观看次数", "num", channelid); resultObject.put("viewCounts", String.format("%.2f", viewCounts)); //观看时长(分钟) double viewDuration = getCount("观看时长", "num", channelid); resultObject.put("viewDuration", String.format("%.2f", viewDuration)); //观看人数(人) double viewers = getCount("观看人数", "num", channelid); resultObject.put("viewers", String.format("%.2f", viewers)); System.err.println(viewCounts + "," + viewDuration + "," + viewers); //人均观看次数(次) resultObject.put("viewCountsAvg", String.format("%.2f", viewers == 0.0 ? 0 : viewCounts / viewers)); //人均观看时长(分钟) resultObject.put("viewDurationAvg", String.format("%.2f", viewers == 0.0 ? 0 : viewDuration / viewers)); return getSucReturnObject().setData(resultObject).toString(); } /** * 私域直播数据统计(场次) * * @return * @throws P2Exception */ public String getSYLiveDataCountForSession() throws P2Exception { String sessionid = content.getString("sessionid"); double livepv = 0; double liveuv = 0; double duration = 0; PaoSetRemote paoSetRemote = getP2ServerSystemPaoSet("tlive_sessiondata", "sessionid = '" + sessionid + "'"); if (!paoSetRemote.isEmpty()) { PaoRemote paoRemote = paoSetRemote.getPao(0); //人数 livepv = paoRemote.getDouble("livepv"); //人次 liveuv = paoRemote.getDouble("liveuv"); //时长 duration = paoRemote.getDouble("duration"); } JSONObject resultObject = new JSONObject(); resultObject.put("livepv", String.format("%.2f", livepv)); resultObject.put("liveuv", String.format("%.2f", liveuv)); resultObject.put("duration", String.format("%.2f", duration)); resultObject.put("liveuvAvg", String.format("%.2f", livepv == 0 ? 0 : liveuv / livepv)); resultObject.put("durationAvg", String.format("%.2f", livepv == 0 ? 0 : duration / livepv)); return getSucReturnObject().setData(resultObject).toString(); } /** * 私域直播用户观看列表 * * @return */ public String getSYLiveUserList() { String channelid = content.getString("channelid"); String sessionid = content.getString("sessionid"); SQLFactory sqlFactory = new SQLFactory(this, "私域直播用户观看列表"); sqlFactory.addParameter("channelid", channelid); sqlFactory.addParameter("sessionid", sessionid); Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL()); return getSucReturnObject().setData(rows).toString(); } /** * 私域直播申请 * * @return */ public String applySYLive() throws P2Exception { PaoSetRemote paoSetRemote = getP2ServerSystemPaoSet("tliveapply", "siteid = '" + siteid + "' AND fissecret = 1 AND fstatus = '申请'"); if (paoSetRemote.isEmpty()) { PaoRemote paoRemote = paoSetRemote.addAtEnd(); paoRemote.setValue("CHANGEBY", username, 11L); paoRemote.setValue("CHANGEDATE", getDateTime_Str(), 11L); paoRemote.setValue("CREATEBY", username, 11L); paoRemote.setValue("CREATEDATE", getDateTime_Str(), 11L); paoRemote.setValue("FISSECRET", true, 11L); paoRemote.setValue("FSTATUS", "申请", 11L); paoRemote.setValue("FNOTES", "私域直播申请", 11L); paoRemote.setValue("SITEID", siteid); paoRemote.setValue("TAGENTSID", tagentsid); paoSetRemote.save(); return getSucReturnObject().setData("私域直播申请成功").toString(); } else { return getErrReturnObject().setErrMsg("私域直播已申请").toString(); } } /** * 私域直播场次列表查询 * * @return */ public String getSYLiveSessionList() { String channelid = content.getString("channelid"); SQLFactory sqlFactory = new SQLFactory(this, "私域直播场次列表查询", pageSize, pageNumber, "createdate"); sqlFactory.addParameter("channelid", channelid); Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL()); return getSucReturnObject().setDataByPaging(rows).preloading(1).toString(); } /** * 获取数量 * * @param SQLMODELNAME SQL名称 * @param fieldname 取数据的字段名称 * @return */ public double getCount(String SQLMODELNAME, String fieldname, String channelid) { SQLFactory sqlFactory = new SQLFactory(this, SQLMODELNAME); sqlFactory.addParameter("channelid", channelid); System.err.println(sqlFactory.getSQL()); Rows rows = sqlFactory.runSqlQuery(); return rows.isEmpty() ? 0 : rows.get(0).getDouble(fieldname); } }