杜洪波
2026-03-16 b3208178914f94a4ecd1acdd3465e5bdedafac95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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;
    }
}