wzp
2025-09-03 73d7f7aed91cdaaaf61e6177dc02c71ef5ca51e8
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package com.ruoyi.system.service.impl;
 
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysClientAppMapper;
import com.ruoyi.system.domain.SysClientApp;
import com.ruoyi.system.service.ISysClientAppService;
import com.ruoyi.common.utils.SecurityUtils;
 
import static com.ruoyi.common.utils.SecurityUtils.md5;
 
/**
 * 客户应用配置 服务层实现
 */
@Service
public class SysClientAppServiceImpl implements ISysClientAppService {
    @Autowired
    private SysClientAppMapper sysClientAppMapper;
 
    /**
     * 查询客户应用配置信息
     * 
     * @param appId 客户应用配置主键
     * @return 客户应用配置信息
     */
    @Override
    public SysClientApp selectSysClientAppByAppId(Long appId) {
        return sysClientAppMapper.selectSysClientAppByAppId(appId);
    }
 
    /**
     * 通过应用标识查询客户应用配置
     * 
     * @param appKey 应用标识
     * @return 客户应用配置
     */
    @Override
    public SysClientApp selectSysClientAppByAppKey(String appKey)
    {
        return sysClientAppMapper.selectSysClientAppByAppKey(appKey);
    }
 
    /**
     * 查询客户应用配置列表
     * 
     * @param sysClientApp 客户应用配置信息
     * @return 客户应用配置集合
     */
    @Override
    public List<SysClientApp> selectSysClientAppList(SysClientApp sysClientApp) {
        return sysClientAppMapper.selectSysClientAppList(sysClientApp);
    }
 
    /**
     * 新增客户应用配置
     * 
     * @param sysClientApp 客户应用配置信息
     * @return 结果
     */
    @Override
    public int insertSysClientApp(SysClientApp sysClientApp) {
        return sysClientAppMapper.insertSysClientApp(sysClientApp);
    }
 
    /**
     * 修改客户应用配置
     * 
     * @param sysClientApp 客户应用配置信息
     * @return 结果
     */
    @Override
    public int updateSysClientApp(SysClientApp sysClientApp) {
        if (sysClientApp == null || sysClientApp.getAppId() == null) {
            throw new RuntimeException("应用ID不能为空");
        }
        
        try {
            // 设置更新人
            sysClientApp.setUpdateBy(SecurityUtils.getUsername());
            
            // 检查记录是否存在
            SysClientApp existApp = sysClientAppMapper.selectSysClientAppByAppId(sysClientApp.getAppId());
            if (existApp == null) {
                throw new RuntimeException("应用配置不存在");
            }
            
            // 打印更新前的数据
            System.out.println("更新前的数据: " + existApp);
            System.out.println("要更新的数据: " + sysClientApp);
            
            // 执行更新
            int row = sysClientAppMapper.updateSysClientApp(sysClientApp);
            System.out.println("更新结果: " + row);
            
            // 验证更新是否成功
            SysClientApp updatedApp = sysClientAppMapper.selectSysClientAppByAppId(sysClientApp.getAppId());
            System.out.println("更新后的数据: " + updatedApp);
            
            // 如果数据库确实更新了,但返回值异常,我们返回1表示成功
            if (row < 0 && updatedApp != null) {
                System.out.println("检测到数据库已更新,但返回值异常,返回1");
                return 1;
            }
            
            return row;
        } catch (Exception e) {
            System.err.println("更新异常: " + e.getMessage());
            e.printStackTrace();
            throw e;
        }
    }
 
    /**
     * 批量删除客户应用配置
     * 
     * @param appIds 需要删除的客户应用配置主键
     * @return 结果
     */
    @Override
    public int deleteSysClientAppByAppIds(Long[] appIds) {
        return sysClientAppMapper.deleteSysClientAppByAppIds(appIds);
    }
 
    /**
     * 删除客户应用配置信息
     * 
     * @param appId 客户应用配置主键
     * @return 结果
     */
    @Override
    public int deleteSysClientAppByAppId(Long appId) {
        return sysClientAppMapper.deleteSysClientAppByAppId(appId);
    }
 
    @Override
    public boolean validateSign(String appId, String sign, String timestamp) {
        // 根据appId获取应用信息
        SysClientApp clientApp = sysClientAppMapper.selectSysClientAppByAppKey(appId);
        if (clientApp == null) {
            System.out.println("未找到appId为 " + appId + " 的应用配置");
            return false;
        }
 
        System.out.println("找到应用配置: " + clientApp);
 
        // 验证应用是否有效
        if (!"0".equals(clientApp.getStatus())) {
            System.out.println("应用状态无效: " + clientApp.getStatus());
            return false;
        }
 
        // 验证有效期
        if (clientApp.getValidStartTime() != null && clientApp.getValidEndTime() != null) {
            long currentTime = System.currentTimeMillis();
            if (currentTime < clientApp.getValidStartTime().getTime() 
                || currentTime > clientApp.getValidEndTime().getTime()) {
                System.out.println("应用已过期");
                return false;
            }
        }
 
        // 生成签名
        String serverSign = generateSign(appId, clientApp.getSecurityKey(), timestamp);
        System.out.println("服务器生成的签名: " + serverSign);
        System.out.println("客户端提供的签名: " + sign);
        System.out.println("签名是否匹配: " + sign.equals(serverSign));
        
        // 比较签名
        return sign.equals(serverSign);
    }
 
    /**
     * 生成签名
     * 签名规则:MD5(appId + timestamp + securityKey)
     */
    private String generateSign(String appId, String securityKey, String timestamp) {
        String signStr = appId + timestamp + securityKey;
        System.out.println("签名原始字符串: " + signStr);
        String result = md5(signStr);
        System.out.println("MD5结果: " + result);
        return result;
    }