package com.yeshi.buwan.controller.admin.api; import com.google.gson.*; import com.yeshi.buwan.domain.VideoResource; import com.yeshi.buwan.service.imp.VideoResourceService; import com.yeshi.buwan.vo.video.CommonVideoSearchQuery; import net.sf.json.JSONObject; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.yeshi.utils.JsonUtil; import org.yeshi.utils.TimeUtil; import com.google.gson.reflect.TypeToken; 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; import java.util.Map; import java.util.stream.Collectors; import com.yeshi.buwan.domain.video.VideoResourceInfoMap; import com.yeshi.buwan.service.inter.video.VideoResourceInfoMapService; @Controller @RequestMapping("admin/new/api/video/video_resource_info_map") public class VideoResourceInfoMapAdminController { @Resource private VideoResourceInfoMapService videoResourceInfoMapService; @Resource private VideoResourceService videoResourceService; private String loadPrint(String callback, String root) { return root; } @RequestMapping("list") public void list(CommonVideoSearchQuery query, int page, int limit, String callback, PrintWriter out) { List list = videoResourceInfoMapService.listByDetailSystemId(query.getDetailSystemId(), page, limit); if (list != null) { List ridList = new ArrayList<>(); for (VideoResourceInfoMap map : list) { ridList.add(map.getResourceId() + ""); } List resourceList = videoResourceService.getResource(ridList); Map map = resourceList.stream().collect(Collectors.toMap(VideoResource::getId, videoResource -> videoResource)); for (VideoResourceInfoMap info : list) { info.setResource(map.get(info.getResourceId() + "")); } } long count = videoResourceInfoMapService.countByDetailSystemId(query.getDetailSystemId()); JSONObject data = new JSONObject(); Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonSerializer() { @Override public JsonElement serialize(Date date, Type type, JsonSerializationContext jsonSerializationContext) { return date == null ? new JsonPrimitive("") : new JsonPrimitive(TimeUtil.getGernalTime(date.getTime(), "yyyy.MM.dd HH:mm")); } }).create(); data.put("list", gson.toJson(list)); data.put("count", count); out.print(loadPrint(callback, JsonUtil.loadTrueResult(data))); } @RequestMapping("delete") public void delete(String ids, String callback, PrintWriter out) { Type type = new TypeToken>() { }.getType(); List idList = new Gson().fromJson(ids, type); videoResourceInfoMapService.delete(idList); out.print(loadPrint(callback, JsonUtil.loadTrueResult(""))); } @RequestMapping("add") public void add(VideoResourceInfoMap bean, HttpSession session, String callback, PrintWriter out) { try { videoResourceInfoMapService.add(bean); out.print(loadPrint(callback, JsonUtil.loadTrueResult(""))); } catch (Exception e) { out.print(loadPrint(callback, JsonUtil.loadFalseResult(e.getMessage()))); } } @RequestMapping("get") public void get(String id, HttpSession session, String callback, PrintWriter out) { VideoResourceInfoMap entity = videoResourceInfoMapService.get(id); entity.setResource(videoResourceService.getResource(entity.getResourceId() + "")); if (entity != null) { out.print(loadPrint(callback, JsonUtil.loadTrueResult(entity))); } else { out.print(loadPrint(callback, JsonUtil.loadFalseResult("ID不存在"))); } } @RequestMapping("update") public void update(VideoResourceInfoMap bean, HttpSession session, String callback, PrintWriter out) { if (bean.getId() == null) { out.print(loadPrint(callback, JsonUtil.loadFalseResult("ID不能为空"))); } try { videoResourceInfoMapService.update(bean); } catch (Exception e) { out.print(loadPrint(callback, JsonUtil.loadFalseResult(e.getMessage()))); } out.print(loadPrint(callback, JsonUtil.loadTrueResult(""))); } }