diff --git a/src/main/java/com/gxwebsoft/cms/controller/CmsWebsiteFieldController.java b/src/main/java/com/gxwebsoft/cms/controller/CmsWebsiteFieldController.java index a121ef6..dc1c4a4 100644 --- a/src/main/java/com/gxwebsoft/cms/controller/CmsWebsiteFieldController.java +++ b/src/main/java/com/gxwebsoft/cms/controller/CmsWebsiteFieldController.java @@ -1,15 +1,23 @@ 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.cms.service.CmsWebsiteFieldService; import com.gxwebsoft.cms.entity.CmsWebsiteField; 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.PageResult; import com.gxwebsoft.common.core.web.BatchParam; import io.swagger.v3.oas.annotations.tags.Tag; 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.multipart.MultipartFile; import javax.annotation.Resource; import java.util.HashMap; @@ -115,4 +123,39 @@ public class CmsWebsiteFieldController extends BaseController { }); return success(config); } + + /** + * excel批量导入应用参数 + */ + @PreAuthorize("hasAuthority('cms:cmsWebsiteField:save')") + @Operation(summary = "批量导入应用参数") + @Transactional(rollbackFor = {Exception.class}) + @PostMapping("/import") + public ApiResult> importBatch(MultipartFile file) { + ImportParams importParams = new ImportParams(); + try { + List 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); + } } diff --git a/src/main/java/com/gxwebsoft/cms/param/CmsWebsiteFieldImportParam.java b/src/main/java/com/gxwebsoft/cms/param/CmsWebsiteFieldImportParam.java new file mode 100644 index 0000000..b24632c --- /dev/null +++ b/src/main/java/com/gxwebsoft/cms/param/CmsWebsiteFieldImportParam.java @@ -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; +} \ No newline at end of file