package com.product.saas.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.product.admin.service.LoginAuthService; 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.saas.config.SaasConst; import com.product.saas.service.idel.ITenantApplyService; import com.product.tool.flow.service.FlowService; import com.product.util.BaseUtil; @Component public class TenantApplyService extends AbstractBaseService implements ITenantApplyService{ @Autowired BaseDao baseDao; @Autowired FlowService flowService; @Autowired LoginAuthService loginAuthService; @Autowired FileManagerService fileManagerService; /** * 租户信息读取和验证 * @param fse * @return */ public boolean validTenantInfo(FieldSetEntity fse) { // 获取手机号和验证码 String phone = fse.getString("phone"); String code = fse.getString("code"); // 验证验证码是否正确 loginAuthService.verifyPhoneCaptcha(phone, code); String unitName = fse.getString("unitName"); // 验证信息是否匹配 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("手机号和单位已经被使用", "手机号和单位已经被使用"); } } } } /** * 获取指定租户申请,根据申请人手机号 * @param applicantPhone 申请人手机号 * @return */ public FieldSetEntity getTenantApplyByPhone(String applicantPhone) { StringBuilder sql = new StringBuilder(); sql.append("SELECT A.*, B.file_name AS business_license_name, B.attachment_size AS business_license_size, C.dict_label AS apply_status_label \n"); sql.append("FROM product_sys_tenant_apply A \n"); sql.append("LEFT JOIN product_sys_attachments B ON B.uuid = A.business_license \n"); sql.append("LEFT JOIN product_sys_dict C ON C.dict_value = A.apply_status AND C.dict_name = 'SYSTEM_TENANT_APPLY_STATUS' \n"); sql.append("WHERE A.applicant_phone = ?"); return baseDao.getFieldSetEntityBySQL(sql.toString(), new Object[] {applicantPhone}, false); } /** * 保存租户申请 * @param fse * @return */ @Override @Transactional public boolean saveTenantApply(FieldSetEntity fse) { // 获取送审标志 String flowSign = fse.getString("approval_sign"); if (!BaseUtil.strIsNull(flowSign) || "1".equals(flowSign)) { fse.setValue("apply_status", 2); 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 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); // 保存记录 return baseDao.saveFieldSetEntity(fse); } } public boolean cancelTenantApply(FieldSetEntity fse){ return true; } }