yujian
2019-12-10 82b11aa8190638990687b0b36b815a3b5c142bd4
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
package com.yeshi.fanli.service.impl.redpack;
 
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.DateUtil;
 
import com.google.gson.Gson;
import com.yeshi.fanli.dao.mybatis.redpack.RedPackForbidMapper;
import com.yeshi.fanli.dto.msg.MsgRedPackExchangeContentDTO;
import com.yeshi.fanli.entity.bus.msg.MsgMoneyDetail.MsgTypeMoneyTypeEnum;
import com.yeshi.fanli.entity.redpack.RedPackBalance;
import com.yeshi.fanli.entity.redpack.RedPackForbid;
import com.yeshi.fanli.entity.redpack.RedPackForbidRecord;
import com.yeshi.fanli.exception.redpack.RedPackBalanceException;
import com.yeshi.fanli.exception.redpack.RedPackDetailException;
import com.yeshi.fanli.exception.redpack.RedPackForbidException;
import com.yeshi.fanli.service.inter.money.msg.UserMoneyMsgNotificationService;
import com.yeshi.fanli.service.inter.redpack.RedPackBalanceService;
import com.yeshi.fanli.service.inter.redpack.RedPackForbidRecordService;
import com.yeshi.fanli.service.inter.redpack.RedPackForbidService;
import com.yeshi.fanli.util.factory.RedPackDetailFactory;
 
@Service
public class RedPackForbidServiceImpl implements RedPackForbidService {
 
    @Resource
    private RedPackForbidMapper redPackForbidMapper;
    
    @Resource
    private RedPackForbidRecordService redPackForbidRecordService;
    
    @Resource
    private RedPackBalanceService redPackBalanceService;
    
    @Resource
    private UserMoneyMsgNotificationService userMoneyMsgNotificationService;
    
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addForbid(RedPackForbidRecord record) throws RedPackForbidException {
        Long uid = record.getUid();
        if (uid == null || uid <= 0)
            throw new RedPackForbidException(1,"用户id不能为空");
        
        Integer type = record.getType();
        if (type == null)
            throw new RedPackForbidException(1,"封禁时间不能为空");
        
        String time = null;
        Date endTime = null;
        Date nowTime = new Date();
        if (type == RedPackForbidRecord.TYPE_ONE_MONTH) {
            time = "1个月";
            endTime = DateUtil.plusMonths(nowTime, 1);
        } else if (type == RedPackForbidRecord.TYPE_THREE_MONTH) {
            time = "3个月";
            endTime = DateUtil.plusMonths(nowTime, 3);
        } else if (type == RedPackForbidRecord.TYPE_SIX_MONTH) {
            time = "半年";
            endTime = DateUtil.plusMonths(nowTime, 6);
        } else if (type == RedPackForbidRecord.TYPE_ONE_YEAR) {
            time = "1年";
            endTime = DateUtil.plusYears(nowTime, 1);
        } else if (type == RedPackForbidRecord.TYPE_HUNDRED_YEAR) {
            time = "永久";
            endTime = DateUtil.plusYears(nowTime, 100);
        } else {
            throw new RedPackForbidException(1,"封禁时间类型不正确");
        }
    
        // 查询余额
        BigDecimal balance = new BigDecimal(0);
        RedPackBalance redPackBalance = redPackBalanceService.selectForUpdate(uid);
        if (redPackBalance != null && redPackBalance.getMoney() != null)
            balance = redPackBalance.getMoney();
        
        // 加入封禁
        RedPackForbid redPackForbid = new RedPackForbid();
        redPackForbid.setId(uid);
        redPackForbid.setEndTime(endTime);
        redPackForbid.setCreateTime(nowTime);
        redPackForbidMapper.insertSelective(redPackForbid);
        
        // 加入封禁记录
        record.setEndTime(endTime);
        record.setMoney(balance);
        record.setCreateTime(nowTime);
        redPackForbidRecordService.insertSelective(record);
        
        // 清空红包
        try {
            redPackBalanceService.resetRedPack(uid, RedPackDetailFactory.createForbid(record));
        } catch (RedPackBalanceException e) {
            throw new RedPackForbidException(1, e.getMsg());
        } catch (RedPackDetailException e) {
            throw new RedPackForbidException(1, e.getMsg());
        }
        
        //消息
        MsgRedPackExchangeContentDTO dto = new MsgRedPackExchangeContentDTO();
        dto.setTime(time);
        dto.setReason("红包产生、使用、赠送环节涉嫌违规");
        dto.setHandle("清空所有红包余额");
        userMoneyMsgNotificationService.redPackMsg(uid, MsgTypeMoneyTypeEnum.redPackForbid, new Gson().toJson(dto), "封禁无法申诉,请按照规则使用红包功能  ");
    }
 
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deduct(RedPackForbidRecord record) throws RedPackForbidException {
        Long uid = record.getUid();
        if (uid == null || uid <= 0)
            throw new RedPackForbidException(1,"用户id不能为空");
        
        BigDecimal money = record.getMoney();
        if (money == null)
            throw new RedPackForbidException(1,"扣除金额不能为空");
        
        // 清空红包
        try {
            redPackBalanceService.subRedPack(uid, money, RedPackDetailFactory.createDeduct(record));
        } catch (RedPackBalanceException e) {
            throw new RedPackForbidException(1, e.getMsg());
        } catch (RedPackDetailException e) {
            throw new RedPackForbidException(1, e.getMsg());
        }
        
        //消息
        MsgRedPackExchangeContentDTO dto = new MsgRedPackExchangeContentDTO();
        dto.setBalance(redPackBalanceService.getBalance(uid).setScale(2).toString());
        dto.setMoney(money.setScale(2).toString());
        dto.setReason("红包产生、使用、赠送环节涉嫌违规");
        userMoneyMsgNotificationService.redPackMsg(uid, MsgTypeMoneyTypeEnum.redPackDeduct, new Gson().toJson(dto), "扣除金额无法申诉,请按照规则获得红包");
    }
    
    @Override
    public void delete(List<Long> idsList) {
        if (idsList != null)
            for (Long id : idsList) {
                redPackForbidMapper.deleteByPrimaryKey(id);
                //消息
                MsgRedPackExchangeContentDTO dto = new MsgRedPackExchangeContentDTO();
                dto.setReason("封禁时间到期,自动解封");
                dto.setTime("已可以重新使用红包功能");
                dto.setHandle("已被清空");
                userMoneyMsgNotificationService.redPackMsg(id, MsgTypeMoneyTypeEnum.redPackForbidRemove, new Gson().toJson(dto), "封禁无法申诉,请按照规则使用红包功能  ");
            }
 
    }
    
    @Override
    public boolean verifyForbid(Long uid) {
        if (uid != null) {
            RedPackForbid redPackForbid = redPackForbidMapper.selectByPrimaryKey(uid);
            if (redPackForbid != null) {
                if (redPackForbid.getEndTime() == null || redPackForbid.getEndTime().getTime() > java.lang.System.currentTimeMillis()) {
                    return true;
                } else {
                    // 时间已过,解除封禁
                    redPackForbidMapper.deleteByPrimaryKey(uid);
                    return false;
                }
            }
        }
        return false;
    }
    
    
}