喻健
2018-11-27 c113fb5b3be1e872b84799c79e76607c40c2c4ba
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
package com.yeshi.fanli.controller.apph5;
 
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.taobao.TbImgUtil;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.push.DeviceActive;
import com.yeshi.fanli.entity.push.PushGoods;
import com.yeshi.fanli.entity.push.PushGoodsGroup;
import com.yeshi.fanli.entity.push.PushGoodsRecord;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.goods.CommonGoods;
import com.yeshi.fanli.service.inter.hongbao.HongBaoManageService;
import com.yeshi.fanli.service.inter.push.DeviceActiveService;
import com.yeshi.fanli.service.inter.push.PushGoodsGroupService;
import com.yeshi.fanli.service.inter.push.PushGoodsRecordService;
import com.yeshi.fanli.service.inter.push.PushGoodsService;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
 
/**
 * 今日推送
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("api/apph5/v1/push")
public class AppH5PushController {
 
    @Resource(name = "taskExecutor")
    private TaskExecutor executor;
 
    @Resource
    private HongBaoManageService manageService;
 
    @Resource
    private PushGoodsService pushGoodsService;
 
    @Resource
    private PushGoodsGroupService pushGoodsGroupService;
 
    @Resource
    private DeviceActiveService deviceActiveService;
 
    @Resource
    private PushGoodsRecordService pushGoodsRecordService;
 
    /**
     * 推送商品详情页
     * 
     * @param callback
     * @param id
     * @param out
     */
    @RequestMapping(value = "getpushgoods")
    public void getPushGoodsDetail(String callback, AcceptData acceptData, String deviceToken, Long id, PrintWriter out) {
 
        if (id == null) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("参数不正确"));
            return;
        }
 
        try {
            PushGoods pushGoods = pushGoodsService.selectByPrimaryKey(id);
            if (pushGoods == null) {
                JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("此推送记录已不存在"));
                return;
            }
 
            JSONArray array = new JSONArray();
            Gson gson = JsonUtil.getConvertBigDecimalToStringSubZeroBuilder(new GsonBuilder())
                    .excludeFieldsWithoutExposeAnnotation().setDateFormat("yyyy-MM-dd").create();
 
            List<PushGoodsGroup> list = pushGoodsGroupService.getAllInfoByPushId(id);
 
            if (list != null && list.size() > 0) {
 
                Map<String, String> map = manageService.convertMap();
                String proportion = map.get("hongbao_goods_proportion");
                String fcRate = map.get("hongbao_fc_ratio");
 
                for (PushGoodsGroup pushGoodsGroup : list) {
                    CommonGoods commonGoods = pushGoodsGroup.getCommonGoods();
                    if (commonGoods == null) {
                        continue;
                    }
 
                    TaoBaoGoodsBrief goodsBrief = TaoBaoUtil.convert(commonGoods);
                    int biz30day = goodsBrief.getBiz30day();
                    if (biz30day >= 10000) {
                        double sales = biz30day;
                        String salesCountMidea = String.format("%.1f", sales / 10000);
                        goodsBrief.setSalesCount(salesCountMidea + "万");
                    } else {
                        goodsBrief.setSalesCount(biz30day + "");
                    }
 
                    // 改变图片尺寸
                    String pictUrl = commonGoods.getPicture();
                    if (!StringUtil.isNullOrEmpty(pictUrl) && !pictUrl.contains("320x320")) {
                        commonGoods.setPicture(TbImgUtil.getTBSize320Img(pictUrl));
                    }
 
                    array.add(gson.toJson(TaoBaoUtil.getTaoBaoGoodsBriefExtra(goodsBrief, proportion, fcRate, null)));
                }
            }
 
            JSONObject data = new JSONObject();
            data.put("title", pushGoods.getTitle());
            data.put("content", pushGoods.getContent());
            data.put("result_list", array);
 
            JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data));
 
            DeviceActive deviceActive = deviceActiveService.getDeviceByDeviceAndPlatform(acceptData.getDevice(),
                    deviceToken, acceptData.getPlatform());
 
            // 列表参数、设备参数信息不为空
            if (deviceActive != null) {
                // 记录访问信息
                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
 
                            List<PushGoodsRecord> listRecord = pushGoodsRecordService.listByPushIdAndDeviceId(
                                    pushGoods.getId(), deviceActive.getId());
                            if (listRecord == null || listRecord.size() == 0) {
                                PushGoodsRecord pushGoodsRecord = new PushGoodsRecord();
 
                                pushGoodsRecord.setCreateTime(new Date());
                                pushGoodsRecord.setPushGoods(pushGoods);
                                pushGoodsRecord.setDeviceActive(deviceActive);
                                pushGoodsRecordService.insert(pushGoodsRecord);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
}