admin
2020-05-23 891d1535999075a354ff1014ae5dbede5cfddc29
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
package com.yeshi.fanli.service.manger.order;
 
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Component;
 
import com.yeshi.fanli.dao.mybatis.order.UserOrderDailyStatisticDTO;
import com.yeshi.fanli.dao.mybatis.order.UserOrderDailyStatisticMapper;
import com.yeshi.fanli.entity.bus.user.ThreeSaleDetail;
import com.yeshi.fanli.entity.order.OrderMoneyDailyCount;
import com.yeshi.fanli.entity.order.OrderMoneyDailyCount.SourceTypeEnum;
import com.yeshi.fanli.entity.order.dividents.TeamDividentsSourceOrderUserMap;
import com.yeshi.fanli.service.inter.order.OrderMoneyDailyCountService;
import com.yeshi.fanli.service.inter.user.invite.ThreeSaleDetailService;
import com.yeshi.fanli.util.TimeUtil;
 
@Component
public class OrderMoneyStatisticManager {
 
    @Resource
    private UserOrderDailyStatisticMapper userOrderDailyStatisticMapper;
 
    @Resource
    private OrderMoneyDailyCountService orderMoneyDailyCountService;
 
    @Resource
    private ThreeSaleDetailService threeSaleDetailService;
 
    private static SourceTypeEnum getSourceTypeByDesc(String name) {
        for (SourceTypeEnum type : SourceTypeEnum.values()) {
            if (type.getDesc().equalsIgnoreCase(name))
                return type;
        }
        return null;
    }
 
    /**
     * 同步自购与分享赚相关数据
     * @Title: syncFanliAndShareData
     * @Description: 
     * @param startTime
     * @param endTime 
     * void 返回类型
     * @throws
     */
    public void syncFanliAndShareData(Date startTime, Date endTime) {
        List<UserOrderDailyStatisticDTO> dtoList = userOrderDailyStatisticMapper
                .listFanLiAndShareByPlaceOrderTime(startTime, endTime);
 
        for (UserOrderDailyStatisticDTO dto : dtoList) {
            OrderMoneyDailyCount count = new OrderMoneyDailyCount();
            count.setUid(dto.getUid());
            count.setCountDay(new Date(TimeUtil.convertToTimeTemp(dto.getDay(), "yyyy-MM-dd")));
            if (dto.getMoney() != null)
                count.setIncome(
                        dto.getMoney().setScale(2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).intValue());
            count.setOrderNum(dto.getOrderCount());
            count.setSourceType(getSourceTypeByDesc(dto.getType()));
            count.setYearMonth(
                    TimeUtil.getGernalTime(TimeUtil.convertToTimeTemp(dto.getDay(), "yyyy-MM-dd"), "yyyy-MM"));
            orderMoneyDailyCountService.saveDailyCount(count);
        }
    }
 
    /**
     * 同步团队订单相关数据(1级/2级)
     * @Title: syncTeamData
     * @Description: 
     * @param startTime
     * @param endTime 
     * void 返回类型
     * @throws
     */
    public void syncTeamData(Date startTime, Date endTime) {
        List<UserOrderDailyStatisticDTO> dtoList = userOrderDailyStatisticMapper.listTeamByPlaceOrderTime(startTime,
                endTime);
 
        for (UserOrderDailyStatisticDTO dto : dtoList) {
            OrderMoneyDailyCount count = new OrderMoneyDailyCount();
            count.setUid(dto.getUid());
            count.setCountDay(new Date(TimeUtil.convertToTimeTemp(dto.getDay(), "yyyy-MM-dd")));
            if (dto.getMoney() != null)
                count.setIncomeTeam(
                        dto.getMoney().setScale(2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).intValue());
            count.setOrderNumTeam(dto.getOrderCount());
            count.setSourceType(getSourceTypeByDesc(dto.getType()));
            count.setYearMonth(
                    TimeUtil.getGernalTime(TimeUtil.convertToTimeTemp(dto.getDay(), "yyyy-MM-dd"), "yyyy-MM"));
            orderMoneyDailyCountService.saveDailyCount(count);
        }
    }
 
    /**
     * 同步订单数量相关数据
     * @Title: syncOrderNum
     * @Description: 
     * @param minTime
     * @param maxTime 
     * void 返回类型
     * @throws
     */
    // 同步订单数量数据
    public void syncOrderNum(Date minTime, Date maxTime) {
        List<UserOrderDailyStatisticDTO> dtoList = userOrderDailyStatisticMapper.listOrderCountByPlaceOrderTime(minTime,
                maxTime);
        Set<Long> uidSets = new HashSet<>();
        // 统计今天的数据
        for (UserOrderDailyStatisticDTO dto : dtoList) {
            uidSets.add(dto.getUid());
        }
 
        Map<Long, List<ThreeSaleDetail>> bossMap = new HashMap<>();
 
        Map<String, Integer> countMap = new HashMap<>();
 
        // 聚合数据
        for (UserOrderDailyStatisticDTO dto : dtoList) {
            if (bossMap.get(dto.getUid()) == null) {
                List<ThreeSaleDetail> list = threeSaleDetailService.listByWorkerUid(dto.getUid());
                bossMap.put(dto.getUid(), list);
            }
            List<ThreeSaleDetail> tsList = bossMap.get(dto.getUid());
            if (tsList != null)
                for (ThreeSaleDetail tsd : tsList) {
                    String key = tsd.getBossUid() + "#" + dto.getDay() + "#";
                    if (tsd.getLevel() < 3)
                        key += tsd.getLevel();
                    else
                        key += 3;
                    if (countMap.get(key) == null)
                        countMap.put(key, 0);
                    countMap.put(key, countMap.get(key) + dto.getOrderCount());
                }
 
        }
 
        // 插入数据
        for (Iterator<String> its = countMap.keySet().iterator(); its.hasNext();) {
            String key = its.next();
            String uid = key.split("#")[0];
            String day = key.split("#")[1];
            int level = Integer.parseInt(key.split("#")[2]);
 
            OrderMoneyDailyCount count = new OrderMoneyDailyCount();
            count.setUid(Long.parseLong(uid));
            count.setCountDay(new Date(TimeUtil.convertToTimeTemp(day, "yyyy-MM-dd")));
            if (level == 1)
                count.setDirectOrderNum(countMap.get(key));
            else if (level == 2)
                count.setInDirectOrderNum(countMap.get(key));
            else
                count.setBeyondOrderNum(countMap.get(key));
 
            count.setYearMonth(TimeUtil.getGernalTime(TimeUtil.convertToTimeTemp(day, "yyyy-MM-dd"), "yyyy-MM"));
            orderMoneyDailyCountService.saveDailyCount(count);
 
        }
 
    }
 
    /**
     * 同步分红订单统计数据
     * @Title: syncDividents
     * @Description: 
     * @param minTime
     * @param maxTime 
     * void 返回类型
     * @throws
     */
    public void syncDividents(Date minTime, Date maxTime) {
        List<UserOrderDailyStatisticDTO> list = userOrderDailyStatisticMapper.listDividentsOrderByCreateTime(minTime,
                maxTime);
        if (list != null)
            for (UserOrderDailyStatisticDTO dto : list) {
                OrderMoneyDailyCount count = new OrderMoneyDailyCount();
                count.setUid(dto.getUid());
                count.setCountDay(new Date(TimeUtil.convertToTimeTemp(dto.getDay(), "yyyy-MM-dd")));
                if (Integer.parseInt(dto.getType()) == TeamDividentsSourceOrderUserMap.TYPE_TEAM_DIVIDENTS) {
                    count.setTeamReward(dto.getMoney().setScale(2, BigDecimal.ROUND_HALF_UP)
                            .multiply(new BigDecimal(100)).intValue());
                    count.setTeamRewardNum(dto.getOrderCount());
                } else {
                    count.setTeamSubsidy(dto.getMoney().setScale(2, BigDecimal.ROUND_HALF_UP)
                            .multiply(new BigDecimal(100)).intValue());
                    count.setTeamSubsidyNum(dto.getOrderCount());
                }
 
                count.setYearMonth(
                        TimeUtil.getGernalTime(TimeUtil.convertToTimeTemp(dto.getDay(), "yyyy-MM-dd"), "yyyy-MM"));
                if(dto.getDay().equalsIgnoreCase("2020-05-03")&&dto.getUid()==389677L){
                    System.out.println(dto);
                }
                
                orderMoneyDailyCountService.saveDailyCount(count);
 
            }
    }
 
}