package com.yeshi.makemoney.app.controller.admin.msg; import com.google.gson.*; import com.yeshi.makemoney.app.entity.msg.UserMsgType; import com.yeshi.makemoney.app.vo.AcceptAdminData; import com.yeshi.makemoney.app.vo.admin.msg.AppPageNotifyAdminVO; import net.sf.json.JSONArray; 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.lang.reflect.Type; import java.util.Date; import java.util.List; import com.yeshi.makemoney.app.entity.msg.AppPageNotifyMsg; import com.yeshi.makemoney.app.service.inter.msg.AppPageNotifyMsgService; import com.yeshi.makemoney.app.service.query.msg.AppPageNotifyMsgQuery; @Controller @RequestMapping("/admin/api/msg/appnotify") public class AppPageNotifyMsgAdminController { @Resource private AppPageNotifyMsgService appPageNotifyMsgService; @ResponseBody @RequestMapping("list") public String list(AppPageNotifyMsgQuery query, int page, int limit) { List list = appPageNotifyMsgService.list(query, page, limit); long count = appPageNotifyMsgService.count(query); 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")); } }).registerTypeAdapter(AppPageNotifyMsg.AppPageNotifyMsgType.class, new JsonSerializer() { @Override public JsonElement serialize(AppPageNotifyMsg.AppPageNotifyMsgType date, Type type, JsonSerializationContext jsonSerializationContext) { return date == null ? new JsonPrimitive("") : new JsonPrimitive(date.getName()); } }).create(); data.put("list", gson.toJson(list)); data.put("count", count); return JsonUtil.loadTrueResult(data); } @ResponseBody @RequestMapping("delete") public String delete(String ids) { Type type = new TypeToken>() { }.getType(); List idList = new Gson().fromJson(ids, type); appPageNotifyMsgService.delete(idList); return JsonUtil.loadTrueResult(""); } @ResponseBody @RequestMapping("add") public String add(AppPageNotifyAdminVO bean, AcceptAdminData acceptAdminData) { try { appPageNotifyMsgService.add(bean.toEntity(acceptAdminData.getSystem())); return JsonUtil.loadTrueResult(""); } catch (Exception e) { return JsonUtil.loadFalseResult(e.getMessage()); } } @ResponseBody @RequestMapping("get") public String get(String id, HttpSession session) { AppPageNotifyMsg entity = appPageNotifyMsgService.get(id); if (entity != null) { return JsonUtil.loadTrueResult(AppPageNotifyAdminVO.create(entity)); } else { return JsonUtil.loadFalseResult("ID不存在"); } } @ResponseBody @RequestMapping("update") public String update(AppPageNotifyAdminVO bean, HttpSession session) { if (bean.getId() == null) { return JsonUtil.loadFalseResult("ID不能为空"); } try { AppPageNotifyMsg entity = bean.toEntity(null); entity.setMd5(entity.toMD5()); appPageNotifyMsgService.update(entity); } catch (Exception e) { return JsonUtil.loadFalseResult(e.getMessage()); } return JsonUtil.loadTrueResult(""); } @ResponseBody @RequestMapping("getMsgTypes") public String getMsgTypes() { JSONArray array = new JSONArray(); for (AppPageNotifyMsg.AppPageNotifyMsgType type : AppPageNotifyMsg.AppPageNotifyMsgType.values()) { JSONObject data = new JSONObject(); data.put("key", type.name()); data.put("value", type.getName()); array.add(data); } return JsonUtil.loadTrueResult(array); } }