package com.yeshi.fanli.controller.client;
|
|
import java.io.PrintWriter;
|
import java.lang.reflect.Type;
|
import java.util.ArrayList;
|
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.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.hongbao.HongBaoManageService;
|
import com.yeshi.fanli.util.Constant;
|
import com.yeshi.fanli.util.taobao.DaTaoKeUtil;
|
import com.yeshi.fanli.vo.dynamic.DynamicClassVO;
|
|
import net.sf.json.JSONObject;
|
|
/**
|
* 动态
|
*
|
* @author Administrator
|
*
|
*/
|
@Controller
|
@RequestMapping("api/v1/dynamic")
|
public class DynamicController {
|
|
@Resource
|
private HongBaoManageService hongBaoManageService;
|
|
@Resource
|
private TaoBaoGoodsBriefService taoBaoGoodsBriefService;
|
|
@Resource
|
private JumpDetailV2Service jumpDetailV2Service;
|
|
@Resource
|
private DynamicInfoService dynamicInfoService;
|
|
/**
|
* 查询顶部分类
|
*
|
* @param acceptData
|
* @param page
|
* @param cid
|
* @param out
|
*/
|
@RequestMapping(value = "getClass", method = RequestMethod.POST)
|
public void getClass(AcceptData acceptData, Integer cid, PrintWriter out) {
|
try {
|
// ios 只返回子集分类
|
if (cid != null) {
|
List<GoodsClass> list = new ArrayList<GoodsClass>();
|
|
switch (cid) {
|
case 1:
|
list.add(new GoodsClass(0L, "今日单品"));
|
list.addAll(DaTaoKeUtil.goodsClasses);
|
break;
|
case 2:
|
break;
|
case 3:
|
break;
|
case 4:
|
break;
|
default:
|
break;
|
}
|
|
JSONObject data = new JSONObject();
|
data.put("list", JsonUtil.getApiCommonGson().toJson(list));
|
out.print(JsonUtil.loadTrueResult(data));
|
return;
|
}
|
|
// Android 返回分类以及顶部数据
|
List<DynamicClassVO> list = new ArrayList<DynamicClassVO>();
|
DynamicClassVO c1 = new DynamicClassVO();
|
c1.setId(1L);
|
c1.setName("热销单品");
|
|
List<GoodsClass> listSub = new ArrayList<GoodsClass>();
|
listSub.add(new GoodsClass(0L, "今日单品"));
|
listSub.addAll(DaTaoKeUtil.goodsClasses);
|
c1.setListSub(listSub);
|
|
DynamicClassVO c2 = new DynamicClassVO();
|
c2.setId(2L);
|
c2.setName("好货推荐");
|
c2.setListSub(new ArrayList<GoodsClass>());
|
|
DynamicClassVO c3 = new DynamicClassVO();
|
c3.setId(3L);
|
c3.setName("有家好店");
|
c3.setListSub(new ArrayList<GoodsClass>());
|
|
DynamicClassVO c4 = new DynamicClassVO();
|
c4.setId(4L);
|
c4.setName("邀请素材");
|
c4.setListSub(new ArrayList<GoodsClass>());
|
|
list.add(c1);
|
list.add(c2);
|
list.add(c3);
|
list.add(c4);
|
|
JSONObject data = new JSONObject();
|
data.put("list", JsonUtil.getApiCommonGson().toJson(list));
|
out.print(JsonUtil.loadTrueResult(data));
|
} catch (Exception e) {
|
out.print(JsonUtil.loadFalseResult("查询失败"));
|
e.printStackTrace();
|
}
|
}
|
|
@RequestMapping(value = "getList", method = RequestMethod.POST)
|
public void getList2(AcceptData acceptData, Integer page, Long cid, Long subId, PrintWriter out) {
|
try {
|
if (cid == null) {
|
out.print(JsonUtil.loadFalseResult("主分类id不能为空"));
|
return;
|
}
|
|
long count = 0;
|
List<DynamicInfo> list = dynamicInfoService.query((page - 1) * Constant.PAGE_SIZE, Constant.PAGE_SIZE, cid,
|
subId);
|
if (list == null) {
|
list = new ArrayList<DynamicInfo>();
|
} 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));
|
} catch (Exception e) {
|
out.print(JsonUtil.loadFalseResult("查询失败"));
|
e.printStackTrace();
|
}
|
}
|
|
@RequestMapping(value = "shareRecord", method = RequestMethod.POST)
|
public void shareRecord(AcceptData acceptData, String id, Long uid, PrintWriter out) {
|
try {
|
DynamicInfo dynamicInfo = dynamicInfoService.getById(id);
|
if (dynamicInfo == null) {
|
out.print(JsonUtil.loadFalseResult("该动态信息已不存在"));
|
return;
|
}
|
|
Integer shareCount = dynamicInfo.getShareCount();
|
dynamicInfo.setShareCount(shareCount + 1);
|
dynamicInfoService.updateShareCount(dynamicInfo);
|
|
JSONObject data = new JSONObject();
|
data.put("shareCount", shareCount +1);
|
out.print(JsonUtil.loadTrueResult(data));
|
|
} catch (Exception e) {
|
out.print(JsonUtil.loadFalseResult("记录失败"));
|
}
|
|
}
|
|
|
|
private Gson getGson() {
|
GsonBuilder gb = JsonUtil.getConvertBigDecimalToStringSubZeroBuilder(new GsonBuilder());
|
gb.excludeFieldsWithoutExposeAnnotation();
|
gb.registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
|
@Override
|
public JsonElement serialize(Date value, Type theType, JsonSerializationContext context) {
|
String desc = "";
|
if (value != null) {
|
// 判断是否是同一天
|
long old = value.getTime();
|
long now = System.currentTimeMillis();
|
long oldDay = old / (1000 * 60 * 60 * 24L);
|
long nowDay = now / (1000 * 60 * 60 * 24L);
|
if (oldDay == nowDay) {// 同一天
|
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 (nowDay - oldDay == 1) {
|
desc = "昨天";
|
} else {
|
desc = (nowDay - oldDay) + "天前";
|
}
|
return new JsonPrimitive(desc);
|
}
|
|
return new JsonPrimitive("");
|
}
|
});
|
|
Gson gson = gb.create();
|
return gson;
|
}
|
}
|