admin
2019-02-18 ed732dea219670c75c4664e522e10c90469a2b33
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
package com.yeshi.fanli.goods.service.impl.usergoods;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import javax.annotation.Resource;
 
import org.fanli.facade.goods.entity.usergoods.UserGoodsStorage;
import org.fanli.facade.goods.entity.usergoods.UserShareGoodsRecord.ShareSourceTypeEnum;
import org.fanli.facade.goods.exception.taobao.CommonGoodsException;
import org.fanli.facade.goods.exception.taobao.TaobaoGoodsDownException;
import org.fanli.facade.goods.exception.usergoods.UserGoodsStorageException;
import org.fanli.facade.goods.exception.usergoods.UserShareGoodsRecordException;
import org.fanli.facade.goods.service.taobao.CommonGoodsService;
import org.fanli.facade.goods.service.usergoods.UserGoodsStorageService;
import org.fanli.facade.goods.service.usergoods.UserShareGoodsRecordService;
import org.fanli.facade.goods.utils.RedisGoodsManager;
import org.fanli.facade.goods.utils.factory.CommonGoodsFactory;
import org.fanli.facade.goods.utils.taobao.TaoBaoUtil;
import org.fanli.facade.goods.utils.taobao.TaoKeGoodsApiUtil;
import org.fanli.facade.system.exception.TaoKeApiException;
import org.fanli.facade.system.service.common.ConfigService;
import org.fanli.facade.system.service.hongbao.HongBaoManageService;
import org.springframework.core.task.TaskExecutor;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.MoneyBigDecimalUtil;
 
import com.alibaba.dubbo.config.annotation.Service;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yeshi.fanli.base.entity.goods.CommonGoods;
import com.yeshi.fanli.base.entity.goods.TaoBaoGoodsBrief;
import com.yeshi.fanli.goods.dao.usergoods.UserGoodsStorageMapper;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
@Service(version = "1.0.0")
public class UserGoodsStorageServiceImpl implements UserGoodsStorageService {
 
    @Resource(name = "taskExecutor")
    private TaskExecutor executor;
 
    @Resource
    private RedisGoodsManager redisManager;
 
    @Resource
    private ConfigService configService;
 
    @Resource
    private CommonGoodsService commonGoodsService;
 
    @Resource
    private UserGoodsStorageMapper userGoodsStorageMapper;
 
    @Resource
    private UserShareGoodsRecordService userShareGoodsRecordService;
 
    @Resource
    private HongBaoManageService hongBaoManageService;
 
    @Override
    public int deleteByPrimaryKey(Long id) {
        return userGoodsStorageMapper.deleteByPrimaryKey(id);
    }
 
    @Override
    public int insert(UserGoodsStorage record) {
        return userGoodsStorageMapper.insert(record);
    }
 
    @Override
    public int insertSelective(UserGoodsStorage record) {
        return userGoodsStorageMapper.insertSelective(record);
    }
 
    @Override
    public UserGoodsStorage selectByPrimaryKey(Long id) {
        return userGoodsStorageMapper.selectByPrimaryKey(id);
    }
 
    @Override
    public int updateByPrimaryKeySelective(UserGoodsStorage record) {
        return userGoodsStorageMapper.updateByPrimaryKeySelective(record);
    }
 
    @Override
    public int updateByPrimaryKey(UserGoodsStorage record) {
        return userGoodsStorageMapper.updateByPrimaryKey(record);
    }
 
    /**
     * 根据主键批量删除
     * 
     * @param list
     * @return
     */
    @Override
    public int deleteBatchByPrimaryKey(List<Long> list) {
        return userGoodsStorageMapper.deleteBatchByPrimaryKey(list);
    }
 
    /**
     * 根据主键 、uid 批量删除
     * 
     * @param list
     * @return
     */
    @Override
    public int deleteBatchByUidAndPrimaryKey(Long uid, List<Long> list) {
        return userGoodsStorageMapper.deleteBatchByUidAndPrimaryKey(uid, list);
    }
 
    @Override
    public List<UserGoodsStorage> listQueryByUid(long start, int count, Long uid) {
        return userGoodsStorageMapper.listQueryByUid(start, count, uid);
    }
 
    @Override
    public UserGoodsStorage getByUidAndAuctionId(Long uid, Long auctionId) {
        return userGoodsStorageMapper.getByUidAndAuctionId(uid, auctionId);
    }
 
    @Override
    public long countQueryByUid(Long uid) {
        return userGoodsStorageMapper.countQueryByUid(uid);
    }
 
    @Override
    public void updateShareState(Long shareId) {
        userGoodsStorageMapper.updateShareState(shareId);
    }
 
    @Override
    public void save(Long uid, Set<Long> auctionIds) throws UserGoodsStorageException {
 
        if (uid == null) {
            throw new UserGoodsStorageException(1, "用户尚未登录");
        }
 
        if (auctionIds == null || auctionIds.size() == 0) {
            throw new UserGoodsStorageException(1, "商品ID为空");
        }
 
        for (Long auctionId : auctionIds) {
 
            TaoBaoGoodsBrief goodsBrief = null;
 
            try {
                // 获取商品详情
                goodsBrief = redisManager.getTaoBaoGoodsBrief(auctionId);
            } catch (TaobaoGoodsDownException e) {
 
                // 更新简版 下架状态
                CommonGoods commonGoods = new CommonGoods();
                commonGoods.setGoodsId(auctionId);
                commonGoods.setGoodsType(CommonGoods.GOODS_TYPE_TB);
                commonGoods.setState(CommonGoods.STATE_OFFLINE);
 
                throw new UserGoodsStorageException(1, "该商品已下架");
            }
 
            // 转换简版商品信息
            CommonGoods commonGoods = CommonGoodsFactory.create(goodsBrief);
            try {
                commonGoodsService.addOrUpdateCommonGoods(commonGoods);
            } catch (CommonGoodsException e) {
                e.printStackTrace();
                throw new UserGoodsStorageException(1, "商品信息更新失败");
            }
 
            Long cid = commonGoods.getId();
 
            UserGoodsStorage goodsStorage = userGoodsStorageMapper.getByUidAndCid(uid, cid);
 
            if (goodsStorage != null) {
                // 初始话状态
                goodsStorage.setState(UserGoodsStorage.STATE_NORMAL);
                goodsStorage.setUpdateTime(new Date());
                userGoodsStorageMapper.updateByPrimaryKeySelective(goodsStorage);
            } else {
                goodsStorage = new UserGoodsStorage();
                goodsStorage.setUid(uid);
                ;
                goodsStorage.setState(UserGoodsStorage.STATE_NORMAL);
                goodsStorage.setCommonGoods(commonGoods);
                goodsStorage.setCreateTime(new Date());
                goodsStorage.setUpdateTime(new Date());
                userGoodsStorageMapper.insertSelective(goodsStorage);
            }
 
        }
    }
 
    @Override
    public JSONArray getMyStorage(int page, int pageSize, Long uid) throws UserGoodsStorageException {
 
        JSONArray array = new JSONArray();
 
        List<UserGoodsStorage> listStorage = listQueryByUid(page, pageSize, uid);
        if (listStorage == null || listStorage.size() == 0) {
            return array;
        }
 
        List<Long> listGid = new ArrayList<Long>();
        for (UserGoodsStorage userGoodsStorage : listStorage) {
            CommonGoods commonGoods = userGoodsStorage.getCommonGoods();
            if (commonGoods == null) {
                continue;
            }
            listGid.add(commonGoods.getGoodsId());
        }
 
        // API网络接口验证是否在售
        List<TaoBaoGoodsBrief> listTaoKeGoods = null;
        try {
            listTaoKeGoods = TaoKeGoodsApiUtil.getBatchGoodsInfo(listGid);
        } catch (TaoKeApiException e) {
            e.printStackTrace();
        } catch (TaobaoGoodsDownException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        BigDecimal rate = hongBaoManageService.getFanLiRate();
        Gson gson = JsonUtil.getConvertBigDecimalToStringSubZeroBuilder(new GsonBuilder())
                .excludeFieldsWithoutExposeAnnotation().setDateFormat("yyyy-MM-dd").create();
 
        for (UserGoodsStorage userGoodsStorage : listStorage) {
            CommonGoods commonGoods = userGoodsStorage.getCommonGoods();
            if (commonGoods == null) {
                continue;
            }
 
            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);
            }
 
            TaoBaoGoodsBrief goodsBrief = TaoBaoUtil.convert(commonGoods);
 
            // 判断是否已分享
            Integer storageState = userGoodsStorage.getState();
            if (storageState != null && storageState == UserGoodsStorage.STATE_SHARED) {
                Integer goodsState = goodsBrief.getState();
                if (goodsState != null && goodsState != 1) {
                    goodsBrief.setState(2); // 已分享状态 优先显示已下架
                }
            }
 
            String json = gson.toJson(TaoBaoUtil.getTaoBaoGoodsBriefExtra(goodsBrief, rate.toString(), null));
 
            JSONObject dataObject = new JSONObject();
            dataObject.put("storageId", userGoodsStorage.getId());
            dataObject.put("goods", json);
            array.add(dataObject);
        }
 
        return array;
    }
 
    @Override
    public boolean isExistStorage(Long uid, Long auctionId) {
        boolean state = false;
        if (uid != null && auctionId != null) {
            UserGoodsStorage userGoodsStorage = userGoodsStorageMapper.getByUidAndAuctionId(uid, auctionId);
            if (userGoodsStorage != null) {
                state = true;
            }
        }
        return state;
    }
 
    @Override
    public JSONObject shareGoods(Long uid, List<Long> listStorageID)
            throws UserGoodsStorageException, UserShareGoodsRecordException {
 
        List<UserGoodsStorage> listStorage = userGoodsStorageMapper.listQueryByIds(listStorageID);
        if (listStorage == null || listStorage.size() == 0) {
            throw new UserGoodsStorageException(1, "选品库商品不存在");
        }
 
        List<UserGoodsStorage> listResult = new ArrayList<UserGoodsStorage>();
        // 还原顺序 用于制图
        for (Long sid : listStorageID) {
            for (UserGoodsStorage torage : listStorage) {
                Long id = torage.getId();
                if (sid == id || sid.equals(id)) {
                    listResult.add(torage);
                    break;
                }
            }
        }
 
        BigDecimal totalMoney = new BigDecimal(0.00);
        List<TaoBaoGoodsBrief> listGoodsBrief = new ArrayList<TaoBaoGoodsBrief>();
 
        BigDecimal rate = hongBaoManageService.getFanLiRate();
 
        for (UserGoodsStorage userGoodsStorage : listResult) {
            CommonGoods commonGoods = userGoodsStorage.getCommonGoods();
            TaoBaoGoodsBrief goodsBrief = TaoBaoUtil.convert(commonGoods);
            listGoodsBrief.add(goodsBrief);
            BigDecimal money = TaoBaoUtil.getGoodsHongBaoMoney(goodsBrief, rate);
            totalMoney = MoneyBigDecimalUtil.add(totalMoney, money);
        }
 
        String revenue = listGoodsBrief.size() + "个商品预估分享奖金:¥" + totalMoney;
 
        Map<String, Object> map = userShareGoodsRecordService.save(uid, ShareSourceTypeEnum.storage, null,
                listGoodsBrief);
 
        JSONObject data = new JSONObject();
        data.put("revenue", revenue);
        data.put("shareId", map.get("shareId"));
        data.put("shareImg", map.get("loadResult"));
        data.put("notifyDesc", configService.get("goods_share_multiple_notify"));
 
        return data;
    }
}