yujian
2020-05-14 48b511dbf38054a4d6bc4383681ce50fd11df378
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
package com.yeshi.fanli.service.impl.user;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
 
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yeshi.fanli.dao.user.PreviewInfoDao;
import com.yeshi.fanli.entity.bus.user.BindingAccount;
import com.yeshi.fanli.entity.bus.user.Extract;
import com.yeshi.fanli.entity.bus.user.PreviewInfo;
import com.yeshi.fanli.entity.bus.user.PreviewInfo.PreviewEnum;
import com.yeshi.fanli.entity.bus.user.ThreeSaleDetail;
import com.yeshi.fanli.exception.user.PreviewInfoException;
import com.yeshi.fanli.service.inter.money.extract.BindingAccountService;
import com.yeshi.fanli.service.inter.user.PreviewInfoService;
import com.yeshi.fanli.service.inter.user.invite.ThreeSaleDetailService;
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TimeUtil;
import com.yeshi.fanli.vo.money.MoneyStatisticVO;
import com.yeshi.fanli.vo.user.MineInfoVO;
 
@Service
public class PreviewInfoServiceImpl implements PreviewInfoService {
 
    @Resource
    private PreviewInfoDao previewInfoDao;
    
    @Resource
    private BindingAccountService bindingAccountService;
    
    @Resource
    private RedisManager redisManager;
    
    @Resource
    private ThreeSaleDetailService threeSaleDetailService;
    
    
    
    @Override
    public void save(Long createUid, Long preUid, PreviewEnum type, String content) {
        String id = StringUtil.Md5(createUid + "#" + type.name());
        
        PreviewInfo info = new PreviewInfo();
        info.setId(id);
        info.setUid(preUid);
        info.setCreateUid(createUid);
        info.setContent(content);
        info.setType(type);
        info.setUpdateTime(new Date());
        previewInfoDao.save(info);
        
        // 缓存10分钟
        String key = StringUtil.Md5("preview" + preUid + "#" + type.name());
        redisManager.cacheCommonString(key, info.getContent(), 60 * 10);
    }
    
    
    @Override
    public String getRedisContent(Long uid, PreviewEnum previewEnum) {
        String key = StringUtil.Md5("preview" + uid + "#" + previewEnum.name());
        return redisManager.getCommonString(key);
    }
    
    
    @Override
    public PreviewInfo get(Long uid, PreviewEnum previewEnum) {
        String id = StringUtil.Md5(uid + "#" + previewEnum.name());
        return previewInfoDao.get(id);
    }
    
    @Override
    public void saveMoneyInfo(Long uid, MoneyStatisticVO vo) throws PreviewInfoException {
        if (vo == null)
            throw new PreviewInfoException(1, "请完善页面信息");
        save(uid, uid, PreviewEnum.moneyInfo, new Gson().toJson(vo));
    }
    
    
    @Override
    public void saveExtractRecord(Long uid, String time, BigDecimal money) throws PreviewInfoException {
        BindingAccount alipay = bindingAccountService.getBindingAccountByUidAndType(uid, BindingAccount.TYPE_ALIPAY);
        if (alipay == null)
            throw new PreviewInfoException(1, "未绑定提现账号");
        if (StringUtil.isNullOrEmpty(time))
            throw new PreviewInfoException(1, "提现时间不能为空");
        if (money.compareTo(BigDecimal.ZERO) <= 0)
            throw new PreviewInfoException(1, "提现金额不能小于0");
        
        Gson gson = new Gson();
        List<Extract> list = null;
        
        Extract extract = new Extract();
        extract.setName(alipay.getName());
        extract.setAccount(alipay.getAccount());
        extract.setMoney(money);
        extract.setReceiveTime(TimeUtil.parseDotCommon(time));
        extract.setId(java.lang.System.currentTimeMillis());
        
        String id = StringUtil.Md5(uid + "#" + PreviewEnum.extractRecord.name());
        PreviewInfo previewInfo = previewInfoDao.get(id);
        if (previewInfo == null) {
             list = new ArrayList<Extract>();
             list.add(extract);
        } else {
            String content = previewInfo.getContent();
            if (!StringUtil.isNullOrEmpty(content)) {
                list = gson.fromJson(content, new TypeToken<ArrayList<Extract>>() {}.getType());
            }
            
            if (list == null) {
                list = new ArrayList<>();
            }
            list.add(extract);
            
            // 与之前数据比对排序
            if (list.size() > 1) {
                Comparator<Extract> cm = new Comparator<Extract>() {
                    @Override
                    public int compare(Extract o1, Extract o2) {
                        return (int) (o2.getReceiveTime().getTime() - o1.getReceiveTime().getTime());
                    }
                };
                Collections.sort(list, cm);
            }
        }
        save(uid, uid, PreviewEnum.extractRecord, gson.toJson(list));
    }
    
    
 
    @Override
    public void deleteExtractRecord(Long uid, long tid) {
        String id = StringUtil.Md5(uid + "#" + PreviewEnum.extractRecord.name());
        PreviewInfo previewInfo = previewInfoDao.get(id);
        if (previewInfo == null) {
             return;
        } 
        
        String content = previewInfo.getContent();
        if (StringUtil.isNullOrEmpty(content)) {
            return;
        }
        
        Gson gson = new Gson();
        List<Extract> list = gson.fromJson(content, new TypeToken<ArrayList<Extract>>() {}.getType());
        if (list == null || list.size() == 0) {
            return;
        }
        
        for (Extract extract: list) {
            if (extract.getId() == tid) {
                list.remove(extract);
                break;
            }
        }
        save(uid, uid, PreviewEnum.extractRecord, gson.toJson(list));
    }
    
    
    
    @Override
    public void saveMineInfo(MineInfoVO infoVO) throws PreviewInfoException  {
        Long uid = infoVO.getUid();
        Long preUid = infoVO.getPreUid();
        if (uid == null || preUid == null)
            throw new PreviewInfoException(1, "请完善所有数据");
        
        if (uid.longValue() != preUid.longValue()) {
            // preUid 必须是下级
            ThreeSaleDetail detail = threeSaleDetailService.getByBossUidAndWorkerUid(uid, preUid);
            if (detail == null) 
                throw new PreviewInfoException(1, preUid + "不是您的下级");
        }
        
        if (StringUtil.isNullOrEmpty(infoVO.getSelfRebate()) || StringUtil.isNullOrEmpty(infoVO.getShareBonus())
                || StringUtil.isNullOrEmpty(infoVO.getTeamBonus())) {
            throw new PreviewInfoException(1, "请完善所有数据");
        }
        
        Gson gson = new Gson();
        save(uid, preUid, PreviewEnum.mineInfo, gson.toJson(infoVO));
    }
    
 
    @Override
    public String saveMoneyArrival(Long uid, BigDecimal money, String time,String orderNo) throws PreviewInfoException {
        if (uid == null )
            throw new PreviewInfoException(1, "请先登录");
        if (money == null )
            throw new PreviewInfoException(1, "请输入金额");
        if(StringUtil.isNullOrEmpty(time))
            throw new PreviewInfoException(1, "请输入创建时间");
        if(StringUtil.isNullOrEmpty(time))
            throw new PreviewInfoException(1, "请输入订单号");
        String content = ""; // TODO
        // save(uid, PreviewEnum.moneyArrival, gson.toJson(infoVO));
        return content;
    }
}