| | |
| | | package com.product.saas.service; |
| | | |
| | | import java.io.File; |
| | | import java.util.Date; |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import com.product.admin.config.CmnConst; |
| | | import com.product.admin.service.CodeService; |
| | | import com.product.admin.service.LoginAuthService; |
| | | import com.product.core.config.Global; |
| | | import com.product.core.dao.BaseDao; |
| | | import com.product.core.entity.FieldSetEntity; |
| | | import com.product.core.exception.BaseException; |
| | | import com.product.core.service.support.AbstractBaseService; |
| | | import com.product.core.transfer.Transactional; |
| | | import com.product.file.service.FileManagerService; |
| | | import com.product.module.sys.service.UserService; |
| | | import com.product.saas.config.SaasCode; |
| | | import com.product.saas.config.SaasConst; |
| | | import com.product.saas.service.idel.ITenantApplyService; |
| | | import com.product.tool.flow.service.FlowService; |
| | | import com.product.util.BaseUtil; |
| | | |
| | | import cn.hutool.core.io.FileUtil; |
| | | |
| | | /** |
| | | * ç§æ·ç³è¯· |
| | | * |
| | | */ |
| | | @Component |
| | | public class TenantApplyService extends AbstractBaseService implements ITenantApplyService{ |
| | | |
| | | @Autowired |
| | | BaseDao baseDao; |
| | | |
| | | @Autowired |
| | | CodeService codeService; |
| | | |
| | | @Autowired |
| | | UserService userService; |
| | | |
| | | @Autowired |
| | | FlowService flowService; |
| | |
| | | // éªè¯ä¿¡æ¯æ¯å¦å¹é
|
| | | FieldSetEntity fseTenantByPhone = baseDao.getFieldSetEntityByFilter(SaasConst.PRODUCT_SYS_TENANT_APPLY, "applicant_phone = ?", new Object[] {phone}, false); |
| | | FieldSetEntity fseTenantByName = baseDao.getFieldSetEntityByFilter(SaasConst.PRODUCT_SYS_TENANT_APPLY, "unit_name = ?", new Object[] {unitName}, false); |
| | | if (fseTenantByPhone == null) { |
| | | if(fseTenantByName == null) { |
| | | // é½ä¸åå¨ï¼éªè¯éè¿ |
| | | return true; |
| | | } else { |
| | | throw new BaseException("åä½å·²ç»è¢«ä½¿ç¨", "åä½å·²ç»è¢«ä½¿ç¨"); |
| | | } |
| | | } else { |
| | | if(fseTenantByName == null) { |
| | | throw new BaseException("ææºå·å·²ç»è¢«ä½¿ç¨", "ææºå·å·²ç»è¢«ä½¿ç¨"); |
| | | } else { |
| | | if (fseTenantByName.getUUID().equals(fseTenantByPhone.getUUID())) { |
| | | // é½åå¨äºå䏿¡æ°æ®ï¼éªè¯éè¿ |
| | | return true; |
| | | } else { |
| | | throw new BaseException("ææºå·ååä½å·²ç»è¢«ä½¿ç¨", "ææºå·ååä½å·²ç»è¢«ä½¿ç¨"); |
| | | } |
| | | } |
| | | } |
| | | // æ
åµ1ï¼ææºå·ååä½åé½ä¸åå¨ -> éè¿ |
| | | if (fseTenantByPhone == null && fseTenantByName == null) { |
| | | return true; |
| | | } |
| | | // æ
åµ2ï¼åªå卿æºå· -> ææºå·å·²ç³è¯· |
| | | if (fseTenantByPhone != null && fseTenantByName == null) { |
| | | throw new BaseException(SaasCode.TENANT_APPLY_VALID_UNIT_HAS_APPLY.getValue(), SaasCode.TENANT_APPLY_VALID_UNIT_HAS_APPLY.getText()); |
| | | } |
| | | // æ
åµ3ï¼åªåå¨åä½å -> åä½å·²ç³è¯· |
| | | if (fseTenantByPhone == null && fseTenantByName != null) { |
| | | throw new BaseException(SaasCode.TENANT_APPLY_VALID_PHONE_HAS_APPLY.getValue(), SaasCode.TENANT_APPLY_VALID_PHONE_HAS_APPLY.getText()); |
| | | } |
| | | // æ
åµ4ï¼é½åå¨ -> æ£æ¥æ¯å¦æ¯å䏿¡è®°å½ |
| | | if (fseTenantByPhone.getUUID().equals(fseTenantByName.getUUID())) { |
| | | return true; |
| | | } |
| | | // æ
åµ5ï¼é½åå¨ä½ä¸æ¯å䏿¡è®°å½ -> ææºå·ååä½åé½å·²ç³è¯· |
| | | throw new BaseException(SaasCode.TENANT_APPLY_VALID_UNIT_AND_PHONE_HAS_APPLY.getValue(), SaasCode.TENANT_APPLY_VALID_UNIT_AND_PHONE_HAS_APPLY.getText()); |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | /** |
| | | * ä¿åç§æ·ç³è¯· |
| | | * approval_signï¼é审æ è¯ï¼0ï¼ä¿åï¼1ï¼éå®¡ææ¿æ´»ï¼ |
| | | * @param fse |
| | | * @return |
| | | */ |
| | |
| | | // è·åé审æ å¿ |
| | | String flowSign = fse.getString("approval_sign"); |
| | | if (!BaseUtil.strIsNull(flowSign) || "1".equals(flowSign)) { |
| | | // ä¿åç§æ·ä¿¡æ¯ |
| | | fse.setValue("apply_status", 2); |
| | | fse.setValue("is_passed", 0); |
| | | baseDao.saveFieldSetEntity(fse); |
| | | // è·åé审å¼å§çåä¸ä¸ªèç¹ä¿¡æ¯ |
| | | StringBuilder sql = new StringBuilder(); |
| | | sql.append("SELECT * FROM product_sys_flow_node \n"); |
| | | sql.append("WHERE uuid IN ( \n"); |
| | | sql.append(" SELECT target_uuid FROM product_sys_flow_link \n"); |
| | | sql.append(" WHERE source_uuid IN ( \n"); |
| | | sql.append(" SELECT uuid FROM product_sys_flow_node \n"); |
| | | sql.append(" WHERE module_type = 1 AND flow_uuid IN ( \n"); |
| | | sql.append(" SELECT uuid FROM product_sys_flow_model \n"); |
| | | sql.append(" WHERE type_code = ? \n"); |
| | | sql.append(" ) \n"); |
| | | sql.append(" ) \n"); |
| | | sql.append(") \n"); |
| | | FieldSetEntity fseFlowNode = baseDao.getFieldSetEntityBySQL(sql.toString(), new Object[] {SaasConst.FLOW_TENANT_APPLY}, false); |
| | | if (fseFlowNode == null || BaseUtil.strIsNull(fseFlowNode.getString("default_users"))) { |
| | | throw new BaseException("ç§æ·ç³è¯·æµç¨æè¯¯", "ç§æ·ç³è¯·æµç¨æè¯¯"); |
| | | |
| | | // æ¥è¯¢éå®¡è®°å½ |
| | | FieldSetEntity fseTask = baseDao.getFieldSetEntityByFilter("product_sys_flow_task", "table_name = 'product_sys_tenant_apply' AND record_uuid = ?", new Object[] {fse.getUUID()}, false); |
| | | if (fseTask == null) { |
| | | // è·åé审å¼å§çåä¸ä¸ªèç¹ä¿¡æ¯ |
| | | StringBuilder sql = new StringBuilder(); |
| | | sql.append("SELECT * FROM product_sys_flow_node \n"); |
| | | sql.append("WHERE uuid IN ( \n"); |
| | | sql.append(" SELECT target_uuid FROM product_sys_flow_link \n"); |
| | | sql.append(" WHERE source_uuid IN ( \n"); |
| | | sql.append(" SELECT uuid FROM product_sys_flow_node \n"); |
| | | sql.append(" WHERE module_type = 1 AND flow_uuid IN ( \n"); |
| | | sql.append(" SELECT uuid FROM product_sys_flow_model \n"); |
| | | sql.append(" WHERE type_code = ? \n"); |
| | | sql.append(" ) \n"); |
| | | sql.append(" ) \n"); |
| | | sql.append(") \n"); |
| | | FieldSetEntity fseFlowNode = baseDao.getFieldSetEntityBySQL(sql.toString(), new Object[] {SaasConst.FLOW_TENANT_APPLY}, false); |
| | | if (fseFlowNode == null || BaseUtil.strIsNull(fseFlowNode.getString("default_users"))) { |
| | | throw new BaseException("ç§æ·ç³è¯·æµç¨æè¯¯", "ç§æ·ç³è¯·æµç¨æè¯¯"); |
| | | } |
| | | // å°è£
é审忰 |
| | | FieldSetEntity fseFlowTask = new FieldSetEntity(SaasConst.PRODUCT_SYS_TENANT_APPLY); |
| | | fseFlowTask.setValue("uuid", fse.getUUID()); |
| | | fseFlowTask.setValue("type_code", SaasConst.FLOW_TENANT_APPLY); |
| | | fseFlowTask.setValue("accepters", fseFlowNode.getString("default_users")); |
| | | fseFlowTask.setValue("flow_title", "ç§æ·ç³è¯·-" + fse.getString("unit_name")); |
| | | // æ§è¡é审 |
| | | flowService.autoSendFlow(fseFlowTask); |
| | | } else { |
| | | // æ¿æ´»éå®¡ä»»å¡ |
| | | FieldSetEntity fseTaskDetail = new FieldSetEntity("product_sys_flow_detail"); |
| | | fseTaskDetail.setValue("task_uuid", fseTask.getUUID()); |
| | | fseTaskDetail.setValue("is_sub_flow", "0"); |
| | | fseTaskDetail.setValue("opinion", "ç§æ·æ¤ååéæ°æäº¤"); |
| | | flowService.activate(fseTaskDetail); |
| | | } |
| | | // å°è£
é审忰 |
| | | FieldSetEntity fseFlowTask = new FieldSetEntity(SaasConst.PRODUCT_SYS_TENANT_APPLY); |
| | | fseFlowTask.setValue("uuid", fse.getUUID()); |
| | | fseFlowTask.setValue("type_code", SaasConst.FLOW_TENANT_APPLY); |
| | | fseFlowTask.setValue("accepters", fseFlowNode.getString("default_users")); |
| | | fseFlowTask.setValue("flow_title", "ç§æ·ç³è¯·-" + fse.getString("unit_name")); |
| | | // æ§è¡é审 |
| | | flowService.autoSendFlow(fseFlowTask); |
| | | return true; |
| | | } else { |
| | | fse.setValue("apply_status", 1); |
| | | // ä¿åè®°å½ |
| | | fse.setValue("is_passed", 0); |
| | | // ä¿åç§æ·ä¿¡æ¯ |
| | | return baseDao.saveFieldSetEntity(fse); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * æ¤éç§æ·ç³è¯· |
| | | * @param fse |
| | | * @return |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | public boolean cancelTenantApply(FieldSetEntity fse){ |
| | | return true; |
| | | // è·åå¯¹åºæµç¨ä»»å¡ |
| | | FieldSetEntity fseTask = baseDao.getFieldSetEntityByFilter("product_sys_flow_task", "table_name = 'product_sys_tenant_apply' AND record_uuid = ?", new Object[] {fse.getUUID()}, false); |
| | | if (fseTask == null) { |
| | | throw new BaseException(SaasCode.TENANT_AAPLY_INFO_FLOW_TASK_ERROR.getValue(), SaasCode.TENANT_AAPLY_INFO_FLOW_TASK_ERROR.getText()); |
| | | } |
| | | // ç»æ¢æµç¨ |
| | | flowService.stop(fseTask); |
| | | // æ è®°ç³è¯·æ°æ®ä¸ºæªé审 |
| | | return baseDao.executeUpdate("UPDATE product_sys_tenant_apply SET apply_status = 1 WHERE applicant_phone = ? AND unit_name = ? ", |
| | | new Object[] {fse.getString("applicant_phone"), fse.getString("unit_name")}); |
| | | } |
| | | |
| | | /** |
| | | * å·æ°ç³è¯·åç¶æ(æµç¨å¤çå¨è°ç¨) |
| | | * @param fseApply |
| | | */ |
| | | public void refreshApplyStatus(FieldSetEntity fseApply) { |
| | | if ("1".equals(fseApply.getString("is_passed"))) { |
| | | // å®¡æ ¸éè¿ |
| | | baseDao.executeUpdate("UPDATE product_sys_tenant_apply SET apply_status = 3 WHERE uuid = ?", new Object[] {fseApply.getUUID()}); |
| | | } else { |
| | | // å®¡æ ¸å¤±è´¥ |
| | | baseDao.executeUpdate("UPDATE product_sys_tenant_apply SET apply_status = 4 WHERE uuid = ?", new Object[] {fseApply.getUUID()}); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * ç³è¯·å®æ |
| | | * @param fseApply |
| | | */ |
| | | @Transactional |
| | | public void applyFinish(FieldSetEntity fseApply) { |
| | | // å建客æ·ä¿¡æ¯ |
| | | FieldSetEntity fseClient = new FieldSetEntity(SaasConst.PRODUCT_SYS_CLIENTS); |
| | | fseClient.setValue("client_name", fseApply.getString("unit_name")); |
| | | fseClient.setValue("client_tel", fseApply.getString("applicant_phone")); |
| | | fseClient.setValue("client_code", codeService.createCode(SaasConst.PRODUCT_SYS_CLIENTS, "client_code", "")); |
| | | fseClient.setValue("attachment_capacity", "2130000000"); |
| | | fseClient.setValue("platform_admin", fseApply.getString("platform_admin")); |
| | | fseClient.setValue("expiration_date", fseApply.getString("expiration_date")); |
| | | fseClient.setValue("client_unit_type", fseApply.getString("unit_type")); |
| | | fseClient.setValue("client_address_province_id", fseApply.getString("area_province")); |
| | | fseClient.setValue("client_address_city_id", fseApply.getString("area_city")); |
| | | fseClient.setValue("client_address_county_id", fseApply.getString("area_county")); |
| | | fseClient.setValue("client_address_line_one", fseApply.getString("address")); |
| | | fseClient.setValue(CmnConst.CREATED_BY, "1"); |
| | | fseClient.setValue(CmnConst.CREATED_UTC_DATETIME, new Date()); |
| | | baseDao.add(fseClient); |
| | | // å¤å¶licenseå°web模å |
| | | copyLicense(fseApply.getString("license"), fseClient.getString("client_code")); |
| | | |
| | | // å建ç»ç»æºæåä½ |
| | | FieldSetEntity fseOrgUnit = new FieldSetEntity(CmnConst.PRODUCT_SYS_ORG_LEVELS); |
| | | fseOrgUnit.setValue("client_uuid", fseClient.getUUID()); |
| | | fseOrgUnit.setValue("org_level_name", fseApply.getString("unit_name")); |
| | | fseOrgUnit.setValue("org_level_all", fseApply.getString("unit_name")); |
| | | fseOrgUnit.setValue("org_level_code", codeService.createCode("product_sys_org_levels", "org_level_code", null)); |
| | | fseOrgUnit.setValue("org_level_type", 0); |
| | | fseOrgUnit.setValue("sequence", 1); |
| | | fseOrgUnit.setValue(CmnConst.CREATED_BY, "1"); |
| | | fseOrgUnit.setValue(CmnConst.CREATED_UTC_DATETIME, new Date()); |
| | | baseDao.add(fseOrgUnit); |
| | | // åå»ºç§æ·ç®¡çåç¨æ·ä¿¡æ¯ |
| | | FieldSetEntity fseUser = new FieldSetEntity(CmnConst.PRODUCT_SYS_USERS); |
| | | fseUser.setValue(CmnConst.USER_NAME, fseApply.getString("applicant_name")); |
| | | fseUser.setValue(CmnConst.USER_ACCOUNT, fseApply.getString("applicant_phone")); |
| | | fseUser.setValue(CmnConst.USER_PRIMARY_EMAIL, fseApply.getString("applicant_email")); |
| | | fseUser.setValue(CmnConst.USER_MOBILE_NUMBER, fseApply.getString("applicant_phone")); |
| | | fseUser.setValue(CmnConst.USER_PWD, userService.createPassWord(fseApply.getString("applicant_phone"), fseApply.getString("applicant_pwd"))); |
| | | fseUser.setValue(CmnConst.IS_MANAGER, 1); |
| | | fseUser.setValue(CmnConst.STATUS, 1); |
| | | fseUser.setValue(CmnConst.CREATED_BY, "1"); |
| | | fseUser.setValue(CmnConst.CREATED_UTC_DATETIME, new Date()); |
| | | baseDao.add(fseUser); |
| | | FieldSetEntity fseUserInfo = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_USERS, fseUser.getUUID(), false); |
| | | // åå»ºç§æ·ç®¡çåä¿¡æ¯ |
| | | FieldSetEntity fseManager = new FieldSetEntity(CmnConst.PRODUCT_SYS_ORG_MANAGER); |
| | | fseManager.setValue(CmnConst.USER_ID, fseUserInfo.getString(CmnConst.USER_ID)); |
| | | fseManager.setValue(CmnConst.ORG_LEVEL_UUID, fseOrgUnit.getUUID()); |
| | | fseManager.setValue(CmnConst.MANAGER_TYPE, 2); |
| | | fseManager.setValue(CmnConst.ROLE_UUIDS, "eabb00f3-2118-4165-967b-a7d88f472f67-notchange"); |
| | | fseManager.setValue(CmnConst.IS_USED, 1); |
| | | fseManager.setValue(CmnConst.CLIENT_UUID, fseClient.getUUID()); |
| | | fseManager.setValue(CmnConst.CREATED_BY, "1"); |
| | | fseManager.setValue(CmnConst.CREATED_UTC_DATETIME, new Date()); |
| | | baseDao.add(fseManager); |
| | | } |
| | | |
| | | /** |
| | | * å¤å¶licenseå°æå®ç®å½ |
| | | * @param fileUUID éä»¶UUID |
| | | * @param clientCode 客æ·ç¼ç |
| | | */ |
| | | public void copyLicense(String fileUUID, String clientCode) { |
| | | |
| | | // è·åæä»¶ä¿¡æ¯ |
| | | FieldSetEntity fse = baseDao.getFieldSetEntity(SaasConst.PRODUCT_SYS_ATTACHMENTS, fileUUID, false); |
| | | if (fse == null) { |
| | | throw new BaseException(SaasCode.TENANT_APPLY_LICENSE_DATA_NO_EXIST.getValue(), SaasCode.TENANT_APPLY_LICENSE_DATA_NO_EXIST.getText()); |
| | | } |
| | | |
| | | // æå»ºæºæä»¶è·¯å¾ |
| | | String baseDir = System.getProperty("user.dir"); |
| | | String localDir = Global.getSystemConfig("local.dir", ""); |
| | | String attachmentUrl = fse.getString("attachment_url"); |
| | | String attachmentTitle = fse.getString("attachment_title"); |
| | | |
| | | String filePath = baseDir + File.separator + localDir + File.separator + attachmentUrl + File.separator + attachmentTitle; |
| | | |
| | | // æ£æ¥æºæä»¶æ¯å¦åå¨ |
| | | if (!FileUtil.exist(filePath)) { |
| | | throw new BaseException(SaasCode.TENANT_APPLY_LICENSE_FILE_NO_EXIST.getValue(), SaasCode.TENANT_APPLY_LICENSE_FILE_NO_EXIST.getText()); |
| | | } |
| | | |
| | | // æå»ºç®æ ç®å½ |
| | | String targetDir = baseDir + File.separator + "resources" + File.separator; |
| | | FileUtil.mkdir(targetDir); |
| | | |
| | | // æå»ºç®æ æä»¶åï¼license + clientCode + .dat |
| | | // æ 论åæä»¶æ¯å¦ææ©å±åï¼é½ä½¿ç¨.datæ©å±å |
| | | String targetFileName = "license" + clientCode + ".dat"; |
| | | String targetPath = targetDir + targetFileName; |
| | | |
| | | // å¤å¶æä»¶ |
| | | try { |
| | | // 使ç¨Hutoolå¤å¶ï¼ç¬¬ä¸ä¸ªåæ°true表示è¦çå·²åå¨çæä»¶ |
| | | FileUtil.copy(filePath, targetPath, true); |
| | | // éªè¯å¤å¶ç»æ |
| | | if (FileUtil.exist(targetPath)) { |
| | | long sourceSize = FileUtil.size(new File(filePath)); |
| | | long targetSize = FileUtil.size(new File(targetPath)); |
| | | if (sourceSize != targetSize) { |
| | | throw new BaseException(SaasCode.TENANT_APPLY_LICENSE_FILE_COPY_FAIL.getValue(), SaasCode.TENANT_APPLY_LICENSE_FILE_COPY_FAIL.getValue() + "ç®æ æä»¶ä¸å®æ´"); |
| | | } |
| | | } else { |
| | | throw new BaseException(SaasCode.TENANT_APPLY_LICENSE_FILE_COPY_FAIL.getValue(), SaasCode.TENANT_APPLY_LICENSE_FILE_COPY_FAIL.getValue() + "ç®æ æä»¶ä¸åå¨"); |
| | | } |
| | | } catch (Exception e) { |
| | | throw new BaseException(SaasCode.TENANT_APPLY_LICENSE_FILE_COPY_FAIL.getValue(), SaasCode.TENANT_APPLY_LICENSE_FILE_COPY_FAIL.getValue() + e.getMessage()); |
| | | } |
| | | } |
| | | } |