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());
|
}
|
}
|
}
|