|
| 1 | +package com.wk.paas.service; |
| 2 | + |
| 3 | +import cn.hutool.json.JSONArray; |
| 4 | +import cn.hutool.json.JSONObject; |
| 5 | +import cn.hutool.json.JSONUtil; |
| 6 | +import com.intellij.openapi.project.Project; |
| 7 | +import com.intellij.openapi.ui.Messages; |
| 8 | +import com.intellij.openapi.vfs.VirtualFileManager; |
| 9 | +import com.wd.paas.generator.CodeGenerateService; |
| 10 | +import com.wd.paas.generator.common.enums.GenerateOperationTypeEnum; |
| 11 | +import com.wd.paas.generator.common.enums.ProjectTemplateType; |
| 12 | +import com.wd.paas.generator.generate.visitor.velocitytemplate.TemplateContext; |
| 13 | +import com.wd.paas.generator.generate.visitor.velocitytemplate.TemplateVisitor; |
| 14 | +import com.wk.paas.service.dto.*; |
| 15 | +import com.wk.paas.window.BindAppVersion; |
| 16 | +import com.wk.paas.window.SelectElementDialog; |
| 17 | +import com.wk.paas.window.setting.AppDSLBuilder; |
| 18 | +import com.wk.paas.window.setting.BindAppInfoSettings; |
| 19 | +import com.wk.paas.window.setting.CodeGenerateConfiguration; |
| 20 | +import com.wk.paas.window.setting.LoginAccountInfoSettings; |
| 21 | +import org.apache.commons.collections.CollectionUtils; |
| 22 | +import org.apache.commons.lang3.StringUtils; |
| 23 | + |
| 24 | +import javax.swing.*; |
| 25 | +import java.util.*; |
| 26 | + |
| 27 | +public class GenerateCodeService { |
| 28 | + |
| 29 | + private final Project project; |
| 30 | + |
| 31 | + public GenerateCodeService(Project project) { |
| 32 | + this.project = project; |
| 33 | + } |
| 34 | + |
| 35 | + public void execute() { |
| 36 | + // 获取应用信息 |
| 37 | + BindAppInfoSettings projectConfig = BindAppInfoSettings.getInstance(project); |
| 38 | + ApplicationDTO applicationDTO = projectConfig.getApplicationDTO(); |
| 39 | + ApplicationVersionDTO applicationVersionDTO = projectConfig.getApplicationVersionDTO(); |
| 40 | + if (applicationDTO == null || applicationVersionDTO == null) { |
| 41 | + Messages.showMessageDialog("请先关联一个平台应用", "系统警告", Messages.getWarningIcon()); |
| 42 | + SwingUtilities.invokeLater(() -> new BindAppVersion(project)); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + // 获取生成配置信息 |
| 47 | + CodeGenerateConfiguration config = CodeGenerateConfiguration.getInstance(project); |
| 48 | + if (config == null) { |
| 49 | + Messages.showMessageDialog("请先配置生成信息", "系统警告", Messages.getWarningIcon()); |
| 50 | + SwingUtilities.invokeLater(() -> new SelectElementDialog(project)); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + List<DomainDesignVersionDTO> domainSelectedList = config.getDomainSelectedList(); |
| 55 | + List<BusinessSceneVersionDTO> sceneSelectedList = config.getSceneSelectedList(); |
| 56 | + if (CollectionUtils.isEmpty(domainSelectedList) && CollectionUtils.isEmpty(sceneSelectedList)) { |
| 57 | + Messages.showMessageDialog("至少选择一个生成的模块", "系统警告", Messages.getWarningIcon()); |
| 58 | + SwingUtilities.invokeLater(() -> new SelectElementDialog(project)); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + String outputPathText = config.getOutPath(); |
| 63 | + boolean isInitCode = config.isInitCodeRadioButton(); |
| 64 | + boolean isColaSingle = config.isColaSingleRadioButton(); |
| 65 | + boolean isInitProjectStruct = config.isInitProjectStructCheckBox(); |
| 66 | + |
| 67 | + // 重写应用信息 |
| 68 | + applicationDTO.setIdentity(config.getOverrideProjectIdentity()); |
| 69 | + applicationDTO.setPackageName(config.getOverrideProjectPackage()); |
| 70 | + applicationVersionDTO.setCurrentVersion(config.getOverrideProjectVersion()); |
| 71 | + |
| 72 | + // 操作类型 |
| 73 | + GenerateOperationTypeEnum executeType = isInitCode ? GenerateOperationTypeEnum.INIT_CODE : GenerateOperationTypeEnum.UPDATE_CODE; |
| 74 | + |
| 75 | + // 项目架构 |
| 76 | + ProjectTemplateType projectType = isColaSingle ? ProjectTemplateType.COLA_SINGLE : ProjectTemplateType.COLA; |
| 77 | + |
| 78 | + // 实时解析DSL |
| 79 | + AppDSLBuilder appDSLBuilder = new AppDSLBuilder(); |
| 80 | + appDSLBuilder.buildAppInfo(applicationDTO); |
| 81 | + appDSLBuilder.buildAppVersionInfo(applicationVersionDTO); |
| 82 | + String applicationDSL = buildAppDSLJson(appDSLBuilder.getApplicationJson(), config); |
| 83 | + |
| 84 | + CodeGenerateService codeGenerateService = new CodeGenerateService(applicationDSL); |
| 85 | + TemplateContext templateContext = new TemplateContext(outputPathText); |
| 86 | + templateContext.setIsGenerateProjectFrame(isInitProjectStruct ? Boolean.TRUE : Boolean.FALSE); |
| 87 | + templateContext.setProjectTemplateType(projectType); |
| 88 | + templateContext.setOperationTypeEnum(executeType); |
| 89 | + |
| 90 | + TemplateVisitor templateVisitor = new TemplateVisitor(templateContext); |
| 91 | + try { |
| 92 | + codeGenerateService.run(templateVisitor); |
| 93 | + Messages.showMessageDialog("代码生成完成!", "执行成功", Messages.getInformationIcon()); |
| 94 | + } catch (Exception e) { |
| 95 | + Messages.showMessageDialog(e.getMessage(), "代码生成服务异常", Messages.getErrorIcon()); |
| 96 | + } finally { |
| 97 | + VirtualFileManager.getInstance().syncRefresh(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private String buildAppDSLJson(JSONObject applicationJson, CodeGenerateConfiguration config) { |
| 102 | + |
| 103 | + LoginAccountInfoSettings instance = LoginAccountInfoSettings.getInstance(); |
| 104 | + String mail = instance.getAccount(); |
| 105 | + String password = instance.catchPassword(); |
| 106 | + if (StringUtils.isBlank(mail) || StringUtils.isBlank(password)) { |
| 107 | + Messages.showMessageDialog(project, "请先登录", "系统警告", Messages.getWarningIcon()); |
| 108 | + } |
| 109 | + new LoginService().login(mail, password); |
| 110 | + |
| 111 | + List<DomainDesignVersionDTO> domainSelectedList = Optional.ofNullable(config.getDomainSelectedList()).orElse(new ArrayList<>()); |
| 112 | + List<BusinessSceneVersionDTO> sceneSelectedList = Optional.ofNullable(config.getSceneSelectedList()).orElse(new ArrayList<>()); |
| 113 | + |
| 114 | + // 实时加载DSL |
| 115 | + JSONArray domainDesignArray = JSONUtil.createArray(); |
| 116 | + for (DomainDesignVersionDTO domainDesignVersionDTO : domainSelectedList) { |
| 117 | + DomainDesignVersionDTO designVersionDTO = new QueryDomainVersionService().queryById(domainDesignVersionDTO.getId()); |
| 118 | + JSONObject domainDesignJson = JSONUtil.parseObj(designVersionDTO.getDomainDesignDsl()); |
| 119 | + domainDesignJson.putAll(getDomainDesignInfoMap(domainDesignVersionDTO, domainDesignVersionDTO.getDomainDesignDTO())); |
| 120 | + domainDesignArray.add(domainDesignJson); |
| 121 | + } |
| 122 | + |
| 123 | + JSONArray businessSceneArray = JSONUtil.createArray(); |
| 124 | + for (BusinessSceneVersionDTO businessSceneVersionDTO : sceneSelectedList) { |
| 125 | + BusinessSceneVersionDTO sceneVersionDTO = new QueryBusinessVersionService().queryById(businessSceneVersionDTO.getId()); |
| 126 | + JSONObject businessSceneJson = JSONUtil.parseObj(sceneVersionDTO.getDsl()); |
| 127 | + businessSceneJson.putAll(getBusinessSceneInfoMap(businessSceneVersionDTO, businessSceneVersionDTO.getBusinessSceneDTO())); |
| 128 | + businessSceneArray.add(businessSceneJson); |
| 129 | + } |
| 130 | + |
| 131 | + // 更新为用户勾选的模块 |
| 132 | + applicationJson.remove("businessDomains"); |
| 133 | + applicationJson.remove("businessScenarios"); |
| 134 | + applicationJson.putOnce("businessDomains", domainDesignArray); |
| 135 | + applicationJson.putOnce("businessScenarios", businessSceneArray); |
| 136 | + |
| 137 | + return applicationJson.toString(); |
| 138 | + } |
| 139 | + |
| 140 | + private static Map<String, Object> getDomainDesignInfoMap(DomainDesignVersionDTO domainDesignVersionDO, DomainDesignDTO domainDesignDO) { |
| 141 | + Map<String, Object> map = new HashMap<>(); |
| 142 | + map.put("name", domainDesignDO.getIdentity()); |
| 143 | + map.put("title", domainDesignDO.getName()); |
| 144 | + map.put("description", domainDesignDO.getDescription()); |
| 145 | + map.put("version", domainDesignVersionDO.getCurrentVersion()); |
| 146 | + return map; |
| 147 | + } |
| 148 | + |
| 149 | + private static Map<String, Object> getBusinessSceneInfoMap(BusinessSceneVersionDTO businessSceneVersionDO, BusinessSceneDTO businessSceneDO) { |
| 150 | + Map<String, Object> map = new HashMap<>(); |
| 151 | + map.put("name", businessSceneDO.getIdentity()); |
| 152 | + map.put("title", businessSceneDO.getName()); |
| 153 | + map.put("description", businessSceneDO.getDescription()); |
| 154 | + map.put("version", businessSceneVersionDO.getCurrentVersion()); |
| 155 | + return map; |
| 156 | + } |
| 157 | +} |
0 commit comments