Administrator
2018-12-10 94c29daf7cb722635c1735e8d8bd403105e999d8
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
package com.yeshi.fanli.service.impl.user;
 
import java.math.BigDecimal;
import java.util.ArrayList;
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.Service;
import org.yeshi.utils.DateUtil;
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.dao.mybatis.share.UserShareGoodsGroupMapper;
import com.yeshi.fanli.entity.bus.share.UserShareGoodsGroup;
import com.yeshi.fanli.entity.goods.CommonGoods;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.exception.share.UserShareGoodsRecordException;
import com.yeshi.fanli.exception.taobao.TaobaoGoodsDownException;
import com.yeshi.fanli.service.inter.goods.CommonGoodsService;
import com.yeshi.fanli.service.inter.hongbao.HongBaoManageService;
import com.yeshi.fanli.service.inter.user.UserShareGoodsGroupService;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.factory.CommonGoodsFactory;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
 
@Service
public class UserShareGoodsGroupServiceImpl implements UserShareGoodsGroupService {
 
    @Resource(name = "taskExecutor")
    private TaskExecutor executor;
    
    @Resource
    private HongBaoManageService hongBaoManageService;
    
    @Resource
    private RedisManager redisManager;
    
    @Resource
    private CommonGoodsService commonGoodsService;
    
    @Resource
    private UserShareGoodsGroupMapper userShareGoodsGroupMapper;
 
    @Override
    public int insert(UserShareGoodsGroup record) {
        return userShareGoodsGroupMapper.insert(record);
    }
 
    @Override
    public int insertSelective(UserShareGoodsGroup record) {
        return userShareGoodsGroupMapper.insertSelective(record);
    }
 
    @Override
    public UserShareGoodsGroup selectByPrimaryKey(Long id) {
        return userShareGoodsGroupMapper.selectByPrimaryKey(id);
    }
 
    @Override
    public int updateByPrimaryKeySelective(UserShareGoodsGroup record) {
        return userShareGoodsGroupMapper.updateByPrimaryKeySelective(record);
    }
 
    @Override
    public int updateByPrimaryKey(UserShareGoodsGroup record) {
        return userShareGoodsGroupMapper.updateByPrimaryKey(record);
    }
 
    @Override
    public List<UserShareGoodsGroup> listByRecordId(Long recordId) {
        return userShareGoodsGroupMapper.listByRecordId(recordId);
    }
 
    @Override
    public UserShareGoodsGroup getSingleGoods(Long cid, Long uid) {
        return userShareGoodsGroupMapper.getSingleGoods(cid, uid);
    }
 
    @Override
    public int insertBatch(List<UserShareGoodsGroup> list) {
        return userShareGoodsGroupMapper.insertBatch(list);
    }
 
    @Override
    public UserShareGoodsGroup getNewestRecord(Long uid, Long auctionId) {
        return userShareGoodsGroupMapper.getNewestRecord(uid, auctionId);
    }
 
    
    /**
     * 更新浏览记录数据
     * @param shareId 分享id
     * @param count 浏览次数
     */
    @Override
    public void updateBrowseRecord (Long shareId, int count) throws UserShareGoodsRecordException{
 
        if (shareId == null) {
            throw new UserShareGoodsRecordException(1, "分享id为空");
        }
        
        List<UserShareGoodsGroup> list = listByRecordId(shareId);
        if (list == null || list.size() == 0) {
            throw new UserShareGoodsRecordException(1, "分享商品已不存在");
        }
        
        List<UserShareGoodsGroup> listUpdate = new ArrayList<UserShareGoodsGroup>();
        
        for (UserShareGoodsGroup group: list) {
            // 注意: 修改记录数据、但不可修改更新设置
            UserShareGoodsGroup shareGoodsGroup = new UserShareGoodsGroup(group.getId());
            
            shareGoodsGroup.setTotalBrowse(group.getTotalBrowse() + count);
            
            Date browseTime = group.getBrowseTime();
            if (DateUtil.isSameDay(browseTime, new Date())) {
                shareGoodsGroup.setTodayBrowse(group.getTodayBrowse() + count);
            } else {
                shareGoodsGroup.setTodayBrowse(count);
                shareGoodsGroup.setBrowseTime(new Date());
            }
            
            listUpdate.add(shareGoodsGroup);
        }
        
        userShareGoodsGroupMapper.updateBatchSelective(listUpdate);
        
    }
    
    
    /**
     * 更新订单记录数据
     * @param uid
     * @param auctionId
     */
    @Override
    public void updateOrderRecord (Long uid, TaoBaoGoodsBrief taoBaoGoodsBrief, int count) 
            throws UserShareGoodsRecordException{
        
        if (uid == null) {
            throw new UserShareGoodsRecordException(1, "用户ID不能为空");
        }
        
        if (taoBaoGoodsBrief == null) {
            throw new UserShareGoodsRecordException(1, "商品不能为空");
        }
        
        UserShareGoodsGroup newestRecord = getNewestRecord(uid, taoBaoGoodsBrief.getAuctionId());
        
        if (newestRecord != null) {
            // 注意: 修改记录数据、但不可修改更新设置
            UserShareGoodsGroup shareGoodsGroup = new UserShareGoodsGroup(newestRecord.getId());
            shareGoodsGroup.setTotalOrder(newestRecord.getTotalOrder() + count);
            
            // 单个商品预计金额
            String rateStr = hongBaoManageService.get("hongbao_goods_proportion");
            BigDecimal money = TaoBaoUtil.getGoodsHongBaoMoney(taoBaoGoodsBrief, new BigDecimal(rateStr));
            
            // 更新预计收益 
            BigDecimal resultMoney = MoneyBigDecimalUtil.mul(new BigDecimal(shareGoodsGroup.getTotalOrder()), money);
            shareGoodsGroup.setTotalMoney(resultMoney);
            
            updateByPrimaryKeySelective(shareGoodsGroup);
        }
    }
    
    @Override
    public JSONObject getGoodsGroup(Long recordId) throws UserShareGoodsRecordException {
        
        List<CommonGoods> listUpadteCommonGoods = new ArrayList<CommonGoods>();
 
        JSONArray array = new JSONArray();
 
        List<UserShareGoodsGroup> list = listByRecordId(recordId);
        if (list != null && list.size() > 0) {
 
            Date nowDate = new Date();
            long nh = 1000 * 60 * 60;
            long nd = 1000 * 24 * 60 * 60;
 
            Gson gson = JsonUtil.getConvertBigDecimalToStringSubZeroBuilder(new GsonBuilder())
                    .excludeFieldsWithoutExposeAnnotation().setDateFormat("yyyy-MM-dd").create();
 
            Map<String, String> map = hongBaoManageService.convertMap();
            String proportion = map.get("hongbao_goods_proportion");
            String fcRate = map.get("hongbao_fc_ratio");
 
            for (UserShareGoodsGroup userShareGoodsGroup : list) {
                CommonGoods commonGoods = userShareGoodsGroup.getCommonGoods();
                if (commonGoods == null) {
                    continue;
                }
 
                TaoBaoGoodsBrief goodsBrief = null;
                JSONObject dataObject = new JSONObject();
 
                // 更新一个小时之内的商品不再更新
                Integer goodsState = commonGoods.getState();
                // 状态未下架时进行更新
                if (goodsState != null && (goodsState != 1 || !goodsState.equals(1))) {
                    Date updateTime = commonGoods.getUpdateTime();
                    if (updateTime != null) {
                        long diff = nowDate.getTime() - updateTime.getTime();
                        // 计算差多少小时
                        long hour = diff % nd / nh;
                        if (hour > 1) {
                            try {
                                if (commonGoods.getGoodsType() == CommonGoods.GOODS_TYPE_TB) {
                                    goodsBrief = redisManager.getTaoBaoGoodsBrief(commonGoods.getGoodsId());
                                    goodsBrief.setState(0);
                                    goodsState = 0;
                                }
                            } catch (TaobaoGoodsDownException e) {
                                // 已下架
                                goodsState = 1;
                                commonGoods.setState(1);
                                e.printStackTrace();
                            }
                        }
                    }
                }
 
                if (goodsBrief == null) {
                    goodsBrief = TaoBaoUtil.convert(commonGoods);
                } else {
                    CommonGoods upadeteCommonGoods = CommonGoodsFactory.create(goodsBrief);
                    upadeteCommonGoods.setState(goodsState);
                    listUpadteCommonGoods.add(upadeteCommonGoods);
                }
 
                // 改变图片尺寸
                String pictUrl = commonGoods.getPicture();
                if (!StringUtil.isNullOrEmpty(pictUrl) && !pictUrl.contains("320x320")) {
                    commonGoods.setPicture(TbImgUtil.getTBSize320Img(pictUrl));
                }
 
                String json = gson
                        .toJson(TaoBaoUtil.getTaoBaoGoodsBriefExtra(goodsBrief, proportion, fcRate));
 
 
                dataObject.put("groupId", userShareGoodsGroup.getId());
                dataObject.put("goods", json);
                array.add(dataObject);
            }
 
        }
        
        JSONObject data = new JSONObject();
        data.put("count", list.size());
        data.put("result_list", array);
 
        if (listUpadteCommonGoods.size() > 0) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        // 更新简版信息
                        commonGoodsService.updateBatchCommonGoods(listUpadteCommonGoods);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        
        return data;
    }
 
}