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> list(ManageLicense dataScope, Query query) { IPage 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); } }