ny
昨天 b6f169fe43a2b13f351aefc152374fc7f0bc8cb7
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
191
192
193
194
195
196
package jnpf.base.service.impl;
 
 
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jnpf.base.entity.EmailConfigEntity;
import jnpf.base.entity.SysConfigEntity;
import jnpf.base.mapper.SysconfigMapper;
import jnpf.base.service.SuperServiceImpl;
import jnpf.base.service.SysconfigService;
import jnpf.message.model.mail.MailAccount;
import jnpf.message.model.mail.Pop3Util;
import jnpf.message.model.mail.SmtpUtil;
import jnpf.model.BaseSystemInfo;
import jnpf.model.SocialsSysConfig;
import jnpf.util.CacheKeyUtil;
import jnpf.util.JsonUtil;
import jnpf.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * 系统配置
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2019年9月27日 上午9:18
 */
@Service
public class SysconfigServiceImpl extends SuperServiceImpl<SysconfigMapper, SysConfigEntity> implements SysconfigService {
 
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private CacheKeyUtil cacheKeyUtil;
    @Autowired
    private Pop3Util pop3Util;
 
    @Override
    public List<SysConfigEntity> getList(String type) {
        List<SysConfigEntity> list = new ArrayList<>();
        if ("WeChat".equals(type)) {
            QueryWrapper<SysConfigEntity> queryWrapper = new QueryWrapper<>();
            list = this.list(queryWrapper).stream().filter(t -> "QYHConfig".equals(t.getCategory()) || "MPConfig".equals(t.getCategory())).collect(Collectors.toList());
        }
        if ("SysConfig".equals(type)) {
            QueryWrapper<SysConfigEntity> queryWrapper = new QueryWrapper<>();
            list = this.list(queryWrapper).stream().filter(t -> !"QYHConfig".equals(t.getCategory()) && !"MPConfig".equals(t.getCategory())).collect(Collectors.toList());
        }
        return list;
    }
 
    @Override
    public BaseSystemInfo getWeChatInfo() {
        Map<String, String> objModel = new HashMap<>(16);
        List<SysConfigEntity> list = this.getList("WeChat");
        for (SysConfigEntity entity : list) {
            objModel.put(entity.getFkey(), entity.getValue());
        }
        BaseSystemInfo baseSystemInfo = JsonUtil.getJsonToBean(objModel, BaseSystemInfo.class);
        return baseSystemInfo;
    }
 
    @Override
    public BaseSystemInfo getSysInfo() {
        String cacheKey = cacheKeyUtil.getSystemInfo();
        if (redisUtil.exists(cacheKey)) {
            String json = String.valueOf(redisUtil.getString(cacheKey));
            return JsonUtil.getJsonToBean(json, BaseSystemInfo.class);
        }
        Map<String, String> objModel = new HashMap<>(16);
        List<SysConfigEntity> list = this.getList("SysConfig");
        for (SysConfigEntity entity : list) {
            objModel.put(entity.getFkey(), entity.getValue());
        }
        BaseSystemInfo baseSystemInfo = JsonUtil.getJsonToBean(objModel, BaseSystemInfo.class);
        redisUtil.insert(cacheKey, JsonUtil.getObjectToString(baseSystemInfo));
        return baseSystemInfo;
    }
 
    @Override
    @DSTransactional
    public void save(List<SysConfigEntity> entitys) {
        String cacheKey = cacheKeyUtil.getSystemInfo();
        redisUtil.remove(cacheKey);
        this.baseMapper.deleteFig();
        for (SysConfigEntity entity : entitys) {
            entity.setCategory("SysConfig");
            this.baseMapper.insert(entity);
        }
    }
 
    @Override
    @DSTransactional
    public boolean saveMp(List<SysConfigEntity> entitys) {
        String cacheKey = cacheKeyUtil.getWechatConfig();
        int flag = 0;
        redisUtil.remove(cacheKey);
        this.baseMapper.deleteMpFig();
        for (SysConfigEntity entity : entitys) {
            entity.setCategory("MPConfig");
            if (this.baseMapper.insert(entity) > 0) {
                flag++;
            }
        }
        if (entitys.size() == flag) {
            return true;
        }
        return false;
    }
 
    @Override
    @DSTransactional
    public void saveQyh(List<SysConfigEntity> entitys) {
        String cacheKey = cacheKeyUtil.getWechatConfig();
        redisUtil.remove(cacheKey);
        this.baseMapper.deleteQyhFig();
        for (SysConfigEntity entity : entitys) {
            entity.setCategory("QYHConfig");
            this.baseMapper.insert(entity);
        }
    }
 
    @Override
    public String checkLogin(EmailConfigEntity configEntity) {
        MailAccount mailAccount = new MailAccount();
        mailAccount.setAccount(configEntity.getAccount());
        mailAccount.setPassword(configEntity.getPassword());
        mailAccount.setPop3Host(configEntity.getPop3Host());
        mailAccount.setPop3Port(configEntity.getPop3Port());
        mailAccount.setSmtpHost(configEntity.getSmtpHost());
        mailAccount.setSmtpPort(configEntity.getSmtpPort());
        if ("1".equals(String.valueOf(configEntity.getEmailSsl()))) {
            mailAccount.setSsl(true);
        } else {
            mailAccount.setSsl(false);
        }
        if (mailAccount.getSmtpHost() != null) {
            return SmtpUtil.checkConnected(mailAccount);
        }
        if (mailAccount.getPop3Host() != null) {
            return pop3Util.checkConnected(mailAccount);
        }
        return "false";
    }
 
    @Override
    public String getValueByKey(String keyStr) {
        QueryWrapper<SysConfigEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysConfigEntity::getFkey, keyStr);
        List<SysConfigEntity> list = list(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            return list.get(0).getValue();
        }
        return null;
    }
 
    @Override
    public SocialsSysConfig getSocialsConfig() {
        String cacheKey = cacheKeyUtil.getSocialsConfig();
        if (redisUtil.exists(cacheKey)) {
            String json = String.valueOf(redisUtil.getString(cacheKey));
            return JsonUtil.getJsonToBean(json, SocialsSysConfig.class);
        }
        Map<String, String> objModel = new HashMap<>();
        QueryWrapper<SysConfigEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysConfigEntity::getCategory, "SocialsConfig");
        List<SysConfigEntity> configList = this.list(queryWrapper);
        for (SysConfigEntity entity : configList) {
            objModel.put(entity.getFkey(), entity.getValue());
        }
        SocialsSysConfig config = JsonUtil.getJsonToBean(objModel, SocialsSysConfig.class);
        redisUtil.insert(cacheKey, JsonUtil.getObjectToString(config));
        return config;
    }
 
    @Override
    public void saveSocials(List<SysConfigEntity> entitys) {
        String cacheKey = cacheKeyUtil.getSocialsConfig();
        redisUtil.remove(cacheKey);
        QueryWrapper<SysConfigEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysConfigEntity::getCategory, "SocialsConfig");
        this.remove(queryWrapper);
        for (SysConfigEntity entity : entitys) {
            this.baseMapper.insert(entity);
        }
    }
}