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
package com.yeshi.fanli.goods.service.impl.usergoods;
 
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.fanli.facade.goods.entity.usergoods.UserShareGoodsGroup;
import org.fanli.facade.goods.entity.usergoods.UserShareGoodsRecord.ShareSourceTypeEnum;
import org.fanli.facade.goods.exception.usergoods.UserShareGoodsRecordException;
import org.fanli.facade.goods.service.taobao.CommonGoodsService;
import org.fanli.facade.goods.service.usergoods.UserShareGoodsGroupService;
import org.fanli.facade.order.entity.hongbao.HongBao;
import org.fanli.facade.system.service.hongbao.HongBaoManageService;
import org.yeshi.utils.DateUtil;
import org.yeshi.utils.MoneyBigDecimalUtil;
 
import com.alibaba.dubbo.config.annotation.Service;
import com.yeshi.fanli.goods.dao.usergoods.UserShareGoodsGroupMapper;
 
@Service(version = "1.0.0")
public class UserShareGoodsGroupServiceImpl implements UserShareGoodsGroupService {
 
    @Resource
    private HongBaoManageService hongBaoManageService;
    
    @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(ShareSourceTypeEnum.goodsDetail,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 uid
     * @param auctionId
     */
    @Override
    public void updateOrderRecord (HongBao hongBao) throws UserShareGoodsRecordException{
        
        if (hongBao == null) {
            throw new UserShareGoodsRecordException(1, "hongBao不能为空");
        }
        
        if (hongBao.getUserInfo() == null) {
            throw new UserShareGoodsRecordException(1, "用户信息不能为空");
        }
        
        Long uid = hongBao.getUserInfo().getId();
        if (uid == null) {
            throw new UserShareGoodsRecordException(1, "用户ID不能为空");
        }
        
        if (hongBao.getAuctionId() == null) {
            throw new UserShareGoodsRecordException(1, "商品ID不能为空");
        }
        
        UserShareGoodsGroup newestRecord = getNewestRecord(uid, hongBao.getAuctionId());
        
        if (newestRecord != null) {
            // 更新订单数量
            UserShareGoodsGroup shareGoodsGroup = new UserShareGoodsGroup(newestRecord.getId());
            Integer totalOrder = newestRecord.getTotalOrder();
            
            if (totalOrder == null) {
                totalOrder = 0;
            }
            shareGoodsGroup.setTotalOrder(totalOrder + 1);
            
            // 更新预计收益 
            BigDecimal totalMoney = newestRecord.getTotalMoney();
            if (totalMoney == null) {
                totalMoney = new BigDecimal(0);
            }
            BigDecimal resultMoney = MoneyBigDecimalUtil.add(totalMoney, hongBao.getMoney());
            shareGoodsGroup.setTotalMoney(resultMoney);
            
            updateByPrimaryKeySelective(shareGoodsGroup);
        }
    }
    
    
    /**
     * 更新订单记录数据
     * @param uid
     * @param auctionId
     */
    @Override
    public void updateBrowseRecord (Long uid, Long auctionId, int count) 
            throws UserShareGoodsRecordException{
        
        if (uid == null) {
            throw new UserShareGoodsRecordException(1, "用户ID不能为空");
        }
        
        if (auctionId == null) {
            throw new UserShareGoodsRecordException(1, "商品Id不能为空");
        }
        
        UserShareGoodsGroup group = getNewestRecord(uid, auctionId);
        
        if (group != null) {
            UserShareGoodsGroup shareGoodsGroup = new UserShareGoodsGroup(group.getId());
            // 累计浏览
            Integer totalBrowse = group.getTotalBrowse();
            if (totalBrowse == null) {
                totalBrowse = 0;
            }
            shareGoodsGroup.setTotalBrowse(totalBrowse + count);
            
            // 今日浏览
            Date date = new Date();
            Date browseTime = group.getBrowseTime();
            if (DateUtil.isSameDay(browseTime, date)) {
                shareGoodsGroup.setTodayBrowse(group.getTodayBrowse() + count);
            } else {
                shareGoodsGroup.setTodayBrowse(count);
            }
            shareGoodsGroup.setBrowseTime(date);
            
            userShareGoodsGroupMapper.updateByPrimaryKeySelective(shareGoodsGroup);
        }
    }
}