新增功能:更新所有数据的path和path_name字段,优化品类维护模块,支持路径信息的生成与更新。
This commit is contained in:
@ -113,4 +113,17 @@ public class CoscoCategoryMaintenanceController{
|
||||
|
||||
return BaseResponse.success(coscoCategoryMaintenanceService.getList(coscoCategoryMaintenanceVO));
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新所有数据的path和path_name字段
|
||||
*
|
||||
* @param versionId 版本ID
|
||||
* @return 返回结果
|
||||
*/
|
||||
@ApiOperation("更新所有数据的path和path_name字段")
|
||||
@GetMapping("/updatePathAndPathName/{versionId}")
|
||||
public BaseResponse<Boolean> updatePathAndPathName(@ApiParam(value = "版本ID", required = true) @PathVariable Long versionId) {
|
||||
|
||||
return BaseResponse.success(coscoCategoryMaintenanceService.updateAllPathAndPathName(versionId));
|
||||
}
|
||||
}
|
||||
|
@ -51,4 +51,12 @@ public interface CoscoCategoryMaintenanceMapper extends IBaseMapper<CoscoCategor
|
||||
|
||||
void deleteByVersionId(@Param("versionId") String versionId);
|
||||
|
||||
/**
|
||||
* 更新path和path_name字段
|
||||
* @param id 主键ID
|
||||
* @param path 路径
|
||||
* @param pathName 路径名称
|
||||
*/
|
||||
void updatePathAndPathName(@Param("id") Long id, @Param("path") String path, @Param("pathName") String pathName);
|
||||
|
||||
}
|
||||
|
@ -8,6 +8,7 @@
|
||||
<result column="version_id" jdbcType="VARCHAR" property="versionId"/>
|
||||
<result column="parent_id" jdbcType="VARCHAR" property="parentId"/>
|
||||
<result column="path" jdbcType="VARCHAR" property="path"/>
|
||||
<result column="path_name" jdbcType="VARCHAR" property="pathName"/>
|
||||
<result column="category_name" jdbcType="VARCHAR" property="categoryName"/>
|
||||
<result column="code" jdbcType="VARCHAR" property="code"/>
|
||||
<result column="status" jdbcType="VARCHAR" property="status"/>
|
||||
@ -49,15 +50,15 @@
|
||||
|
||||
|
||||
<insert id="saveData">
|
||||
INSERT INTO cosco_category_maintenance (`version_id`, `parent_id`, `path`, `category_name`, `code`, `status`)
|
||||
INSERT INTO cosco_category_maintenance (`version_id`, `parent_id`, `path`, `path_name`, `category_name`, `code`, `status`)
|
||||
VALUES ( #{categoryMaintenance.versionId},#{categoryMaintenance.parentId},#{categoryMaintenance.path},
|
||||
#{categoryMaintenance.categoryName},#{categoryMaintenance.code},#{categoryMaintenance.status});
|
||||
#{categoryMaintenance.pathName},#{categoryMaintenance.categoryName},#{categoryMaintenance.code},#{categoryMaintenance.status});
|
||||
|
||||
</insert>
|
||||
|
||||
<update id="updateDataById">
|
||||
UPDATE cosco_category_maintenance SET `version_id` = #{categoryMaintenance.versionId}, `parent_id` = #{categoryMaintenance.parentId},
|
||||
`path` = #{categoryMaintenance.path}, `category_name` = #{categoryMaintenance.categoryName},
|
||||
`path` = #{categoryMaintenance.path}, `path_name` = #{categoryMaintenance.pathName}, `category_name` = #{categoryMaintenance.categoryName},
|
||||
`code` = #{categoryMaintenance.code}, `status` = #{categoryMaintenance.status}
|
||||
WHERE `id` = #{categoryMaintenance.id};
|
||||
</update>
|
||||
@ -83,10 +84,16 @@
|
||||
|
||||
<insert id="batchSaveCategory">
|
||||
INSERT INTO cosco_category_maintenance
|
||||
(id,version_id, parent_id,category_name,code,status) VALUES
|
||||
(id,version_id, parent_id, path, path_name, category_name,code,status) VALUES
|
||||
<foreach collection="list" item="item" separator=",">
|
||||
(#{item.id},#{item.versionId}, #{item.parentId},#{item.categoryName},#{item.code},#{item.status})
|
||||
(#{item.id},#{item.versionId}, #{item.parentId}, #{item.path}, #{item.pathName}, #{item.categoryName},#{item.code},#{item.status})
|
||||
</foreach>
|
||||
</insert>
|
||||
|
||||
<update id="updatePathAndPathName">
|
||||
UPDATE cosco_category_maintenance
|
||||
SET path = #{path}, path_name = #{pathName}
|
||||
WHERE id = #{id}
|
||||
</update>
|
||||
|
||||
</mapper>
|
||||
|
@ -53,6 +53,12 @@ public class CoscoCategoryMaintenance implements Serializable {
|
||||
@ApiModelProperty(value = "路径")
|
||||
private String path;
|
||||
|
||||
/**
|
||||
* 路径名称(从根到当前节点的所有category_name,以逗号分隔)
|
||||
*/
|
||||
@ApiModelProperty(value = "路径名称")
|
||||
private String pathName;
|
||||
|
||||
/**
|
||||
* 品类名称
|
||||
*/
|
||||
|
@ -30,4 +30,11 @@ public interface ICoscoCategoryMaintenanceService extends IBaseService<CoscoCate
|
||||
Boolean saveData(CoscoCategoryMaintenance coscoCategoryMaintenance);
|
||||
|
||||
Boolean updateDataById(CoscoCategoryMaintenance coscoCategoryMaintenance);
|
||||
|
||||
/**
|
||||
* 更新所有数据的path和path_name字段
|
||||
* @param versionId 版本ID
|
||||
* @return 更新结果
|
||||
*/
|
||||
Boolean updateAllPathAndPathName(Long versionId);
|
||||
}
|
||||
|
@ -169,13 +169,135 @@ public class CoscoCategoryMaintenanceServiceImpl extends BaseServiceImpl<CoscoCa
|
||||
// return BaseResponse.fail("您输入的编码已存在,请重新输入!");
|
||||
//// CommonExceptionEnum.FRAME_EXCEPTION_COMMON_REPEAT.throwException();
|
||||
// }
|
||||
|
||||
// 先保存获取ID
|
||||
coscoCategoryMaintenanceMapper.saveData(coscoCategoryMaintenance);
|
||||
|
||||
// 生成path和path_name并更新
|
||||
generatePathAndPathName(coscoCategoryMaintenance);
|
||||
coscoCategoryMaintenanceMapper.updatePathAndPathName(coscoCategoryMaintenance.getId(),
|
||||
coscoCategoryMaintenance.getPath(),
|
||||
coscoCategoryMaintenance.getPathName());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean updateDataById(CoscoCategoryMaintenance coscoCategoryMaintenance) {
|
||||
// 生成path和path_name
|
||||
generatePathAndPathName(coscoCategoryMaintenance);
|
||||
|
||||
coscoCategoryMaintenanceMapper.updateDataById(coscoCategoryMaintenance);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成path和path_name
|
||||
* @param coscoCategoryMaintenance 品类维护对象
|
||||
*/
|
||||
private void generatePathAndPathName(CoscoCategoryMaintenance coscoCategoryMaintenance) {
|
||||
Long parentId = coscoCategoryMaintenance.getParentId();
|
||||
|
||||
if (parentId == null || parentId == 0L) {
|
||||
// 根节点
|
||||
coscoCategoryMaintenance.setPath(coscoCategoryMaintenance.getId().toString());
|
||||
coscoCategoryMaintenance.setPathName(coscoCategoryMaintenance.getCategoryName());
|
||||
} else {
|
||||
// 非根节点,需要查询父节点信息
|
||||
CoscoCategoryMaintenance parent = this.getById(parentId);
|
||||
if (parent != null) {
|
||||
String parentPath = parent.getPath();
|
||||
String parentPathName = parent.getPathName();
|
||||
|
||||
// 生成path:父节点path + 当前节点id
|
||||
coscoCategoryMaintenance.setPath(parentPath + "," + coscoCategoryMaintenance.getId());
|
||||
|
||||
// 生成path_name:父节点path_name + 当前节点category_name
|
||||
coscoCategoryMaintenance.setPathName(parentPathName + "," + coscoCategoryMaintenance.getCategoryName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean updateAllPathAndPathName(Long versionId) {
|
||||
// 查询指定版本的所有根节点
|
||||
LambdaQueryWrapper<CoscoCategoryMaintenance> queryWrapper = Wrappers.lambdaQuery();
|
||||
queryWrapper.eq(CoscoCategoryMaintenance::getVersionId, versionId)
|
||||
.eq(CoscoCategoryMaintenance::getParentId, 0L);
|
||||
|
||||
List<CoscoCategoryMaintenance> rootNodes = this.list(queryWrapper);
|
||||
|
||||
// 递归更新每个根节点及其子节点的path和path_name
|
||||
for (CoscoCategoryMaintenance rootNode : rootNodes) {
|
||||
updateNodePathAndPathName(rootNode, versionId);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归更新节点的path和path_name
|
||||
* @param node 当前节点
|
||||
* @param versionId 版本ID
|
||||
*/
|
||||
private void updateNodePathAndPathName(CoscoCategoryMaintenance node, Long versionId) {
|
||||
// 生成当前节点的path和path_name
|
||||
String path = generatePath(node, versionId);
|
||||
String pathName = generatePathName(node, versionId);
|
||||
|
||||
// 更新数据库
|
||||
coscoCategoryMaintenanceMapper.updatePathAndPathName(node.getId(), path, pathName);
|
||||
|
||||
// 查询子节点
|
||||
LambdaQueryWrapper<CoscoCategoryMaintenance> queryWrapper = Wrappers.lambdaQuery();
|
||||
queryWrapper.eq(CoscoCategoryMaintenance::getVersionId, versionId)
|
||||
.eq(CoscoCategoryMaintenance::getParentId, node.getId());
|
||||
|
||||
List<CoscoCategoryMaintenance> children = this.list(queryWrapper);
|
||||
|
||||
// 递归更新子节点
|
||||
for (CoscoCategoryMaintenance child : children) {
|
||||
updateNodePathAndPathName(child, versionId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成节点的path
|
||||
* @param node 当前节点
|
||||
* @param versionId 版本ID
|
||||
* @return path字符串
|
||||
*/
|
||||
private String generatePath(CoscoCategoryMaintenance node, Long versionId) {
|
||||
if (node.getParentId() == null || node.getParentId() == 0L) {
|
||||
return node.getId().toString();
|
||||
}
|
||||
|
||||
// 查询父节点
|
||||
CoscoCategoryMaintenance parent = this.getById(node.getParentId());
|
||||
if (parent != null) {
|
||||
return generatePath(parent, versionId) + "," + node.getId();
|
||||
}
|
||||
|
||||
return node.getId().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成节点的path_name
|
||||
* @param node 当前节点
|
||||
* @param versionId 版本ID
|
||||
* @return path_name字符串
|
||||
*/
|
||||
private String generatePathName(CoscoCategoryMaintenance node, Long versionId) {
|
||||
if (node.getParentId() == null || node.getParentId() == 0L) {
|
||||
return node.getCategoryName();
|
||||
}
|
||||
|
||||
// 查询父节点
|
||||
CoscoCategoryMaintenance parent = this.getById(node.getParentId());
|
||||
if (parent != null) {
|
||||
return generatePathName(parent, versionId) + "," + node.getCategoryName();
|
||||
}
|
||||
|
||||
return node.getCategoryName();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user