yujian
2019-10-28 9319c42fe02d041cee260ca0db8df67bcdf1ea0a
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
package com.yeshi.fanli.service.impl.redpack;
 
import java.math.BigDecimal;
import java.util.Date;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.DateUtil;
 
import com.yeshi.fanli.dao.mybatis.redpack.RedPackGiveRecordMapper;
import com.yeshi.fanli.entity.bus.user.TokenRecord;
import com.yeshi.fanli.entity.bus.user.TokenRecord.TokenTypeEnum;
import com.yeshi.fanli.entity.redpack.RedPackDetail.RedPackDetailTypeEnum;
import com.yeshi.fanli.entity.redpack.RedPackGiveRecord;
import com.yeshi.fanli.exception.redpack.RedPackGiveRecordException;
import com.yeshi.fanli.service.inter.redpack.RedPackBalanceService;
import com.yeshi.fanli.service.inter.redpack.RedPackConfigService;
import com.yeshi.fanli.service.inter.redpack.RedPackGiveRecordService;
import com.yeshi.fanli.service.inter.user.TokenRecordService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.TokenUtil;
import com.yeshi.fanli.util.factory.RedPackDetailFactory;
 
@Service
public class RedPackGiveRecordServiceImpl implements RedPackGiveRecordService {
 
    @Resource
    private RedPackGiveRecordMapper redPackGiveRecordMapper;
 
    @Resource
    private RedPackConfigService redPackConfigService;
    
    @Resource
    private RedPackBalanceService redPackBalanceService;
 
    
    @Resource
    private TokenRecordService tokenRecordService;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String giving(Long uid, BigDecimal amount) throws RedPackGiveRecordException {
        if (uid == null || amount == null)
            throw new RedPackGiveRecordException(1, "参数不正确");
 
        String giveMin = redPackConfigService.getValueByKey("give_money_min");
        String giveMax = redPackConfigService.getValueByKey("give_money_max");
        if (amount.compareTo(new BigDecimal(giveMin)) < 0 || amount.compareTo(new BigDecimal(giveMax)) > 0)
            throw new RedPackGiveRecordException(1, "赠送金额至少" + giveMin + "元至多" + giveMax + "元");
        
        BigDecimal balance = redPackBalanceService.getBalance(uid);
        if (balance == null || amount.compareTo(balance) > 0)
            throw new RedPackGiveRecordException(1, "余额不足");
 
        Date nowDate = new Date();
        // 赠送记录
        RedPackGiveRecord giveRecord = new RedPackGiveRecord();
        giveRecord.setAmount(amount);
        giveRecord.setGiveUid(uid);
        giveRecord.setState(RedPackGiveRecord.STATE_INIT);
        giveRecord.setGiveTime(nowDate);
        giveRecord.setEndTime(DateUtil.plusDayDate(Constant.GIVE_DAYS, new Date()));
        redPackGiveRecordMapper.insertSelective(giveRecord);
 
        // 口令记录
        TokenRecord tokenRecord = new TokenRecord();
        tokenRecord.setUid(uid);
        tokenRecord.setIdentify(giveRecord.getId() + "");
        tokenRecord.setType(TokenTypeEnum.redPack);
        tokenRecord.setStartTime(nowDate);
        tokenRecord.setEndTime(DateUtil.plusDayDate(Constant.TOKEN_DAYS, new Date()));
        tokenRecord.setState(0);
        tokenRecordService.insertSelective(tokenRecord);
 
        // 创建口令
        String token = TokenUtil.createToken(tokenRecord.getId());
        tokenRecord.setToken(token);
        tokenRecordService.updateByPrimaryKeySelective(tokenRecord);
 
        String tips = redPackConfigService.getValueByKey("give_tips");
        String projectChineseName = Constant.systemCommonConfig.getProjectChineseName();
        while (tips.contains("{APP名称}")) {
            tips = tips.replace("{APP名称}", projectChineseName);
        }
        tips = tips.replace("{口令}", token).replace("{下载链接}", redPackConfigService.getValueByKey("app_down_link")).replace("{面额}",
                amount.setScale(0).toString());
        
        // 减少红包
        try {
            redPackBalanceService.subRedPack(uid, amount, RedPackDetailFactory.createGiveOthers(giveRecord, null, RedPackDetailTypeEnum.giveOthers));
        } catch (Exception e) {
            throw new RedPackGiveRecordException(1, "红包创建失败");
        }
        return tips;
    }
}