Helius
2022-07-04 c305b09fbd20849a39631ddd43980cb09e1805e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.xcong.farmer.cms.modules.system.service.Impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcong.farmer.cms.common.exception.GlobalException;
import com.xcong.farmer.cms.common.utils.FileUtils;
import com.xcong.farmer.cms.configurations.GlobalExceptionHandler;
import com.xcong.farmer.cms.core.template.TemplateConfiguration;
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.mapper.CmsTemplateMapper;
import com.xcong.farmer.cms.modules.system.service.ICmsTemplateService;
import com.xcong.farmer.cms.modules.system.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
/**
 * @author wzy
 * @date 2022-07-04
 **/
@Service
public class CmsTemplateServiceImpl extends ServiceImpl<CmsTemplateMapper, CmsTemplateEntity> implements ICmsTemplateService {
 
    @Autowired
    private TemplateConfiguration cfg;
 
    private List<String> fileSuffix = Arrays.asList(".zip", ".html");
 
    @Override
    public void updateTemplate(MultipartFile upload) {
        String templatePath = cfg.templatePath;
        String staticPath = cfg.staticPath;
 
        String filename = upload.getOriginalFilename();
        String suffix = filename.substring(filename.lastIndexOf("."));
 
        if (!fileSuffix.contains(suffix)) {
            throw new GlobalException("请上传正确格式文件");
        }
 
        try {
            if (".zip".equals(suffix)) {
                String path = FileUtils.path(templatePath, filename);
                System.out.println(path);
                File file = new File(path);
                upload.transferTo(file);
                FileUtils.zipUpload(file, templatePath, staticPath);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(suffix);
    }
 
    @Override
    public IPage<CmsTemplateEntity> findInPage(TemplateListDto templateListDto) {
        Long companyId = LoginUserUtil.getCompanyId();
        IPage<CmsTemplateEntity> page = new Page<>(templateListDto.getPageNum(), templateListDto.getPageSize());
 
        CmsTemplateEntity template = new CmsTemplateEntity();
        template.setCompanyId(companyId);
        return this.baseMapper.selectInPage(page, template);
    }
 
    @Override
    public void delete(Long id) {
        Long companyId = LoginUserUtil.getCompanyId();
        this.baseMapper.delete(id, companyId);
    }
}