package com.ks.goldcorn.controller.admin; import com.google.gson.*; import com.ks.goldcorn.exception.GoldSourceException; import com.ks.goldcorn.pojo.DO.GoldCornAppInfo; import com.ks.goldcorn.pojo.DO.GoldCornConsumeSource; import com.ks.goldcorn.service.GoldCornAppManager; import com.ks.goldcorn.service.remote.GoldCornConsumeSourceService; import com.ks.goldcorn.vo.CommonSearchQuery; import net.sf.json.JSONArray; 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 org.yeshi.utils.JsonUtil; import org.yeshi.utils.StringUtil; import org.yeshi.utils.TimeUtil; import javax.annotation.Resource; import java.lang.reflect.Type; import java.util.Date; import java.util.List; @Controller @RequestMapping("admin/api/source/consume") public class ConsumeSourceController { Logger logger = LoggerFactory.getLogger(ConsumeSourceController.class); @Resource private GoldCornConsumeSourceService goldCornConsumeSourceService; @Resource private GoldCornAppManager goldCornAppManager; /** * @param query * @return */ @RequestMapping("list") @ResponseBody public String list(CommonSearchQuery query, String appCode) { JSONObject data = new JSONObject(); GoldCornAppInfo app = goldCornAppManager.selectByAppCode(appCode); Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonSerializer() { @Override public JsonElement serialize(Date arg0, Type arg1, JsonSerializationContext arg2) { if (arg0 != null) { return new JsonPrimitive(TimeUtil.getGernalTime(arg0.getTime(), "yyyy-MM-dd HH:mm")); } else { return new JsonPrimitive(""); } } } ).create(); List list = goldCornConsumeSourceService.searchByName(appCode, query.getKey(), query.getPage(), query.getLimit()); if (list != null) { for (GoldCornConsumeSource source : list) { source.setApp(app); } } long count = goldCornConsumeSourceService.countByName(appCode, query.getKey()); data.put("count", count); data.put("data", gson.toJson(list)); return JsonUtil.loadTrueResult(data); } @RequestMapping("update") @ResponseBody public String update(GoldCornConsumeSource source) { if (source == null || source.getId() == null) { return JsonUtil.loadFalseResult("id不能为空"); } try { goldCornConsumeSourceService.updateSource(source); } catch (GoldSourceException e) { return JsonUtil.loadFalseResult(e.getCode(), e.getMessage()); } return JsonUtil.loadTrueResult("更新成功"); } @RequestMapping("add") @ResponseBody public String add(GoldCornConsumeSource source, String appCode) { if (source == null) { return JsonUtil.loadTrueResult("数据不能为空"); } if (StringUtil.isNullOrEmpty(appCode)) { return JsonUtil.loadTrueResult("请选择应用"); } GoldCornAppInfo app = goldCornAppManager.selectByAppCode(appCode); if (app == null) { return JsonUtil.loadTrueResult("应用不存在"); } source.setAppId(app.getId()); try { goldCornConsumeSourceService.addSource(source); return JsonUtil.loadTrueResult(""); } catch (GoldSourceException e) { return JsonUtil.loadFalseResult(e.getCode(), e.getMessage()); } } @RequestMapping("delete") @ResponseBody public String delete(String ids) { JSONArray array = null; try { array = JSONArray.fromObject(ids); } catch (Exception e) { } if (array == null || array.size() == 0) { return JsonUtil.loadFalseResult("id为空"); } for (int i = 0; i < array.size(); i++) { goldCornConsumeSourceService.deleteSource(array.optLong(i)); } return JsonUtil.loadTrueResult(""); } }