feat(cms): 添加应用参数Excel批量导入功能- 新增CmsWebsiteFieldImportParam类用于Excel数据映射
- 实现基于EasyPOI的Excel导入解析逻辑 - 添加文件上传接口支持批量导入应用参数 - 集成数据校验和默认值设置机制- 添加权限控制和事务管理确保数据一致性- 提供导入成功/失败的结果反馈机制
This commit is contained in:
@@ -1,15 +1,23 @@
|
|||||||
package com.gxwebsoft.cms.controller;
|
package com.gxwebsoft.cms.controller;
|
||||||
|
|
||||||
|
import cn.afterturn.easypoi.excel.ExcelImportUtil;
|
||||||
|
import cn.afterturn.easypoi.excel.entity.ImportParams;
|
||||||
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import com.gxwebsoft.common.core.web.BaseController;
|
import com.gxwebsoft.common.core.web.BaseController;
|
||||||
import com.gxwebsoft.cms.service.CmsWebsiteFieldService;
|
import com.gxwebsoft.cms.service.CmsWebsiteFieldService;
|
||||||
import com.gxwebsoft.cms.entity.CmsWebsiteField;
|
import com.gxwebsoft.cms.entity.CmsWebsiteField;
|
||||||
import com.gxwebsoft.cms.param.CmsWebsiteFieldParam;
|
import com.gxwebsoft.cms.param.CmsWebsiteFieldParam;
|
||||||
|
import com.gxwebsoft.cms.param.CmsWebsiteFieldImportParam;
|
||||||
|
import com.gxwebsoft.common.core.utils.JSONUtil;
|
||||||
import com.gxwebsoft.common.core.web.ApiResult;
|
import com.gxwebsoft.common.core.web.ApiResult;
|
||||||
import com.gxwebsoft.common.core.web.PageResult;
|
import com.gxwebsoft.common.core.web.PageResult;
|
||||||
import com.gxwebsoft.common.core.web.BatchParam;
|
import com.gxwebsoft.common.core.web.BatchParam;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -115,4 +123,39 @@ public class CmsWebsiteFieldController extends BaseController {
|
|||||||
});
|
});
|
||||||
return success(config);
|
return success(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* excel批量导入应用参数
|
||||||
|
*/
|
||||||
|
@PreAuthorize("hasAuthority('cms:cmsWebsiteField:save')")
|
||||||
|
@Operation(summary = "批量导入应用参数")
|
||||||
|
@Transactional(rollbackFor = {Exception.class})
|
||||||
|
@PostMapping("/import")
|
||||||
|
public ApiResult<List<String>> importBatch(MultipartFile file) {
|
||||||
|
ImportParams importParams = new ImportParams();
|
||||||
|
try {
|
||||||
|
List<CmsWebsiteFieldImportParam> list = ExcelImportUtil.importExcel(file.getInputStream(), CmsWebsiteFieldImportParam.class, importParams);
|
||||||
|
list.forEach(d -> {
|
||||||
|
CmsWebsiteField item = JSONUtil.parseObject(JSONUtil.toJSONString(d), CmsWebsiteField.class);
|
||||||
|
assert item != null;
|
||||||
|
if (ObjectUtil.isNotEmpty(item)) {
|
||||||
|
// 设置默认值
|
||||||
|
if (item.getDeleted() == null) {
|
||||||
|
item.setDeleted(0);
|
||||||
|
}
|
||||||
|
if (item.getSortNumber() == null) {
|
||||||
|
item.setSortNumber(0);
|
||||||
|
}
|
||||||
|
if (item.getEncrypted() == null) {
|
||||||
|
item.setEncrypted(false);
|
||||||
|
}
|
||||||
|
cmsWebsiteFieldService.save(item);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return success("成功导入" + list.size() + "条", null);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return fail("导入失败", null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package com.gxwebsoft.cms.param;
|
||||||
|
|
||||||
|
import cn.afterturn.easypoi.excel.annotation.Excel;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 应用参数导入参数
|
||||||
|
*
|
||||||
|
* @author 科技小王子
|
||||||
|
* @since 2024-09-10 20:36:14
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class CmsWebsiteFieldImportParam implements Serializable {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@Excel(name = "自增ID")
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
@Excel(name = "类型,0文本 1图片 2其他")
|
||||||
|
private Integer type;
|
||||||
|
|
||||||
|
@Excel(name = "名称")
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@Excel(name = "默认值")
|
||||||
|
private String defaultValue;
|
||||||
|
|
||||||
|
@Excel(name = "可修改的值 [on|off]")
|
||||||
|
private String modifyRange;
|
||||||
|
|
||||||
|
@Excel(name = "备注")
|
||||||
|
private String comments;
|
||||||
|
|
||||||
|
@Excel(name = "css样式")
|
||||||
|
private String style;
|
||||||
|
|
||||||
|
@Excel(name = "值")
|
||||||
|
private String value;
|
||||||
|
|
||||||
|
@Excel(name = "国际化语言")
|
||||||
|
private String lang;
|
||||||
|
|
||||||
|
@Excel(name = "是否加密")
|
||||||
|
private Boolean encrypted;
|
||||||
|
|
||||||
|
@Excel(name = "商户ID")
|
||||||
|
private Long merchantId;
|
||||||
|
|
||||||
|
@Excel(name = "排序")
|
||||||
|
private Integer sortNumber;
|
||||||
|
|
||||||
|
@Excel(name = "租户ID")
|
||||||
|
private Integer tenantId;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user