admin
2018-12-10 5d59c0ad6e82b6255cf38c3dba715395af1d60a7
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package com.yeshi.fanli.controller.admin;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
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.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.tencentcloud.COSManager;
 
import com.yeshi.fanli.entity.bus.recommend.DynamicRecommend;
import com.yeshi.fanli.entity.bus.recommend.MaskKey;
import com.yeshi.fanli.entity.bus.recommend.RecommendReply;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.taobao.SearchFilter;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoBaoSearchResult;
import com.yeshi.fanli.service.MaskKeyService;
import com.yeshi.fanli.service.inter.goods.RecommendDetailsService;
import com.yeshi.fanli.service.inter.goods.RecommendReplyService;
import com.yeshi.fanli.service.inter.hongbao.HongBaoManageService;
import com.yeshi.fanli.service.inter.user.DynamicRecommendService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.tag.PageEntity;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.GsonUtil;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
 
import net.sf.json.JSONObject;
 
@Controller
@RequestMapping("admin/new/api/v1/dynamic")
public class DynamicRecommendAdminController {
 
    @Resource
    private DynamicRecommendService dynamicRecommendService;
 
    @Resource
    private RecommendReplyService recommendReplyService;
 
    @Resource
    private RecommendDetailsService recommendDetailsService;
 
    @Resource
    private MaskKeyService maskKeyService;
 
    @Resource
    private HongBaoManageService hongBaoManageService;
 
    @Resource
    private UserInfoService userInfoService;
 
    /**
     * 
     * @param key
     *            1.用户ID 2.用户名称 3.发布内容
     * @param page
     * @param out
     */
    @RequestMapping(value = "getDynamicRecommends", method = RequestMethod.POST)
    public void getDynamicRecommends(String key, int page, int type, PrintWriter out) {
        List<DynamicRecommend> dynamicRecommends = dynamicRecommendService.getDynamicRecommendList(key, type, page);
        int count = dynamicRecommendService.getCount(key, type);
        PageEntity pe = new PageEntity();
        pe.setPageIndex(page);
        pe.setPageSize(Constant.PAGE_SIZE);
        pe.setTotalCount(count);
        JSONObject data = new JSONObject();
        data.put("count", count);
        data.put("pe", pe);
        data.put("dynamicRecommends", GsonUtil.toJsonByUserAp(dynamicRecommends));
        out.append(JsonUtil.loadTrueResult(data));
    }
 
    @RequestMapping(value = "deleteDynamicRecommends", method = RequestMethod.POST)
    public void deleteDynamicRecommends(long[] drids, PrintWriter out) {
        for (long id : drids) {
            dynamicRecommendService.delete(id);
        }
        out.print(JsonUtil.loadTrueResult(""));
    }
 
    @RequestMapping(value = "getRecommendReplys", method = RequestMethod.POST)
    public void getRecommendReplys(long drid, int page, PrintWriter out) {
        List<RecommendReply> list = recommendReplyService.getRecommendReplys(drid, page);
        int count = recommendReplyService.getCount(drid);
        PageEntity pe = new PageEntity();
        pe.setPageIndex(page);
        pe.setPageSize(Constant.PAGE_SIZE);
        pe.setTotalCount(count);
        JSONObject data = new JSONObject();
        data.put("count", count);
        data.put("pe", pe);
        data.put("list", GsonUtil.toJsonByUserAp(list));
        out.append(JsonUtil.loadTrueResult(data));
    }
 
    @RequestMapping(value = "deleteRecommendReplys", method = RequestMethod.POST)
    public void deleteRecommendReplys(long[] rrids, PrintWriter out) {
        for (long id : rrids) {
            recommendReplyService.delete(id);
        }
        out.print(JsonUtil.loadTrueResult(""));
    }
 
    @RequestMapping(value = "getMaskKeys", method = RequestMethod.POST)
    public void getMaskKeys(String key, int page, PrintWriter out) {
        List<MaskKey> list = maskKeyService.getMaskKeys(key, page);
        int count = maskKeyService.getCount(key);
        PageEntity pe = new PageEntity();
        pe.setPageIndex(page);
        pe.setPageSize(Constant.PAGE_SIZE);
        pe.setTotalCount(count);
        JSONObject data = new JSONObject();
        data.put("count", count);
        data.put("pe", pe);
        data.put("list", GsonUtil.toJson(list));
        out.append(JsonUtil.loadTrueResult(data));
    }
 
    @RequestMapping(value = "addMaskKey", method = RequestMethod.POST)
    public void addMaskKey(String key, PrintWriter out) {
        long id = maskKeyService.addMaskKey(key);
        if (id > 0) {
            out.append(JsonUtil.loadTrueResult(""));
            return;
        } else {
            out.append(JsonUtil.loadFalseResult("添加失败"));
        }
    }
 
    @RequestMapping(value = "deleteMaskKeys", method = RequestMethod.POST)
    public void deleteMaskKeys(long[] ids, PrintWriter out) {
        for (long id : ids) {
            maskKeyService.delete(id);
        }
        out.append(JsonUtil.loadTrueResult(""));
    }
 
    @RequestMapping(value = "getGoodsInfo", method = RequestMethod.POST)
    public void getGoodsInfo(String url, PrintWriter out) {
 
        TaoBaoGoodsBrief tb = null;
        String URL_REGEX = "(((http|https)://)|(www\\.))[a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6}(:[0-9]{1,4})?(/[a-zA-Z0-9\\&%_\\./-~-]*)?";
 
        Pattern p = Pattern.compile(URL_REGEX);
        Matcher matcher = p.matcher(url);
        if (!matcher.find()) {
            out.print(JsonUtil.loadFalseResult("暂不支持该类型!"));
            return;
        }
        url = matcher.group();
        if (url.contains("ju.taobao.com") || url.contains(".juhuasuan.com")) {// 聚划�?
            int index = url.indexOf("item_id");
            if (index < 0) {
                out.println(JsonUtil.loadFalseResult("暂未找到该商品,请稍后再试!"));
                return;
            }
            url = url.substring(index);
            int last = url.indexOf("&");
            String id = "";
            if (last > 0)
                id = url.substring(url.indexOf("=") + 1, url.indexOf("&"));
            else {
                id = url.substring(url.indexOf("=" + 1));
            }
            tb = TaoBaoUtil.isAlimama(id);
            if (tb == null) {
                tb = TaoBaoUtil.parsePhoneTmAndTb(id);
            }
        } else if (url.contains("http://zmnxbc.com")) { // 手机端天猫APP分享
            tb = TaoBaoUtil.parsePhoneShareUrlByTM(url);
        } else if (url.contains("c.b1yt.com")) { // 手机端淘宝和�?��APP分享
            tb = TaoBaoUtil.parsePhoneShareUrlByTB(url);
        } else if (url.contains("h5.m.taobao") || url.contains("detail.m.tmall") || url.contains("item.taobao")
                || url.contains("detail.tmall")) { // 手机页面和电脑页�?
            int index = url.indexOf("id");
            if (index < 0) {
                out.println(JsonUtil.loadFalseResult("暂未找到该商品,请稍后再试!"));
                return;
            }
            url = url.substring(index);
            int last = url.indexOf("&");
            String id = "";
            if (last > 0)
                id = url.substring(url.indexOf("=") + 1, url.indexOf("&"));
            else {
                id = url.substring(url.indexOf("=") + 1);
            }
            tb = TaoBaoUtil.isAlimama(id);
            if (tb == null) {
                tb = TaoBaoUtil.parsePhoneTmAndTb(id);
            }
        }
 
        if (tb == null) {
            out.println(JsonUtil.loadFalseResult("暂未找到该商品,请稍后再试!"));
            return;
        }
        JSONObject data = new JSONObject();
        JSONObject taoBaoGoodsJSON = new JSONObject();
        taoBaoGoodsJSON.put("title", tb.getTitle());
        taoBaoGoodsJSON.put("zkPrice", tb.getZkPrice());
        taoBaoGoodsJSON.put("auctionId", tb.getAuctionId());
        data.put("taoBaoGoodsBrief", taoBaoGoodsJSON);
        data.put("tbImgs", tb.getImgList());
        out.print(JsonUtil.loadTrueResult(data));
 
    }
 
    @RequestMapping(value = "makePublic", method = RequestMethod.POST)
    public void makePublic(DynamicRecommend dynamicRecommend,
            @RequestParam(value = "files", required = false) CommonsMultipartFile[] files, PrintWriter out) {
        if (dynamicRecommend == null) {
            out.print(JsonUtil.loadFalseResult("dynamicRecommend不能为空"));
            return;
        }
        if (dynamicRecommend.getReason() == null || "".equals(dynamicRecommend.getReason().trim())) {
            out.print(JsonUtil.loadFalseResult("推荐理由不能为空"));
            return;
        }
        if (dynamicRecommend.getAuctionId() == null) {
            out.print(JsonUtil.loadFalseResult("商品ID不能为空"));
            return;
        }
        if (dynamicRecommend.getUrl() == null || "".equals(dynamicRecommend.getUrl().trim())) {
            out.print(JsonUtil.loadFalseResult("url不能为空"));
            return;
        }
        if (dynamicRecommend.getuPicUrl() == null && files == null) {
            out.print(JsonUtil.loadFalseResult("请选择一张图片"));
            return;
        }
 
        String URL_REGEX = "(((http|https)://)|(www\\.))[a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6}(:[0-9]{1,4})?(/[a-zA-Z0-9\\&%_\\./-~-]*)?";
 
        Pattern p = Pattern.compile(URL_REGEX);
        Matcher matcher = p.matcher(dynamicRecommend.getUrl());
        if (!matcher.find()) {
            out.print(JsonUtil.loadFalseResult("暂不支持该类型!"));
            return;
        }
        String url = matcher.group();
        dynamicRecommend.setUrl(url);
        if (files != null) {
            COSManager cosManager = COSManager.getInstance();
            String paths = "";
            for (CommonsMultipartFile file : files) {
                try {
                    String filename = file.getOriginalFilename();
                    int index = filename.lastIndexOf(".");
                    String type = filename.substring(index);
                    String filePath = cosManager
                            .uploadFile(file.getInputStream(), UUID.randomUUID().toString().replace("-", "") + type)
                            .getUrl();
                    paths += "," + filePath;
                } catch (IOException e) {
                    e.printStackTrace();
                    out.println(JsonUtil.loadFalseResult("上传图片失败"));
                    return;
                }
            }
            paths = paths.replaceFirst(",", "");
            String getuPicUrl = dynamicRecommend.getuPicUrl();
            if (getuPicUrl == null) {
                dynamicRecommend.setuPicUrl(paths);
            } else {
                dynamicRecommend.setuPicUrl(getuPicUrl + "," + paths);
            }
        }
 
        TaoBaoGoodsBrief taoBaoGoodsBrief = null;
        taoBaoGoodsBrief = TaoBaoUtil.isAlimama(dynamicRecommend.getAuctionId());
        if (taoBaoGoodsBrief == null) {
            taoBaoGoodsBrief = TaoBaoUtil.parsePhoneTmAndTb(dynamicRecommend.getAuctionId());
            dynamicRecommend.setHongbao(new BigDecimal(0));
        } else {
            SearchFilter filter = new SearchFilter();
            filter.setKey(String.format(TaoBaoUtil.TB_URL, dynamicRecommend.getAuctionId()));
            TaoBaoSearchResult search = TaoBaoUtil.search(filter);
            List<TaoBaoGoodsBrief> taoBaoGoodsBriefs = search.getTaoBaoGoodsBriefs();
            if (taoBaoGoodsBriefs.size() == 0) {
                out.println(JsonUtil.loadFalseResult("发布失败,商品已下架"));
                return;
            }
            taoBaoGoodsBrief = taoBaoGoodsBriefs.get(0);
            BigDecimal proportion = hongBaoManageService.getFanLiRate();
            BigDecimal tkRate = taoBaoGoodsBrief.getTkRate();
            BigDecimal zkPrice = taoBaoGoodsBrief.getZkPrice();
            BigDecimal tkp = proportion.divide(BigDecimal.valueOf(100))
                    .multiply(tkRate.divide(BigDecimal.valueOf(100)));
            BigDecimal hongbao = MoneyBigDecimalUtil.mul(zkPrice, tkp);
            dynamicRecommend.setHongbao(hongbao);
        }
        String title = taoBaoGoodsBrief.getTitle();
        int type = taoBaoGoodsBrief.getUserType();
        dynamicRecommend.setGname(title);
        dynamicRecommend.setZkPrice(taoBaoGoodsBrief.getZkPrice());
        dynamicRecommend.setgPicUrl(taoBaoGoodsBrief.getPictUrl());
        dynamicRecommend.setCreatetime(System.currentTimeMillis());
        if (type == 0) { // tb
            dynamicRecommend.setGoodsType(1); // tb
        } else {
            dynamicRecommend.setGoodsType(2); // 天猫
        }
        String reason = dynamicRecommend.getReason();
        reason = maskKeyService.maskAction(reason);
        dynamicRecommend.setReason(reason);
 
        long u = userInfoService.getRandomShamUser();
        if (u == 0) {
            out.print(JsonUtil.loadFalseResult("暂无虚拟用户"));
            return;
        }
        dynamicRecommend.setUserInfo(new UserInfo(u));
        dynamicRecommend.setType(1);
        long makePublic = dynamicRecommendService.makePublic(dynamicRecommend);
        if (makePublic > 0) {
            JSONObject data = new JSONObject();
            data.put("auctionId", makePublic);
            out.println(JsonUtil.loadTrueResult(data));
        } else {
            out.println(JsonUtil.loadFalseResult("发布失败"));
        }
    }
 
}