package com.product.patch.service;
|
|
import com.alibaba.fastjson.JSONArray;
|
import com.product.common.io.FileUtils;
|
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.spring.context.SpringMVCContextHolder;
|
import com.product.module.sys.entity.SystemUser;
|
import com.product.patch.config.CmnConst;
|
import com.product.patch.config.ErrorCode;
|
import com.product.patch.service.idel.IPatchDeployService;
|
import com.product.patch.util.RSAUtil;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import java.io.File;
|
import java.io.IOException;
|
import java.text.SimpleDateFormat;
|
import java.util.Date;
|
import java.util.Map;
|
|
/**
|
* Copyright © 6c
|
*
|
* @Date: 2022-04-02 16:59
|
* @Author: 6c
|
* @Description: 补丁部署
|
*/
|
@Service
|
public class PatchDeployService extends AbstractBaseService implements IPatchDeployService {
|
@Autowired
|
private BaseDao baseDao;
|
|
public void testDeploy() {
|
// FieldSetEntity fse = new FieldSetEntity();
|
// fse.setTableName("temp_table");
|
// deploy(fse);
|
|
String clientUUID = "a34f8818-d36d-4d82-b1b0-dbc0d2e70a03";
|
String fileName = "D:/worksoft/IDEA/workspace/product-server_v2.0.0/product-server-patch/patch/deploy_content.txt";
|
String decodeData = extractPatchContent(clientUUID);
|
FileUtils.writeToFile(fileName, decodeData, "utf-8", false);
|
checkPatch(decodeData);
|
}
|
|
/**
|
* 部署
|
* @param fse
|
*/
|
public void deploy(FieldSetEntity fse) {
|
SystemUser curUser = SpringMVCContextHolder.getCurrentUser();
|
String clientUUID = curUser == null ? fse.getString(CmnConst.FIELD_CLIENT_UUID) : curUser.getClientUuid();
|
String decodeData = extractPatchContent(clientUUID);
|
// 验证补丁
|
checkPatch(decodeData);
|
|
}
|
|
/**
|
* 提取补丁内容
|
* @param clientUUID
|
* @return
|
*/
|
private String extractPatchContent(String clientUUID) {
|
try {
|
String filePath = "D:/worksoft/IDEA/workspace/product-server_v2.0.0/product-server-patch/patch/2022-04-08-admin.patch";
|
File file = new File(filePath);
|
String decodedData = FileUtils.readFileToString(file, "utf-8");
|
// 解密
|
FieldSetEntity mainFse = baseDao.getFieldSetEntityByFilter(CmnConst.TABLE_PRODUCT_SYS_ORG_LEVELS, "org_level_id=1", new Object[]{}, false);
|
FieldSetEntity mainCryptFse = baseDao.getFieldSetEntityByFilter(CmnConst.TABLE_PRODUCT_SYS_PATCH_KEY, "org_level_uuid=?", new Object[]{mainFse.getUUID()}, false);
|
String mainPublicKey = mainCryptFse.getString(CmnConst.FIELD_PUBLIC_KEY);
|
FieldSetEntity clientCryptFse = baseDao.getFieldSetEntityByFilter(CmnConst.TABLE_PRODUCT_SYS_PATCH_KEY, "org_level_uuid=?", new Object[]{clientUUID}, false);
|
String clientPrivateKey = clientCryptFse.getString(CmnConst.FIELD_PIRVATE_KEY);
|
decodedData = RSAUtil.privateDecrypt(decodedData, RSAUtil.getPrivateKey(clientPrivateKey));
|
decodedData = RSAUtil.publicDecrypt(decodedData, RSAUtil.getPublicKey(mainPublicKey));
|
return decodedData;
|
} catch (Exception e) {
|
e.printStackTrace();
|
throw new BaseException(ErrorCode.EXTRACT_EXTRACT_CONTENT_FAIL);
|
}
|
}
|
|
/**
|
* 验证补丁内容
|
* @param content
|
*/
|
private void checkPatch(String content) {
|
String curDate = new SimpleDateFormat(CmnConst.DATE_FORMAT_1).format(new Date());
|
if (!content.startsWith(curDate + " " + CmnConst.DEFAULT_OPERATOR)) {
|
throw new BaseException(ErrorCode.EXTRACT_UNQUALIFIED);
|
}
|
}
|
|
private Map<String, String> getCurDatabaseInfo(JSONArray resultArr) {
|
return null;
|
}
|
}
|