Commit abcd380b by 范富荣 Committed by 崔彦峰

feat(service): 优化商品操作日志记录功能

- 移除废弃的注释代码和未使用的枚举字段
- 添加商品部位列表、比例换算和扣减标准等新字段跟踪
- 实现部位列表专用的字段剥离和格式化逻辑
- 优化日志记录中的数据同步问题,确保前端延迟入库数据正确记录
- 统一使用thiz引用替代重复的数据库查询调用
- 改进集合和映射类型的归一化处理机制
1 parent 01b51efd
......@@ -600,7 +600,6 @@ public class BaseGoodsService {
baseGoodsMapper.insertSelective(baseGoods);
goodsDTO.setId(baseGoods.getId());
// 记录操作日志
// BaseGoodsDTO baseGoodsDTO = getDetailByBaseGoodsId(baseGoods.getId());
BaseGoodsDTO baseGoodsDTO = thiz.getByBaseGoodsId(baseGoods.getId());
goodsOperationLogService.logAdd(baseGoodsDTO.getGoodsInfo(), baseGoodsDTO.getId(), baseGoodsDTO.getCommodityCode(), null, GoodsOperationLogService.GoodsType.BASE, goodsDTO.getCreatedBy());
......@@ -618,7 +617,6 @@ public class BaseGoodsService {
upgradeGoodsService.addUpgradeGoodsByList(upgradeGoodsDTOS, baseGoods.getId(), baseGoods.getCreatedBy());
}
} else {//修改操作
// BaseGoodsDTO oldBaseGoods = getDetailByBaseGoodsId(goodsDTO.getId());
BaseGoodsDTO oldBaseGoods = oldBaseGoodsFromCaller != null ? oldBaseGoodsFromCaller : thiz.getByBaseGoodsId(goodsDTO.getId());
BaseGoods baseGoods = updateForGoods(goodsDTO);
BaseGoodsDTO baseGoodsDTO = getById(goodsDTO.getId());
......@@ -629,8 +627,14 @@ public class BaseGoodsService {
baseGoodsMapper.updateByPrimaryKeySelective(baseGoods);
// 记录操作日志
// BaseGoodsDTO newBaseGoods = getDetailByBaseGoodsId(goodsDTO.getId());
BaseGoodsDTO newBaseGoods = thiz.getByBaseGoodsId(goodsDTO.getId());
// 将前端请求中由于延后入库导致数据库尚未同步的数据(如部位列表)塞入对象,供下方操作日志正确捕捉其变化
if (goodsDTO.getBaseGoodsDTO() != null && goodsDTO.getBaseGoodsDTO().getGoodsInfo() != null) {
if (goodsDTO.getBaseGoodsDTO().getGoodsInfo().getGoodsPositionList() != null) {
newBaseGoods.getGoodsInfo().setGoodsPositionList(goodsDTO.getBaseGoodsDTO().getGoodsInfo().getGoodsPositionList());
newBaseGoods.getGoodsInfo().setConvertedValue(goodsDTO.getBaseGoodsDTO().getGoodsInfo().getConvertedValue());
}
}
goodsOperationLogService.logUpdate(oldBaseGoods.getGoodsInfo(), newBaseGoods.getGoodsInfo(), newBaseGoods.getId(), newBaseGoods.getCommodityCode(), null, GoodsOperationLogService.GoodsType.BASE, goodsDTO.getUpdatedBy());
List<UpgradeGoodsDTO> upgradeGoodsDTOS = goodsDTO.getGoodsSpecificationList().stream().map(i -> {
......
package cn.com.glsx.dj.smartcarlife.service.common;
import cn.com.glsx.dj.smartcarlife.dto.GoodsOperationFieldDTO;
import cn.com.glsx.dj.smartcarlife.enums.CarLifeEnum;
import cn.com.glsx.dj.smartcarlife.mapper.GoodsOperationLogMapper;
import cn.com.glsx.dj.smartcarlife.model.GoodsOperationLog;
import cn.com.glsx.framework.core.beans.BaseModel;
......@@ -187,7 +188,7 @@ public class GoodsOperationLogService {
fieldList.add(new GoodsOperationFieldDTO(desc, field.getName(), toFieldValue(val)));
} else if (val instanceof Collection || val instanceof Map) {
String desc = getFieldDesc(field);
fieldList.add(new GoodsOperationFieldDTO(desc, field.getName(), JSONUtil.toJsonStr(val)));
fieldList.add(new GoodsOperationFieldDTO(desc, field.getName(), normalizeForLog(field.getName(), val)));
} else {
captureFieldsRecursive(val, fieldList, depth + 1, visited);
}
......@@ -244,13 +245,14 @@ public class GoodsOperationLogService {
newValueList.add(new GoodsOperationFieldDTO(desc, field.getName(), toFieldValue(newVal)));
}
} else if (oldVal instanceof Collection || newVal instanceof Collection || oldVal instanceof Map || newVal instanceof Map) {
// 归一化比较:去除系统自动生成的字段(id、时间戳等),仅比较业务字段
String oldNormalized = normalizeForCompare(oldVal);
String newNormalized = normalizeForCompare(newVal);
// 归一化比较与记录:去除系统自动生成的字段(id、时间戳等),针对特定字段(如部位列表)进行业务化转换
String fieldName = field.getName();
String oldNormalized = normalizeForLog(fieldName, oldVal);
String newNormalized = normalizeForLog(fieldName, newVal);
if (!Objects.equals(oldNormalized, newNormalized)) {
String desc = getFieldDesc(field);
oldValueList.add(new GoodsOperationFieldDTO(desc, field.getName(), oldVal == null ? "" : JSONUtil.toJsonStr(oldVal)));
newValueList.add(new GoodsOperationFieldDTO(desc, field.getName(), newVal == null ? "" : JSONUtil.toJsonStr(newVal)));
oldValueList.add(new GoodsOperationFieldDTO(desc, fieldName, oldNormalized));
newValueList.add(new GoodsOperationFieldDTO(desc, fieldName, newNormalized));
}
} else {
compareFields(oldVal, newVal, oldValueList, newValueList);
......@@ -281,26 +283,28 @@ public class GoodsOperationLogService {
));
/**
* 对集合/Map内部的数据进行归一化处理(卸妆)。
*
* 将从数据库查出来的包含 id、updatedDate 的旧数据,与前端传来的干净新数据
* 进行系统字段剥离(stripSystemFields),只保留纯净的业务属性,再转 JSON 字符串用于比对。
* 这样可以防止因“新数据没有 id,旧数据有 id”被系统误判为发生了业务数据修改。
* 对集合/Map内部的数据进行归一化处理(卸妆)并格式化用于日志。
*
* 1. 针对 goodsPositionList:仅保留 position (转换名称) 和 deductionStandard。
* 2. 其他:剥离 Collection/Map 内部对象的系统字段(stripSystemFields),转 JSON 字符串。
*/
private String normalizeForCompare(Object val) {
if (val == null) return "null";
private String normalizeForLog(String fieldName, Object val) {
if (val == null) return "";
if (val instanceof Collection) {
List<Map<String, Object>> normalized = new ArrayList<>();
for (Object item : (Collection<?>) val) {
if (item == null) {
normalized.add(Collections.emptyMap());
} else if (isSimpleType(item.getClass())) {
// 简单类型元素直接放入map
Map<String, Object> m = new LinkedHashMap<>();
m.put("_value", item);
normalized.add(m);
} else {
normalized.add(stripSystemFields(item));
if ("goodsPositionList".equals(fieldName)) {
normalized.add(stripGoodsPositionFields(item));
} else {
normalized.add(stripSystemFields(item));
}
}
}
return JSONUtil.toJsonStr(normalized);
......@@ -309,6 +313,43 @@ public class GoodsOperationLogService {
}
/**
* 部位列表专用剥离:仅保留 position (转名) 和 deductionStandard
*/
private Map<String, Object> stripGoodsPositionFields(Object obj) {
Map<String, Object> result = new LinkedHashMap<>();
try {
Field posField = findField(obj.getClass(), "position");
if (posField != null) {
posField.setAccessible(true);
Object val = posField.get(obj);
if (val instanceof Number) {
String posName = CarLifeEnum.PositionEnum.getNameByValue(((Number) val).byteValue());
result.put("position", StringUtils.defaultString(posName, val.toString()));
}
}
Field stdField = findField(obj.getClass(), "deductionStandard");
if (stdField != null) {
stdField.setAccessible(true);
result.put("deductionStandard", stdField.get(obj));
}
} catch (Exception e) {
log.error("解析部位列表字段失败", e);
}
return result;
}
private Field findField(Class<?> clazz, String fieldName) {
while (clazz != null && clazz != Object.class) {
try {
return clazz.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
clazz = clazz.getSuperclass();
}
}
return null;
}
/**
* 剥离对象中的系统字段,只保留业务字段用于对比
*/
private Map<String, Object> stripSystemFields(Object obj) {
......
......@@ -34,13 +34,15 @@ public enum GoodsTrackedFieldEnum {
VIDEO_URL("videoUrl", "商品主图视频地址"),
GOODS_SOURCE("goodsSource", "素材商品来源"),
ACCESSORY_TYPE("accessoryType", "配件性质:1-原厂件 2-品牌件"),
GOODS_POSITION_LIST("goodsPositionList", "部位列表"),
CONVERTED_VALUE("convertedValue", "比例换算(米/单)"),
DEDUCTION_STANDARD("deductionStandard", "扣减标准(米/单)"),
// ==================== SKU(UpgradeGoods / UpgradeGoodsDTO)字段 ====================
ATTRIBUTE("attribute", "属性"),
SPECIFICATION_TYPE("specificationType", "规格类型"),
CAR_TYPE_CONFIG_LIST("carTypeConfigList", "车型配置列表"),
COMMODITY_CODE("commodityCode", "商品id编码"),
// BASE_GOODS_ID("baseGoodsId", "基础商品ID"),
SPECIFICATION_NAME_SEQUENCE("specificationNameSequence", "规格名序列"),
SPECIFICATION_TYPE_SEQUENCE("specificationTypeSequence", "规格类型序列"),
SPECIFICATION_DATA("specificationData", "规格值序列"),
......@@ -48,7 +50,6 @@ public enum GoodsTrackedFieldEnum {
PURCHASE_PRICE("purchasePrice", "采购价"),
WARNING_VALUE("warningValue", "库存预警值下限"),
WARNING_VALUE_MAX("warningValueMax", "库存预警值上限"),
// ENTITY_CARD_FLAG("entityCardFlag", "是否实体卡标识"),
UPGRADE_GOODS_NAME("upgradeGoodsName", "商品名称"),
THIRD_PARTY_CODE("thirdPartyCode", "第三方编码"),
THIRD_PARTY_NAME("thirdPartyName", "第三方名称"),
......@@ -58,7 +59,7 @@ public enum GoodsTrackedFieldEnum {
OPERATION_COEFFICIENT("operationCoefficient", "运营系数"),
OPERATION_COST("operationCost", "运营成本"),
APPLICABLE_AREA("applicableArea", "适用区域"),
// SHOPPING_MALL_FLAG("shoppingMallFlag", "是否上架采购商城(0:否,1:是)"),
POSITION("position", "部位:1-前挡玻璃 2-侧后挡玻璃 3-单帖侧窗一个 4-单帖后挡一个 5-单帖小天窗 6-单帖中天窗 7-单帖全景天窗"),
;
private final String fieldName;
......
......@@ -164,6 +164,13 @@ public class ProductManagerService {
//保存基础、升级商品信息
record.getUpgradeGoods().setGoodsCode(goodsInfoDTO.getGoodsCode());
record.getUpgradeGoods().setUpdatedBy(record.getUpgradeGoods().getCreatedBy());
// 把 goodsInfo 暂存到 BaseGoodsDTO 以便内部日志模块抓取未入库的 goodsPositionList 的前端变更
if (record.getUpgradeGoods().getBaseGoodsDTO() == null) {
record.getUpgradeGoods().setBaseGoodsDTO(new BaseGoodsDTO());
}
record.getUpgradeGoods().getBaseGoodsDTO().setGoodsInfo(goodsInfoDTO);
if(saveDraft) {
baseGoodsService.saveTemp(record.getUpgradeGoods());
}else {
......
......@@ -1167,7 +1167,7 @@ public class StaffBaseInfoService {
List<StaffBaseInfoDTO> list = staffBaseInfoMapper.listShopStaffInfoGroupByUUid(condition);
if (CollectionUtils.isNotEmpty(list)) {
//过滤角色,只要驻店人员
return filterStaffRole(list, 1);
return filterStaffRole(list, 0);
}
return null;
}
......@@ -1179,9 +1179,14 @@ public class StaffBaseInfoService {
* @return
*/
public List<StaffBaseInfoDTO> filterStaffRole(List<StaffBaseInfoDTO> list, int flag) {
//获取园区店集合
List<Integer> merchantIdList = list.stream().filter(x -> x.getMerchantId() != null).map(StaffBaseInfoDTO::getMerchantId).distinct().collect(Collectors.toList());
if (CollectionUtils.isEmpty(merchantIdList)) return null;
MerchantStaffRequest staffRequest = new MerchantStaffRequest();
staffRequest.setRoleName(env.getProperty("glsk_staff_role_name", String.class, "驻店人员"));
staffRequest.setDelFlag((byte) 0);
//设置门店列表
staffRequest.setMerchantNoList(merchantIdList);
if (flag == 0) {
List<String> uuidList = list.stream().filter(x -> StringUtils.isNotBlank(x.getLoginUuid())).map(StaffBaseInfoDTO::getLoginUuid).collect(Collectors.toList());
if (CollectionUtils.isEmpty(uuidList)) return null;
......@@ -1204,7 +1209,7 @@ public class StaffBaseInfoService {
}
} else {
log.error("查询督导人员列表失败,请求参数={}, message={}", JSON.toJSONString(staffRequest), response.getMessage());
throw new RpcServiceException(DefaultErrorEnum.RPC_ERROR, response.getMessage());
return null;
}
}
......
......@@ -271,7 +271,7 @@
</select>
<select id="listShopStaffBaseInfo" parameterType="cn.com.glsx.dj.smartcarlife.dto.merchantPerformanceConfig.StaffBaseInfoDTO" resultMap="DTOResultMap">
select si.staff_name, si.phone_number as staff_phone
select si.staff_name, si.phone_number as staff_phone,smf.sub_merchant_id as merchant_id
from dj_car_life_scene_merchant_ref smf
inner join dj_car_life_staff_info si on smf.sub_merchant_id = si.merchant_id
where smf.deleted_flag = 'N' and smf.sub_merchant_type in(6,8) and si.deleted_flag = 'N'
......@@ -287,7 +287,7 @@
<select id="listShopStaffInfoGroupByUUid" parameterType="cn.com.glsx.dj.smartcarlife.dto.merchantPerformanceConfig.StaffBaseInfoDTO" resultMap="DTOResultMap">
select si.staff_uuid, si.staff_name, si.phone_number as staff_phone
select si.staff_uuid, si.staff_name, si.phone_number as staff_phone,smf.sub_merchant_id as merchant_id
from dj_car_life_scene_merchant_ref smf
inner join dj_car_life_staff_info si on smf.sub_merchant_id = si.merchant_id
where smf.deleted_flag = 'N' and smf.sub_merchant_type in(6,8) and si.deleted_flag = 'N'
......
......@@ -32,7 +32,7 @@ public class StaffBaseInfoServiceTest extends BaseServiceTest{
@Test
public void listShopStaffBaseInfoTest() throws Exception {
StaffBaseInfoDTO condition = new StaffBaseInfoDTO();
condition.setMerchantIdList(Arrays.asList(44209390));
condition.setMerchantIdList(Arrays.asList(44211547));
List<StaffBaseInfoDTO> list = staffBaseInfoService.listShopStaffBaseInfo(condition);
System.out.println( list);
List<StaffBaseInfoDTO> listByUUid = staffBaseInfoService.listShopStaffInfoGroupByUUid(condition);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!