admin
2021-01-18 d6df4ca797ee1c6ce8fa78768f5425f187734bd9
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
package com.ks.lucky.service.impl;
 
import com.google.gson.Gson;
import com.ks.daylucky.exception.LuckySponsorMoneyRecordException;
import com.ks.lib.common.exception.ParamsException;
import com.ks.lucky.exception.LuckySponsorException;
import com.ks.lucky.mapper.LuckySponsorsMapper;
import com.ks.lucky.pojo.DO.LuckySponsorMoneyRecord;
import com.ks.lucky.pojo.DO.LuckySponsors;
import com.ks.lucky.service.LuckySponsorMoneyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
 
@Service
public class LuckySponsorMoneyServiceImpl implements LuckySponsorMoneyService {
 
    @Resource
    private LuckySponsorsMapper luckySponsorsMapper;
 
    @Resource
    private LuckySponsorMoneyRecordManager luckySponsorMoneyRecordManager;
 
    /**
     * 设置余额
     *
     * @param sponsorId
     * @param balance
     */
    private void setMoney(Long sponsorId, BigDecimal balance) {
        //扣资金
        LuckySponsors update = new LuckySponsors();
        update.setId(sponsorId);
        update.setBalance(balance);
        update.setUpdateTime(new Date());
        luckySponsorsMapper.updateByPrimaryKeySelective(update);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void publishActivity(Long sponsorId, Long activityId, BigDecimal money) throws LuckySponsorException, LuckySponsorMoneyRecordException {
        LuckySponsors sponsor = luckySponsorsMapper.selectByPrimaryKeyForUpdate(sponsorId);
        if (sponsor == null) {
            throw new LuckySponsorException(LuckySponsorException.CODE_NOT_EXIST, "赞助商不存在");
        }
 
        if (sponsor.getBalance().compareTo(money) < 0) {
            throw new LuckySponsorException(1, "赞助商余额不足");
        }
 
        setMoney(sponsorId, sponsor.getBalance().subtract(money));
        //加记录
        LuckySponsorMoneyRecord record = new LuckySponsorMoneyRecord();
        record.setActivityId(activityId);
        record.setMoney(new BigDecimal(0).subtract(money));
        record.setType(LuckySponsorMoneyRecord.RecordType.publishActivity);
        record.setSponsorId(sponsorId);
        record.setRemarks("发布活动");
        try {
            luckySponsorMoneyRecordManager.addRecord(record);
        } catch (ParamsException e) {
            throw new LuckySponsorException(ParamsException.CODE_PARAMS_NOT_ENOUGH, e.getMsg());
        }
 
    }
 
 
    @Override
    public void drawnNotReceive(Long sponsorId, Long activityId, BigDecimal money, Long drawnId) throws LuckySponsorException, LuckySponsorMoneyRecordException, ParamsException {
        LuckySponsors sponsor = luckySponsorsMapper.selectByPrimaryKeyForUpdate(sponsorId);
        if (sponsor == null) {
            throw new LuckySponsorException(LuckySponsorException.CODE_NOT_EXIST, "赞助商不存在");
        }
 
        setMoney(sponsorId, sponsor.getBalance().add(money));
        //加记录
        LuckySponsorMoneyRecord record = new LuckySponsorMoneyRecord();
        record.setActivityId(activityId);
        record.setMoney(money);
        record.setType(LuckySponsorMoneyRecord.RecordType.drawnNotReceive);
        record.setSponsorId(sponsorId);
        record.setRemarks("中奖未领取");
        record.setExtraParams(new Gson().toJson(new LuckySponsorMoneyRecord.DrawnInfo(drawnId)));
        try {
            luckySponsorMoneyRecordManager.addRecord(record);
        } catch (
                ParamsException e) {
            throw new LuckySponsorException(ParamsException.CODE_PARAMS_NOT_ENOUGH, e.getMsg());
        }
    }
}