杜洪波
15 小时以前 bb229d0324c082a85b14c72ddf7ef1a46622266d
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.product.saas.service;
 
import java.io.File;
import java.util.ArrayList;
import java.util.List;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import com.product.core.cache.DataPoolCacheImpl;
import com.product.core.config.Global;
import com.product.core.dao.BaseDao;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.service.support.AbstractBaseService;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.module.sys.entity.SystemUser;
import com.product.saas.config.SaasCode;
import com.product.saas.config.SaasConst;
import com.product.util.BaseUtil;
 
import cn.hutool.core.io.FileUtil;
 
@Component
public class TenantContractService extends AbstractBaseService{
 
    @Autowired
    BaseDao baseDao;
    
    public DataTableEntity listContract(FieldSetEntity fse) {
        SystemUser user = SpringMVCContextHolder.getCurrentUser();
        if (user == null) {
            return null;
        }
        List<Object> param = new ArrayList<>();;
        StringBuilder sql = new StringBuilder();
        sql.append("\n SELECT A.*");
        sql.append("\n FROM product_sys_contract A");
        sql.append("\n LEFT JOIN product_sys_clients B ON B.uuid = A.client_uuid");
        sql.append("\n WHERE ").append(fse.getString("filter"));
        FieldSetEntity fsePost = user.getJobPost();
        if (fsePost == null) {
            param.add(user.getClient_uuid());
            sql.append("\n AND A.client_uuid = ?");
        } else {
            if(fsePost.getString("job_post_name").contains("平台管理员")) {
                param.add(user.getUser_id());
                sql.append("\n AND B.platform_admin = ?");
            }
        }
        DataTableEntity dte = baseDao.listTable(sql.toString(), param.toArray(), fse.getInteger("pagesize"), fse.getInteger("cpage"));
        if (!BaseUtil.dataTableIsEmpty(dte)) {
            baseDao.loadPromptData(dte);
        }
        return dte;
    }
    
    /**
     *     获取合同详情(流程详情调用)
     * @param fse
     * @return
     * @throws BaseException
     */
    public FieldSetEntity findContract(FieldSetEntity fse) throws BaseException {
        return findContract(fse.getUUID());
    }
    
    /**
     *     获取合同信息
     * @param clientUUID
     * @return
     */
    public FieldSetEntity findContract(String uuid) {
        // 获取合同信息
        FieldSetEntity fseContract = baseDao.getFieldSetEntityBySQL("SELECT A.*,B.client_unit_type FROM product_sys_contract A LEFT JOIN  product_sys_clients B ON B.uuid = A.client_uuid WHERE A.uuid = ?",
                new Object[] {uuid}, false);
        if(fseContract != null) {
            DataTableEntity dteClient = DataPoolCacheImpl.getInstance().getCacheData("客户信息", new String[]{fseContract.getString("client_uuid")});
            fseContract.setValue("client_unit_type", dteClient.getFieldSetEntity(0).getString("client_unit_type"));
            fseContract.setValue("expiration_date", dteClient.getFieldSetEntity(0).getString("expiration_date"));
        }
        return fseContract;
    }
    
    /**
     *  合同流程结束覆盖License(结束节点处理器调用)
     *  
     * @param fseContract
     */
    public void contractFinish(FieldSetEntity fseContract) {
//        String isPayment = fseContract.getString("is_payment");
        FieldSetEntity fseClient = baseDao.getFieldSetEntity(SaasConst.PRODUCT_SYS_CLIENTS, fseContract.getString(SaasConst.CLIENT_UUID), false);
        copyLicense(fseContract.getString("license_doc"), fseClient.getString("client_code"));
        
    }
    
    /**
     *     复制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());
        }
    }
}