package com.yeshi.fanli.controller.client.v2; import java.io.PrintWriter; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.yeshi.utils.JsonUtil; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonPrimitive; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import com.yeshi.fanli.entity.accept.AcceptData; import com.yeshi.fanli.entity.bus.clazz.GoodsClass; import com.yeshi.fanli.entity.bus.homemodule.Special; import com.yeshi.fanli.entity.bus.homemodule.SpecialLabel; import com.yeshi.fanli.entity.dynamic.DynamicInfo; import com.yeshi.fanli.service.inter.common.JumpDetailV2Service; import com.yeshi.fanli.service.inter.dynamic.DynamicInfoService; import com.yeshi.fanli.service.inter.goods.TaoBaoGoodsBriefService; import com.yeshi.fanli.service.inter.homemodule.SpecialService; import com.yeshi.fanli.service.inter.order.config.HongBaoManageService; import com.yeshi.fanli.util.Constant; import com.yeshi.fanli.util.taobao.DaTaoKeUtil; import com.yeshi.fanli.vo.msg.ClientTextStyleVO; import net.sf.json.JSONObject; /** * 动态 * * @author Administrator * */ @Controller @RequestMapping("api/v2/dynamic") public class DynamicControllerV2 { @Resource private HongBaoManageService hongBaoManageService; @Resource private TaoBaoGoodsBriefService taoBaoGoodsBriefService; @Resource private JumpDetailV2Service jumpDetailV2Service; @Resource private DynamicInfoService dynamicInfoService; @Resource private SpecialService specialService; /** * 动态商品列表 * @param acceptData * @param page * @param cid * @param subId * @param out */ @RequestMapping(value = "getList", method = RequestMethod.POST) public void getList(AcceptData acceptData, Integer page, Long cid, Long subId, PrintWriter out) { if (cid == null) { out.print(JsonUtil.loadFalseResult("主分类id不能为空")); return; } if (cid != null && cid == 5) { // 活动主题 getSpecialList(acceptData, page, subId, out); return; } long count = 0; int platform = 1; if ("ios".equalsIgnoreCase(acceptData.getPlatform())) { platform = 2; } int version = Integer.parseInt(acceptData.getVersion()); List list = dynamicInfoService.queryV2(platform, version, (page - 1) * Constant.PAGE_SIZE, Constant.PAGE_SIZE, cid, subId); if (list == null) { list = new ArrayList(); } else { count = dynamicInfoService.count(cid, subId); } JSONObject data = new JSONObject(); data.put("count", count); data.put("list", getGson().toJson(list)); out.print(JsonUtil.loadTrueResult(data)); } /** * 活动列表 * @param acceptData * @param out */ private void getSpecialList(AcceptData acceptData, Integer page, Long subId, PrintWriter out) { if (subId == null) { out.print(JsonUtil.loadFalseResult("分类id不能为空")); return; } // 平台区分 int platformCode = Constant.getPlatformCode(acceptData.getPlatform()); List listKey = new ArrayList(); if (subId == 1) { // 淘宝 listKey.add("special_channel_tb"); } else if (subId == 2) { // 京东 listKey.add("special_channel_jd"); } else if (subId == 3) { // 拼多多 listKey.add("special_channel_pdd"); } else { // 全部 listKey.add("special_channel_tb"); listKey.add("special_channel_jd"); listKey.add("special_channel_pdd"); } List list = specialService.listByPlaceKeyHasLabel((page - 1) * Constant.PAGE_SIZE, Constant.PAGE_SIZE, listKey, platformCode, Integer.parseInt(acceptData.getVersion())); long time = System.currentTimeMillis(); // 删除尚未启用的过期的 for (int i = 0; i < list.size(); i++) { Special special = list.get(i); if (special.getState() == 1L) { list.remove(i--); } else { if (special.getStartTime() != null && special.getEndTime() != null) special.setTimeTask(true); else special.setTimeTask(false); if (special.isTimeTask()) { if (time < special.getStartTime().getTime() || time > special.getEndTime().getTime()) { list.remove(i--); } else// 设置倒计时 { special.setCountDownTime((special.getEndTime().getTime() - time) / 1000); } } List listLabels = special.getListLabels(); if (listLabels != null && !listLabels.isEmpty()) { List labels = new ArrayList<>(); for (SpecialLabel specialLabel: listLabels) { labels.add(new ClientTextStyleVO(specialLabel.getName(), specialLabel.getBgColor())); } special.setLabels(labels); } } } long count = specialService.countByPlaceKeyList( listKey, platformCode, Integer.parseInt(acceptData.getVersion())); GsonBuilder gsonBuilder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation(); Gson gson = gsonBuilder.create(); JSONObject data = new JSONObject(); data.put("count", count); data.put("list", gson.toJson(list)); out.print(JsonUtil.loadTrueResult(data)); } /** * 查询顶部分类 * * @param acceptData * @param page * @param cid * @param out */ @RequestMapping(value = "getClass", method = RequestMethod.POST) public void getClass(AcceptData acceptData, Integer cid, PrintWriter out) { // ios 只返回子集分类 if (cid != null) { List list = new ArrayList(); switch (cid) { case 1: list.add(new GoodsClass(0L, "今日单品")); list.addAll(DaTaoKeUtil.goodsClasses); break; case 2: break; case 3: break; case 4: break; case 5: list.add(new GoodsClass(0L, "全部")); list.add(new GoodsClass(1L, "淘宝")); list.add(new GoodsClass(2L, "京东")); list.add(new GoodsClass(3L, "拼多多")); break; default: break; } JSONObject data = new JSONObject(); data.put("list", JsonUtil.getApiCommonGson().toJson(list)); out.print(JsonUtil.loadTrueResult(data)); return; } // Android 返回分类以及顶部数据 List listSub = new ArrayList(); listSub.add(new GoodsClass(0L, "今日单品")); listSub.addAll(DaTaoKeUtil.goodsClasses); GoodsClass menu1 = new GoodsClass(1L, "热销"); menu1.setListSub(listSub); GoodsClass menu2 = new GoodsClass(2L, "推荐"); menu2.setListSub(new ArrayList()); GoodsClass menu3 = new GoodsClass(3L, "好店"); menu3.setListSub(new ArrayList()); GoodsClass menu4 = new GoodsClass(4L, "邀请"); menu4.setListSub(new ArrayList()); GoodsClass menu5 = new GoodsClass(5L, "活动"); List sub5 = new ArrayList(); sub5.add(new GoodsClass(0L, "全部")); sub5.add(new GoodsClass(1L, "淘宝")); sub5.add(new GoodsClass(2L, "京东")); sub5.add(new GoodsClass(3L, "拼多多")); menu5.setListSub(sub5); List list = new ArrayList(); list.add(menu1); list.add(menu5); list.add(menu2); list.add(menu3); list.add(menu4); JSONObject data = new JSONObject(); data.put("list", JsonUtil.getApiCommonGson().toJson(list)); out.print(JsonUtil.loadTrueResult(data)); } /** * 时间处理 * @return */ private Gson getGson() { GsonBuilder gb = JsonUtil.getConvertBigDecimalToStringSubZeroBuilder(new GsonBuilder()); gb.excludeFieldsWithoutExposeAnnotation(); gb.registerTypeAdapter(Date.class, new JsonSerializer() { @Override public JsonElement serialize(Date value, Type theType, JsonSerializationContext context) { String desc = ""; if (value != null) { // 判断是否是同一天 Calendar calendar = Calendar.getInstance(); calendar.setTime(value); int y1 = calendar.get(Calendar.YEAR);//获取年份 int d1 = calendar.get(Calendar.DAY_OF_YEAR);//获取年中第几天 Date nowDate = new Date(); Calendar calendar2 = Calendar.getInstance(); calendar2.setTime(nowDate); int y2 = calendar2.get(Calendar.YEAR);//获取年份 int d2 = calendar2.get(Calendar.DAY_OF_YEAR);//获取年中第几天 long old = value.getTime(); long now = nowDate.getTime(); if (y1 == y2) { if (d1 == d2) { long cha = now - old; if (cha < 1000 * 60 * 2L) { desc = "刚刚"; }else if (cha < 1000 * 60 * 60L) { desc = (cha / (1000 * 60)) + "分钟前"; }else { desc = (cha / (1000 * 60 * 60)) + "小时前"; } } else if (d2 - d1 == 1) { desc = "昨天"; } else { desc = (d2 - d1) + "天前"; } } else { int timeDistance = 0; for (int i = y1; i < y2; i++) { if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) { timeDistance += 366; // 闰年 } else { timeDistance += 365; // 不是闰年 } } desc = timeDistance + (d2 - d1) + "天前"; } return new JsonPrimitive(desc); } return new JsonPrimitive(""); } }); Gson gson = gb.create(); return gson; } }