From d39169d41038ed62d1f6a9b25513b247a1a8c02f Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Tue, 30 Aug 2022 16:48:57 +0800
Subject: [PATCH] fix

---
 src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/CmsTemplateServiceImpl.java |   58 +++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 45 insertions(+), 13 deletions(-)

diff --git a/src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/CmsTemplateServiceImpl.java b/src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/CmsTemplateServiceImpl.java
index cad593b..f08dbff 100644
--- a/src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/CmsTemplateServiceImpl.java
+++ b/src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/CmsTemplateServiceImpl.java
@@ -15,9 +15,12 @@
 import com.xcong.farmer.cms.modules.system.dto.AdminSaveTemplateInfoDto;
 import com.xcong.farmer.cms.modules.system.dto.TemplateListDto;
 import com.xcong.farmer.cms.modules.system.entity.CmsTemplateEntity;
+import com.xcong.farmer.cms.modules.system.entity.CompanyEntity;
 import com.xcong.farmer.cms.modules.system.mapper.CmsTemplateMapper;
+import com.xcong.farmer.cms.modules.system.mapper.CompanyMapper;
 import com.xcong.farmer.cms.modules.system.service.ICmsTemplateService;
 import com.xcong.farmer.cms.modules.system.util.LoginUserUtil;
+import lombok.extern.slf4j.Slf4j;
 import org.jsoup.Jsoup;
 import org.jsoup.nodes.Document;
 import org.jsoup.nodes.Element;
@@ -28,6 +31,7 @@
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
@@ -40,11 +44,16 @@
  * @author wzy
  * @date 2022-07-04
  **/
+@Slf4j
 @Service
 public class CmsTemplateServiceImpl extends ServiceImpl<CmsTemplateMapper, CmsTemplateEntity> implements ICmsTemplateService {
 
     @Autowired
     private CmsProperties properties;
+
+    @Autowired
+    private CompanyMapper companyMapper;
+
 
     private List<String> fileSuffix = Arrays.asList(".zip", ".html");
 
@@ -53,6 +62,11 @@
         String templatePath = properties.getTemplatePath();
         String staticPath = properties.getStaticPath();
         Long companyId = LoginUserUtil.getCompanyId();
+        CompanyEntity company = companyMapper.selectById(companyId);
+
+        String companyCode = company.getCode();
+        templatePath = FileUtils.path(templatePath, companyCode);
+        staticPath = FileUtils.path(staticPath, companyCode);
 
         String filename = upload.getOriginalFilename();
         String suffix = filename.substring(filename.lastIndexOf("."));
@@ -91,14 +105,14 @@
                         continue;
                     }
 
-                    insertTemplate(templateFile, companyId);
+                    insertTemplate(templateFile, companyId, companyCode);
                 }
             }
 
             if (".html".equals(suffix)) {
                 FileUtil.touch(file);
 
-                insertTemplate(file, companyId);
+                insertTemplate(file, companyId, companyCode);
             }
         } catch (IOException e) {
             e.printStackTrace();
@@ -106,13 +120,13 @@
         }
     }
 
-    private void insertTemplate(File file, Long companyId) throws IOException {
+    private void insertTemplate(File file, Long companyId, String companyCode) throws IOException {
         Document parse = Jsoup.parse(file, null);
         String attr = parse.head().attr("name");
 
-        staticPathParser(parse, "img", "src");
-        staticPathParser(parse, "link", "href");
-        staticPathParser(parse, "script", "src");
+        staticPathParser(parse, "img", "src", companyCode);
+        staticPathParser(parse, "link", "href", companyCode);
+        staticPathParser(parse, "script", "src", companyCode);
 
         FileOutputStream outputStream = new FileOutputStream(file);
         outputStream.write(parse.html().getBytes());
@@ -133,7 +147,7 @@
         this.baseMapper.insert(cmsTemplate);
     }
 
-    private void staticPathParser(Document document, String tagName, String attrKey) {
+    private void staticPathParser(Document document, String tagName, String attrKey, String companyCode) {
         Elements elements = document.getElementsByTag(tagName);
         if (elements.isEmpty()) {
             return;
@@ -142,7 +156,7 @@
         for (Element element : elements) {
             String attr = element.attr(attrKey);
             if (StrUtil.isNotBlank(attr) && !attr.contains("http://") && !attr.contains("https://")) {
-                element.attr(attrKey, cmsProperties.getStaticPath() + attr);
+                element.attr(attrKey, cmsProperties.getStaticUrl() + companyCode + "/" + attr);
             }
         }
     }
@@ -167,7 +181,7 @@
     public Result dropdownList() {
         Long companyId = LoginUserUtil.getCompanyId();
         QueryWrapper<CmsTemplateEntity> objectQueryWrapper = new QueryWrapper<>();
-        objectQueryWrapper.eq("company_id",companyId);
+        objectQueryWrapper.eq("company_id", companyId);
         List<CmsTemplateEntity> cmsTemplateEntities = this.baseMapper.selectList(objectQueryWrapper);
         return Result.ok(cmsTemplateEntities);
     }
@@ -178,8 +192,10 @@
     @Override
     public Result viewTemplateInfo(Long id) {
         Result result = new Result();
+
+        CompanyEntity companyEntity = companyMapper.selectById(LoginUserUtil.getCompanyId());
         CmsTemplateEntity cmsTemplateEntity = this.baseMapper.selectById(id);
-        String templatePath = cmsProperties.getTemplatePath();
+        String templatePath = FileUtils.path(cmsProperties.getTemplatePath(), companyEntity.getCode());
         String pathName = FileUtils.path(templatePath, cmsTemplateEntity.getPath());
 
         byte[] bytes = new byte[0];
@@ -200,14 +216,17 @@
     public Result saveTemplateInfo(AdminSaveTemplateInfoDto adminSaveTemplateInfoDto) {
         Long companyId = LoginUserUtil.getCompanyId();
         Long id = adminSaveTemplateInfoDto.getId();
-        CmsTemplateEntity cmsTemplateEntity = this.baseMapper.selectByIdAndCompanyId(id,companyId);
+
+        CompanyEntity company = this.companyMapper.selectById(companyId);
+        CmsTemplateEntity cmsTemplateEntity = this.baseMapper.selectByIdAndCompanyId(id, companyId);
         String name = cmsTemplateEntity.getName();
         Integer type = cmsTemplateEntity.getType();
-        String templatePath = cmsProperties.getTemplatePath();
+        String templatePath = FileUtils.path(cmsProperties.getTemplatePath(), company.getCode());
         String path = cmsTemplateEntity.getPath();
 
-        this.baseMapper.delete(id,companyId);
+        this.baseMapper.delete(id, companyId);
         String pathNew = FileUtils.path(templatePath, path);
+        log.info("模板写入地址:{}", pathNew);
         File file = new File(pathNew);
         FileUtil.touch(file);
 
@@ -225,4 +244,17 @@
         return Result.ok("保存成功");
     }
 
+    @Override
+    public Result downloadTemplate() {
+        Long companyId = LoginUserUtil.getCompanyId();
+        CompanyEntity company = this.companyMapper.selectById(companyId);
+
+        String templatePath = FileUtils.path(cmsProperties.getTemplatePath(), company.getCode());
+        String staticPath = FileUtils.path(cmsProperties.getStaticPath(), company.getCode());
+
+        String fileName = "template.zip";
+        ZipUtil.zip(templatePath, staticPath + "/template.zip", true);
+
+        return Result.ok("success", cmsProperties.getStaticUrl() + fileName);
+    }
 }

--
Gitblit v1.9.1