ludc
2023-03-16 86b6157299a50579f454e4fb45a09ff21d252dab
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.core.report.provider;
 
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bstek.ureport.provider.report.ReportFile;
import com.bstek.ureport.provider.report.ReportProvider;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.report.entity.ReportFileEntity;
import org.springblade.core.report.props.ReportDatabaseProperties;
import org.springblade.core.report.service.IReportFileService;
 
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * 数据库文件处理
 *
 * @author Chill
 */
@AllArgsConstructor
public class DatabaseProvider implements ReportProvider {
 
    private final ReportDatabaseProperties properties;
    private final IReportFileService service;
 
    @Override
    public InputStream loadReport(String file) {
        ReportFileEntity reportFileEntity = service.getOne(Wrappers.<ReportFileEntity>lambdaQuery().eq(ReportFileEntity::getName, getFileName(file)));
        byte[] content = reportFileEntity.getContent();
        return new ByteArrayInputStream(content);
    }
 
    @Override
    public void deleteReport(String file) {
        service.remove(Wrappers.<ReportFileEntity>lambdaUpdate().eq(ReportFileEntity::getName, getFileName(file)));
    }
 
    @Override
    public List<ReportFile> getReportFiles() {
        List<ReportFileEntity> list = service.list();
        List<ReportFile> reportFiles = new ArrayList<>();
        list.forEach(reportFileEntity -> reportFiles.add(new ReportFile(reportFileEntity.getName(), reportFileEntity.getUpdateTime())));
        return reportFiles;
    }
 
    @Override
    public void saveReport(String file, String content) {
        String fileName = getFileName(file);
        ReportFileEntity reportFileEntity = service.getOne(Wrappers.<ReportFileEntity>lambdaQuery().eq(ReportFileEntity::getName, fileName));
        Date now = DateUtil.now();
        if (reportFileEntity == null) {
            reportFileEntity = new ReportFileEntity();
            reportFileEntity.setName(fileName);
            reportFileEntity.setContent(content.getBytes());
            reportFileEntity.setCreateTime(now);
            reportFileEntity.setIsDeleted(BladeConstant.DB_NOT_DELETED);
        } else {
            reportFileEntity.setContent(content.getBytes());
        }
        reportFileEntity.setUpdateTime(now);
        service.saveOrUpdate(reportFileEntity);
    }
 
    @Override
    public String getName() {
        return properties.getName();
    }
 
    @Override
    public boolean disabled() {
        return properties.isDisabled();
    }
 
    @Override
    public String getPrefix() {
        return properties.getPrefix();
    }
 
    /**
     * 获取标准格式文件名
     *
     * @param name 原文件名
     */
    private String getFileName(String name) {
        if (name.startsWith(getPrefix())) {
            name = name.substring(getPrefix().length());
        }
        return name;
    }
 
}