add
yj
2024-12-05 b9900893177c78fc559223521fe839aa21000017
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
185
186
187
188
189
190
package com.iotechn.unimall.data.open;
 
import cn.hutool.crypto.asymmetric.RSA;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iotechn.unimall.data.domain.OpenPlatformClientDO;
import com.iotechn.unimall.data.domain.OpenPlatformNotifyDO;
import com.iotechn.unimall.data.mapper.OpenPlatformClientMapper;
import com.iotechn.unimall.data.mapper.OpenPlatformNotifyMapper;
import com.dobbinsoft.fw.support.component.open.OpenPlatformStorageStrategy;
import com.dobbinsoft.fw.support.component.open.model.OPClient;
import com.dobbinsoft.fw.support.component.open.model.OPClientPermission;
import com.dobbinsoft.fw.support.component.open.model.OPNotify;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * ClassName: OpenPlatformStorageDatabaseImpl
 * Description: TODO 缓存
 *
 * @author: e-weichaozheng
 * @date: 2021-04-25
 */
@Component
public class OpenPlatformStorageDatabaseImpl implements OpenPlatformStorageStrategy {
 
    @Autowired
    private OpenPlatformClientMapper openPlatformClientMapper;
 
    @Autowired
    private OpenPlatformNotifyMapper openPlatformNotifyMapper;
 
    @Override
    public boolean checkClientExists(String clientCode) {
        return openPlatformClientMapper.selectCount(new QueryWrapper<OpenPlatformClientDO>().eq("code", clientCode)) > 0;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OPClient initClient(String clientCode, String notifyUrl, String publicKey) {
        if (openPlatformClientMapper.selectCount(new QueryWrapper<OpenPlatformClientDO>().eq("code", clientCode)) > 0) {
            return null;
        }
        RSA rsa = new RSA();
        // 生成一对RSA密钥
        String privateKeyBase64 = rsa.getPrivateKeyBase64();
        String publicKeyBase64 = rsa.getPublicKeyBase64();
        OpenPlatformClientDO openPlatformClientDO = new OpenPlatformClientDO();
        openPlatformClientDO.setCode(clientCode);
        openPlatformClientDO.setNotifyUrl(notifyUrl);
        openPlatformClientDO.setPublicKey1(publicKeyBase64);
        openPlatformClientDO.setPublicKey2(publicKey);
        openPlatformClientDO.setPrivateKey1(privateKeyBase64);
        openPlatformClientDO.setPermissionList("[]");
        if (openPlatformClientMapper.insert(openPlatformClientDO) > 0) {
            OPClient opClient = new OPClient();
            opClient.setCode(clientCode);
            opClient.setNotifyUrl(notifyUrl);
            opClient.setPublicKey1(publicKeyBase64);
            opClient.setPublicKey2(publicKey);
            opClient.setPermissionList(new ArrayList<>());
            openPlatformClientDO.setPrivateKey1(privateKeyBase64);
            return opClient;
        }
        return null;
    }
 
    @Override
    public boolean setClientNotifyUrl(String clientCode, String notifyUrl) {
        OpenPlatformClientDO openPlatformClientDO = new OpenPlatformClientDO();
        openPlatformClientDO.setNotifyUrl(notifyUrl);
        return openPlatformClientMapper.update(openPlatformClientDO, new QueryWrapper<OpenPlatformClientDO>().eq("code", clientCode)) > 0;
    }
 
    @Override
    public boolean setClientPublicKey(String clientCode, String publicKey) {
        OpenPlatformClientDO openPlatformClientDO = new OpenPlatformClientDO();
        openPlatformClientDO.setPublicKey2(publicKey);
        return openPlatformClientMapper.update(openPlatformClientDO, new QueryWrapper<OpenPlatformClientDO>().eq("code", clientCode)) > 0;
    }
 
    @Override
    public OPClient getClient(String clientCode) {
        OpenPlatformClientDO openPlatformClientDO = openPlatformClientMapper.selectOne(new QueryWrapper<OpenPlatformClientDO>().eq("code", clientCode));
        OPClient opClient = new OPClient();
        opClient.setPublicKey1(openPlatformClientDO.getPublicKey1());
        opClient.setPublicKey2(openPlatformClientDO.getPublicKey2());
        opClient.setCode(openPlatformClientDO.getCode());
        if (StringUtils.isEmpty(openPlatformClientDO.getPermissionList())) {
            opClient.setPermissionList(new ArrayList<>());
        } else {
            List<OPClientPermission> permissionList = JSONObject.parseArray(openPlatformClientDO.getPermissionList(), OPClientPermission.class);
            opClient.setPermissionList(permissionList);
        }
        return opClient;
    }
 
    @Override
    public boolean setClientPermissionList(String clientCode, List<OPClientPermission> permissionList) {
        OpenPlatformClientDO openPlatformClientDO = new OpenPlatformClientDO();
        openPlatformClientDO.setPermissionList(JSONObject.toJSONString(permissionList));
        return openPlatformClientMapper.update(openPlatformClientDO, new QueryWrapper<OpenPlatformClientDO>().eq("code", clientCode)) > 0;
    }
 
    @Override
    public boolean checkApiPermission(String clientCode, String group, String method) {
        OpenPlatformClientDO openPlatformClientDO = openPlatformClientMapper.selectOne(new QueryWrapper<OpenPlatformClientDO>().eq("code", clientCode));
        List<OPClientPermission> permissionList = JSONObject.parseArray(openPlatformClientDO.getPermissionList(), OPClientPermission.class);
        OPClientPermission permission = new OPClientPermission();
        permission.setGroup(group);
        permission.setMethod(method);
        return permissionList.contains(permission);
    }
 
    @Override
    public OPClient getClientWithPK(String clientCode) {
        OpenPlatformClientDO openPlatformClientDO = openPlatformClientMapper.selectOne(new QueryWrapper<OpenPlatformClientDO>().eq("code", clientCode));
        OPClient opClient = new OPClient();
        opClient.setPublicKey1(openPlatformClientDO.getPublicKey1());
        opClient.setPrivateKey1(openPlatformClientDO.getPrivateKey1());
        opClient.setPublicKey2(openPlatformClientDO.getPublicKey2());
        opClient.setCode(openPlatformClientDO.getCode());
        if (StringUtils.isEmpty(openPlatformClientDO.getPermissionList())) {
            opClient.setPermissionList(new ArrayList<>());
        } else {
            List<OPClientPermission> permissionList = JSONObject.parseArray(openPlatformClientDO.getPermissionList(), OPClientPermission.class);
            opClient.setPermissionList(permissionList);
        }
        return opClient;
    }
 
    @Override
    public String storeNotify(OPNotify opNotify) {
        OpenPlatformNotifyDO openPlatformNotifyDO = new OpenPlatformNotifyDO();
        openPlatformNotifyDO.setClientCode(opNotify.getClientCode());
        openPlatformNotifyDO.setNextNotify(opNotify.getNextNotify());
        openPlatformNotifyDO.setParams(JSONObject.toJSONString(opNotify.getParams()));
        openPlatformNotifyDO.setStatus(opNotify.getStatus());
        openPlatformNotifyDO.setTimes(opNotify.getTimes());
 
        if (openPlatformNotifyMapper.insert(openPlatformNotifyDO) > 0) {
            return openPlatformNotifyDO.getId() + "";
        }
        return null;
    }
 
    @Override
    public List<OPNotify> getNeedNotify() {
        QueryWrapper<OpenPlatformNotifyDO> queryWrapper = new QueryWrapper<OpenPlatformNotifyDO>().eq("status", 0).gt("next_notify", new Date());
        Integer count = openPlatformNotifyMapper.selectCount(queryWrapper);
        if (count == 0) {
            return new ArrayList<>();
        }
        List<OpenPlatformNotifyDO> openPlatformNotifyDOS = openPlatformNotifyMapper.selectList(queryWrapper);
        return openPlatformNotifyDOS.stream().map(item -> {
            OPNotify opNotify = new OPNotify();
            opNotify.setId(item.getId() + "");
            opNotify.setStatus(item.getStatus());
            opNotify.setTimes(item.getTimes());
            opNotify.setNextNotify(item.getNextNotify());
            opNotify.setClientCode(item.getClientCode());
            opNotify.setParams(JSONObject.parseArray(item.getParams(), String.class));
            return opNotify;
        }).collect(Collectors.toList());
    }
 
    @Override
    public boolean updateNotify(OPNotify opNotify) {
        OpenPlatformNotifyDO openPlatformNotifyDO = new OpenPlatformNotifyDO();
        openPlatformNotifyDO.setClientCode(opNotify.getClientCode());
        openPlatformNotifyDO.setNextNotify(opNotify.getNextNotify());
        openPlatformNotifyDO.setParams(opNotify.getParams() == null ? null : JSONObject.toJSONString(opNotify.getParams()));
        openPlatformNotifyDO.setStatus(opNotify.getStatus());
        openPlatformNotifyDO.setTimes(opNotify.getTimes());
        openPlatformNotifyDO.setId(Long.parseLong(opNotify.getId()));
        return openPlatformNotifyMapper.updateById(openPlatformNotifyDO) > 0;
    }
 
    @Override
    public boolean customTryNotify() {
        return false;
    }
}