WalletServiceImpl.java 8.06 KB
Newer Older
licc's avatar
licc committed
1 2
package cn.wisenergy.service.app.impl;

3 4
import cn.wisenergy.common.utils.DateUtil;
import cn.wisenergy.common.utils.R;
licc's avatar
licc committed
5
import cn.wisenergy.mapper.AccountMapper;
6
import cn.wisenergy.mapper.LastAccountMapper;
7 8
import cn.wisenergy.mapper.TradeRecordMapper;
import cn.wisenergy.model.app.AccountInfo;
9
import cn.wisenergy.model.app.LastMonthAccount;
10 11 12
import cn.wisenergy.model.app.TradeRecord;
import cn.wisenergy.model.enums.TradeRecordEnum;
import cn.wisenergy.model.vo.*;
licc's avatar
licc committed
13 14
import cn.wisenergy.service.app.WalletService;
import lombok.extern.slf4j.Slf4j;
15
import org.apache.commons.lang3.StringUtils;
licc's avatar
licc committed
16 17
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
18 19 20 21
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
22
import java.util.Calendar;
23 24
import java.util.Date;
import java.util.List;
licc's avatar
licc committed
25 26 27 28 29 30 31

/**
 * @author 86187
 */
@Service
@Slf4j
public class WalletServiceImpl implements WalletService {
32 33
    private static final String PATTERN = "yyyy-MM";

licc's avatar
licc committed
34 35 36
    @Autowired
    private AccountMapper accountMapper;

37 38 39
    @Autowired
    private TradeRecordMapper tradeRecordMapper;

40 41 42
    @Autowired
    private LastAccountMapper lastAccountMapper;

licc's avatar
licc committed
43 44

    @Override
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
    public R<MoneyPackageVo> getMoneyPackage(String userId) {
        log.info("shop-mall[]WalletServiceImpl[]getMoneyPackage[]input.param.userId:" + userId);
        if (StringUtils.isBlank(userId)) {
            return R.error("入参为空!");
        }

        Date date = new Date();
        String yearMonth = DateUtil.convertDateToStr(date, PATTERN);

        //1、获取用户账户信息
        AccountInfo accountInfo = accountMapper.getByUserIdAndTime(userId, yearMonth);
        if (null == accountInfo) {
            return R.error("用户账户信息不存在!");
        }
        MoneyPackageVo moneyPackageVo = new MoneyPackageVo();
        moneyPackageVo.setUserId(accountInfo.getUserId());
        moneyPackageVo.setMoneyIncome(accountInfo.getEarningsMonth());
        moneyPackageVo.setTotalIncome(accountInfo.getEarningsTotal());
        moneyPackageVo.setCurrentMoneyCan(accountInfo.getExtractMoney());

        return R.ok(moneyPackageVo);
    }

    @Override
    public R<WithdrawalAmountVo> getWithdrawalAmount(String userId) {
        log.info("shop-mall[]WalletServiceImpl[]getWithdrawalAmount[]input.param.userId:" + userId);
        if (StringUtils.isBlank(userId)) {
            return R.error("入参为空!");
        }

        Date date = new Date();
        String yearMonth = DateUtil.convertDateToStr(date, PATTERN);

78 79
        WithdrawalAmountVo withdrawalAmountVo = new WithdrawalAmountVo();
        withdrawalAmountVo.setUserId(userId);
80
        withdrawalAmountVo.setWithdrawRule("这是一条规则!");
81

82 83 84
        //1、获取用户账户信息
        AccountInfo accountInfo = accountMapper.getByUserIdAndTime(userId, yearMonth);
        if (null == accountInfo) {
85 86 87
            withdrawalAmountVo.setCurrentMoneyCan(new BigDecimal(0));
        } else {
            withdrawalAmountVo.setCurrentMoneyCan(accountInfo.getExtractMoney());
88 89
        }

90 91 92 93 94 95 96 97 98 99 100 101
        //2、 获取上月未提现
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.MONTH, -1);
        Date lastDate = cal.getTime();
        String lastMonthTime = DateUtil.convertDateToStr(lastDate, PATTERN);
        LastMonthAccount lastMonthAccount = lastAccountMapper.getByUserIdAndTime(userId, lastMonthTime);
        if (null == lastMonthAccount) {
            withdrawalAmountVo.setLastMoneyNot(new BigDecimal(0));
        } else {
            withdrawalAmountVo.setLastMoneyNot(lastMonthAccount.getExtractMoney());
        }
102 103 104 105 106 107 108 109 110 111
        return R.ok(withdrawalAmountVo);
    }

    @Override
    public R<List<AccumulatedIncomeVo>> showIncomeRecord(String userId) {
        log.info("shop-mall[]WalletServiceImpl[]showIncomeRecord[]input.param.userId:" + userId);
        if (StringUtils.isBlank(userId)) {
            return R.error("入参为空!");
        }

licc's avatar
licc committed
112
        //获取包括本月在内的六个月的收益
113 114
        List<AccumulatedIncomeVo> list = tradeRecordMapper.getSixMonthIncome(userId);
        return R.ok(list);
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    }

    @Override
    public R<MoneyPackageDetailVo> queryIncomeDetail(String userId) {
        log.info("shop-mall[]WalletServiceImpl[]queryIncomeDetail[]input.param.userId:" + userId);
        if (StringUtils.isBlank(userId)) {
            return R.error("入参为空!");
        }

        Date date = new Date();
        String yearMonth = DateUtil.convertDateToStr(date, PATTERN);
        //获取当月账户信息
        AccountInfo accountInfo = accountMapper.getByUserIdAndTime(userId, yearMonth);
        if (null == accountInfo) {
            return R.error("账户信息为空!");
        }

        MoneyPackageDetailVo detailVo = new MoneyPackageDetailVo();
        detailVo.setUserId(userId);
licc's avatar
licc committed
134 135 136 137 138 139 140 141 142 143 144

        //获取培育奖信息
        BigDecimal earningsMonth = new BigDecimal(0);
        TradeRecord trade = tradeRecordMapper.getByUserIdAndTypeAndStatus(userId, TradeRecordEnum.CULTIVATING_PRIZE.getCode(),
                new Date(), 0);
        if (null != trade) {
            earningsMonth = accountInfo.getEarningsMonth().add(trade.getMoney());
        } else {
            earningsMonth = accountInfo.getEarningsMonth();
        }
        detailVo.setTotalIncome(earningsMonth);
145

licc's avatar
licc committed
146 147
        //获取本月交易记录
        List<TradeRecord> list = getTradeRecordList(userId);
148 149 150 151 152 153 154 155 156 157 158 159 160 161
        List<IncomeDetailVo> incomeDetailVos = new ArrayList<>();
        for (TradeRecord tradeRecord : list) {
            IncomeDetailVo incomeDetailVo = new IncomeDetailVo();
            String typeName = TradeRecordEnum.getByCode(tradeRecord.getTradeType());

            incomeDetailVo.setTypeName(typeName);
            incomeDetailVo.setMoney(tradeRecord.getMoney());
            incomeDetailVo.setIncomeTime(tradeRecord.getCreateTime());
            incomeDetailVos.add(incomeDetailVo);
        }

        detailVo.setList(incomeDetailVos);

        return R.ok(detailVo);
licc's avatar
licc committed
162
    }
163 164 165 166 167 168 169 170 171 172 173 174

    @Override
    public R<List<WithdrawalRecordVo>> getWithdrawalRecord(String userId, String yearMonth) {
        log.info("shop-mall[]WalletServiceImpl[]queryIncomeDetail[]input.param.yearMonth:" + yearMonth);
        if (StringUtils.isBlank(yearMonth)) {
            return R.error("入参为空!");
        }

        Date date = DateUtil.convertStrToDate(yearMonth, PATTERN);
        List<WithdrawalRecordVo> list = tradeRecordMapper.getWithdrawalRecord(userId, date);
        return R.ok(list);
    }
licc's avatar
licc committed
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

    private List<TradeRecord> getTradeRecordList(String userId) {
        List<TradeRecord> list = new ArrayList<>();
        Date date = new Date();
        //获取返佣、月度肥料、培育奖、最大进步奖、运营中心补贴类型交易记录
        //获取返佣本月交易记录
        List<TradeRecord> orderRebate = tradeRecordMapper.getRebateList(userId, date);
        if (!CollectionUtils.isEmpty(orderRebate)) {
            list.addAll(orderRebate);
        }

        //获取月度肥料本月交易记录
        TradeRecord monthFertilizer = tradeRecordMapper.getByUserIdAndType(userId,
                TradeRecordEnum.MONTHLY_FERTILIZER.getCode(), date);
        if (null != monthFertilizer) {
            list.add(monthFertilizer);
        }

        //获取培育奖本月交易记录
        TradeRecord trainRecord = tradeRecordMapper.getByUserIdAndType(userId,
                TradeRecordEnum.CULTIVATING_PRIZE.getCode(), date);
        if (null != trainRecord) {
            list.add(trainRecord);
        }

        //获取最大进步奖本月交易记录
        TradeRecord prizeRecord = tradeRecordMapper.getByUserIdAndType(userId,
                TradeRecordEnum.PROGRESS_PRIZE.getCode(), date);
        if (null != prizeRecord) {
            list.add(prizeRecord);
        }

        //获取运营中心补贴本月交易记录
        TradeRecord centerRecord = tradeRecordMapper.getByUserIdAndType(userId,
                TradeRecordEnum.RUN_CENTER_SUBSIDY.getCode(), date);
        if (null != centerRecord) {
            list.add(centerRecord);
        }
        return list;
    }
licc's avatar
licc committed
215
}