yujian
2019-11-12 8f5ca1d337950be2a20cdb1a91a29a86fde1b07d
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
package com.yeshi.fanli.service.impl.redpack;
 
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
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.google.gson.Gson;
import com.yeshi.fanli.dao.mybatis.redpack.RedPackExchangeMapper;
import com.yeshi.fanli.dto.msg.MsgRedPackExchangeContentDTO;
import com.yeshi.fanli.entity.bus.msg.MsgMoneyDetail.MsgTypeMoneyTypeEnum;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.common.AdminUser;
import com.yeshi.fanli.entity.money.UserMoneyDetail;
import com.yeshi.fanli.entity.money.UserMoneyDetail.UserMoneyDetailTypeEnum;
import com.yeshi.fanli.entity.redpack.RedPackDetail;
import com.yeshi.fanli.entity.redpack.RedPackDetail.RedPackDetailTypeEnum;
import com.yeshi.fanli.entity.redpack.RedPackExchange;
import com.yeshi.fanli.exception.redpack.RedPackBalanceException;
import com.yeshi.fanli.exception.redpack.RedPackExchangeException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.money.UserMoneyService;
import com.yeshi.fanli.service.inter.money.msg.UserMoneyMsgNotificationService;
import com.yeshi.fanli.service.inter.redpack.RedPackBalanceService;
import com.yeshi.fanli.service.inter.redpack.RedPackConfigService;
import com.yeshi.fanli.service.inter.redpack.RedPackDetailService;
import com.yeshi.fanli.service.inter.redpack.RedPackExchangeService;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TimeUtil;
import com.yeshi.fanli.util.factory.RedPackDetailFactory;
 
@Service
public class RedPackExchangeServiceImpl implements RedPackExchangeService {
 
    @Resource
    private RedPackExchangeMapper redPackExchangeMapper;
    
    @Resource
    private RedPackConfigService redPackConfigService;
    
    @Resource
    private RedPackBalanceService redPackBalanceService;
    
    @Resource
    private UserMoneyService userMoneyService;
 
    @Resource
    private RedPackDetailService redPackDetailService;
    
    @Resource
    private UserMoneyMsgNotificationService userMoneyMsgNotificationService;
    
    
    @Override
    public List<RedPackExchange> query(Integer start, Integer count, String key, Integer state){
        return redPackExchangeMapper.query(start, count, key, state);
    }
    
    @Override
    public Long count(String key, Integer state){
        return redPackExchangeMapper.count(key, state);
    }
    
    @Override
    public Long countByUidAndState(long uid, Integer state) {
        return redPackExchangeMapper.countByUidAndState(uid, state);
    }
    
    @Override
    public BigDecimal countMoneyByUidAndState(Long uid, Integer state){
        return redPackExchangeMapper.countMoneyByUidAndState(uid, state);
    }
    
    @Override
    public RedPackExchange selectByPrimaryKey(long id){
        return redPackExchangeMapper.selectByPrimaryKey(id);
    }
    
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void exchangeCash(Long uid, BigDecimal amount) throws RedPackExchangeException {
        if (uid == null || amount == null)
            throw new RedPackExchangeException(1, "参数不正确");
 
        String extractBanlenMin = redPackConfigService.getValueByKey("extract_banlen_min");
    
        BigDecimal balance = null;
        try {
            balance = redPackBalanceService.getBalance(uid);
        } catch (RedPackBalanceException e1) {
            throw new RedPackExchangeException(1, e1.getMsg());
        }
        
        if (balance == null || balance.compareTo(new BigDecimal(extractBanlenMin)) < 0)
            throw new RedPackExchangeException(1, "余额不足" + extractBanlenMin + "元");
        
        if (balance.compareTo(amount) < 0)
            throw new RedPackExchangeException(1, "提现余额不足");
        
        String moneyMin = redPackConfigService.getValueByKey("extract_money_min");
        String moneyMax = redPackConfigService.getValueByKey("extract_money_max");
        if (amount.compareTo(new BigDecimal(moneyMin)) < 0 || amount.compareTo(new BigDecimal(moneyMax)) > 0)
            throw new RedPackExchangeException(1, "提现金额至少" + moneyMin + "元至多" + moneyMax + "元");
        
        Date nowDate = new Date();
        
        // 提现申请
        RedPackExchange exchange = new RedPackExchange();
        exchange.setUid(uid);
        exchange.setMoney(amount);
        exchange.setState(RedPackExchange.STATE_INIT);
        exchange.setCreateTime(nowDate);
        redPackExchangeMapper.insertSelective(exchange);
        
        // 减少红包
        try {
            redPackBalanceService.subRedPack(uid, amount, RedPackDetailFactory.createExchange(exchange));
        } catch (Exception e) {
            throw new RedPackExchangeException(1, "红包信息异常");
        }
    }
    
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void passExchange(final long id, AdminUser admin) throws RedPackExchangeException {
        RedPackExchange record = redPackExchangeMapper.selectByPrimaryKey(id);
        if (record == null) 
            throw new RedPackExchangeException(1,"申请记录已不存在");
 
        if (RedPackExchange.STATE_INIT != record.getState())
            throw new RedPackExchangeException(1,"该申请已被处理,请刷新");
 
        Date nowDate = new Date();
        record.setAuditId(admin.getId());
        record.setAuditTime(nowDate);
        record.setState(RedPackExchange.STATE_SUCCESS);
        redPackExchangeMapper.updateByPrimaryKeySelective(record);
        
        // 资金明细-添加资金
        UserMoneyDetail detail = new UserMoneyDetail();
        detail.setCreateTime(new Date());
        detail.setIdentifyCode(StringUtil.Md5(UserMoneyDetailTypeEnum.redPackExchange.name() + ":" + record.getId()));
        detail.setMoney(record.getMoney());
        detail.setType(UserMoneyDetailTypeEnum.redPackExchange);
        detail.setTitle(UserMoneyDetailTypeEnum.redPackExchange.getDesc());
        detail.setDescInfo("于"+ TimeUtil.formatDate(record.getCreateTime()) +"提现");
        detail.setUpdateTime(new Date());
        detail.setUserInfo(new UserInfo(record.getUid()));
        
        // 添加资金
        userMoneyService.addUserMoney(record.getUid(), record.getMoney(), detail);
        
        // 更新红包提现明细
        try {
            String identifyCode = StringUtil.Md5(RedPackDetailTypeEnum.redExchange.name() + ":" + record.getId());
            RedPackDetail redPackDetail = redPackDetailService.getByIdentifyCode(identifyCode);
            // 创建明细更新
            RedPackDetail updateDetail = RedPackDetailFactory.updateExchangePass(redPackDetail.getId(), record);
            redPackDetailService.updateByPrimaryKeySelective(updateDetail);
        } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
            throw new RedPackExchangeException(1,"更新提现明细出错");
        }
        
        try {
            //消息
            SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd HH:mm");
            String beizu = "红包提现成功后请到“我的-账户余额”中查看";
            MsgRedPackExchangeContentDTO dto = new MsgRedPackExchangeContentDTO();
            dto.setTitle("红包提现");
            dto.setTime(sd.format(record.getCreateTime()));
            dto.setHandle("人工审核" + sd.format(new Date())); 
            dto.setMoney("¥" + record.getMoney().setScale(2));        
            dto.setBalance("¥" + redPackBalanceService.getBalance(record.getUid()));
            userMoneyMsgNotificationService.redPackMsg(record.getUid(), MsgTypeMoneyTypeEnum.redPackExchangePass, new Gson().toJson(dto), beizu);
        } catch (RedPackBalanceException e) {
            throw new RedPackExchangeException(1, e.getMsg());
        }
    }
    
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void rejectExchange(long id, String reason, AdminUser admin) throws RedPackExchangeException {
        RedPackExchange record = redPackExchangeMapper.selectByPrimaryKey(id);
        if (record == null) 
            throw new RedPackExchangeException(1,"申请记录已不存在");
 
        if (RedPackExchange.STATE_INIT != record.getState())
            throw new RedPackExchangeException(1,"该申请已被处理,请刷新");
        
        record.setReason(reason);
        record.setAuditTime(new Date());
        record.setAuditId(admin.getId());
        record.setState(RedPackExchange.STATE_REJECT);
        redPackExchangeMapper.updateByPrimaryKeySelective(record);
        
        // 退回红包
        try {
            redPackBalanceService.addRedPack(record.getUid(), record.getMoney(), RedPackDetailFactory.createExchangeReject(record));
        } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
            throw new RedPackExchangeException(1, "红包退回时出错");
        }
        
        //消息
        SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd HH:mm");
        MsgRedPackExchangeContentDTO dto = new MsgRedPackExchangeContentDTO();
        dto.setTitle("红包提现");
        dto.setReason(reason);
        dto.setHandle(""); // TODO 处理方式待定
        dto.setTime(sd.format(record.getCreateTime()));
        userMoneyMsgNotificationService.redPackMsg(record.getUid(), MsgTypeMoneyTypeEnum.redPackExchangeReject, new Gson().toJson(dto), null);
    }
 
}