package jnpf.base.service.impl; import cn.hutool.core.collection.CollUtil; 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.model.systemconfig.UserFieldConfig; 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 lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; import java.util.stream.Collectors; /** * 系统配置 * * @author JNPF开发平台组 * @version V3.1.0 * @copyright 引迈信息技术有限公司 * @date 2019年9月27日 上午9:18 */ @Service @RequiredArgsConstructor public class SysconfigServiceImpl extends SuperServiceImpl implements SysconfigService { private static final String USER_FIELD_CATEGORY = "UserFieldConfig"; private static final String USER_FIELD_KEY = "permission.user.fields"; private static final Set FORM_FIELDS = Set.of("account", "realName", "jobNumber", "positionId", "gender", "email", "mobilePhone", "ranks", "sortCode", "enabledMark", "description", "headIcon", "nation", "nativePlace", "certificatesType", "entryDate", "certificatesNumber", "education", "birthday", "telePhone", "landline", "urgentContacts", "urgentTelePhone", "postalAddress", "roleId"); private static final Set LIST_FIELDS = Set.of("account", "realName", "jobNumber", "gender", "mobilePhone", "position", "creatorTime", "lastModifyTime", "sortCode", "enabledMark", "disableTime"); private static final Set CORE_FIELDS = Set.of("account", "realName"); @Override public UserFieldConfig getUserFieldConfig() { SysConfigEntity entity = getOne(new QueryWrapper().lambda() .eq(SysConfigEntity::getCategory, USER_FIELD_CATEGORY).eq(SysConfigEntity::getFkey, USER_FIELD_KEY), false); return entity == null ? new UserFieldConfig() : UserFieldConfig.fromCompactString(entity.getValue()); } @Override public void saveUserFieldConfig(UserFieldConfig config) { if (config == null) throw new IllegalArgumentException("用户字段配置不能为空"); validateFormFields(config.getForm()); if (config.getList() == null || !config.getList().keySet().equals(LIST_FIELDS) || config.getList().values().stream().anyMatch(rule -> rule == null || rule.isRequired()) || config.getList().values().stream().noneMatch(UserFieldConfig.FieldRule::isVisible)) { throw new IllegalArgumentException("列表字段配置无效"); } SysConfigEntity entity = getOne(new QueryWrapper().lambda() .eq(SysConfigEntity::getCategory, USER_FIELD_CATEGORY).eq(SysConfigEntity::getFkey, USER_FIELD_KEY), false); if (entity == null) { entity = new SysConfigEntity(); entity.setId(UUID.randomUUID().toString().replace("-", "")); entity.setCategory(USER_FIELD_CATEGORY); entity.setFkey(USER_FIELD_KEY); entity.setFullName("用户管理字段配置"); } entity.setValue(config.toCompactString()); saveOrUpdate(entity); } private void validateFormFields(Map fields) { if (fields == null || !fields.keySet().equals(FORM_FIELDS)) { throw new IllegalArgumentException("表单字段配置无效"); } for (Map.Entry entry : fields.entrySet()) { UserFieldConfig.FieldRule rule = entry.getValue(); if (rule == null || (rule.isRequired() && !rule.isVisible()) || (CORE_FIELDS.contains(entry.getKey()) && (!rule.isVisible() || !rule.isRequired()))) { throw new IllegalArgumentException("字段配置无效: " + entry.getKey()); } } } public static final String SYS_CONFIG ="SysConfig"; public static final String MP_CONFIG ="MPConfig"; public static final String QYH_CONFIG ="QYHConfig"; private final RedisUtil redisUtil; private final CacheKeyUtil cacheKeyUtil; private final Pop3Util pop3Util; @Override public List getList(String type) { List list = new ArrayList<>(); if ("WeChat".equals(type)) { QueryWrapper queryWrapper = new QueryWrapper<>(); list = this.list(queryWrapper).stream().filter(t -> QYH_CONFIG.equals(t.getCategory()) || MP_CONFIG.equals(t.getCategory())).collect(Collectors.toList()); } if (SYS_CONFIG.equals(type)) { QueryWrapper queryWrapper = new QueryWrapper<>(); list = this.list(queryWrapper).stream().filter(t -> !QYH_CONFIG.equals(t.getCategory()) && !MP_CONFIG.equals(t.getCategory()) && !USER_FIELD_CATEGORY.equals(t.getCategory())).collect(Collectors.toList()); } return list; } @Override public BaseSystemInfo getWeChatInfo() { Map objModel = new HashMap<>(16); List list = this.getList("WeChat"); for (SysConfigEntity entity : list) { objModel.put(entity.getFkey(), entity.getValue()); } return JsonUtil.getJsonToBean(objModel, BaseSystemInfo.class); } @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 objModel = new HashMap<>(16); List list = this.getList(SYS_CONFIG); 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 entitys) { String cacheKey = cacheKeyUtil.getSystemInfo(); redisUtil.remove(cacheKey); this.baseMapper.deleteFig(); for (SysConfigEntity entity : entitys) { entity.setCategory(SYS_CONFIG); this.baseMapper.insert(entity); } } @Override @DSTransactional public boolean saveMp(List entitys) { String cacheKey = cacheKeyUtil.getWechatConfig(); int flag = 0; redisUtil.remove(cacheKey); this.baseMapper.deleteMpFig(); for (SysConfigEntity entity : entitys) { entity.setCategory(MP_CONFIG); if (this.baseMapper.insert(entity) > 0) { flag++; } } return entitys.size() == flag; } @Override @DSTransactional public void saveQyh(List entitys) { String cacheKey = cacheKeyUtil.getWechatConfig(); redisUtil.remove(cacheKey); this.baseMapper.deleteQyhFig(); for (SysConfigEntity entity : entitys) { entity.setCategory(QYH_CONFIG); 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()); mailAccount.setSsl("1".equals(String.valueOf(configEntity.getEmailSsl()))); 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 queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(SysConfigEntity::getFkey, keyStr); List list = list(queryWrapper); if (CollUtil.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 objModel = new HashMap<>(); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(SysConfigEntity::getCategory, "SocialsConfig"); List 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 entitys) { String cacheKey = cacheKeyUtil.getSocialsConfig(); redisUtil.remove(cacheKey); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(SysConfigEntity::getCategory, "SocialsConfig"); this.remove(queryWrapper); for (SysConfigEntity entity : entitys) { this.baseMapper.insert(entity); } } }