package com.yeshi.buwan.controller.admin.api; import com.google.gson.*; import com.yeshi.buwan.domain.recommend.HomeRecommendSpecial; import com.yeshi.buwan.domain.recommend.SuperHomeRecommendSpecial; import com.yeshi.buwan.domain.web.DetailSystemSelect; import com.yeshi.buwan.dto.recommend.HomeRecommendSpecialDTO; import com.yeshi.buwan.service.inter.recommend.HomeRecommendSpecialService; import com.yeshi.buwan.util.Constant; import com.yeshi.buwan.util.StringUtil; import com.yeshi.buwan.util.SystemUtil; import com.yeshi.buwan.util.TimeUtil; 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.math.BigDecimal; import java.util.Date; import java.util.List; @Controller @RequestMapping("admin/new/api/homerecommend") public class HomeRecommendSpecialController { @Resource private HomeRecommendSpecialService homeRecommendSpecialService; private Gson getGson() { Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonSerializer() { @Override public JsonElement serialize(Date value, Type theType, JsonSerializationContext context) { if (value == null) { return new JsonPrimitive(""); } else { return new JsonPrimitive(TimeUtil.getGernalTime(value.getTime(), "yyyy.MM.dd HH:mm")); } } }).create(); return gson; } /** * 获取专题列表 * * @param session * @param out */ @RequestMapping(value = "/getSpcialList", method = RequestMethod.POST) public void getSpcialList(HttpSession session, String detailSystemId, String key, int page, PrintWriter out) { key = StringUtil.isNullOrEmpty(key) ? null : key.trim(); detailSystemId = StringUtil.isNullOrEmpty(detailSystemId) ? null : detailSystemId.trim(); if (detailSystemId != null && detailSystemId.equalsIgnoreCase("0")) detailSystemId = null; String systemId = SystemUtil.getAdminSelectedSystemId(session); List list = homeRecommendSpecialService.list(systemId, detailSystemId, key, page, Constant.pageCount); if (list != null) for (HomeRecommendSpecialDTO dto : list) { List dssList = dto.getDetailSystemSelectList(); for (DetailSystemSelect select : dssList) { select.getDetailSystem().setSystem(null); select.getDetailSystem().setInfo(null); } } long count = homeRecommendSpecialService.count(systemId, detailSystemId, key); JSONObject data = new JSONObject(); data.put("data", getGson().toJson(list)); data.put("count", count); data.put("pageEntity", new PageEntity(page, Constant.pageCount, (int) count)); JSONObject object = new JSONObject(); object.put("code", 0); object.put("data", data); out.print(object); } @RequestMapping(value = "/getSpecial", method = RequestMethod.POST) public void getSpecial(String id, PrintWriter out) { HomeRecommendSpecial special = homeRecommendSpecialService.getSpecial(id); JSONObject object = new JSONObject(); if (special != null) { object.put("code", 0); object.put("data", new Gson().toJson(special)); } else { object.put("code", 1); object.put("msg", "专题不存在"); } out.print(object); } @RequestMapping(value = "/addSpecial", method = RequestMethod.POST) public void addSpecial(String name, int weight, String bannerSizeRate, String dataKey, String detailsystemids, HttpSession session, PrintWriter out) { HomeRecommendSpecial special = new HomeRecommendSpecial(); special.setName(name); special.setWeight(weight); special.setDataKey(dataKey); special.setBannerSizeRate(new BigDecimal(bannerSizeRate)); special.setSystemId(SystemUtil.getAdminSelectedSystemId(session)); JSONObject object = new JSONObject(); try { homeRecommendSpecialService.addSpecial(special); //主键 if (!StringUtil.isNullOrEmpty(detailsystemids)) { String[] ds = detailsystemids.split(","); for (String d : ds) { SuperHomeRecommendSpecial s = new SuperHomeRecommendSpecial(); s.setSpecialId(special.getId()); s.setSpecial(special); s.setDetailSystemId(d); homeRecommendSpecialService.addSuperSpecial(s); } } object.put("code", 0); } catch (Exception e) { object.put("code", 1); object.put("msg", e.getMessage()); } out.print(object); } @RequestMapping(value = "/deleteSpcial", method = RequestMethod.POST) public void deleteSpecial(String ids, PrintWriter out) { String[] sts = ids.split(","); for (String id : sts) { homeRecommendSpecialService.deleteSpecial(id); } JSONObject object = new JSONObject(); object.put("code", 0); object.put("msg", "操作成功"); out.print(object); } @RequestMapping(value = "/updateSpecial", method = RequestMethod.POST) public void updateSpecial(String id, String name, int weight, String bannerSizeRate, HttpSession session, PrintWriter out) { HomeRecommendSpecial special = new HomeRecommendSpecial(); special.setId(id); special.setName(name); special.setWeight(weight); special.setBannerSizeRate(new BigDecimal(bannerSizeRate)); JSONObject object = new JSONObject(); try { homeRecommendSpecialService.updateSpecial(special); //拉取super List superSpecialList = homeRecommendSpecialService.listSuperSpecialBySpecialId(id); for (SuperHomeRecommendSpecial s : superSpecialList) { s.setShowName(special.getName()); homeRecommendSpecialService.updateSuperSpecial(s); } object.put("code", 0); } catch (Exception e) { object.put("code", 1); object.put("msg", e.getMessage()); } out.print(object); } @RequestMapping(value = "/addSuperSpecial", method = RequestMethod.POST) public void addSuperSpecial(String id, String detailSystemId, HttpSession session, PrintWriter out) { SuperHomeRecommendSpecial superHomeRecommendSpecial = new SuperHomeRecommendSpecial(); superHomeRecommendSpecial.setSpecialId(id); superHomeRecommendSpecial.setDetailSystemId(detailSystemId); JSONObject object = new JSONObject(); try { homeRecommendSpecialService.addSuperSpecial(superHomeRecommendSpecial); object.put("code", 0); } catch (Exception e) { object.put("code", 1); object.put("msg", e.getMessage()); } out.print(object); } @RequestMapping(value = "/deleteSuperSpecial", method = RequestMethod.POST) public void deleteSuperSpecial(String id, String detailSystemId, PrintWriter out) { homeRecommendSpecialService.deleteSuperSpecial(id, detailSystemId); JSONObject object = new JSONObject(); object.put("code", 0); object.put("msg", "操作成功"); out.print(object); } }