yujian
2019-07-16 394c3eb18c7b9a2af63d65a916ed86aad0f7e2c2
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.client.v2;
 
import java.io.PrintWriter;
import java.math.BigDecimal;
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.entity.accept.AcceptData;
import com.yeshi.fanli.entity.bus.user.UserGoodsStorage;
import com.yeshi.fanli.entity.goods.CommonGoods;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
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.hongbao.HongBaoManageService;
import com.yeshi.fanli.service.inter.user.UserGoodsStorageService;
import com.yeshi.fanli.util.Constant;
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 UserGoodsStorageService userGoodsStorageService;
 
    /**
     * 批量添加选品库
     * 
     * @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("share_jd_open");
            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();
 
        BigDecimal fanLiRate = hongBaoManageService.getFanLiRate();
        BigDecimal shareRate = hongBaoManageService.getShareRate();
 
        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, null, fanLiRate, shareRate);
            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));
    }
 
}