shicf
2025-05-13 664acafa02cddbd86771ade63afb78db2de0109e
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
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;
    }
}