package com.yeshi.buwan.controller.admin.api; import com.google.gson.*; import com.google.gson.reflect.TypeToken; import com.yeshi.buwan.domain.live.TVLiveCategoryChannelMap; import com.yeshi.buwan.domain.live.TVLiveChannel; import com.yeshi.buwan.domain.live.TVLiveChannelResourceMap; import com.yeshi.buwan.domain.live.TVLiveResource; import com.yeshi.buwan.service.imp.SystemService; import com.yeshi.buwan.service.inter.live.TVLiveCategoryChannelService; import com.yeshi.buwan.service.inter.live.TVLiveChannelResourceService; import com.yeshi.buwan.service.inter.live.TVLiveChannelService; import com.yeshi.buwan.util.Constant; import com.yeshi.buwan.util.JsonUtil; import com.yeshi.buwan.util.StringUtil; import com.yeshi.buwan.util.TimeUtil; import com.yeshi.buwan.vo.tvlive.TVLiveChannelResourceMapAdminVO; import com.yeshi.buwan.web.tag.PageEntity; import net.sf.json.JSONObject; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import javax.annotation.Resource; import javax.servlet.http.HttpSession; import java.io.PrintWriter; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Date; import java.util.List; @Controller @RequestMapping("admin/new/api/tvlive/channel") public class TVLiveChannelAdminController { @Resource private TVLiveChannelService tvLiveChannelService; @Resource private TVLiveCategoryChannelService tvLiveCategoryChannelService; @Resource private SystemService systemService; @Resource private TVLiveChannelResourceService tvLiveChannelResourceService; private Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonSerializer() { @Override public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) { return src == null ? new JsonPrimitive("") : new JsonPrimitive(TimeUtil.getGernalTime(src.getTime(), "yyyy.MM.dd HH:mm")); } }).create(); @RequestMapping(value = "/getChannelList", method = RequestMethod.POST) public void getChannelList(HttpSession session, String categoryId, String key, Integer state, int page, PrintWriter out) { List channelList; long count; if (StringUtil.isNullOrEmpty(categoryId)) { channelList = tvLiveChannelService.list(key, state, page, Constant.pageCount); count = tvLiveChannelService.count(key, state); } else { channelList = tvLiveCategoryChannelService.listChannelByCid(categoryId, key, page, Constant.pageCount); count = tvLiveCategoryChannelService.countChannelByCid(categoryId, key); } JSONObject data = new JSONObject(); data.put("pageEntity", new PageEntity(page, Constant.pageCount, (int) count)); data.put("data", gson.toJson(channelList)); out.print(JsonUtil.loadTrueAdmin(data.toString())); } @RequestMapping(value = "/addChannel", method = RequestMethod.POST) public void addChannel(TVLiveChannel channel, HttpSession session, PrintWriter out) { if (StringUtil.isNullOrEmpty(channel.getName()) || StringUtil.isNullOrEmpty(channel.gethPicture())) { out.print(JsonUtil.loadFalseAdmin("数据不完整")); return; } channel.setState(TVLiveChannel.STATE_NORMAL); try { tvLiveChannelService.addChannel(channel); out.print(JsonUtil.loadTrueAdmin("")); } catch (Exception e) { out.print(JsonUtil.loadFalseAdmin(e.getMessage())); return; } } @RequestMapping(value = "/addCategoryChannel", method = RequestMethod.POST) public void addCategoryChannel(String categoryId, String channelIds, Integer weight, HttpSession session, PrintWriter out) { if (StringUtil.isNullOrEmpty(categoryId) || StringUtil.isNullOrEmpty(channelIds)) { out.print(JsonUtil.loadFalseAdmin("数据不完整")); return; } String[] channelIdArray = channelIds.split(","); for (String channelId : channelIdArray) { TVLiveCategoryChannelMap map = new TVLiveCategoryChannelMap(); map.setWeight(weight); map.setCategoryId(categoryId); map.setChannelId(channelId); tvLiveCategoryChannelService.addMap(map); } out.print(JsonUtil.loadTrueAdmin("")); } @RequestMapping(value = "/deleteCategoryChannel", method = RequestMethod.POST) public void deleteCategoryChannel(String categoryId, String channelIds, HttpSession session, PrintWriter out) { if (StringUtil.isNullOrEmpty(categoryId) || StringUtil.isNullOrEmpty(channelIds)) { out.print(JsonUtil.loadFalseAdmin("数据不完整")); return; } String[] channelIdArray = channelIds.split(","); for (String channelId : channelIdArray) { TVLiveCategoryChannelMap map = tvLiveCategoryChannelService.selectByCategoryIdAndChannelId(categoryId, channelId); if (map != null) tvLiveCategoryChannelService.deleteByPrimaryKey(map.getId()); } out.print(JsonUtil.loadTrueAdmin("")); } @RequestMapping(value = "/updateCategoryChannelWeight", method = RequestMethod.POST) public void updateCategoryChannelWeight(String categoryId, String channelId, Integer weight, HttpSession session, PrintWriter out) { TVLiveCategoryChannelMap oldMap = tvLiveCategoryChannelService.selectByCategoryIdAndChannelId(categoryId, channelId); if (oldMap == null) { out.print(JsonUtil.loadFalseAdmin("分类的频道不存在")); return; } TVLiveCategoryChannelMap map = new TVLiveCategoryChannelMap(); map.setId(oldMap.getId()); map.setWeight(weight); tvLiveCategoryChannelService.update(map); out.print(JsonUtil.loadTrueAdmin("")); } //获取频道资源 /** * 获取频道的播放资源 * * @param session * @param id * @param out */ @RequestMapping(value = "/getChannelResourceList", method = RequestMethod.POST) public void getChannelResourceList(HttpSession session, String id, PrintWriter out) { if (StringUtil.isNullOrEmpty(id)) { out.print(JsonUtil.loadFalseAdmin("频道ID不能为空")); return; } List mapList = tvLiveChannelResourceService.listByChannelId(id); List voList = new ArrayList<>(); if (mapList != null) for (TVLiveChannelResourceMap map : mapList) { voList.add(TVLiveChannelResourceMapAdminVO.create(map)); } JSONObject data = new JSONObject(); data.put("data", gson.toJson(voList)); data.put("channelId", id); out.print(JsonUtil.loadTrueAdmin(data.toString())); } /** * 更新频道播放资源 * * @param session * @param id * @param resources * @param out */ @RequestMapping(value = "/updateChannelResources", method = RequestMethod.POST) public void updateChannelResources(HttpSession session, String id, String resources, PrintWriter out) { if (StringUtil.isNullOrEmpty(id)) { out.print(JsonUtil.loadFalseAdmin("频道ID不能为空")); return; } Type type = new TypeToken>() { }.getType(); List voList = gson.fromJson(resources, type); List list = new ArrayList<>(); for (TVLiveChannelResourceMapAdminVO vo : voList) { TVLiveChannelResourceMap map = new TVLiveChannelResourceMap(); map.setId(vo.getId()); map.setWeight(vo.getWeight()); map.setChannelId(id); map.setPlayUrl(vo.getPlayUrl()); map.setSubResourceName(vo.getName()); list.add(map); } List originMapList = tvLiveChannelResourceService.listByChannelId(id); if (originMapList == null) originMapList = new ArrayList<>(); List originMapIds = new ArrayList<>(); for (TVLiveChannelResourceMap map : originMapList) { originMapIds.add(map.getId()); } //更新需要更新的 for (int i = 0; i < list.size(); i++) { if (list.get(i).getId() != null && originMapIds.contains(list.get(i).getId())) { originMapIds.remove(list.get(i).getId()); tvLiveChannelResourceService.update(list.get(i)); list.remove(i--); } } //删除不需要更新的 for (String mid : originMapIds) { tvLiveChannelResourceService.delete(mid); } //添加新的 for (TVLiveChannelResourceMap map : list) { //咪咕不能手动添加 map.setResource(TVLiveResource.other); tvLiveChannelResourceService.add(map); } out.print(JsonUtil.loadTrueAdmin("")); } }