吉安感知网项目-后端
xiebin
2026-01-06 d207a86cdf1ab52ef8cb7cd83bad8fceab8038cf
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package org.sxkj.system.controller;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
 
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.sxkj.common.model.ResponseResult;
import org.sxkj.common.utils.ValidUtil;
import org.sxkj.common.utils.license.LicenseUtils;
import org.sxkj.system.entity.ManageLicense;
import org.sxkj.system.service.IManageLicenseService;
 
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Date;
import java.util.Objects;
 
@NonDS
@RestController
@AllArgsConstructor
@RequestMapping("/license")
@Api(value = "license", tags = "license")
public class ManageLicenseController extends BladeController {
    @Autowired
    private IManageLicenseService manageLicenseService;
 
    private static String licensePath;
 
    @Value("${licensePath}")
    private void setIssuer(String licensePath) {
        this.licensePath = licensePath;
    }
 
 
    /**
     * 创建createLicense
     *
     * @param manageLicense
     * @return
     */
    @PostMapping("/createLicense")
    public R createLicense(@RequestBody ManageLicense manageLicense) throws IOException {
        paramCheck(manageLicense);
        manageLicense.setCreateTime(new Date());
        manageLicenseService.createLicense(manageLicense);
 
        return R.success("");
    }
 
    public void paramCheck(ManageLicense manageLicense) {
        Integer licenseType = manageLicense.getLicenseType();
        ValidUtil.assertNull(licenseType, "licenseType 参数不能为空");
        if (ManageLicense.CHECK_EXPIRE_TIME.equals(licenseType)) {
            ValidUtil.assertNull(manageLicense.getExpireDay(), "过期时间不能为空");
            ValidUtil.assertIf(manageLicense.getExpireDay().before(new Date()), "不能设置当日及之前的时间!");
        }
        Boolean checkMachineCode = manageLicense.getCheckMachineCode();
        if (Objects.nonNull(checkMachineCode) && checkMachineCode) {
            ValidUtil.assertIf(StringUtils.isEmpty(manageLicense.getMachineCode()), "确认需要校验机器码,机器码不能为空!");
            ValidUtil.assertIf(manageLicense.getMachineCode().length() > 500, "机器码太多了!");
        }
        ValidUtil.assertIf(StringUtils.isEmpty(manageLicense.getLicenseName()), "机构名称不能为空!");
        ValidUtil.assertIf(manageLicense.getLicenseName().length() > 200, "机构名过长!");
    }
 
    /**
     * 分页
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页")
    public R<IPage<ManageLicense>> list(ManageLicense dataScope, Query query) {
        IPage<ManageLicense> page = manageLicenseService.page(Condition.getPage(query),
            Condition.getQueryWrapper(dataScope).orderByDesc("create_time"));
        return R.data(page);
    }
 
    @PostMapping(value = "uploadLicense", consumes = "multipart/form-data")
    public R saveAttachFile(@RequestPart MultipartFile file) throws IOException {
        ValidUtil.assertNull(file, "文件不能为空!");
        ValidUtil.assertIf(StringUtils.isEmpty(licensePath), "licensePath 不能为空!");
        // Create the target file object
        File targetFile = new File(licensePath);
 
        // Ensure parent directory exists
        File parent = targetFile.getParentFile();
        if (parent != null && !parent.exists()) {
            parent.mkdirs();
        }
 
        // Save the file
        try (InputStream inputStream = file.getInputStream()) {
            Files.copy(inputStream, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
        }
 
        return R.success("");
    }
 
 
    /**
     * 获取过期时间
     * @return
     */
    @GetMapping("/findLicenseDate")
    @SneakyThrows
    public ResponseResult findLicenseDate() {
        Date licenseExipreDate = LicenseUtils.getLicenseExipreDate(licensePath);
        return ResponseResult.success(licenseExipreDate);
    }
}