yujian
2020-01-17 bf0a347104579e5f7a2ed6e660ebb9f17f76ffc0
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
package com.yeshi.fanli.service.impl.order;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.yeshi.fanli.dao.mybatis.order.UserOrderWeiQuanRecordMapper;
import com.yeshi.fanli.dto.HongBaoDTO;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.order.CommonOrder;
import com.yeshi.fanli.entity.order.HongBaoOrder;
import com.yeshi.fanli.entity.order.UserOrderWeiQuanRecord;
import com.yeshi.fanli.entity.taobao.TaoBaoWeiQuanDrawBack;
import com.yeshi.fanli.entity.taobao.TaoBaoWeiQuanOrder;
import com.yeshi.fanli.service.inter.money.tb.TaoBaoWeiQuanDrawBackService;
import com.yeshi.fanli.service.inter.order.CommonOrderService;
import com.yeshi.fanli.service.inter.order.HongBaoOrderService;
import com.yeshi.fanli.service.inter.order.HongBaoV2Service;
import com.yeshi.fanli.service.inter.order.UserOrderWeiQuanRecordService;
import com.yeshi.fanli.service.inter.order.tb.TaoBaoWeiQuanOrderService;
import com.yeshi.fanli.util.Constant;
 
@Service
public class UserOrderWeiQuanRecordServiceImpl implements UserOrderWeiQuanRecordService {
 
    @Resource
    private UserOrderWeiQuanRecordMapper userOrderWeiQuanRecordMapper;
 
    @Resource
    private TaoBaoWeiQuanOrderService taoBaoWeiQuanOrderService;
 
    @Resource
    private TaoBaoWeiQuanDrawBackService taoBaoWeiQuanDrawBackService;
 
    @Resource
    private HongBaoV2Service hongBaoV2Service;
 
    @Resource
    private CommonOrderService commonOrderService;
 
    @Resource
    private HongBaoOrderService hongBaoOrderService;
 
    @Override
    public void syncPrevious() {
        BigDecimal zero = new BigDecimal(0);
        for (int page = 1; page < Integer.MAX_VALUE; page++) {
            List<TaoBaoWeiQuanOrder> list = taoBaoWeiQuanOrderService.listByBeginWeiQuan(page, 100);
            if (list == null || list.isEmpty())
                break;
 
            for (TaoBaoWeiQuanOrder weiQuanOrder : list) {
                String orderItemId = weiQuanOrder.getOrderItemId();
 
                List<HongBaoDTO> listV2 = hongBaoV2Service.listByOrderTradeId(orderItemId);
                for (HongBaoDTO hongBaoV2 : listV2) {
                    Long uid = hongBaoV2.getUserInfo().getId();
                    TaoBaoWeiQuanDrawBack drawBack = taoBaoWeiQuanDrawBackService.selectByOrderItemIdAndUid(orderItemId,
                            uid);
 
                    int state = 0;
                    BigDecimal money = null;
                    if (drawBack != null) {
                        state = 1; // 已扣款
                        BigDecimal drawBackMoney = drawBack.getDrawBackMoney();
                        if (drawBackMoney.compareTo(zero) > 0) {
                            money = drawBackMoney;
                        }
                    }
 
                    if (money == null) {
                        // (维权金额/结算金额) * 返利金额
                        BigDecimal wqmoney = weiQuanOrder.getMoney();
                        money = wqmoney.multiply(hongBaoV2.getMoney()).divide(hongBaoV2.getSettlement(), 2,
                                BigDecimal.ROUND_UP);
 
                        // 大于返利金额 则等于返利金额
                        if (money.compareTo(hongBaoV2.getMoney()) > 0)
                            money = hongBaoV2.getMoney();
                    }
 
                    UserOrderWeiQuanRecord weiQuanRecord = new UserOrderWeiQuanRecord();
                    weiQuanRecord.setUid(uid);
                    weiQuanRecord.setSourceType(1); // 淘宝
                    weiQuanRecord.setTradeId(orderItemId);
                    weiQuanRecord.setCreateTime(weiQuanOrder.getCreateTime());
                    weiQuanRecord.setMoney(money);
                    weiQuanRecord.setState(state);
                    userOrderWeiQuanRecordMapper.insertSelective(weiQuanRecord);
                }
            }
        }
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addTaoBaoWeiQuan(TaoBaoWeiQuanOrder order) {
        if (!order.getState().contains("维权成功"))
            return;
        // 查询是否存在红包
        CommonOrder commonOrder = commonOrderService.selectBySourceTypeAndTradeId(Constant.SOURCE_TYPE_TAOBAO,
                order.getOrderItemId());
        if (commonOrder == null)
            return;
        HongBaoOrder hongBaoOrder = hongBaoOrderService.selectDetailByCommonOrderId(commonOrder.getId());
        if (hongBaoOrder == null || hongBaoOrder.getHongBaoV2() == null)
            return;
 
        BigDecimal settleMoney = hongBaoOrder.getCommonOrder().getSettlement();
        HongBaoV2 mainHongBao = hongBaoOrder.getHongBaoV2();
        UserOrderWeiQuanRecord ur = userOrderWeiQuanRecordMapper.selectByOrderInfoAndUid(
                mainHongBao.getUserInfo().getId(), order.getOrderItemId(), Constant.SOURCE_TYPE_TAOBAO);
        if (ur == null) {
            UserOrderWeiQuanRecord record = new UserOrderWeiQuanRecord();
            record.setCreateTime(new Date());
            record.setMoney(hongBaoOrder.getHongBaoV2().getMoney().multiply(order.getMoney()).divide(settleMoney, 2,
                    RoundingMode.UP));
            record.setSourceType(Constant.SOURCE_TYPE_TAOBAO);
            record.setState(0);
            record.setTradeId(order.getOrderItemId());
            record.setUid(hongBaoOrder.getHongBaoV2().getUserInfo().getId());
 
            if (record.getMoney().compareTo(hongBaoOrder.getHongBaoV2().getMoney()) > 0)
                record.setMoney(hongBaoOrder.getHongBaoV2().getMoney());
 
            userOrderWeiQuanRecordMapper.insertSelective(record);
        }
        List<HongBaoV2> children = hongBaoV2Service.listChildrenById(mainHongBao.getId());
        for (HongBaoV2 v2 : children) {
            UserOrderWeiQuanRecord record = userOrderWeiQuanRecordMapper.selectByOrderInfoAndUid(
                    v2.getUserInfo().getId(), order.getOrderItemId(), Constant.SOURCE_TYPE_TAOBAO);
            if (record == null) {
                record = new UserOrderWeiQuanRecord();
                record.setCreateTime(new Date());
                record.setMoney(v2.getMoney().multiply(order.getMoney()).divide(settleMoney, 2, RoundingMode.UP));
                record.setSourceType(Constant.SOURCE_TYPE_TAOBAO);
                record.setState(0);
                record.setTradeId(order.getOrderItemId());
                record.setUid(v2.getUserInfo().getId());
 
                if (record.getMoney().compareTo(v2.getMoney()) > 0)
                    record.setMoney(v2.getMoney());
 
                userOrderWeiQuanRecordMapper.insertSelective(record);
            }
        }
    }
 
    @Override
    public UserOrderWeiQuanRecord selectByOrderInfoAndUid(Long uid, String tradeId, int sourceType) {
        UserOrderWeiQuanRecord record = userOrderWeiQuanRecordMapper.selectByOrderInfoAndUid(uid, tradeId, sourceType);
 
        return record;
    }
 
    
    @Override
    public Integer countWeiQaunOrderNumberByDate(String preDay) {
        return  userOrderWeiQuanRecordMapper.countWeiQaunOrderNumberByDate(preDay);
    }
    
    
    
    @Override
    public BigDecimal countWeiQaunOrderMoneyByDate(String preDay) {
        return  userOrderWeiQuanRecordMapper.countWeiQaunOrderMoneyByDate(preDay);
    }
}