多店铺需求
This commit is contained in:
parent
3a5f65e8ec
commit
ca8d49bac7
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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:当用户补充了手机号码或则修改了手机号码,则需要按照手机号码进行会员信息合并,把逻辑1,2,3重新执行一遍
|
||||
// 查询逻辑:在下单时选择会员信息时,查询时用适用门店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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,...}]';
|
||||
Loading…
Reference in New Issue