|
@@ -0,0 +1,186 @@
|
|
|
+package com.citu.module.menduner.reward.service.record;
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
|
|
+import com.citu.framework.common.pojo.PageParam;
|
|
|
+import com.citu.framework.common.pojo.PageResult;
|
|
|
+import com.citu.module.menduner.reward.controller.base.record.EnterpriseAccountRecordPageReqVO;
|
|
|
+import com.citu.module.menduner.reward.dal.dataobject.record.EnterpriseAccountRecordDO;
|
|
|
+import com.citu.module.menduner.reward.dal.mysql.record.EnterpriseAccountRecordMapper;
|
|
|
+import com.citu.module.menduner.reward.enums.MathOperationEnum;
|
|
|
+import com.citu.module.menduner.reward.enums.record.AccountRecordTypeEnum;
|
|
|
+import com.citu.module.menduner.reward.enums.record.BalanceBizTypeEnum;
|
|
|
+import com.citu.module.menduner.reward.enums.record.PointBizTypeEnum;
|
|
|
+import com.citu.module.menduner.system.api.account.AccountRespDTO;
|
|
|
+import com.citu.module.menduner.system.api.account.EnterpriseAccountApi;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import static com.citu.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
+import static com.citu.module.menduner.reward.enums.ErrorCodeConstants.USER_BALANCE_NOT_ENOUGH;
|
|
|
+import static com.citu.module.menduner.reward.enums.ErrorCodeConstants.USER_POINT_NOT_ENOUGH;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 企业账户变动 Service 实现类
|
|
|
+ *
|
|
|
+ * @author Rayson
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+@Validated
|
|
|
+public class EnterpriseAccountRecordServiceImpl implements EnterpriseAccountRecordService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private EnterpriseAccountRecordMapper mapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private EnterpriseAccountApi enterpriseAccountApi;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PageResult<EnterpriseAccountRecordDO> page(EnterpriseAccountRecordPageReqVO pageReqVO) {
|
|
|
+ return mapper.selectPage(pageReqVO);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @DSTransactional
|
|
|
+ public void createPointRecord(Long enterpriseId,
|
|
|
+ Long userId,
|
|
|
+ String title,
|
|
|
+ MathOperationEnum operation,
|
|
|
+ Integer point,
|
|
|
+ PointBizTypeEnum bizType,
|
|
|
+ String bizId) {
|
|
|
+ if (0 == point) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 1. 校验用户积分余额
|
|
|
+ AccountRespDTO userAccountResp = enterpriseAccountApi
|
|
|
+ .createEnterpriseAccountIfAbsent(enterpriseId, userId).getCheckedData();
|
|
|
+ Integer userPoint = ObjectUtil.defaultIfNull(userAccountResp.getPoint(), 0);
|
|
|
+ int totalPoint = userPoint + point; // 用户变动后的积分
|
|
|
+ if (totalPoint < 0) {
|
|
|
+ throw exception(USER_POINT_NOT_ENOUGH);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (MathOperationEnum.SUBTRACT.equals(operation)) {
|
|
|
+ if (userPoint - point < 0) {
|
|
|
+ throw exception(USER_POINT_NOT_ENOUGH);
|
|
|
+ }
|
|
|
+ totalPoint = userPoint - point;
|
|
|
+ // 减法
|
|
|
+ point = -Math.abs(point);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 更新用户积分
|
|
|
+ boolean success = enterpriseAccountApi.updatePoint(enterpriseId, userId, point).getCheckedData();
|
|
|
+ if (!success) {
|
|
|
+ throw exception(USER_POINT_NOT_ENOUGH);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 增加用户账户变动
|
|
|
+ EnterpriseAccountRecordDO record = new EnterpriseAccountRecordDO()
|
|
|
+ .setEnterpriseId(enterpriseId)
|
|
|
+ .setUserId(userId)
|
|
|
+ .setType(AccountRecordTypeEnum.POINT.getType())
|
|
|
+ .setBizId(bizId)
|
|
|
+ .setBizType(bizType.getType())
|
|
|
+ .setTitle(bizType.getName())
|
|
|
+ .setOperation(operation.getOperator())
|
|
|
+ .setDescription(StrUtil.format(bizType.getDescription(), point))
|
|
|
+ .setPoint(point).setTotalPoint(totalPoint);
|
|
|
+
|
|
|
+ if (PointBizTypeEnum.EVENT.equals(bizType)) {
|
|
|
+ if (MathOperationEnum.SUBTRACT.equals(operation)) {
|
|
|
+ record.setDescription(StrUtil.format(bizType.getDescription(), title, point));
|
|
|
+ } else {
|
|
|
+ record.setDescription(StrUtil.format(bizType.getDescription(), title, operation.getOperator(), point));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ mapper.insert(record);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @DSTransactional
|
|
|
+ public void createBalanceRecord(Long enterpriseId,
|
|
|
+ Long userId,
|
|
|
+ String title,
|
|
|
+ MathOperationEnum operation,
|
|
|
+ BigDecimal balance,
|
|
|
+ BalanceBizTypeEnum bizType,
|
|
|
+ String bizId) {
|
|
|
+ if (0 == balance.compareTo(BigDecimal.ZERO)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1. 校验用户余额
|
|
|
+ AccountRespDTO userAccountResp = enterpriseAccountApi.
|
|
|
+ createEnterpriseAccountIfAbsent(enterpriseId, userId).getCheckedData();
|
|
|
+
|
|
|
+ BigDecimal userBalance = ObjectUtil.defaultIfNull(userAccountResp.getBalance(), BigDecimal.ZERO);
|
|
|
+ BigDecimal totalBalance = userBalance.add(balance);
|
|
|
+
|
|
|
+ if (totalBalance.compareTo(BigDecimal.ZERO) < 0) {
|
|
|
+ throw exception(USER_BALANCE_NOT_ENOUGH);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (MathOperationEnum.SUBTRACT.equals(operation)) {
|
|
|
+ if (userBalance.subtract(balance).compareTo(BigDecimal.ZERO) < 0) {
|
|
|
+ throw exception(USER_BALANCE_NOT_ENOUGH);
|
|
|
+ }
|
|
|
+ totalBalance = userBalance.subtract(balance);
|
|
|
+ balance = balance.negate();
|
|
|
+ // 标记操作为减少
|
|
|
+ operation = MathOperationEnum.SUBTRACT;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 更新用户余额
|
|
|
+ boolean success = enterpriseAccountApi.updateBalance(enterpriseId, userId, balance).getCheckedData();
|
|
|
+ if (!success) {
|
|
|
+ throw exception(USER_BALANCE_NOT_ENOUGH);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 增加用户账户变动
|
|
|
+ EnterpriseAccountRecordDO record = new EnterpriseAccountRecordDO()
|
|
|
+ .setEnterpriseId(enterpriseId)
|
|
|
+ .setUserId(userId)
|
|
|
+ .setType(AccountRecordTypeEnum.BALANCE.getType())
|
|
|
+ .setBizId(bizId)
|
|
|
+ .setBizType(bizType.getType())
|
|
|
+ .setTitle(bizType.getName())
|
|
|
+ .setOperation(operation.getOperator())
|
|
|
+ .setDescription(StrUtil.format(bizType.getDescription(), balance))
|
|
|
+ .setBalance(balance)
|
|
|
+ .setTotalBalance(totalBalance);
|
|
|
+
|
|
|
+ mapper.insert(record);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Integer getPointCount(Long enterpriseId,
|
|
|
+ Long userId,
|
|
|
+ String url,
|
|
|
+ LocalDateTime startDate,
|
|
|
+ LocalDateTime endDate) {
|
|
|
+ List<EnterpriseAccountRecordDO> list = mapper.getEnterprisePointList(enterpriseId, userId, startDate, endDate);
|
|
|
+ if (ObjectUtil.isEmpty(list)) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return list.stream().mapToInt(EnterpriseAccountRecordDO::getPoint).sum();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PageResult<EnterpriseAccountRecordDO> page(Long enterpriseId,
|
|
|
+ Long userId,
|
|
|
+ String type,
|
|
|
+ PageParam pageVO) {
|
|
|
+ return mapper.selectPageByUserId(enterpriseId, userId, type, pageVO);
|
|
|
+ }
|
|
|
+}
|