KKSU
2024-04-26 1da6dad3fd2ac6d51125aa1f2d459ae1f146f8ff
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
package cc.mrbird.febs.dapp.service.impl;
 
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.common.enumerates.DataDicEnum;
import cc.mrbird.febs.common.enumerates.FlowTypeEnum;
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.common.utils.RedisUtils;
import cc.mrbird.febs.dapp.dto.TransferDto;
import cc.mrbird.febs.dapp.dto.ZhiyaAddDto;
import cc.mrbird.febs.dapp.entity.*;
import cc.mrbird.febs.dapp.mapper.*;
import cc.mrbird.febs.dapp.service.AsyncCjService;
import cc.mrbird.febs.dapp.service.DappWalletService;
import cc.mrbird.febs.dapp.service.IAdminBannerService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.List;
 
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
public class AdminBannerServiceImpl extends ServiceImpl<PlatformBannerMapper, PlatformBanner> implements IAdminBannerService {
 
    private final PlatformBannerMapper platformBannerMapper;
    private final DappCoinPriceMapper dappCoinPriceMapper;
    private final DappHdRecordMapper dappHdRecordMapper;
    private final DataDictionaryCustomMapper dataDictionaryCustomMapper;
    private final AsyncCjService asyncCjService;
    private final DappMemberDao dappMemberDao;
    private final DappAchieveMapper dappAchieveMapper;
    private final DappWalletService dappWalletService;
    private final DappFundFlowDao dappFundFlowDao;
    private final RedisUtils redisUtils;
 
    @Override
    public IPage<PlatformBanner> findPlatformBannerInPage(PlatformBanner platformBannerEntity,
                                                          QueryRequest request) {
        Page<PlatformBanner> page = new Page<>(request.getPageNum(), request.getPageSize());
        IPage<PlatformBanner> platformBannerEntitys = platformBannerMapper.findPlatformBannerInPage(page, platformBannerEntity);
        return platformBannerEntitys;
    }
 
    @Override
    public PlatformBanner selectPlatformBannerById(long id) {
        PlatformBanner platformBannerEntity = platformBannerMapper.selectById(id);
        return platformBannerEntity;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public FebsResponse platformBannerConfirm(@Valid PlatformBanner platformBannerEntity) {
        platformBannerMapper.updateById(platformBannerEntity);
        return new FebsResponse().success();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public FebsResponse platformBannerDelete(@NotNull(message = "{required}") Long id) {
        platformBannerMapper.deleteById(id);
        return new FebsResponse().success();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void platformBannerAdd(@Valid PlatformBanner platformBannerEntity) {
        PlatformBanner platformBannerEntityAdd = new PlatformBanner();
        platformBannerEntityAdd.setImageUrl(platformBannerEntity.getImageUrl());
        platformBannerEntityAdd.setIsInside(platformBannerEntity.getIsInside());
        platformBannerEntityAdd.setIsJump(platformBannerEntity.getIsJump());
        platformBannerEntityAdd.setIsTop(platformBannerEntity.getIsTop());
        platformBannerEntityAdd.setJumpUrl(platformBannerEntity.getJumpUrl());
        platformBannerEntityAdd.setName(platformBannerEntity.getName());
        platformBannerEntityAdd.setShowPort(platformBannerEntity.getShowPort());
        platformBannerEntityAdd.setSort(platformBannerEntity.getSort());
        platformBannerMapper.insert(platformBannerEntityAdd);
 
    }
 
    @Override
    public IPage<DappCoinPrice> findCoinPriceInPage(DappCoinPrice dappCoinPrice, QueryRequest request) {
        Page<DappCoinPrice> page = new Page<>(request.getPageNum(), request.getPageSize());
        IPage<DappCoinPrice> platformBannerEntitys = dappHdRecordMapper.findCoinPriceInPage(page, dappCoinPrice);
        return platformBannerEntitys;
    }
 
    @Override
    public FebsResponse coinPriceAdd(String price) {
        BigDecimal bigDecimal = new BigDecimal(price).setScale(8, BigDecimal.ROUND_DOWN);
        DappCoinPrice dappCoinPrice = new DappCoinPrice();
        dappCoinPrice.setPrice(bigDecimal);
        dappCoinPriceMapper.insert(dappCoinPrice);
        DataDictionaryCustom dataDictionaryCustom = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                DataDicEnum.GFA_PRICE.getValue(),
                DataDicEnum.GFA_PRICE.getValue()
        );
 
        dataDictionaryCustom.setValue(bigDecimal.toString());
        dataDictionaryCustomMapper.updateById(dataDictionaryCustom);
 
        asyncCjService.redisCacheUpdate(DataDicEnum.GFA_PRICE.getValue(),bigDecimal.toString(),0);
        return new FebsResponse().success();
    }
 
    @Override
    public IPage<DappHdRecord> findHdRecordInPage(DappHdRecord dappHdRecord, QueryRequest request) {
        String startTime = dappHdRecord.getStartTime();
        String endTime = dappHdRecord.getEndTime();
        if(StrUtil.isNotBlank(startTime) && StrUtil.isNotBlank(endTime)){
            DateTime dateStartTime= DateUtil.parseDate(startTime);
            DateTime dateEndTime = DateUtil.parseDate(endTime);
            int compare = DateUtil.compare(dateStartTime, dateEndTime);
            if(compare >= 0){
                throw new FebsException("请输入正确的开始时间和结束时间");
            }
        }
        Page<DappHdRecord> page = new Page<>(request.getPageNum(), request.getPageSize());
        IPage<DappHdRecord> dappHdRecordIPage = dappHdRecordMapper.findHdRecordInPage(page, dappHdRecord);
        return dappHdRecordIPage;
    }
 
    @Override
    public IPage<DappMemberEntity> inviteList(DappMemberEntity dappMemberEntity, QueryRequest request) {
        Page<DappMemberEntity> page = new Page<>(request.getPageNum(), request.getPageSize());
        IPage<DappMemberEntity> dappMemberEntityIPage = dappMemberDao.findInviteListInPage(page, dappMemberEntity);
        List<DappMemberEntity> records = dappMemberEntityIPage.getRecords();
        if(CollUtil.isNotEmpty(records)){
            for(DappMemberEntity memberEntity : records){
                DappMemberEntity refererMember = dappMemberDao.selectMemberInfoByInviteId(memberEntity.getRefererId());
                if(ObjectUtil.isNotEmpty(refererMember)){
                    memberEntity.setRefererAddress(refererMember.getAddress());
                }
                QueryWrapper<DappAchieve> objectQueryWrapper = new QueryWrapper<>();
                objectQueryWrapper.eq("member_id",memberEntity.getId());
                objectQueryWrapper.eq("state",1);
                List<DappAchieve> dappAchieves = dappAchieveMapper.selectList(objectQueryWrapper);
                if(CollUtil.isNotEmpty(dappAchieves)){
                    BigDecimal teamAchieveMemberSum = dappAchieves.stream().map(item-> item.getAmount().multiply(item.getPrice())).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2,BigDecimal.ROUND_DOWN);
                    memberEntity.setZhiYaAmount(teamAchieveMemberSum);
                }
            }
        }
        return dappMemberEntityIPage;
    }
 
    @Override
    public FebsResponse zhiyaAdd(ZhiyaAddDto zhiyaAddDto) {
        String address = zhiyaAddDto.getAddress();
        String hash = zhiyaAddDto.getZyHash();
        BigDecimal amount = new BigDecimal(zhiyaAddDto.getAmount()).setScale(2,BigDecimal.ROUND_DOWN);
 
        DappMemberEntity dappMemberEntity = dappMemberDao.selectByAddress(address, null);
        if(ObjectUtil.isEmpty(dappMemberEntity)){
            throw new FebsException("地址不存在");
        }
        if(BigDecimal.ZERO.compareTo(amount) >= 0){
            throw new FebsException("金额不正确");
        }
 
        QueryWrapper<DappFundFlowEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("from_hash",hash);
        List<DappFundFlowEntity> dappFundFlowEntities = dappFundFlowDao.selectList(queryWrapper);
        if(CollUtil.isNotEmpty(dappFundFlowEntities)){
            throw new FebsException("HASH已经产生了质押记录");
        }
 
        DappFundFlowEntity fundFlow = new DappFundFlowEntity(
                dappMemberEntity.getId(),
                amount.negate(),
                FlowTypeEnum.ZHI_YA.getValue(),
                DappFundFlowEntity.WITHDRAW_STATUS_ING,
                BigDecimal.ZERO,
                null);
        dappFundFlowDao.insert(fundFlow);
 
        fundFlow.setFromHash(hash);
        fundFlow.setStatus(DappFundFlowEntity.WITHDRAW_STATUS_AGREE);
        fundFlow.setContent(FlowTypeEnum.ZHI_YA.getDescrition());
        dappFundFlowDao.updateById(fundFlow);
 
        //用户质押代币数量
        Long memberId = fundFlow.getMemberId();
        //新增一条用户质押记录
        DappAchieve dappAchieve = new DappAchieve();
        dappAchieve.setMemberId(memberId);
        dappAchieve.setAmount(amount);
        BigDecimal achieveReleasePercent = new BigDecimal("0.01").multiply(
                new BigDecimal(redisUtils.getString(DataDicEnum.GFA_ACHIEVE_RELEASE.getValue())).setScale(2,BigDecimal.ROUND_DOWN)
        );
        dappAchieve.setAmountDay(achieveReleasePercent.multiply(amount).setScale(2,BigDecimal.ROUND_DOWN));
        BigDecimal achieveOut = new BigDecimal(redisUtils.getString(DataDicEnum.GFA_ACHIEVE_OUT.getValue())).setScale(2,BigDecimal.ROUND_DOWN);
        dappAchieve.setAmountMax(achieveOut.multiply(amount).setScale(2,BigDecimal.ROUND_DOWN));
        dappAchieve.setState(DappAchieve.STATUS_ING);
        dappAchieve.setAmountDone(BigDecimal.ZERO);
        dappAchieveMapper.insert(dappAchieve);
        asyncCjService.insertTeamPerk(fundFlow.getId(), dappAchieve.getId());
        return new FebsResponse().success();
    }
}