演示模块增加导入功能

This commit is contained in:
数据小王子 2024-04-12 21:27:02 +08:00
parent 0b3f82660f
commit 4224ffdc64
19 changed files with 550 additions and 20 deletions

View File

@ -21,6 +21,11 @@
<artifactId>ruoyi-common-core</artifactId>
</dependency>
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common-excel</artifactId>
</dependency>
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common-security</artifactId>

View File

@ -1,11 +1,14 @@
package com.ruoyi.mf.controller;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.excel.core.ExcelResult;
import com.ruoyi.common.core.core.domain.R;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
@ -14,15 +17,18 @@ import com.ruoyi.common.web.annotation.RepeatSubmit;
import com.ruoyi.common.web.core.BaseController;
import jakarta.annotation.Resource;
import com.ruoyi.mf.domain.vo.MfProductVo;
import com.ruoyi.mf.domain.vo.MfProductImportVo;
import com.ruoyi.mf.domain.bo.MfProductBo;
import com.ruoyi.mf.listener.MfProductImportListener;
import com.ruoyi.mf.service.IMfProductService;
import org.springframework.web.multipart.MultipartFile;
/**
* 产品树Controller
*
* @author 数据小王子
* 2024-01-06
* 2024-04-12
*/
@Validated
@RequiredArgsConstructor
@ -56,6 +62,26 @@ public class MfProductController extends BaseController
ExcelUtil.exportExcel(list, "产品树", MfProductVo.class, response);
}
/**
* 导入数据
*
* @param file 导入文件
* @param updateSupport 是否更新已存在数据
*/
@Log(title = "产品树", businessType = BusinessType.IMPORT)
@SaCheckPermission("mf:product:import")
@PostMapping("/importData")
public R<Void> importData(MultipartFile file, boolean updateSupport) throws Exception {
ExcelResult<MfProductImportVo> result = ExcelUtil.importExcel(file.getInputStream(), MfProductImportVo.class, new MfProductImportListener(updateSupport));
return R.ok(result.getAnalysis());
}
@SaCheckPermission("mf:product:import")
@PostMapping("/importTemplate")
public void importTemplate(HttpServletResponse response) {
ExcelUtil.exportExcel(new ArrayList<>(), "产品树", MfProductImportVo.class, response);
}
/**
* 获取产品树详细信息
*/

View File

@ -1,11 +1,14 @@
package com.ruoyi.mf.controller;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.excel.core.ExcelResult;
import com.ruoyi.common.core.core.domain.R;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
@ -14,8 +17,11 @@ import com.ruoyi.common.web.annotation.RepeatSubmit;
import com.ruoyi.common.web.core.BaseController;
import jakarta.annotation.Resource;
import com.ruoyi.mf.domain.vo.MfStudentVo;
import com.ruoyi.mf.domain.vo.MfStudentImportVo;
import com.ruoyi.mf.domain.bo.MfStudentBo;
import com.ruoyi.mf.listener.MfStudentImportListener;
import com.ruoyi.mf.service.IMfStudentService;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.orm.core.page.TableDataInfo;
@ -23,7 +29,7 @@ import com.ruoyi.common.orm.core.page.TableDataInfo;
* 学生信息表Controller
*
* @author 数据小王子
* 2024-01-05
* 2024-04-12
*/
@Validated
@RequiredArgsConstructor
@ -56,6 +62,26 @@ public class MfStudentController extends BaseController
ExcelUtil.exportExcel(list, "学生信息表", MfStudentVo.class, response);
}
/**
* 导入数据
*
* @param file 导入文件
* @param updateSupport 是否更新已存在数据
*/
@Log(title = "学生信息表", businessType = BusinessType.IMPORT)
@SaCheckPermission("mf:student:import")
@PostMapping("/importData")
public R<Void> importData(MultipartFile file, boolean updateSupport) throws Exception {
ExcelResult<MfStudentImportVo> result = ExcelUtil.importExcel(file.getInputStream(), MfStudentImportVo.class, new MfStudentImportListener(updateSupport));
return R.ok(result.getAnalysis());
}
@SaCheckPermission("mf:student:import")
@PostMapping("/importTemplate")
public void importTemplate(HttpServletResponse response) {
ExcelUtil.exportExcel(new ArrayList<>(), "学生信息表", MfStudentImportVo.class, response);
}
/**
* 获取学生信息表详细信息
*/

View File

@ -1,6 +1,5 @@
package com.ruoyi.mf.domain;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.Table;
@ -13,7 +12,7 @@ import com.ruoyi.common.orm.core.domain.TreeEntity;
* 产品树对象 mf_product
*
* @author 数据小王子
* 2024-01-06
* 2024-04-12
*/
@Data
@EqualsAndHashCode(callSuper = true)
@ -23,7 +22,7 @@ public class MfProduct extends TreeEntity
@Serial
private static final long serialVersionUID = 1L;
/** 产品id */
/** 产品编号 */
@Id
private Long productId;

View File

@ -2,7 +2,6 @@ package com.ruoyi.mf.domain;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.Table;
@ -15,7 +14,7 @@ import com.ruoyi.common.orm.core.domain.BaseEntity;
* 学生信息表对象 mf_student
*
* @author 数据小王子
* 2024-01-06
* 2024-04-12
*/
@Data
@EqualsAndHashCode(callSuper = true)

View File

@ -11,7 +11,7 @@ import com.ruoyi.common.orm.core.domain.TreeEntity;
* 产品树业务对象 mf_product
*
* @author 数据小王子
* @date 2024-01-06
* @date 2024-04-12
*/
@Data
@EqualsAndHashCode(callSuper = true)
@ -20,7 +20,7 @@ public class MfProductBo extends TreeEntity
{
/**
* 产品id
* 产品编号
*/
private Long productId;
@ -36,4 +36,5 @@ public class MfProductBo extends TreeEntity
@NotBlank(message = "产品状态0正常 1停用不能为空")
private String status;
}

View File

@ -13,7 +13,7 @@ import com.ruoyi.common.orm.core.domain.BaseEntity;
* 学生信息表业务对象 mf_student
*
* @author 数据小王子
* @date 2024-01-05
* @date 2024-04-12
*/
@Data
@EqualsAndHashCode(callSuper = true)
@ -63,4 +63,5 @@ public class MfStudentBo extends BaseEntity
@JsonFormat(pattern = "yyyy-MM-dd")
private Date studentBirthday;
}

View File

@ -0,0 +1,52 @@
package com.ruoyi.mf.domain.vo;
import com.alibaba.excel.annotation.ExcelProperty;
import com.ruoyi.common.excel.annotation.ExcelDictFormat;
import com.ruoyi.common.excel.convert.ExcelDictConvert;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import lombok.NoArgsConstructor;
/**
* 产品树导入视图对象 mf_product
*
* @author 数据小王子
* @date 2024-04-12
*/
@Data
@NoArgsConstructor
public class MfProductImportVo implements Serializable
{
@Serial
private static final long serialVersionUID = 1L;
/** 产品编号 */
@ExcelProperty(value = "产品编号")
private Long productId;
/** 上级编号 */
@ExcelProperty(value = "上级编号")
private Long parentId;
/** 产品名称 */
@ExcelProperty(value = "产品名称")
private String productName;
/** 显示顺序 */
@ExcelProperty(value = "显示顺序")
private Integer orderNum;
/** 产品状态0正常 1停用 */
@ExcelProperty(value = "产品状态", converter = ExcelDictConvert.class)
@ExcelDictFormat(dictType = "sys_student_status")
private String status;
/** 逻辑删除标志0代表存在 1代表删除 */
@ExcelProperty(value = "逻辑删除标志0代表存在 1代表删除")
private Integer delFlag;
}

View File

@ -17,7 +17,7 @@ import com.ruoyi.common.orm.core.domain.TreeEntity;
* 产品树视图对象 mf_product
*
* @author 数据小王子
* @date 2024-01-06
* @date 2024-04-12
*/
@Data
@ExcelIgnoreUnannotated
@ -29,8 +29,8 @@ public class MfProductVo extends TreeEntity implements Serializable
@Serial
private static final long serialVersionUID = 1L;
/** 产品id */
@ExcelProperty(value = "产品id")
/** 产品编号 */
@ExcelProperty(value = "产品编号")
private Long productId;
/** 产品名称 */
@ -46,4 +46,6 @@ public class MfProductVo extends TreeEntity implements Serializable
@ExcelProperty(value = "逻辑删除标志0代表存在 1代表删除")
private Integer delFlag;
}

View File

@ -0,0 +1,61 @@
package com.ruoyi.mf.domain.vo;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.alibaba.excel.annotation.ExcelProperty;
import com.ruoyi.common.excel.annotation.ExcelDictFormat;
import com.ruoyi.common.excel.convert.ExcelDictConvert;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import lombok.NoArgsConstructor;
/**
* 学生信息表导入视图对象 mf_student
*
* @author 数据小王子
* @date 2024-04-12
*/
@Data
@NoArgsConstructor
public class MfStudentImportVo implements Serializable
{
@Serial
private static final long serialVersionUID = 1L;
/** 学生名称 */
@ExcelProperty(value = "学生名称")
private String studentName;
/** 年龄 */
@ExcelProperty(value = "年龄")
private Integer studentAge;
/** 爱好0代码 1音乐 2电影 */
@ExcelProperty(value = "爱好", converter = ExcelDictConvert.class)
@ExcelDictFormat(dictType = "sys_student_hobby")
private String studentHobby;
/** 性别1男 2女 3未知 */
@ExcelProperty(value = "性别", converter = ExcelDictConvert.class)
@ExcelDictFormat(dictType = "sys_user_gender")
private String studentGender;
/** 状态0正常 1停用 */
@ExcelProperty(value = "状态", converter = ExcelDictConvert.class)
@ExcelDictFormat(dictType = "sys_student_status")
private String studentStatus;
/** 生日 */
@ExcelProperty(value = "生日")
private Date studentBirthday;
/** 逻辑删除标志0代表存在 1代表删除 */
@ExcelProperty(value = "逻辑删除标志0代表存在 1代表删除")
private Integer delFlag;
}

View File

@ -19,7 +19,7 @@ import com.ruoyi.common.orm.core.domain.BaseEntity;
* 学生信息表视图对象 mf_student
*
* @author 数据小王子
* @date 2024-01-05
* @date 2024-04-12
*/
@Data
@ExcelIgnoreUnannotated
@ -66,4 +66,6 @@ public class MfStudentVo extends BaseEntity implements Serializable
@ExcelProperty(value = "逻辑删除标志0代表存在 1代表删除")
private Integer delFlag;
}

View File

@ -0,0 +1,118 @@
package com.ruoyi.mf.listener;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.core.utils.ValidatorUtils;
import com.ruoyi.common.excel.core.ExcelListener;
import com.ruoyi.common.excel.core.ExcelResult;
import com.ruoyi.mf.domain.bo.MfProductBo;
import com.ruoyi.mf.domain.vo.*;
import com.ruoyi.mf.service.*;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
/**
* 产品树自定义导入
*
* @author 数据小王子
*/
@Slf4j
public class MfProductImportListener extends AnalysisEventListener<MfProductImportVo> implements ExcelListener<MfProductImportVo> {
private final IMfProductService mfProductService;
private final Boolean isUpdateSupport;
private int successNum = 0;
private int failureNum = 0;
private final StringBuilder successMsg = new StringBuilder();
private final StringBuilder failureMsg = new StringBuilder();
public MfProductImportListener(Boolean isUpdateSupport) {
this.mfProductService = SpringUtils.getBean(IMfProductService.class);
this.isUpdateSupport = isUpdateSupport;
}
@Override
public void invoke(MfProductImportVo mfProductVo, AnalysisContext context) {
try {
MfProductBo mfProductBo = BeanUtil.toBean(mfProductVo, MfProductBo.class);
//TODO:根据某个字段查询数据库表中是否存在记录不存在就新增存在就更新
MfProductVo mfProductVo1 = null;
mfProductVo1 = mfProductService.selectById(mfProductVo.getProductId());
if (ObjectUtil.isNull(mfProductVo1)) {
//不存在就新增
mfProductBo.setVersion(0);
ValidatorUtils.validate(mfProductBo);
boolean inserted = mfProductService.insertWithPk(mfProductBo);//树表需要前台传来主键值
if (inserted) {
successNum++;
successMsg.append("<br/>").append(successNum).append("、产品树 记录导入成功");
return;
} else {
failureNum++;
failureMsg.append("<br/>").append(failureNum).append("、产品树 记录导入失败");
return;
}
} else if (isUpdateSupport) {
//存在就更新
mfProductBo.setProductId(mfProductVo1.getProductId());//主键
mfProductBo.setVersion(mfProductVo1.getVersion());
boolean updated = mfProductService.update(mfProductBo);
if (updated) {
successNum++;
successMsg.append("<br/>").append(successNum).append("、产品树 记录更新成功");
return;
} else {
failureNum++;
failureMsg.append("<br/>").append(failureNum).append("、产品树 记录更新失败");
return;
}
}
} catch (Exception e) {
failureNum++;
String msg = "<br/>" + failureNum + "、产品树 记录导入失败:";
failureMsg.append(msg).append(e.getMessage());
log.error(msg, e);
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
}
@Override
public ExcelResult<MfProductImportVo> getExcelResult() {
return new ExcelResult<>() {
@Override
public String getAnalysis() {
if (failureNum > 0) {
failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据没有成功导入,错误如下:");
throw new ServiceException(failureMsg.toString());
} else {
successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
}
return successMsg.toString();
}
@Override
public List<MfProductImportVo> getList() {
return null;
}
@Override
public List<String> getErrorList() {
return null;
}
};
}
}

View File

@ -0,0 +1,118 @@
package com.ruoyi.mf.listener;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.core.utils.ValidatorUtils;
import com.ruoyi.common.excel.core.ExcelListener;
import com.ruoyi.common.excel.core.ExcelResult;
import com.ruoyi.mf.domain.bo.MfStudentBo;
import com.ruoyi.mf.domain.vo.*;
import com.ruoyi.mf.service.*;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
/**
* 学生信息表自定义导入
*
* @author 数据小王子
*/
@Slf4j
public class MfStudentImportListener extends AnalysisEventListener<MfStudentImportVo> implements ExcelListener<MfStudentImportVo> {
private final IMfStudentService mfStudentService;
private final Boolean isUpdateSupport;
private int successNum = 0;
private int failureNum = 0;
private final StringBuilder successMsg = new StringBuilder();
private final StringBuilder failureMsg = new StringBuilder();
public MfStudentImportListener(Boolean isUpdateSupport) {
this.mfStudentService = SpringUtils.getBean(IMfStudentService.class);
this.isUpdateSupport = isUpdateSupport;
}
@Override
public void invoke(MfStudentImportVo mfStudentVo, AnalysisContext context) {
try {
MfStudentBo mfStudentBo = BeanUtil.toBean(mfStudentVo, MfStudentBo.class);
//TODO:根据某个字段查询数据库表中是否存在记录不存在就新增存在就更新
MfStudentVo mfStudentVo1 = null;
//mfStudentVo1 = mfStudentService.selectBySomefield(mfStudentVo.getSomefield());
if (ObjectUtil.isNull(mfStudentVo1)) {
//不存在就新增
mfStudentBo.setVersion(0);
ValidatorUtils.validate(mfStudentBo);
boolean inserted = mfStudentService.insert(mfStudentBo);
if (inserted) {
successNum++;
successMsg.append("<br/>").append(successNum).append("、学生信息表 记录导入成功");
return;
} else {
failureNum++;
failureMsg.append("<br/>").append(failureNum).append("、学生信息表 记录导入失败");
return;
}
} else if (isUpdateSupport) {
//存在就更新
mfStudentBo.setStudentId(mfStudentVo1.getStudentId());//主键
mfStudentBo.setVersion(mfStudentVo1.getVersion());
boolean updated = mfStudentService.update(mfStudentBo);
if (updated) {
successNum++;
successMsg.append("<br/>").append(successNum).append("、学生信息表 记录更新成功");
return;
} else {
failureNum++;
failureMsg.append("<br/>").append(failureNum).append("、学生信息表 记录更新失败");
return;
}
}
} catch (Exception e) {
failureNum++;
String msg = "<br/>" + failureNum + "、学生信息表 记录导入失败:";
failureMsg.append(msg).append(e.getMessage());
log.error(msg, e);
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
}
@Override
public ExcelResult<MfStudentImportVo> getExcelResult() {
return new ExcelResult<>() {
@Override
public String getAnalysis() {
if (failureNum > 0) {
failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据没有成功导入,错误如下:");
throw new ServiceException(failureMsg.toString());
} else {
successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
}
return successMsg.toString();
}
@Override
public List<MfStudentImportVo> getList() {
return null;
}
@Override
public List<String> getErrorList() {
return null;
}
};
}
}

View File

@ -8,7 +8,7 @@ import com.ruoyi.mf.domain.MfProduct;
* 产品树Mapper接口
*
* @author 数据小王子
* 2024-01-06
* 2024-04-12
*/
@Mapper
public interface MfProductMapper extends BaseMapper<MfProduct>

View File

@ -8,7 +8,7 @@ import com.ruoyi.mf.domain.MfStudent;
* 学生信息表Mapper接口
*
* @author 数据小王子
* 2023-11-22
* 2024-04-12
*/
@Mapper
public interface MfStudentMapper extends BaseMapper<MfStudent>

View File

@ -10,7 +10,7 @@ import com.ruoyi.common.orm.core.service.IBaseService;
* 产品树Service接口
*
* @author 数据小王子
* 2024-01-06
* 2024-04-12
*/
public interface IMfProductService extends IBaseService<MfProduct>
{
@ -39,6 +39,14 @@ public interface IMfProductService extends IBaseService<MfProduct>
*/
boolean insert(MfProductBo mfProductBo);
/**
* 新增产品树前台提供主键值一般用于导入的场合
*
* @param mfProductBo 产品树Bo
* @return 结果:true 操作成功false 操作失败
*/
boolean insertWithPk(MfProductBo mfProductBo);
/**
* 修改产品树
*

View File

@ -11,7 +11,7 @@ import com.ruoyi.common.orm.core.page.TableDataInfo;
* 学生信息表Service接口
*
* @author 数据小王子
* 2024-01-05
* 2024-04-12
*/
public interface IMfStudentService extends IBaseService<MfStudent>
{
@ -47,6 +47,14 @@ public interface IMfStudentService extends IBaseService<MfStudent>
*/
boolean insert(MfStudentBo mfStudentBo);
/**
* 新增学生信息表前台提供主键值一般用于导入的场合
*
* @param mfStudentBo 学生信息表Bo
* @return 结果:true 操作成功false 操作失败
*/
boolean insertWithPk(MfStudentBo mfStudentBo);
/**
* 修改学生信息表
*

View File

@ -2,10 +2,18 @@ package com.ruoyi.mf.service.impl;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.orm.core.page.PageQuery;
import com.ruoyi.common.orm.core.page.TableDataInfo;
import com.ruoyi.common.orm.core.service.impl.BaseServiceImpl;
import com.ruoyi.common.core.utils.DateUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -20,7 +28,7 @@ import static com.ruoyi.mf.domain.table.MfProductTableDef.MF_PRODUCT;
* 产品树Service业务层处理
*
* @author 数据小王子
* 2024-01-06
* 2024-04-12
*/
@Service
public class MfProductServiceImpl extends BaseServiceImpl<MfProductMapper, MfProduct> implements IMfProductService
@ -80,9 +88,49 @@ public class MfProductServiceImpl extends BaseServiceImpl<MfProductMapper, MfPro
{
MfProduct mfProduct = MapstructUtils.convert(mfProductBo, MfProduct.class);
//获取祖级列表字段
Long parentId = mfProduct.getParentId();
if (parentId == 0) {
mfProduct.setAncestors("0");
} else {
MfProductVo parentMfProduct = selectById(mfProductBo.getParentId());
if (ObjectUtil.isNotNull(parentMfProduct)) {
mfProduct.setAncestors(parentMfProduct.getAncestors()+"," +parentId);
} else {
mfProduct.setAncestors("0");
}
}
return this.save(mfProduct);//使用全局配置的雪花算法主键生成器生成ID值
}
/**
* 新增产品树前台提供主键值一般用于导入的场合
*
* @param mfProductBo 产品树Bo
* @return 结果:true 操作成功false 操作失败
*/
@Override
public boolean insertWithPk(MfProductBo mfProductBo)
{
MfProduct mfProduct = MapstructUtils.convert(mfProductBo, MfProduct.class);
//获取祖级列表字段
Long parentId = mfProduct.getParentId();
if (parentId == 0) {
mfProduct.setAncestors("0");
} else {
MfProductVo parentMfProduct = selectById(mfProductBo.getParentId());
if (ObjectUtil.isNotNull(parentMfProduct)) {
mfProduct.setAncestors(parentMfProduct.getAncestors()+"," +parentId);
} else {
mfProduct.setAncestors("0");
}
}
return mfProductMapper.insertWithPk(mfProduct) > 0;//前台传来主键值
}
/**
* 修改产品树
*
@ -94,12 +142,46 @@ public class MfProductServiceImpl extends BaseServiceImpl<MfProductMapper, MfPro
{
MfProduct mfProduct = MapstructUtils.convert(mfProductBo, MfProduct.class);
if(ObjectUtil.isNotNull(mfProduct) && ObjectUtil.isNotNull(mfProduct.getProductId())) {
//更新祖级列表字段
MfProductVo newParentMfProduct = selectById(mfProduct.getParentId());
MfProductVo oldMfProduct = selectById(mfProduct.getProductId());
if ( ObjectUtil.isNotNull(newParentMfProduct) && ObjectUtil.isNotNull(oldMfProduct) ) {
String newAncestors = newParentMfProduct.getAncestors() + "," + newParentMfProduct.getProductId();
String oldAncestors = oldMfProduct.getAncestors();
mfProduct.setAncestors(newAncestors);
updateMfProductChildren(mfProduct.getProductId(), newAncestors, oldAncestors);
}
boolean updated = this.updateById(mfProduct);
return updated;
}
return false;
}
/**
* 修改子元素关系
*
* @param productId 主键ID
* @param newAncestors 新的父ID集合
* @param oldAncestors 旧的父ID集合
*/
@Transactional
public void updateMfProductChildren(Long productId, String newAncestors, String oldAncestors) {
QueryWrapper queryWrapper = QueryWrapper.create()
.from(MF_PRODUCT)
.where(QueryMethods.findInSet(QueryMethods.number(productId), MF_PRODUCT.ANCESTORS).gt(0));
List<MfProductVo> children = this.listAs(queryWrapper, MfProductVo.class);
for (MfProductVo child : children) {
child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
UpdateChain.of(MfProduct.class)
.set(MfProduct::getAncestors, child.getAncestors())
.where(MfProduct::getProductId).eq(child.getProductId())
.update();
}
}
/**
* 批量删除产品树
*

View File

@ -2,13 +2,18 @@ package com.ruoyi.mf.service.impl;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.orm.core.page.PageQuery;
import com.ruoyi.common.orm.core.page.TableDataInfo;
import com.ruoyi.common.orm.core.service.impl.BaseServiceImpl;
import com.ruoyi.common.core.utils.DateUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -23,7 +28,7 @@ import static com.ruoyi.mf.domain.table.MfStudentTableDef.MF_STUDENT;
* 学生信息表Service业务层处理
*
* @author 数据小王子
* 2024-01-05
* 2024-04-12
*/
@Service
public class MfStudentServiceImpl extends BaseServiceImpl<MfStudentMapper, MfStudent> implements IMfStudentService
@ -95,9 +100,25 @@ public class MfStudentServiceImpl extends BaseServiceImpl<MfStudentMapper, MfStu
{
MfStudent mfStudent = MapstructUtils.convert(mfStudentBo, MfStudent.class);
return this.save(mfStudent);//使用全局配置的雪花算法主键生成器生成ID值
}
/**
* 新增学生信息表前台提供主键值一般用于导入的场合
*
* @param mfStudentBo 学生信息表Bo
* @return 结果:true 操作成功false 操作失败
*/
@Override
public boolean insertWithPk(MfStudentBo mfStudentBo)
{
MfStudent mfStudent = MapstructUtils.convert(mfStudentBo, MfStudent.class);
return mfStudentMapper.insertWithPk(mfStudent) > 0;//前台传来主键值
}
/**
* 修改学生信息表
*
@ -115,6 +136,7 @@ public class MfStudentServiceImpl extends BaseServiceImpl<MfStudentMapper, MfStu
return false;
}
/**
* 批量删除学生信息表
*