feat: 定时任务生成lic

This commit is contained in:
caiyuchao
2025-07-28 16:02:59 +08:00
parent 824ba341a2
commit 19ebf11ae9
8 changed files with 263 additions and 58 deletions

View File

@@ -12,7 +12,8 @@ public enum LicenseStatusEnum {
NOT_APPLIED(0, "未申请"),
IN_APPLICATION(1, "申请中"),
COMPLETED(2, "已完成"); // CRM 系统专用
GENERATING(2, "生成中"),
COMPLETED(3, "已完成"); // CRM 系统专用
;
/**

View File

@@ -2,6 +2,7 @@ package org.agt.module.license;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* @description: 项目的启动类
@@ -9,6 +10,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
* @since: 2025-05-21
*/
@SpringBootApplication
@EnableScheduling
public class AgtLicenseApplication {
public static void main(String[] args) {

View File

@@ -62,7 +62,7 @@ public class LicenseController {
@Operation(summary = "申请License")
@PreAuthorize("@ss.hasPermission('license:license:apply')")
public CommonResult<Boolean> applyLicense(@Valid @RequestBody LicenseSaveReqVO updateReqVO) {
licenseService.applyLicense(updateReqVO);
licenseService.applyLicense(updateReqVO, null);
return success(true);
}

View File

@@ -0,0 +1,63 @@
package org.agt.module.license.dal.dataobject.license;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.agt.framework.mybatis.core.dataobject.BaseDO;
import java.util.List;
/**
* LicenseDetail DO
*
* @author 管理员
*/
@TableName(value = "crm_license_detail", autoResultMap = true)
@KeySequence("crm_license_detail_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class LicenseDetailDO extends BaseDO {
/**
* 主键
*/
@TableId
private Long id;
/**
* 客户ID
*/
private Long licenseId;
/**
* 网元开关
*
* 枚举 {@link TODO lic_ne_switch 对应的类}
*/
@TableField(typeHandler = JacksonTypeHandler.class)
private List<Integer> neList;
/**
* 激活码
*/
private String activationCode;
/**
* 文件URL
*/
private String fileUrl;
/**
* 提供者ID
*/
private Long providerId;
}

View File

@@ -0,0 +1,16 @@
package org.agt.module.license.dal.mysql.license;
import org.agt.framework.mybatis.core.mapper.BaseMapperX;
import org.agt.module.license.dal.dataobject.license.LicenseDetailDO;
import org.apache.ibatis.annotations.Mapper;
/**
* CRM-License Mapper
*
* @author super
*/
@Mapper
public interface LicenseDetailMapper extends BaseMapperX<LicenseDetailDO> {
}

View File

@@ -5,6 +5,9 @@ import org.agt.framework.common.pojo.PageResult;
import org.agt.module.license.controller.admin.license.vo.LicensePageReqVO;
import org.agt.module.license.controller.admin.license.vo.LicenseSaveReqVO;
import org.agt.module.license.dal.dataobject.license.LicenseDO;
import org.agt.module.license.dal.dataobject.license.LicenseDetailDO;
import java.util.List;
/**
* License Service 接口
@@ -33,7 +36,7 @@ public interface LicenseService {
*
* @param updateReqVO 更新信息
*/
void applyLicense(@Valid LicenseSaveReqVO updateReqVO);
void applyLicense(@Valid LicenseSaveReqVO updateReqVO, List<LicenseDetailDO> licenseDetails);
/**
* 生成License

View File

@@ -3,6 +3,7 @@ package org.agt.module.license.service.license;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.agt.framework.common.pojo.PageResult;
@@ -15,9 +16,11 @@ import org.agt.module.license.controller.admin.license.vo.LicensePageReqVO;
import org.agt.module.license.controller.admin.license.vo.LicenseSaveReqVO;
import org.agt.module.license.dal.dataobject.customer.CustomerDO;
import org.agt.module.license.dal.dataobject.license.LicenseDO;
import org.agt.module.license.dal.dataobject.license.LicenseDetailDO;
import org.agt.module.license.dal.dataobject.license.LicenseProviderDO;
import org.agt.module.license.dal.dataobject.project.ProjectDO;
import org.agt.module.license.dal.mysql.customer.CustomerMapper;
import org.agt.module.license.dal.mysql.license.LicenseDetailMapper;
import org.agt.module.license.dal.mysql.license.LicenseMapper;
import org.agt.module.license.dal.mysql.license.LicenseProviderMapper;
import org.agt.module.license.dal.mysql.project.ProjectMapper;
@@ -71,6 +74,9 @@ public class LicenseServiceImpl implements LicenseService {
@Resource
private NotifyMessageSendApi notifySendApi;
@Resource
private LicenseDetailMapper licenseDetailMapper;
@Override
public Long createLicense(LicenseSaveReqVO createReqVO) {
// 校验项目Sn是否唯一
@@ -82,8 +88,16 @@ public class LicenseServiceImpl implements LicenseService {
license.setStatus(LicenseStatusEnum.NOT_APPLIED.getCode());
licenseMapper.insert(license);
List<LicenseNeCodeVO> neCodeList = createReqVO.getNeCodeList();
List<LicenseDetailDO> licenseDetails = BeanUtils.toBean(neCodeList, LicenseDetailDO.class);
for (LicenseDetailDO licenseDetail : licenseDetails) {
licenseDetail.setLicenseId(license.getId());
}
licenseDetailMapper.insertBatch(licenseDetails);
createReqVO.setId(license.getId());
applyLicense(createReqVO);
applyLicense(createReqVO, licenseDetails);
// 返回
return license.getId();
}
@@ -102,7 +116,7 @@ public class LicenseServiceImpl implements LicenseService {
}
@Override
public void applyLicense(LicenseSaveReqVO updateReqVO) {
public void applyLicense(LicenseSaveReqVO updateReqVO, List<LicenseDetailDO> licenseDetails) {
// 发送邮件
// 1. 准备参数
Long userId = updateReqVO.getApprover();
@@ -123,9 +137,9 @@ public class LicenseServiceImpl implements LicenseService {
notifySendApi.sendSingleMessageToAdmin(new NotifySendSingleToUserReqDTO()
.setUserId(userId).setTemplateCode(templateCode).setTemplateParams(templateParams));
List<LicenseNeCodeVO> neCodeList = updateReqVO.getNeCodeList();
// List<LicenseNeCodeVO> neCodeList = updateReqVO.getNeCodeList();
for (LicenseNeCodeVO neCodeVO : neCodeList) {
for (LicenseDetailDO neCodeVO : licenseDetails) {
// 添加数据到License提供者表
LicenseProviderDO licenseProviderDO = new LicenseProviderDO();
licenseProviderDO.setCustomerId(updateReqVO.getCustomerId());
@@ -162,12 +176,13 @@ public class LicenseServiceImpl implements LicenseService {
neCodeVO.setProviderId(licenseProviderDO.getId());
}
licenseDetailMapper.updateBatch(licenseDetails);
// 更新表
updateReqVO.setStatus(LicenseStatusEnum.IN_APPLICATION.getCode());
updateReqVO.setApplicant(WebFrameworkUtils.getLoginUserId());
updateReqVO.setApplicationTime(LocalDateTime.now());
updateReqVO.setNeCodeList(neCodeList);
// updateReqVO.setNeCodeList(neCodeList);
updateLicense(updateReqVO);
}
@@ -175,74 +190,147 @@ public class LicenseServiceImpl implements LicenseService {
public String generate(Long id) {
LicenseDO licenseDO = licenseMapper.selectById(id);
List<LicenseNeCodeVO> neCodeList = licenseDO.getNeCodeList();
for (LicenseNeCodeVO neCodeVO : neCodeList) {
// List<LicenseNeCodeVO> neCodeList = licenseDO.getNeCodeList();
List<LicenseDetailDO> licenseDetailDOS = licenseDetailMapper.selectList(Wrappers.<LicenseDetailDO>lambdaQuery().eq(LicenseDetailDO::getLicenseId, licenseDO.getId()));
// List<LicenseNeCodeVO> neCodeList = BeanUtils.toBean(licenseDetailDOS, LicenseNeCodeVO.class);
for (LicenseDetailDO neCodeVO : licenseDetailDOS) {
LicenseProviderDO licenseProviderDO = licenseProviderMapper.selectById(neCodeVO.getProviderId());
licenseProviderDO.setState(2);
licenseProviderMapper.updateById(licenseProviderDO);
}
try {
System.out.println("线程开始睡眠...");
Thread.sleep(8000); // 暂停2000毫秒即2秒
System.out.println("线程醒来!");
} catch (InterruptedException e) {
log.error("睡眠失败");
}
// try {
// System.out.println("线程开始睡眠...");
// Thread.sleep(8000); // 暂停2000毫秒即2秒
// System.out.println("线程醒来!");
// } catch (InterruptedException e) {
// log.error("睡眠失败");
// }
//
// for (LicenseDetailDO neCodeVO : licenseDetailDOS) {
// LicenseProviderDO licenseProviderDO = licenseProviderMapper.selectById(neCodeVO.getProviderId());
// if (licenseProviderDO.getState() != 3) {
//// continue;
// }
// String newFilePath1 = licenseProviderDO.getNewFilePath1();
//
// List<Integer> neList = neCodeVO.getNeList();
// List<String> neLabels = new ArrayList<>();
// for (Integer ne : neList) {
// String label = DictFrameworkUtils.parseDictDataLabel("lic_ne_all", ne.toString());
// if (StrUtil.isBlank(label)) {
// label = DictFrameworkUtils.parseDictDataLabel("lic_ne_5g", ne.toString());
// if (StrUtil.isBlank(label)) {
// label = DictFrameworkUtils.parseDictDataLabel("lic_ne_4g", ne.toString());
// if (StrUtil.isBlank(label)) {
// label = DictFrameworkUtils.parseDictDataLabel("lic_ne_23g", ne.toString());
// if (StrUtil.isBlank(label)) {
// label = DictFrameworkUtils.parseDictDataLabel("lic_ne_add", ne.toString());
// }
// }
// }
// }
// neLabels.add(label);
// }
// String neListStr = StrUtil.join("_", neLabels);
// if (StrUtil.isNotBlank(neListStr)) {
// neListStr = neListStr + "_";
// }
// String fileName = neListStr + licenseDO.getSerialNo() + "_" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "_system.ini";
//
// // 上传到文件服务
// byte[] fileContent = null;
// try {
// log.info("下载License文件路径为{}", newFilePath1);
// fileContent = FileUtil.readBytes(newFilePath1);
// } catch (Exception e) {
// log.error("读取文件失败", e);
// }
// if (fileContent == null) {
// log.error("读取文件失败为空");
// fileContent = ResourceUtil.readBytes("file/MME_13750602_2024-08-02_system.ini");
// }
//
// String fileURL = fileApi.createFile(fileContent, fileName);
//
// neCodeVO.setFileUrl(fileURL);
// }
//
// licenseDetailMapper.updateBatch(licenseDetailDOS);
// licenseDO.setNeCodeList(neCodeList);
licenseDO.setStatus(LicenseStatusEnum.GENERATING.getCode());
licenseMapper.updateById(licenseDO);
return "";
}
for (LicenseNeCodeVO neCodeVO : neCodeList) {
LicenseProviderDO licenseProviderDO = licenseProviderMapper.selectById(neCodeVO.getProviderId());
if (licenseProviderDO.getState() != 3) {
// continue;
}
String newFilePath1 = licenseProviderDO.getNewFilePath1();
public void genLicenseTask() {
List<LicenseDO> licenses = licenseMapper.selectList(Wrappers.<LicenseDO>lambdaQuery().eq(LicenseDO::getStatus, LicenseStatusEnum.GENERATING.getCode()));
for (LicenseDO licenseDO : licenses) {
List<LicenseDetailDO> licenseDetails = licenseDetailMapper.selectList(Wrappers.<LicenseDetailDO>lambdaQuery().eq(LicenseDetailDO::getLicenseId, licenseDO.getId()));
List<Integer> neList = neCodeVO.getNeList();
List<String> neLabels = new ArrayList<>();
for (Integer ne : neList) {
String label = DictFrameworkUtils.parseDictDataLabel("lic_ne_all", ne.toString());
if (StrUtil.isBlank(label)) {
label = DictFrameworkUtils.parseDictDataLabel("lic_ne_5g", ne.toString());
for (LicenseDetailDO detail : licenseDetails) {
if (StrUtil.isNotBlank(detail.getFileUrl())) {
continue;
}
LicenseProviderDO licenseProviderDO = licenseProviderMapper.selectById(detail.getProviderId());
if (licenseProviderDO.getState() != 3) {
continue;
}
String newFilePath1 = licenseProviderDO.getNewFilePath1();
List<Integer> neList = detail.getNeList();
List<String> neLabels = new ArrayList<>();
for (Integer ne : neList) {
String label = DictFrameworkUtils.parseDictDataLabel("lic_ne_all", ne.toString());
if (StrUtil.isBlank(label)) {
label = DictFrameworkUtils.parseDictDataLabel("lic_ne_4g", ne.toString());
label = DictFrameworkUtils.parseDictDataLabel("lic_ne_5g", ne.toString());
if (StrUtil.isBlank(label)) {
label = DictFrameworkUtils.parseDictDataLabel("lic_ne_23g", ne.toString());
label = DictFrameworkUtils.parseDictDataLabel("lic_ne_4g", ne.toString());
if (StrUtil.isBlank(label)) {
label = DictFrameworkUtils.parseDictDataLabel("lic_ne_add", ne.toString());
label = DictFrameworkUtils.parseDictDataLabel("lic_ne_23g", ne.toString());
if (StrUtil.isBlank(label)) {
label = DictFrameworkUtils.parseDictDataLabel("lic_ne_add", ne.toString());
}
}
}
}
neLabels.add(label);
}
neLabels.add(label);
}
String neListStr = StrUtil.join("_", neLabels);
if (StrUtil.isNotBlank(neListStr)) {
neListStr = neListStr + "_";
}
String fileName = neListStr + licenseDO.getSerialNo() + "_" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "_system.ini";
String neListStr = StrUtil.join("_", neLabels);
if (StrUtil.isNotBlank(neListStr)) {
neListStr = neListStr + "_";
}
String fileName = neListStr + licenseDO.getSerialNo() + "_" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "_system.ini";
// 上传到文件服务
byte[] fileContent = null;
try {
log.info("下载License文件路径为{}", newFilePath1);
fileContent = FileUtil.readBytes(newFilePath1);
} catch (Exception e) {
log.error("读取文件失败", e);
}
if (fileContent == null) {
log.error("读取文件失败为空");
fileContent = ResourceUtil.readBytes("file/MME_13750602_2024-08-02_system.ini");
// 上传到文件服务
byte[] fileContent = null;
try {
log.info("下载License文件路径为{}", newFilePath1);
fileContent = FileUtil.readBytes(newFilePath1);
} catch (Exception e) {
log.error("读取文件失败", e);
}
if (fileContent == null) {
log.error("读取文件失败为空");
fileContent = ResourceUtil.readBytes("file/MME_13750602_2024-08-02_system.ini");
}
String fileURL = fileApi.createFile(fileContent, fileName);
detail.setFileUrl(fileURL);
licenseDetailMapper.updateById(detail);
}
String fileURL = fileApi.createFile(fileContent, fileName);
boolean isCompleted = licenseDetails.stream().allMatch(c -> StrUtil.isNotBlank(c.getFileUrl()));
if (isCompleted) {
licenseDO.setStatus(LicenseStatusEnum.COMPLETED.getCode());
licenseMapper.updateById(licenseDO);
}
neCodeVO.setFileUrl(fileURL);
}
licenseDO.setNeCodeList(neCodeList);
licenseDO.setStatus(LicenseStatusEnum.COMPLETED.getCode());
licenseMapper.updateById(licenseDO);
return "";
}
@Override
@@ -261,12 +349,22 @@ public class LicenseServiceImpl implements LicenseService {
@Override
public LicenseDO getLicense(Long id) {
return licenseMapper.selectById(id);
LicenseDO licenseDO = licenseMapper.selectById(id);
List<LicenseDetailDO> licenseDetailDOS = licenseDetailMapper.selectList(Wrappers.<LicenseDetailDO>lambdaQuery().eq(LicenseDetailDO::getLicenseId, licenseDO.getId()));
List<LicenseNeCodeVO> details = BeanUtils.toBean(licenseDetailDOS, LicenseNeCodeVO.class);
licenseDO.setNeCodeList(details);
return licenseDO;
}
@Override
public PageResult<LicenseDO> getLicensePage(LicensePageReqVO pageReqVO) {
return licenseMapper.selectPage(pageReqVO);
PageResult<LicenseDO> pageResult = licenseMapper.selectPage(pageReqVO);
for (LicenseDO licenseDO : pageResult.getList()) {
List<LicenseDetailDO> licenseDetailDOS = licenseDetailMapper.selectList(Wrappers.<LicenseDetailDO>lambdaQuery().eq(LicenseDetailDO::getLicenseId, licenseDO.getId()));
List<LicenseNeCodeVO> details = BeanUtils.toBean(licenseDetailDOS, LicenseNeCodeVO.class);
licenseDO.setNeCodeList(details);
}
return pageResult;
}
@Override

View File

@@ -0,0 +1,22 @@
package org.agt.module.license.service.license;
import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* @description: License 定时任务
* @author: cyc
* @since: 2025-07-28
*/
@Component
public class LicenseTask {
@Resource
private LicenseServiceImpl licenseService;
@Scheduled(cron ="*/5 * * * * ?")
public void task() {
licenseService.genLicenseTask();
}
}