yujian
2019-07-11 c44ff90c2610ecfbb87ace8dea0d636cf07817cb
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
package com.yeshi.fanli.service.impl.tlj;
 
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
 
import com.yeshi.fanli.dao.mybatis.tlj.UserTaoLiJinReportMapper;
import com.yeshi.fanli.entity.bus.tlj.UserTaoLiJinReport;
import com.yeshi.fanli.exception.tlj.UserTaoLiJinOriginException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.tlj.UserTaoLiJinOriginService;
import com.yeshi.fanli.service.inter.tlj.UserTaoLiJinReportService;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
 
@Service
public class UserTaoLiJinReportServiceImpl implements UserTaoLiJinReportService{
 
    @Resource
    private UserTaoLiJinReportMapper userTaoLiJinReportMapper;
    
    @Resource
    private UserTaoLiJinOriginService userTaoLiJinOriginService;
    
    @Override
    public void insertDefault(String rightsId) {
        if (StringUtil.isNullOrEmpty(rightsId)) {
            return;
        }
        
        UserTaoLiJinReport report = new UserTaoLiJinReport();
        report.setId(rightsId);
        report.setUnfreezeAmount(new BigDecimal(0));
        report.setUnfreezeNum(0);
        report.setRefundAmount(new BigDecimal(0));
        report.setRefundNum(0);
        report.setAlipayAmount(new BigDecimal(0));
        report.setUseAmount(new BigDecimal(0));
        report.setUseNum(0);
        report.setWinAmount(new BigDecimal(0));
        report.setWinNum(0);
        report.setPreCommissionAmount(new BigDecimal(0));
        report.setCreateTime(new Date());
        report.setUpdateTime(new Date());
        userTaoLiJinReportMapper.insertSelective(report);
    }
    
    
    
    
    @Override
    public void updateByPrimaryKeySelective(UserTaoLiJinReport record) {
        if (record == null || record.getId() == null) {
            return;
        }
        record.setUpdateTime(new Date());
        
        userTaoLiJinReportMapper.updateByPrimaryKeySelective(record);
    }
    
    @Override
    public void needUpdateReport() {
        List<UserTaoLiJinReport> list = userTaoLiJinReportMapper.needUpdateReport();
        if (list == null || list.size() == 0) {
            return;
        }
        
        for (UserTaoLiJinReport report: list) {
            UserTaoLiJinReport taoLiJinReport = null;
            try {
                taoLiJinReport = TaoKeApiUtil.getTaoLiJinEffective(report.getId());
            } catch (Exception e) {
                LogHelper.errorDetailInfo(e);
                continue;
            }
            
            if (taoLiJinReport == null) {
                continue;
            }
            
            // 是否需要更新
            boolean needUpdate = false;
            
            if(taoLiJinReport.getWinNum() != report.getWinNum()){
                needUpdate = true;
            }
            if(taoLiJinReport.getWinAmount() != report.getWinAmount()){
                needUpdate = true;
            }
            if(taoLiJinReport.getUnfreezeNum() != report.getUnfreezeNum()){
                needUpdate = true;
            }
            if(taoLiJinReport.getUnfreezeAmount() != report.getUnfreezeAmount()){
                needUpdate = true;
            }
            if(taoLiJinReport.getPreCommissionAmount() != report.getPreCommissionAmount()){
                needUpdate = true;
            }
            if(taoLiJinReport.getUseNum() != report.getUseNum()){
                needUpdate = true;
            }
            if(taoLiJinReport.getUseAmount() != report.getUseAmount()){
                needUpdate = true;
            }
            
            if(taoLiJinReport.getAlipayAmount() != report.getAlipayAmount()){
                needUpdate = true;
            }
            
            if(taoLiJinReport.getRefundNum() != report.getRefundNum()){
                needUpdate = true;
            }
            
            // 失效退回金额发生变化
            BigDecimal refundAmount = taoLiJinReport.getRefundAmount();
            if(refundAmount.compareTo(report.getRefundAmount()) > 0) {
                needUpdate = true;
                try {
                    userTaoLiJinOriginService.refundMoney(taoLiJinReport);
                } catch (UserTaoLiJinOriginException e) {
                    e.printStackTrace();
                }
            }
            
            // 更新报告
            if(needUpdate) {
                taoLiJinReport.setUpdateTime(new Date());
                userTaoLiJinReportMapper.updateByPrimaryKeySelective(taoLiJinReport);
            }
        }
    }
    
    
    @Override
    public void refundNotWin() {
        List<UserTaoLiJinReport> list = userTaoLiJinReportMapper.refundNotWin();
        if (list == null || list.size() == 0) {
            return;
        }
        
        for (UserTaoLiJinReport report: list) {
            // 总个数
            Integer totalNum = report.getTotalNum();
            BigDecimal preFace = report.getPreFace();
            
            // 总金额
            BigDecimal money = MoneyBigDecimalUtil.mul(new BigDecimal(totalNum), preFace);
            
            report.setRefundNum(totalNum);
            report.setRefundAmount(money);
            report.setUpdateTime(new Date());
            
            try {
                // 退回淘礼金
                userTaoLiJinOriginService.refundMoney(report);
                
            } catch (UserTaoLiJinOriginException e) {
                LogHelper.errorDetailInfo(e);
                continue;
            }
            
            // 更新报告
            userTaoLiJinReportMapper.updateByPrimaryKeySelective(report);
        }
    }
    
    @Override
    public UserTaoLiJinReport selectByPrimaryKey(String id) {
        return userTaoLiJinReportMapper.selectByPrimaryKey(id);
    }
    
}