admin
2020-05-19 744594ef1a2f530fc3e86ea9dc48b62247f79420
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
package com.yeshi.fanli.controller.client.v2;
 
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
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.reflect.TypeToken;
import com.yeshi.fanli.dto.ConfigParamsDTO;
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.bus.user.UserGoodsStorage;
import com.yeshi.fanli.entity.goods.CommonGoods;
import com.yeshi.fanli.entity.system.ConfigKeyEnum;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.exception.share.UserShareGoodsRecordException;
import com.yeshi.fanli.exception.taobao.TaoKeApiException;
import com.yeshi.fanli.exception.taobao.TaobaoGoodsDownException;
import com.yeshi.fanli.exception.user.UserGoodsStorageException;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.order.OrderHongBaoMoneyComputeService;
import com.yeshi.fanli.service.inter.order.config.HongBaoManageService;
import com.yeshi.fanli.service.inter.user.UserGoodsStorageService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.VersionUtil;
import com.yeshi.fanli.util.factory.goods.GoodsDetailVOFactory;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
import com.yeshi.fanli.vo.goods.GoodsDetailVO;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
/**
 * 用户商品分享库
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("api/v2/shraeStorage")
public class ShareStorageControllerV2 {
 
    @Resource
    private ConfigService configService;
 
    @Resource
    private HongBaoManageService hongBaoManageService;
    
    @Resource
    private OrderHongBaoMoneyComputeService orderHongBaoMoneyComputeService;
 
    @Resource
    private UserGoodsStorageService userGoodsStorageService;
 
    /**
     * 单个商品加入选品库
     * 
     * @param callback
     * @param storageIds
     *            id数组
     * @param out
     */
    @RequestMapping(value = "accordtorage", method = RequestMethod.POST)
    public void accordtorage(AcceptData acceptData, Long uid, long id, Integer goodsType, PrintWriter out) {
 
        if (uid == null) {
            out.print(JsonUtil.loadFalseResult("用户未登录"));
            return;
        }
 
        if (goodsType == null) {
            goodsType = Constant.SOURCE_TYPE_TAOBAO;
        }
 
        try {
            UserGoodsStorage userGoodsStorage = userGoodsStorageService.getByUidAndAuctionId(uid, id, goodsType);
            boolean storageState = false;
            if (userGoodsStorage != null) {
                // 取消加入选品库
                userGoodsStorageService.deleteByPrimaryKey(userGoodsStorage.getId());
            } else {
                // 加入选品库
                Set<Long> set = new HashSet<Long>();
                set.add(id);
                userGoodsStorageService.save(uid, set, goodsType);
                storageState = true;
            }
 
            JSONObject data = new JSONObject();
            data.put("storageState", storageState);
            out.print(JsonUtil.loadTrueResult(data));
        } catch (Exception e) {
            out.print(JsonUtil.loadFalseResult("操作失败"));
            e.printStackTrace();
        }
    }
 
    /**
     * 批量添加选品库
     * 
     * @param acceptData
     * @param uid
     *            用户id
     * @param ids
     *            简版商品id
     * @param out
     */
    @RequestMapping(value = "addStorage", method = RequestMethod.POST)
    public void addStorage(AcceptData acceptData, Long uid, String ids, PrintWriter out) {
        try {
            Gson gson = new Gson();
            Set<Long> set = gson.fromJson(ids, new TypeToken<HashSet<Long>>() {
            }.getType());
            if (set == null || set.size() == 0) {
                out.print(JsonUtil.loadFalseResult("未选择商品"));
                return;
            }
 
            userGoodsStorageService.addCommonGoods(uid, set);
            out.print(JsonUtil.loadTrueResult("添加成功"));
        } catch (UserGoodsStorageException e) {
            out.print(JsonUtil.loadFalseResult(e.getMsg()));
        }
    }
 
    /**
     * 查询用户选品库数据
     * 
     * @param acceptData
     * @param page
     *            页码 初始值 1
     * @param uid
     *            用户id
     * @param out
     */
    @RequestMapping(value = "getlist", method = RequestMethod.POST)
    public void getlist(AcceptData acceptData, Integer page, Long uid, Integer goodsType, PrintWriter out) {
 
        if (uid == null) {
            out.print(JsonUtil.loadFalseResult("用户未登录"));
            return;
        }
 
        if (goodsType == null) {
            out.print(JsonUtil.loadFalseResult("平台类型不能为空"));
            return;
        }
 
        if (page == null || page < 1) {
            page = 1;
        }
 
        int pageSize = Constant.PAGE_SIZE;
        JSONArray array = new JSONArray();
        JSONObject data = new JSONObject();
 
        if (goodsType == Constant.SOURCE_TYPE_JD) {
            String open = configService.get(ConfigKeyEnum.shareOpenJD.getKey());
            if (!"1".equals(open.trim())) {
                data.put("count", 0);
                data.put("result_list", array);
                out.print(JsonUtil.loadTrueResult(data));
                return;
            }
        }
 
        List<UserGoodsStorage> listStorage = userGoodsStorageService.listQueryByUid((page - 1) * pageSize, pageSize,
                uid, goodsType);
        if (listStorage == null || listStorage.size() == 0) {
            data.put("count", 0);
            data.put("result_list", array);
            out.print(JsonUtil.loadTrueResult(data));
            return;
        }
 
        // API网络接口验证是否在售
        List<TaoBaoGoodsBrief> listTaoKeGoods = null;
        if (goodsType == Constant.SOURCE_TYPE_TAOBAO) {
            List<Long> listGid = new ArrayList<Long>();
            for (UserGoodsStorage userGoodsStorage : listStorage) {
                CommonGoods commonGoods = userGoodsStorage.getCommonGoods();
                if (commonGoods == null) {
                    continue;
                }
                listGid.add(commonGoods.getGoodsId());
            }
 
            try {
                listTaoKeGoods = TaoKeApiUtil.getBatchGoodsInfo(listGid);
            } catch (TaoKeApiException e) {
                e.printStackTrace();
            } catch (TaobaoGoodsDownException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
 
        Gson gson = JsonUtil.getConvertBigDecimalToStringSubZeroBuilder(new GsonBuilder())
                .excludeFieldsWithoutExposeAnnotation().setDateFormat("yyyy-MM-dd").create();
 
        ConfigParamsDTO paramsDTO =  orderHongBaoMoneyComputeService.getShowComputeRate(acceptData.getPlatform(),
                acceptData.getVersion());
 
        for (UserGoodsStorage userGoodsStorage : listStorage) {
            CommonGoods commonGoods = userGoodsStorage.getCommonGoods();
            if (commonGoods == null) {
                continue;
            }
 
            // 淘宝商品验证在售
            if (goodsType == Constant.SOURCE_TYPE_TAOBAO) {
                if (listTaoKeGoods != null && listTaoKeGoods.size() > 0) {
                    int state = 1; // 默认停售
                    Long goodsId = commonGoods.getGoodsId();
                    for (TaoBaoGoodsBrief taoKeGoods : listTaoKeGoods) {
                        Long auctionId = taoKeGoods.getAuctionId();
                        if (goodsId == auctionId || goodsId.equals(auctionId)) {
                            state = 0; // 在售
                            break;
                        }
                    }
                    commonGoods.setState(state);
                }
            }
 
            // 判断是否已分享, 已分享显示已下架
            Integer storageState = userGoodsStorage.getState();
            if (storageState != null && storageState == UserGoodsStorage.STATE_SHARED) {
                Integer goodsState = commonGoods.getState();
                if (goodsState != null && goodsState != 1) {
                    commonGoods.setState(2);
                }
            }
 
            GoodsDetailVO detailVO = GoodsDetailVOFactory.convertCommonGoods(commonGoods, paramsDTO);
            detailVO.setId(commonGoods.getId());
 
            JSONObject dataObject = new JSONObject();
            dataObject.put("storageId", userGoodsStorage.getId());
            dataObject.put("goods", gson.toJson(detailVO));
            array.add(dataObject);
        }
 
        long count = userGoodsStorageService.countQueryByUid(uid, goodsType);
        data.put("count", count);
        data.put("result_list", array);
        out.print(JsonUtil.loadTrueResult(data));
    }
 
    /**
     * 分享商品返回二维码图片
     * 
     * @param callback
     * @param storageIds
     *            id数组
     * @param out
     */
    @RequestMapping(value = "createShare", method = RequestMethod.POST)
    public void createShare(AcceptData acceptData, Long uid, String storageIds, PrintWriter out) {
 
        if (StringUtil.isNullOrEmpty(storageIds)) {
            out.print(JsonUtil.loadFalseResult("分享商品不能为空"));
            return;
        }
 
        if (uid == null) {
            out.print(JsonUtil.loadFalseResult("用户未登录"));
            return;
        }
 
        try {
            Gson gson = new Gson();
            List<Long> listStorageID = gson.fromJson(storageIds, new TypeToken<ArrayList<Long>>() {
            }.getType());
 
            if (!VersionUtil.greaterThan_2_0_5(acceptData.getPlatform(), acceptData.getVersion())) {
 
                if (listStorageID == null || listStorageID.size() < 9) {
                    out.print(JsonUtil.loadFalseResult("分享商品数量不足"));
                    return;
                }
 
                if (listStorageID.size() != 9) {
                    out.print(JsonUtil.loadFalseResult("分享商品数量只能是9个"));
                    return;
                }
 
                JSONObject data = userGoodsStorageService.createShareV2(uid, listStorageID, true);
                out.print(JsonUtil.loadTrueResult(data));
            } else {
                JSONObject data = userGoodsStorageService.createShareV2(uid, listStorageID, false);
                out.print(JsonUtil.loadTrueResult(data));
            }
 
        } catch (UserGoodsStorageException e) {
            out.print(JsonUtil.loadFalseResult("分享失败"));
            e.printStackTrace();
        } catch (UserShareGoodsRecordException e) {
            out.print(JsonUtil.loadFalseResult("分享失败"));
            e.printStackTrace();
        } catch (Exception e) {
            out.print(JsonUtil.loadFalseResult("分享失败"));
            e.printStackTrace();
        }
    }
}