yujian
2020-05-17 de4110d3d3944ffcd797fd8c43f0d455cc731f84
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package com.yeshi.fanli.controller.wxmp.v1;
 
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.StringUtil;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yeshi.fanli.dto.WXMPAcceptData;
import com.yeshi.fanli.entity.bus.homemodule.Special;
import com.yeshi.fanli.entity.bus.homemodule.SpecialLabel;
import com.yeshi.fanli.entity.common.JumpDetailV2;
import com.yeshi.fanli.entity.dynamic.GoodsPicture;
import com.yeshi.fanli.entity.dynamic.WXMPDynamicInfo;
import com.yeshi.fanli.exception.dynamic.WXMPDynamicInfoException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.dynamic.WXMPDynamicInfoService;
import com.yeshi.fanli.service.inter.homemodule.SpecialService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.JumpDetailUtil;
import com.yeshi.fanli.util.jd.JDApiUtil;
import com.yeshi.fanli.vo.homemodule.SpecialVO;
import com.yeshi.fanli.vo.msg.ClientTextStyleVO;
 
import net.sf.json.JSONObject;
 
@Controller("WXMPDynamicController")
@RequestMapping("/wxmp/api/v1/dynamic")
public class DynamicController {
 
    @Resource
    private WXMPDynamicInfoService wxmpDynamicInfoService;
 
    @Resource
    private SpecialService specialService;
 
    /**
     * 首页专题
     * 
     * @param acceptData
     * @param uid
     * @param out
     */
    @RequestMapping(value = "getHotList")
    public void getHotList(WXMPAcceptData acceptData, int page, PrintWriter out) {
 
        List<WXMPDynamicInfo> infoList = wxmpDynamicInfoService.listDynamicInfo(page, Constant.PAGE_SIZE);
        for (WXMPDynamicInfo info : infoList) {
            if (info.getImgs() != null)
                for (GoodsPicture goodsPicture : info.getImgs()) {
                    JSONObject params = new JSONObject();
                    if (goodsPicture.getGoodsVO() != null && goodsPicture.getJumpDetail() != null) {
                        params.put("id", goodsPicture.getGoodsVO().getGoodsId());
                        goodsPicture.setJumpDetail(null);
                        goodsPicture.getGoodsVO().setImgList(null);
                        goodsPicture.getGoodsVO().setShopInfo(null);
                        goodsPicture.getGoodsVO().setCouponInfoList(null);
                    }
                }
        }
 
        long count = wxmpDynamicInfoService.countDynamicInfo();
        Gson gson = JsonUtil.getConvertDateToShortNameBuilder(JsonUtil
                .getConvertBigDecimalToStringSubZeroBuilder(new GsonBuilder().excludeFieldsWithoutExposeAnnotation()))
                .create();
        try {
            JSONObject data = new JSONObject();
            data.put("list", gson.toJson(infoList));
            data.put("count", count);
            data.put("page",page);
            out.print(JsonUtil.loadTrueResult(data));
        } catch (Exception e) {
            out.print(JsonUtil.loadFalseResult(1, "获取数据失败"));
            try {
                LogHelper.errorDetailInfo(e);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }
 
    @RequestMapping(value = "copyText")
    public void copyText(WXMPAcceptData acceptData, String id, Long uid, PrintWriter out) {
        if (uid == null) {
            out.print(JsonUtil.loadFalseResult("用户未登录"));
            return;
        }
 
        if (StringUtil.isNullOrEmpty(id)) {
            out.print(JsonUtil.loadFalseResult("商品ID未空"));
            return;
        }
 
        try {
            String content = wxmpDynamicInfoService.copyContent(id, uid);
            JSONObject data = new JSONObject();
            data.put("content", content);
            out.print(JsonUtil.loadTrueResult(data));
        } catch (WXMPDynamicInfoException e) {
            out.print(JsonUtil.loadFalseResult(e.getCode(), e.getMessage()));
        }
    }
 
    @RequestMapping(value = "getActivityList")
    public void getActivityList(WXMPAcceptData acceptData, int page, PrintWriter out) {
        // 平台区分
        int platformCode = Constant.getPlatformCode(acceptData.getPlatform());
        List<String> listKey = new ArrayList<String>();
        listKey.add("special_channel_jd");
        listKey.add("special_channel_pdd");
 
        List<SpecialVO> 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++) {
            SpecialVO special = list.get(i);
            if (special.getJumpDetail() != null) {
                special.setJumpDetail(JumpDetailUtil.getWXMPJumDetail(special.getJumpDetail()));
            }
 
            if (special.getState() == 1L) {
                list.remove(i--);
            } else {
                if (special.getStartTime() != null && special.getEndTime() != null) {
                    if (time < special.getStartTime().getTime() || time > special.getEndTime().getTime()) {
                        list.remove(i--);
                    } else// 设置倒计时
                    {
                        special.setCountDownTime((special.getEndTime().getTime() - time) / 1000);
                    }
                }
 
                List<SpecialLabel> listLabels = special.getListLabels();
                if (listLabels != null && !listLabels.isEmpty()) {
                    List<ClientTextStyleVO> 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("page", page);
        data.put("list", gson.toJson(list));
        out.print(JsonUtil.loadTrueResult(data));
    }
 
    @RequestMapping(value = "getActivityDetail")
    public void getActivityDetail(WXMPAcceptData acceptData, Long uid, Long id, PrintWriter out) {
        if (uid == null)
            out.print(JsonUtil.loadFalseResult("用户未登录"));
        if (id == null)
            out.print(JsonUtil.loadFalseResult("活动ID为空"));
 
        Special special = specialService.selectByPrimaryKey(id);
        if (special == null) {
            out.print(JsonUtil.loadFalseResult("专题不存在"));
            return;
        }
 
        JumpDetailV2 jumpDetail = special.getJumpDetail();
        jumpDetail.setActivity(null);
        jumpDetail.setController(null);
        String params = special.getParams();
        if (jumpDetail.getType().equalsIgnoreCase("jd")) {// 京东的专题活动
            JSONObject paramsJson = JSONObject.fromObject(params);
            // 京东转链
            String url = paramsJson.optString("url");
            String link = JDApiUtil.convertLinkWithSubUnionId(url, null, JDApiUtil.POSITION_FANLI + "", uid + "");
 
            if (StringUtil.isNullOrEmpty(link)) {
                out.print(JsonUtil.loadFalseResult("转链失败"));
                return;
            }
            paramsJson.put("url", link);
            params = paramsJson.toString();
        }
 
        JSONObject data = new JSONObject();
 
        GsonBuilder gsonBuilder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation();
        Gson gson = gsonBuilder.create();
        data.put("jumpDetail", gson.toJson(jumpDetail));
        data.put("params", params);
        out.print(JsonUtil.loadTrueResult(data));
    }
 
}