package com.yeshi.makemoney.app.service.impl.goldcorn;
|
|
import com.ks.lib.common.exception.ParamsException;
|
import com.yeshi.makemoney.app.dto.mq.GoldCornSettleMQMsg;
|
import com.yeshi.makemoney.app.entity.goldcorn.GoldCornConsumeRecord;
|
import com.yeshi.makemoney.app.entity.goldcorn.GoldCornConsumeType;
|
import com.yeshi.makemoney.app.entity.money.UserMoneyRecord;
|
import com.yeshi.makemoney.app.entity.user.UserInfo;
|
import com.yeshi.makemoney.app.exception.goldcorn.GoldCornConsumeRecordException;
|
import com.yeshi.makemoney.app.exception.goldcorn.GoldCornMoneyExchangeRateRecordException;
|
import com.yeshi.makemoney.app.exception.money.UserMoneyRecordException;
|
import com.yeshi.makemoney.app.exception.user.UserInfoException;
|
import com.yeshi.makemoney.app.service.inter.goldcorn.GoldCornConsumeRecordService;
|
import com.yeshi.makemoney.app.service.inter.goldcorn.GoldCornGetRecordService;
|
import com.yeshi.makemoney.app.service.inter.goldcorn.GoldCornMoneyExchangeRateRecordService;
|
import com.yeshi.makemoney.app.service.inter.goldcorn.GoldCornSettleService;
|
import com.yeshi.makemoney.app.service.inter.money.UserMoneyService;
|
import com.yeshi.makemoney.app.service.inter.user.UserInfoService;
|
import com.yeshi.makemoney.app.utils.factory.UserMoneyRecordFactory;
|
import com.yeshi.makemoney.app.utils.mq.CMQManager;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import javax.annotation.Resource;
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
import java.util.Date;
|
import java.util.List;
|
|
/**
|
* @author hxh
|
* @title: GoldCornSettleServiceImpl
|
* @description: TODO
|
* @date 2022/4/7 11:47
|
*/
|
@Service
|
public class GoldCornSettleServiceImpl implements GoldCornSettleService {
|
|
@Resource
|
private GoldCornGetRecordService goldCornGetRecordService;
|
|
@Resource
|
private GoldCornConsumeRecordService goldCornConsumeRecordService;
|
|
@Resource
|
private GoldCornMoneyExchangeRateRecordService goldCornMoneyExchangeRateRecordService;
|
|
@Resource
|
private UserInfoService userInfoService;
|
|
@Resource
|
private UserMoneyService userMoneyService;
|
|
@Override
|
public void startSettle(String day) {
|
long count = goldCornGetRecordService.countUidsByDay(day);
|
int pageSize = 1000;
|
int page = (int) (count % pageSize == 0 ? count / pageSize : count / pageSize + 1);
|
for (int i = 0; i < page; i++) {
|
List<Long> list = goldCornGetRecordService.getUidsByDay(day, i + 1, pageSize);
|
for (Long uid : list) {
|
//加入到CMQ
|
CMQManager.getInstance().addGoldCornSettleMsg(new GoldCornSettleMQMsg(uid, day, System.currentTimeMillis()));
|
}
|
}
|
}
|
|
@Transactional(rollbackFor = Exception.class)
|
@Override
|
public void settle(Long uid, String day, BigDecimal rate) throws GoldCornConsumeRecordException, UserMoneyRecordException {
|
long cornNum = goldCornGetRecordService.getGoldCornByDay(uid, day);
|
//金币数量
|
GoldCornConsumeRecord record = new GoldCornConsumeRecord();
|
record.setUid(uid);
|
record.setCornNum((int) cornNum);
|
record.setMoney(rate.multiply(new BigDecimal(record.getCornNum()).setScale(2, RoundingMode.FLOOR)));
|
record.setType(GoldCornConsumeType.changeMoney);
|
record.setId(uid + "-" + record.getType().name() + "-" + day);
|
try {
|
UserMoneyRecord moneyRecord = UserMoneyRecordFactory.createGoldCornExchange(record);
|
userMoneyService.addUserMoney(moneyRecord);
|
} catch (ParamsException e) {
|
throw new UserMoneyRecordException(e.getCode(), e.getMsg());
|
}
|
goldCornConsumeRecordService.add(record);
|
}
|
|
@Override
|
public void settle(Long uid, String day) throws UserInfoException, GoldCornMoneyExchangeRateRecordException, UserMoneyRecordException, GoldCornConsumeRecordException {
|
UserInfo user = userInfoService.get(uid);
|
if (user == null) {
|
throw new UserInfoException(UserInfoException.CODE_NOT_EXIST, "用户不存在");
|
}
|
|
BigDecimal rate = null;
|
try {
|
rate = goldCornMoneyExchangeRateRecordService.getRate(day, user.getSystem(), new Date());
|
} catch (ParamsException e) {
|
throw new GoldCornMoneyExchangeRateRecordException(GoldCornMoneyExchangeRateRecordException.CODE_PARAMS_NOT_ENOUGH, e.getMsg());
|
}
|
|
if (rate == null) {
|
throw new GoldCornMoneyExchangeRateRecordException(GoldCornMoneyExchangeRateRecordException.CODE_NOT_EXIST, "未获取到汇率");
|
}
|
|
settle(uid, day, rate);
|
}
|
}
|