diff --git a/mallinkService/src/main/java/com/iformall/mapper/WxCUserBasicInfoMapper.java b/mallinkService/src/main/java/com/iformall/mapper/WxCUserBasicInfoMapper.java index e0a840590..7d4910979 100644 --- a/mallinkService/src/main/java/com/iformall/mapper/WxCUserBasicInfoMapper.java +++ b/mallinkService/src/main/java/com/iformall/mapper/WxCUserBasicInfoMapper.java @@ -44,5 +44,11 @@ public interface WxCUserBasicInfoMapper extends CommonMapper selectNotCUserList(); } diff --git a/mallinkService/src/main/java/com/iformall/mapper/WxCreditHistoryMapper.java b/mallinkService/src/main/java/com/iformall/mapper/WxCreditHistoryMapper.java index ab5146988..c1ce58961 100644 --- a/mallinkService/src/main/java/com/iformall/mapper/WxCreditHistoryMapper.java +++ b/mallinkService/src/main/java/com/iformall/mapper/WxCreditHistoryMapper.java @@ -4,6 +4,7 @@ import com.iformall.common.CommonMapper; import com.iformall.domain.po.WxCreditHistory; import com.iformall.domain.vo.WxCreditHistoryNewIdVo; import com.iformall.domain.vo.WxCreditHistoryVo; +import org.apache.ibatis.annotations.Param; import java.util.Date; import java.util.List; @@ -31,4 +32,6 @@ public interface WxCreditHistoryMapper extends CommonMapper findListAfter(Integer creditType, String dateTime); + + List findListByIds(@Param("userIds") List userIds); } diff --git a/mallinkService/src/main/resources/mapper/WxCUserBasicInfoMapper.xml b/mallinkService/src/main/resources/mapper/WxCUserBasicInfoMapper.xml index 8a0a4530e..744248dc7 100644 --- a/mallinkService/src/main/resources/mapper/WxCUserBasicInfoMapper.xml +++ b/mallinkService/src/main/resources/mapper/WxCUserBasicInfoMapper.xml @@ -526,5 +526,8 @@ UPDATE wx_c_user_basic_info set act_record = act_record +1 where id = #{id} + diff --git a/mallinkService/src/main/resources/mapper/WxCreditHistoryMapper.xml b/mallinkService/src/main/resources/mapper/WxCreditHistoryMapper.xml index cf24ed7be..c89e7fd7a 100644 --- a/mallinkService/src/main/resources/mapper/WxCreditHistoryMapper.xml +++ b/mallinkService/src/main/resources/mapper/WxCreditHistoryMapper.xml @@ -253,4 +253,13 @@ select from wx_credit_history where create_date > #{dateTime} and credit_type = #{creditType} + + diff --git a/mallinkSysAdmin/src/main/java/com/iformall/controller/sys/DataInitController.java b/mallinkSysAdmin/src/main/java/com/iformall/controller/sys/DataInitController.java index aa02b91e5..19a70dfa9 100644 --- a/mallinkSysAdmin/src/main/java/com/iformall/controller/sys/DataInitController.java +++ b/mallinkSysAdmin/src/main/java/com/iformall/controller/sys/DataInitController.java @@ -1,5 +1,8 @@ package com.iformall.controller.sys; +import com.alibaba.fastjson.JSON; +import com.google.common.base.Function; +import com.google.common.collect.*; import com.iformall.common.ErrorCode; import com.iformall.common.IdWorker; import com.iformall.common.ResultData; @@ -8,12 +11,12 @@ import com.iformall.domain.po.*; import com.iformall.enums.EnumMsgModel; import com.iformall.enums.EnumScoreType; import com.iformall.enums.EnumUserType; -import com.iformall.exception.MallinkException; import com.iformall.mapper.*; import com.iformall.service.WxCreditHistoryService; import com.iformall.utils.DateUtils; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; +import org.apache.commons.collections.CollectionUtils; +import org.checkerframework.checker.nullness.qual.Nullable; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -22,6 +25,7 @@ import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.math.BigDecimal; import java.util.*; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; /** @@ -100,6 +104,28 @@ public class DataInitController extends BaseController { return new ResultData(); } + + /** + * 修复wx_c_user_basic_info积分数据 + * + * @return + */ + @GetMapping("/fixCredit4CUserBasic") + public ResultData fixCredit4CUserBasic() { + try { + MallUserInfo userInfo = getUser(); + if (userInfo.isFmSuperAdmin()) { + doFixCredit4CUserBasic(); + return new ResultData(); + } + } catch (Exception e) { + log.error("DataInitController::fixCredit4CUserBasic error ", e); + return new ResultData(ErrorCode.MSG_METHOD_REQUEST_ERROR, e.getMessage()); + } + + return new ResultData(); + } + /** * 核销积分数据修复 * @@ -121,6 +147,47 @@ public class DataInitController extends BaseController { return new ResultData(); } + @Transactional(rollbackFor = Exception.class) + public void doFixCredit4CUserBasic() { + log.debug("cUserBasic积分更新, begin --------------》"); + // 1 查找c_user_basic中c_user不存在且积分为null的用户用户 + List userBasicInfoList = cUserBasicInfoMapper.selectNotCUserList(); + List ids = userBasicInfoList.stream().map(WxCUserBasicInfo::getId).collect(Collectors.toList()); + //log.debug("ids {}", JSON.toJSONString(ids)); + + // 2 获取对应用户的积分记录 + List creditList = creditHistoryMapper.findListByIds(ids); + ImmutableListMultimap userCreditHistory = Multimaps.index(creditList, new Function() { + @Nullable + @Override + public Long apply(@Nullable WxCreditHistory input) { + return input.getCUserId(); + } + }); + + //3 将历史记录最好积分重设积分 + AtomicInteger count = new AtomicInteger(0); + userBasicInfoList.forEach(wxCUserBasicInfo -> { + ImmutableList historyList = userCreditHistory.get(wxCUserBasicInfo.getId()); + WxCUserBasicInfo toUpdateUser = new WxCUserBasicInfo(); + toUpdateUser.setId(wxCUserBasicInfo.getId()); + //用户积分不存在,跳过 + if (CollectionUtils.isEmpty(historyList)) { + //log.debug("cUserBasic积分更新,id: {} 积分记录不存在", wxCUserBasicInfo.getId()); + return; + } + //积分记录存在,使用最新的总积分重新赋值 + WxCreditHistory wxCreditHistory = historyList.get(0); + toUpdateUser.setCredit(wxCreditHistory.getCreditAmount()); + cUserBasicInfoMapper.updateByPrimaryKeySelective(toUpdateUser); + log.debug("cUserBasic积分更新,id: {},原积分: {},新积分: {}, 积分时间: {}", wxCUserBasicInfo.getId(), wxCUserBasicInfo.getCredit(), toUpdateUser.getCredit(), DateUtils.format(wxCreditHistory.getCreateDate())); + count.getAndIncrement(); + }); + + log.debug("cUserBasic积分更新,更新用户 {}", count.intValue()); + log.debug("cUserBasic积分更新, end --------------》"); + } + @Transactional(rollbackFor = Exception.class) public void doFixCredit() { // 1 获取积分记录