package com.yeshi.fanli.controller.client.v1; import java.io.IOException; import java.io.PrintWriter; 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 org.yeshi.utils.entity.FileUploadResult; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.TypeAdapter; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; import com.yeshi.fanli.entity.accept.AcceptData; import com.yeshi.fanli.entity.bus.activity.ActivityShareResult; import com.yeshi.fanli.entity.bus.activity.RecommendActivity; import com.yeshi.fanli.exception.ActivityException; import com.yeshi.fanli.exception.share.UserShareGoodsRecordException; import com.yeshi.fanli.service.inter.activity.ActivityService; import com.yeshi.fanli.util.Constant; import net.sf.json.JSONObject; /** * 动态 * * @author Administrator * */ @Controller @RequestMapping("api/v1/activity") public class ActivityController { @Resource private ActivityService activityService; private Gson getGson() { GsonBuilder gb = JsonUtil.getConvertBigDecimalToStringSubZeroBuilder(new GsonBuilder()); gb.excludeFieldsWithoutExposeAnnotation(); gb.registerTypeAdapter(Date.class, new TypeAdapter() { @Override public void write(JsonWriter out, Date value) throws IOException { 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) + "天前"; } out.value(desc); } } @Override public Date read(JsonReader in) throws IOException { return new Date(); } }); Gson gson = gb.create(); return gson; } /** * 获取动态推荐 * * @param acceptData * @param page * -页码 * @param out */ @RequestMapping(value = "getRecommendActivity", method = RequestMethod.POST) public void getRecommendActivity(AcceptData acceptData, int page, PrintWriter out) { if (page <= 0) page = 1; List list = activityService.getRecommendActivityListCache(page, Constant.PAGE_SIZE); long count = activityService.getRecommendActivityCountCache(); JSONObject data = new JSONObject(); data.put("data", getGson().toJson(list)); data.put("count", count); out.print(JsonUtil.loadTrueResult(data)); } /** * 分享商品 * * @param acceptData * @param uid * @param out */ @RequestMapping(value = "shareGoods", method = RequestMethod.POST) public void shareGoods(AcceptData acceptData, long uid, long activityId, PrintWriter out) { try { ActivityShareResult result = activityService.shareActivityGoods(uid, activityId); JSONObject data = new JSONObject(); data.put("shareTitle", result.getTitle()); List imgList = new ArrayList<>(); for (FileUploadResult uploadResult : result.getImgList()) imgList.add(uploadResult.getUrl()); data.put("shareImgs", imgList); out.print(JsonUtil.loadTrueResult(data)); activityService.addShareCount(activityId, 1); } catch (UserShareGoodsRecordException e) { out.print(JsonUtil.loadFalseResult(e.getCode(), e.getMessage())); } catch (ActivityException e) { out.print(JsonUtil.loadFalseResult(e.getCode(), e.getMessage())); } } /** * 分享商品 * * @param acceptData * @param uid * @param out */ @RequestMapping(value = "sharegoodsv2", method = RequestMethod.POST) public void shareGoodsV2(AcceptData acceptData, long uid, long activityId, PrintWriter out) { try { JSONObject data = activityService.shareGoodsV2(uid, activityId); out.print(JsonUtil.loadTrueResult(data)); activityService.addShareCount(activityId, 1); } catch (UserShareGoodsRecordException e) { out.print(JsonUtil.loadFalseResult(e.getCode(), e.getMessage())); } catch (ActivityException e) { out.print(JsonUtil.loadFalseResult("分享失败")); e.printStackTrace(); } } /** * 分享商品(伴随图片的MD5) * * @param acceptData * @param uid * @param activityId * @param out */ @RequestMapping(value = "shareGoodsWithEncryptInfo", method = RequestMethod.POST) public void shareGoodsWithEncryptInfo(AcceptData acceptData, long uid, long activityId, PrintWriter out) { try { ActivityShareResult result = activityService.shareActivityGoods(uid, activityId); JSONObject data = new JSONObject(); data.put("shareTitle", result.getTitle()); data.put("shareImgs", result.getImgList()); out.print(JsonUtil.loadTrueResult(data)); } catch (UserShareGoodsRecordException e) { out.print(JsonUtil.loadFalseResult(e.getCode(), e.getMessage())); } catch (ActivityException e) { out.print(JsonUtil.loadFalseResult(e.getCode(), e.getMessage())); } } /** * 生成邀请图片 * * @param acceptData * @param uid * @param out */ @RequestMapping(value = "inviteImg", method = RequestMethod.POST) public void inviteImg(AcceptData acceptData, long uid, long activityId, PrintWriter out) { try { ActivityShareResult result = activityService.shareInviteImg(uid, activityId); List imgList = new ArrayList<>(); for (FileUploadResult img : result.getImgList()) imgList.add(img.getUrl()); JSONObject data = new JSONObject(); data.put("shareTitle", result.getTitle()); data.put("shareImgs", imgList); out.print(JsonUtil.loadTrueResult(data)); activityService.addShareCount(activityId, 1); } catch (ActivityException e) { out.print(JsonUtil.loadFalseResult(e.getCode(), e.getMessage())); } } @RequestMapping(value = "addShareCount", method = RequestMethod.POST) public void addShareCount(AcceptData acceptData, long id, PrintWriter out) { activityService.addShareCount(id, 1); RecommendActivity activity = activityService.getRecommendActivityById(id); JSONObject data = new JSONObject(); data.put("shareCount", activity.getShareCount()); out.print(JsonUtil.loadTrueResult(data)); } }