多店铺需求

This commit is contained in:
Tankaikai 2025-04-09 15:32:25 +08:00
parent 3a5f65e8ec
commit ca8d49bac7
3 changed files with 32 additions and 74 deletions

View File

@ -125,17 +125,8 @@ public class ShopUser implements Serializable {
@Column(onInsertValue = "now()", onUpdateValue = "now()")
private LocalDateTime updateTime;
/**
* 是否已经合并数据到主店 1- 0- 默认0
*/
private Integer isMergedToHead;
/**
* 已经合并过来的用户信息jsonArray格式,[{"id":1,"shopId":2,...},{"id":1,"shopId":2,...}]
*/
private String mergedUsers;
/**
* 适用门店id集合逗号分隔例如0,1,2,3,...查询的时候 all_shop_ids like '%,1,%';
*/
private String allShopIds;
}

View File

@ -1,23 +1,20 @@
package com.czg.service.account.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import com.czg.account.entity.ShopUser;
import com.czg.account.service.ShopUserSyncService;
import com.czg.enums.YesNoEnum;
import com.czg.service.account.mapper.ShopUserMapper;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
@ -46,11 +43,27 @@ public class ShopUserSyncServiceImpl implements ShopUserSyncService {
@Override
public synchronized void syncMergeShopUser(Long headShopId, Long branchShopId) {
// 合并前置逻辑查询主店分店手机号码不为空的会员信息分店和主店手机号码进行匹配
// 合并逻辑1如果手机号码一致则把分店会员信息中的 积分余额消费金额消费次数合并到主店会员信息中把分店这些信息清零is_merged_to_head的意思是是否已经合并数据到主店 set is_merged_to_head = 1
// 合并逻辑2分店手机号码在主店中不存在则在主店创建新的会员把分店分店会员信息中的 积分余额消费金额消费次数合并到主店会员信息中把分店这些信息清零set is_merged_to_head = 1
// 合并逻辑3主店没有分店有把分店会员信息copy到主店更改shop_id,all_shop_ids,把分店这些信息清零, 把分店会员信息jsonArray格式化存储至merged_users字段中便于后续查询合并前会员信息新增字段适用门店id用英文逗号隔开0,1,2,3, 查询时用适用门店id like %,1,% and is_merged_to_head = 0
// 合并逻辑0分店没有绑定手机号的会员把shop_id改为主店shop_id把分店会员信息jsonArray格式化存储至主店用户信息的merged_users字段中
// 合并逻辑1主店分店一致如果手机号码一致则把分店会员信息中的 积分余额消费金额消费次数合并到主店会员信息中把分店会员信息jsonArray格式化存储至主店用户信息的merged_users字段中再把分店用户信息删除
// 合并逻辑2主店有分店没有不用采取任何操作使用主店的会员信息
// 合并逻辑3主店没有分店有把shop_id改为主店shop_id把分店会员信息jsonArray格式化存储至merged_users字段中
// 合并逻辑4当用户补充了手机号码或则修改了手机号码则需要按照手机号码进行会员信息合并把逻辑123重新执行一遍
// 查询逻辑在下单时选择会员信息时查询时用适用门店id like %,1,% and is_merged_to_head = 0
List<ShopUser> branchShopNotPhoneUserList = shopUserMapper.selectListByQuery(QueryWrapper.create().eq(ShopUser::getShopId, branchShopId)
.and(q -> {
q.isNull(ShopUser::getPhone).or(q1 -> {
q1.eq(ShopUser::getPhone, "");
});
}));
// 分店没有绑定手机号的会员
for (ShopUser shopUser : branchShopNotPhoneUserList) {
JSONObject branch = JSONObject.from(shopUser, JSONWriter.Feature.WriteMapNullValue);
JSONArray objects = new JSONArray();
objects.add(branch);
shopUser.setShopId(headShopId);
shopUser.setMergedUsers(objects.toJSONString(JSONWriter.Feature.WriteMapNullValue));
shopUserMapper.update(shopUser);
}
List<ShopUser> headShopUserList = shopUserMapper.selectListByQuery(QueryWrapper.create().eq(ShopUser::getShopId, headShopId).isNotNull(ShopUser::getPhone));
List<ShopUser> branchShopUserList = shopUserMapper.selectListByQuery(QueryWrapper.create().eq(ShopUser::getShopId, branchShopId).isNotNull(ShopUser::getPhone));
// 如果分店没有符合条件的会员则不进行合并
@ -91,6 +104,7 @@ public class ShopUserSyncServiceImpl implements ShopUserSyncService {
* @param branchShopUser 分店会员
*/
public void toMergeCase1(ShopUser headShopUser, ShopUser branchShopUser) {
// 合并逻辑1主店分店一致如果手机号码一致则把分店会员信息中的 积分余额消费金额消费次数合并到主店会员信息中把分店会员信息jsonArray格式化存储至主店用户信息的merged_users字段中再把分店用户信息删除
headShopUser.setAccountPoints(NumberUtil.nullToZero(headShopUser.getAccountPoints()) + (NumberUtil.nullToZero(branchShopUser.getAccountPoints())));
headShopUser.setAmount(NumberUtil.nullToZero(headShopUser.getAmount()).add(NumberUtil.nullToZero(branchShopUser.getAmount())));
headShopUser.setConsumeCount(NumberUtil.nullToZero(headShopUser.getConsumeCount()) + (NumberUtil.nullToZero(branchShopUser.getConsumeCount())));
@ -98,18 +112,10 @@ public class ShopUserSyncServiceImpl implements ShopUserSyncService {
headShopUser.setUpdateTime(LocalDateTime.now());
String mergedUsers = StrUtil.emptyToDefault(headShopUser.getMergedUsers(), "[]");
JSONArray objects = JSON.parseArray(mergedUsers);
objects.add(JSONObject.from(branchShopUser));
headShopUser.setMergedUsers(JSON.toJSONString(objects));
String allShopIds = StrUtil.emptyToDefault(headShopUser.getAllShopIds(), "0,".concat(headShopUser.getShopId() + ","));
List<String> split = StrUtil.split(allShopIds, ",", true, true);
String branchShopIdStr = Convert.toStr(branchShopUser.getShopId());
if (!split.contains(branchShopIdStr)) {
split.add(branchShopIdStr);
}
headShopUser.setAllShopIds(CollUtil.join(split, ",").concat(","));
objects.add(JSONObject.from(branchShopUser, JSONWriter.Feature.WriteMapNullValue));
headShopUser.setMergedUsers(JSON.toJSONString(objects, JSONWriter.Feature.WriteMapNullValue));
shopUserMapper.update(headShopUser);
toZero(branchShopUser);
shopUserMapper.update(branchShopUser);
shopUserMapper.delete(branchShopUser);
}
/**
@ -119,15 +125,7 @@ public class ShopUserSyncServiceImpl implements ShopUserSyncService {
* @param branchShopId 分店id
*/
public void toMergeCase2(ShopUser headShopUser, Long branchShopId) {
headShopUser.setUpdateTime(LocalDateTime.now());
String allShopIds = StrUtil.emptyToDefault(headShopUser.getAllShopIds(), "0,".concat(headShopUser.getShopId() + ","));
List<String> split = StrUtil.split(allShopIds, ",", true, true);
String branchShopIdStr = Convert.toStr(branchShopId);
if (!split.contains(branchShopIdStr)) {
split.add(branchShopIdStr);
}
headShopUser.setAllShopIds(CollUtil.join(split, ",").concat(","));
shopUserMapper.update(headShopUser);
// 合并逻辑2主店有分店没有不用采取任何操作使用主店的会员信息
}
/**
@ -137,36 +135,13 @@ public class ShopUserSyncServiceImpl implements ShopUserSyncService {
* @param branchShopUser 分店会员信息
*/
public void toMergeCase3(Long headShopId, ShopUser branchShopUser) {
ShopUser headShopUser = BeanUtil.copyProperties(branchShopUser, ShopUser.class, "id", "shopId","allShopIds");
headShopUser.setShopId(headShopId);
String mergedUsers = StrUtil.emptyToDefault(headShopUser.getMergedUsers(), "[]");
JSONArray objects = JSON.parseArray(mergedUsers);
objects.add(JSONObject.from(branchShopUser));
headShopUser.setMergedUsers(JSON.toJSONString(objects));
String allShopIds = StrUtil.emptyToDefault(headShopUser.getAllShopIds(), "0,".concat(headShopUser.getShopId() + ","));
List<String> split = StrUtil.split(allShopIds, ",", true, true);
String branchShopIdStr = Convert.toStr(branchShopUser.getShopId());
if (!split.contains(branchShopIdStr)) {
split.add(branchShopIdStr);
}
headShopUser.setAllShopIds(CollUtil.join(split, ",").concat(","));
shopUserMapper.insert(headShopUser);
toZero(branchShopUser);
// 合并逻辑3主店没有分店有把shop_id改为主店shop_id把分店会员信息jsonArray格式化存储至merged_users字段中
JSONObject branch = JSONObject.from(branchShopUser, JSONWriter.Feature.WriteMapNullValue);
JSONArray objects = new JSONArray();
objects.add(branch);
branchShopUser.setShopId(headShopId);
branchShopUser.setMergedUsers(objects.toJSONString(JSONWriter.Feature.WriteMapNullValue));
shopUserMapper.update(branchShopUser);
}
/**
* 会员数据归零
*
* @param shopUser 会员信息
*/
public void toZero(ShopUser shopUser) {
shopUser.setAccountPoints(0);
shopUser.setAmount(BigDecimal.ZERO);
shopUser.setConsumeAmount(BigDecimal.ZERO);
shopUser.setConsumeCount(0);
shopUser.setUpdateTime(LocalDateTime.now());
shopUser.setIsMergedToHead(YesNoEnum.YES.value());
}
}

View File

@ -2,12 +2,4 @@
-- tb_shop_user表扩展字段
-- ----------------------------
ALTER TABLE `tb_shop_user`
ADD COLUMN `is_merged_to_head` tinyint NULL DEFAULT 0 COMMENT '是否已经合并数据到主店 1-是 0-否 默认0',
ADD COLUMN `merged_users` text NULL COMMENT '已经合并过来的用户信息jsonArray格式,[{\"id\":1,\"shopId\":2,...},{\"id\":1,\"shopId\":2,...}]',
ADD COLUMN `all_shop_ids` varchar(1200) NULL COMMENT '适用门店id集合逗号分隔例如0,1,2,3,...查询的时候 all_shop_ids like \'%,1,%\';';
-- ----------------------------
-- 处理历史数据
-- ----------------------------
update tb_shop_user set is_merged_to_head = 0;
update tb_shop_user set all_shop_ids = concat('0,', shop_id, ',');
ADD COLUMN `merged_users` text NULL COMMENT '已经合并过来的用户信息jsonArray格式,[{\"id\":1,\"shopId\":2,...},{\"id\":1,\"shopId\":2,...}]';