admin
2020-05-06 24a8d17e007545f7426c48352109aa1a9c6587ee
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
package com.yeshi.fanli.service.impl.tlj;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
 
import com.yeshi.fanli.dao.mybatis.tlj.UserTaoLiJinDetailMapper;
import com.yeshi.fanli.entity.bus.tlj.UserTaoLiJinDetail;
import com.yeshi.fanli.entity.bus.tlj.UserTaoLiJinDetail.TaoLiJinDetailTypeEnum;
import com.yeshi.fanli.entity.bus.tlj.UserTaoLiJinOrigin;
import com.yeshi.fanli.entity.bus.tlj.UserTaoLiJinOrigin.TaoLiJinOriginEnum;
import com.yeshi.fanli.service.inter.tlj.UserTaoLiJinDetailService;
import com.yeshi.fanli.util.TimeUtil;
import com.yeshi.fanli.vo.tlj.TaoLiJinDetailVO;
import com.yeshi.fanli.vo.tlj.TaoLiJinMonthVO;
 
@Service
public class UserTaoLiJinDetailServiceImpl implements UserTaoLiJinDetailService{
 
    @Resource
    private UserTaoLiJinDetailMapper userTaoLiJinDetailMapper;
 
 
    @Override
    public void insertSelective(UserTaoLiJinDetail record) {
        userTaoLiJinDetailMapper.insertSelective(record);
    }
    
    @Override
    public Long countDetail(long uid) {
        return userTaoLiJinDetailMapper.countDetail(uid);
    }
 
 
    @Override
    public BigDecimal countUseMoneyByDate(long uid, int dateType) {
        return userTaoLiJinDetailMapper.countUseMoneyByDate(uid, dateType);
    }
    
    
    @Override
    public List<TaoLiJinDetailVO> listUserMoneyDetailForClient(Long uid, Long detailId, Date maxTime) {
        List<TaoLiJinDetailVO> finalList = new ArrayList<>();
        List<UserTaoLiJinDetail> list = null;
        if (detailId == null) {// 首次请求
            if (maxTime == null)// 没有筛选时间
            {
                list = userTaoLiJinDetailMapper.selectByMaxCreateTime(uid,
                        new Date(System.currentTimeMillis() + 1000 * 60 * 60L), 20);
            } else {// 筛选了时间
                list = userTaoLiJinDetailMapper.selectByMaxCreateTime(uid, maxTime, 20);
            }
            if (list != null && list.size() > 0) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(list.get(0).getCreateTime().getTime());
                TaoLiJinDetailVO vo = new TaoLiJinDetailVO();
                vo.setMonth(new TaoLiJinMonthVO(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1));
                finalList.add(vo);
            } else {
                if (maxTime != null) {//
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(maxTime.getTime());
                    TaoLiJinDetailVO vo = new TaoLiJinDetailVO();
                    vo.setMonth(new TaoLiJinMonthVO(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1));
                    vo.getMonth().setExpend("0");
                    vo.getMonth().setIncome("0");
                    finalList.add(vo);
                }
            }
 
        } else {// 二次请求
 
            int size = 21;
            List<UserTaoLiJinDetail> tempList = userTaoLiJinDetailMapper.selectByUidWithIndexId(uid, detailId, size);
            Set<Date> dateSet = new HashSet<>();// 用于储存是否在同一时间上面(精确到秒)
            if (tempList.size() > 0) {
                for (UserTaoLiJinDetail umd : tempList) {
                    dateSet.add(umd.getCreateTime());
                }
 
                List<UserTaoLiJinDetail> tempList2 = new ArrayList<>();
                while (dateSet.size() == 1 && tempList2.size() != tempList.size() && size < 40) {// 只有一个时间点的数据
                    tempList = tempList2;
                    size += 10;
                    tempList2 = userTaoLiJinDetailMapper.selectByUidWithIndexId(uid, detailId, size);
                    dateSet.clear();
                    for (UserTaoLiJinDetail umd : tempList2) {
                        dateSet.add(umd.getCreateTime());
                    }
                }
                if (tempList2.size() > 0)
                    tempList = tempList2;
            }
 
            for (int i = 0; i < tempList.size(); i++) {
                if (tempList.get(i).getId().longValue() == detailId) {
                    tempList.remove(i);
                    break;
                } else {
                    tempList.remove(i);
                    i--;
                }
            }
            list = tempList;
        }
 
        if (list != null) {
            if (detailId != null && list.size() > 0) {
                UserTaoLiJinDetail umd = userTaoLiJinDetailMapper.selectByPrimaryKey(detailId);
                if (!TimeUtil.getGernalTime(umd.getCreateTime().getTime(), "yyyy-MM")
                        .equalsIgnoreCase(TimeUtil.getGernalTime(list.get(0).getCreateTime().getTime(), "yyyy-MM"))) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(list.get(0).getCreateTime().getTime());
                    TaoLiJinDetailVO vo = new TaoLiJinDetailVO();
                    vo.setMonth(new TaoLiJinMonthVO(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1));
                    finalList.add(vo);
                }
 
            }
            for (int i = 0; i < list.size(); i++) {
                if (i > 0 && !TimeUtil.getGernalTime(list.get(i - 1).getCreateTime().getTime(), "yyyy-MM")
                        .equalsIgnoreCase(TimeUtil.getGernalTime(list.get(i).getCreateTime().getTime(), "yyyy-MM"))) {// 本条数据与上条数据不是同一月则插入月份
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(list.get(i).getCreateTime().getTime());
                    TaoLiJinDetailVO vo = new TaoLiJinDetailVO();
                    vo.setMonth(new TaoLiJinMonthVO(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1));
                    finalList.add(vo);
                }
                TaoLiJinDetailVO vo = new TaoLiJinDetailVO();
                vo.setDetail(list.get(i));
                finalList.add(vo);
            }
        }
 
        // 统计月资金
 
        Map<Integer, TaoLiJinDetailVO> monthMap = new TreeMap<>();
        if (finalList.size() > 1)
            for (int i = 0; i < finalList.size(); i++) {
                if (finalList.get(i).getMonth() != null)
                    monthMap.put(i, finalList.get(i));
            }
 
        if (!monthMap.isEmpty()) {
            List<String> dateFormat = new ArrayList<>();
            Iterator<Integer> keys = monthMap.keySet().iterator();
            while (keys.hasNext()) {
                Integer key = keys.next();
                String date = "";
                date += monthMap.get(key).getMonth().getYear();
                date += "-";
                date += (monthMap.get(key).getMonth().getMonth() + "").length() < 2
                        ? "0" + monthMap.get(key).getMonth().getMonth() : monthMap.get(key).getMonth().getMonth();
                dateFormat.add(date);
            }
            List<TaoLiJinMonthVO> voList = userTaoLiJinDetailMapper.selectMonthMoneyByUid(uid, dateFormat);
 
            int p = 0;
            keys = monthMap.keySet().iterator();
            while (keys.hasNext()) {
                Integer key = keys.next();
                
                BigDecimal expend = new BigDecimal(voList.get(p).getExpend());
                BigDecimal income = new BigDecimal(voList.get(p).getIncome());
                
                // 去除支出负号
                finalList.get(key).getMonth().setExpend(expend.setScale(2).toString().replace("-", ""));
                finalList.get(key).getMonth().setIncome(income.setScale(2).toString());
                p++;
            }
        }
        return finalList;
    }
 
    
    @Override
    public long countUserMoneyDetailForClient(Long uid, Long detailId, Date maxTime) {
        long monthCount = 0L;
        long detailCount = 0L;
        // 未通过时间筛选,查询所有
        if (maxTime == null) {
            detailCount = userTaoLiJinDetailMapper.selectCountByUid(uid);
            // 用于表示当前所有
            monthCount = userTaoLiJinDetailMapper.selectMonthCountByUid(uid, new Date(System.currentTimeMillis() + 1000 * 60 * 60L));
        } else {// 通过时间筛选了的,需要查询所有
            detailCount = userTaoLiJinDetailMapper.selectCountByUidAndMaxCreateTime(uid, maxTime);
            monthCount = userTaoLiJinDetailMapper.selectMonthCountByUid(uid, maxTime);
        }
 
        return monthCount + detailCount;
    }
    
    
    @Override
    public void createAddDetail(UserTaoLiJinOrigin taoLiJinOrigin) {
        UserTaoLiJinDetail detail = new UserTaoLiJinDetail();
        detail.setUid(taoLiJinOrigin.getUid());
        detail.setMoney(taoLiJinOrigin.getMoney());
        
        // 红包退回
        if (taoLiJinOrigin.getOrigin().equals(TaoLiJinOriginEnum.refund)) {
            detail.setType(TaoLiJinDetailTypeEnum.refund);
            detail.setTitle(TaoLiJinDetailTypeEnum.refund.getDesc());
        } else {
            detail.setType(TaoLiJinDetailTypeEnum.add);
            detail.setTitle(TaoLiJinDetailTypeEnum.add.getDesc());
            String desc = taoLiJinOrigin.getOrigin().getDesc();
            if (taoLiJinOrigin.getOrigin().equals(TaoLiJinOriginEnum.newbiesWin) && taoLiJinOrigin.getType() == 1) {
                desc = "分享红包"; // 老用户看到的是分享红包
            } 
            detail.setDescInfo("来源:" + desc);
        }
        detail.setCreateTime(new Date());
        userTaoLiJinDetailMapper.insertSelective(detail);
    }
    
    
    @Override
    public Long countNumByDay(String preDay) {
        return userTaoLiJinDetailMapper.countNumByDay(preDay);
    }
 
    @Override
    public BigDecimal countMoneyByDay(String preDay) {
        return userTaoLiJinDetailMapper.countMoneyByDay(preDay);
    }
}