package com.yeshi.buwan.controller.api; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.yeshi.buwan.domain.live.*; import com.yeshi.buwan.live.migu.MiGuLiveListInfo; import com.yeshi.buwan.live.migu.MiguLiveApiUtil; import com.yeshi.buwan.service.inter.live.MiGuLiveService; import com.yeshi.buwan.service.inter.live.TVLiveCategoryChannelService; import com.yeshi.buwan.service.inter.live.TVLiveCategoryService; import com.yeshi.buwan.service.inter.live.TVLiveChannelResourceService; import com.yeshi.buwan.util.Constant; import com.yeshi.buwan.util.JsonUtilV2; import com.yeshi.buwan.util.StringUtil; import com.yeshi.buwan.vo.AcceptData; import com.yeshi.buwan.vo.tvlive.TVLiveChannelVO; import net.sf.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; /** * 电视直播 */ @Controller @RequestMapping("api/v2/tvlive") public class TVLiveController { Logger logger = LoggerFactory.getLogger(TVLiveController.class); @Resource private TVLiveCategoryService tvLiveCategoryService; @Resource private TVLiveCategoryChannelService tvLiveCategoryChannelService; @Resource private TVLiveChannelResourceService tvLiveChannelResourceService; @Resource private MiGuLiveService miGuLiveService; @RequestMapping("getCategory") @ResponseBody public String getCategory(AcceptData acceptData) { Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); List superList = tvLiveCategoryService.listSuper(acceptData.getDetailSystem().getId(), 1, 20); List channelIds = new ArrayList<>(); for (SuperTVLiveCategory category : superList) { channelIds.add(category.getCid()); } List categoryList = tvLiveCategoryService.listByIds(channelIds); JSONObject data = new JSONObject(); data.put("count", categoryList.size()); data.put("data", gson.toJson(categoryList)); return JsonUtilV2.loadTrueJson(data.toString()); } @RequestMapping("getChannelList") @ResponseBody public String getChannelList(AcceptData acceptData, String cid, int page) { if (StringUtil.isNullOrEmpty(cid)) { return JsonUtilV2.loadFalseJson("cid为空"); } List channelList = tvLiveCategoryChannelService.listChannelByCid(cid, null, page, Constant.pageCount); long count = tvLiveCategoryChannelService.countChannelByCid(cid, null); List voList = new ArrayList<>(); for (TVLiveChannel channel : channelList) { voList.add(TVLiveChannelVO.create(channel)); } JSONObject root = new JSONObject(); root.put("list", new Gson().toJson(voList)); root.put("count", count); return JsonUtilV2.loadTrueJson(root.toString()); } @RequestMapping("getPlayUrl") @ResponseBody public String getPlayUrl(AcceptData acceptData, String id) { if (StringUtil.isNullOrEmpty(id)) { return JsonUtilV2.loadFalseJson("id为空"); } //获取播放链接 List mapList = tvLiveChannelResourceService.listByChannelId(id); if (mapList == null || mapList.size() == 0) { return JsonUtilV2.loadFalseJson("暂无播放源"); } TVLiveChannelResourceMap map = mapList.get(0); String playUrl = null; if (map.getResource() == TVLiveResource.migu) { MiGuLiveListInfo info = miGuLiveService.selectByPrimaryKey(map.getRid()); playUrl = MiguLiveApiUtil.getPlayUrl(info.getPID()); } else if (map.getResource() == TVLiveResource.other) { playUrl = map.getPlayUrl(); } JSONObject root = new JSONObject(); root.put("playUrl", playUrl); return JsonUtilV2.loadTrueJson(root.toString()); } }