com.hn.utils.generate.GenUtils Maven / Gradle / Ivy
package com.hn.utils.generate;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.simple.SimpleDataSource;
import cn.hutool.db.sql.SqlExecutor;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.hn.config.HnConfigUtils;
import com.hn.upload.AliUpload;
import com.hn.upload.QiNiuUpload;
import com.hn.upload.TencentUpload;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.Map;
/**
* 描述:
* 生成sql语句工具类
*
* demo :
* // 数据源配置
* String url = "jdbc:mysql://localhost:3306/test";
* String user = "root";
* String pass = "root";
* GenUtils genUtils = GenUtils.init(url, user, pass);
* // 配置聚合短信配置
* genUtils.juHe("9cc0a5e0580e51ca13ff8bf0a0561d6a","166458");
* // 测试配置是否生效
* HnConfigUtils.init(url,user,pass);
* SmsUtil.send(MsgPlatform.JU_HE,"15755141234","123456");
*
* @author fei
* @since 2019-08-23 09:14
*/
public class GenUtils {
private static final Log log = LogFactory.get();
private static final String CONFIG_TABLE = "hn_config";
private Db db;
private Map map;
private GenUtils() {
}
/**
* 构造函数,初始化数据源
*
* @param url jdbc url
* @param user 用户名
* @param pass 密码
*/
private GenUtils(String url, String user, String pass) {
DataSource ds = new SimpleDataSource(url, user, pass);
genHnConfig(ds);
}
/**
* 构造函数,初始化数据源
*
* @param ds 数据源
*/
private GenUtils(DataSource ds) {
genHnConfig(ds);
}
private GenUtils(Map map) {
this.map = map;
}
/**
* 生成 hn_config 数据表
*
* @param ds 数据源
*/
private void genHnConfig(DataSource ds) {
db = Db.use(ds);
// 执行非查询语句,返回影响的行数
try {
String result = db.queryString("show tables like '" + CONFIG_TABLE + "'");
if (CONFIG_TABLE.equals(result)) {
log.info("数据表已存在:{}", result);
return;
}
// 获取连接
Connection conn = null;
try {
conn = db.getConnection();
} catch (SQLException e) {
throw new SQLException("sql连接失败");
}
int count = SqlExecutor.execute(conn, "CREATE TABLE if not EXISTS `" + CONFIG_TABLE + "` (" +
" `config_id` int(5) NOT NULL AUTO_INCREMENT COMMENT '参数主键'," +
" `config_name` varchar(100) DEFAULT '' COMMENT '参数名称'," +
" `config_key` varchar(100) DEFAULT '' COMMENT '参数键名'," +
" `config_value` varchar(100) DEFAULT '' COMMENT '参数键值'," +
" `create_by` varchar(64) DEFAULT '' COMMENT '创建者'," +
" `create_time` datetime DEFAULT NULL COMMENT '创建时间'," +
" `update_by` varchar(64) DEFAULT '' COMMENT '更新者'," +
" `update_time` datetime DEFAULT NULL COMMENT '更新时间'," +
" PRIMARY KEY (`config_id`)" +
") ENGINE=InnoDB AUTO_INCREMENT=110 DEFAULT CHARSET=utf8mb4 COMMENT='参数配置表'");
log.info("影响行数:{}", count);
SqlExecutor.execute(conn, "ALTER TABLE " + CONFIG_TABLE + " ADD unique(config_key)");
} catch (SQLException e) {
log.info("数据表已存在");
// throw new ConfigException("sql异常:插入hn-config数据表失败");
}
}
/**
* 初始化
*
* @param map 参数map
* @return GenUtils
*/
public static GenUtils init(Map map) {
return new GenUtils(map);
}
/**
* 初始化数据源
*
* @param url jdbc url
* @param user 用户名
* @param pass 密码
* @return GenUtils
*/
public static GenUtils init(String url, String user, String pass) {
return new GenUtils(url, user, pass);
}
/**
* 初始化数据源
*
* @param ds 数据源
* @return GenUtils
*/
public static GenUtils init(DataSource ds) {
return new GenUtils(ds);
}
/**
* 场景类型
*/
private String scene;
/**
* 场景
*
* @param scene 场景类型
* @return this GenUtils
*/
public GenUtils scene(String scene) {
this.scene = scene;
return this;
}
/**
* 创蓝253
*
* @param apiAccount 账号
* @param apiSecret 密码
* @param msgModel 消息模板
*/
public GenUtils chuangLan(String apiAccount, String apiSecret, String msgModel) {
String configKey = getSceneConfigKey("sms.chuangLan");
insert("【创蓝253】账号", configKey.concat(".apiAccount"), apiAccount);
insert("【创蓝253】密码", configKey.concat(".apiSecret"), apiSecret);
insert("【创蓝253】消息模板", configKey.concat(".msgModel"), msgModel);
return this;
}
/**
* 创瑞短信
*
* @param accesskey 开发key
* @param secret 密码
* @param sign 消息模板
* @param templateId 模板id
*/
public GenUtils chuangRui(String accesskey, String secret, String sign, String templateId) {
String configKey = getSceneConfigKey("sms.chuangRui");
insert("【创瑞短信】开发key", configKey.concat(".accesskey"), accesskey);
insert("【创瑞短信】开发秘钥", configKey.concat(".secret"), secret);
insert("【创瑞短信】签名", configKey.concat(".sign"), sign);
insert("【创瑞短信】模板id", configKey.concat(".templateId"), templateId);
return this;
}
/**
* 聚合
*
* @param appkey appkey
* @param mobanId mobanId
*/
public GenUtils juHe(String appkey, String mobanId) {
String configKey = getSceneConfigKey("sms.juhe");
insert("【聚合短信】appkey", configKey.concat(".appkey"), appkey);
insert("【聚合短信】模板ID", configKey.concat(".mobanId"), mobanId);
return this;
}
/**
* 极光推送
*
* @param appkey appKey
* @param masterSecret masterSecret
* @param isProduce 极光 iOS是区分开发还是生产环境的 一般服务端配置为true
*/
public GenUtils jpush(String appkey, String masterSecret, boolean isProduce) {
insert("【极光推送】appKey", "push.jpush.appKey", appkey);
insert("【极光推送】masterSecret", "push.jpush.masterSecret", masterSecret);
insert("【极光推送】isProduce", "push.jpush.isProduce", String.valueOf(isProduce));
return this;
}
/**
* 环信
*
* @param projectId projectId
* @param orgName orgName
* @param appName appName
* @param clientId clientId
* @param clientSecret clientSecret
*/
public GenUtils easemob(String projectId, String orgName, String appName,
String clientId, String clientSecret) {
String configKey = getSceneConfigKey("im.easemob");
insert("【环信】projectId", configKey.concat(".projectId"), projectId);
insert("【环信】Orgname", configKey.concat(".orgName"), orgName);
insert("【环信】appName", configKey.concat(".appName"), appName);
insert("【环信】Client ID", configKey.concat(".clientId"), clientId);
insert("【环信】Client Secret", configKey.concat(".clientSecret"), clientSecret);
return this;
}
/**
* 支付宝
*
* @param appId appId
* @param publicKey 支付宝公钥
* @param privateKey 应用私钥
* @param serviceUrl 应用网关
* @param returnUrl 同步返回地址
* @param notifyUrl 异步通知地址
*/
public GenUtils alipay(String appId, String publicKey, String privateKey,
String serviceUrl, String returnUrl, String notifyUrl) {
String configKey = getSceneConfigKey("pay.alipay");
insert("【支付宝】appId", configKey.concat(".appid"), appId);
insert("【支付宝】支付宝公钥", configKey.concat(".publicKey"), publicKey);
insert("【支付宝】应用私钥", configKey.concat(".privateKey"), privateKey);
insert("【支付宝】应用网关", configKey.concat(".serviceUrl"), serviceUrl);
insert("【支付宝】同步返回地址", configKey.concat(".returnUrl"), returnUrl);
insert("【支付宝】异步通知地址", configKey.concat(".notifyUrl"), notifyUrl);
return this;
}
/**
* 微信支付
*
* @param appid 应用ID
* @param mchId 商户号
* @param partnerKey 商户密钥
* @param notifyUrl 异步通知地址
*/
public GenUtils wxPay(String appid, String mchId, String partnerKey, String notifyUrl) {
String configKey = getSceneConfigKey("pay.wx");
insert("【微信支付】应用ID", configKey.concat(".appid"), appid);
insert("【微信支付】商户号", configKey.concat(".mchId"), mchId);
insert("【微信支付】商户密钥", configKey.concat(".partnerKey"), partnerKey);
insert("【微信支付】异步通知地址", configKey.concat(".notifyUrl"), notifyUrl);
return this;
}
/**
* 钉钉机器人配置
*
* @param accessToken 访问令牌
* @param secretKey 密钥
*/
public GenUtils dingRobot(String accessToken, String secretKey) {
insert("【钉钉机器人】访问令牌", "robot.ding.accessToken", accessToken);
insert("【钉钉机器人】密钥", "robot.ding.secretKey", secretKey);
return this;
}
/**
* 微信小程序配置
*
* @param appId appId
* @param appSecret appSecret
*/
public GenUtils wxSmallProgram(String appId, String appSecret) {
String configKey = getSceneConfigKey("wx.smallProgram");
insert("【微信小程序】appId", configKey.concat(".appId"), appId);
insert("【微信小程序】appSecret", configKey.concat(".appSecret"), appSecret);
return this;
}
/**
* 腾讯地图配置
*
* @param key 开发密钥
*/
public GenUtils qqMap(String key) {
String configKey = getSceneConfigKey("map.qq");
insert("【腾讯地图】key", configKey.concat(".key"), key);
return this;
}
/**
* 阿里云
*
* @param param {@link AliUpload.Param}
*/
public GenUtils uploadAli(AliUpload.Param param) {
String configKey = getSceneConfigKey("upload.ali");
insert("【阿里云存储】endpoint", configKey.concat(".endpoint"), param.getEndpoint());
insert("【阿里云存储】accessKey", configKey.concat(".accessKey"), param.getAccessKey());
insert("【阿里云存储】secretKey", configKey.concat(".secretKey"), param.getSecretKey());
insert("【阿里云存储】bucket", configKey.concat(".bucket"), param.getBucket());
insert("【阿里云存储】fileUrl", configKey.concat(".fileUrl"), param.getFileUrl());
return this;
}
/**
* 七牛云
*
* @param param {@link QiNiuUpload.Param}
*/
public GenUtils uploadQiNiu(QiNiuUpload.Param param) {
String configKey = getSceneConfigKey("upload.qiNiu");
insert("【七牛云存储】accessKey", configKey.concat(".accessKey"), param.getAccessKey());
insert("【七牛云存储】secretKey", configKey.concat(".secretKey"), param.getSecretKey());
insert("【七牛云存储】bucket", configKey.concat(".bucket"), param.getBucket());
insert("【七牛云存储】fileUrl", configKey.concat(".fileUrl"), param.getFileUrl());
insert("【七牛云存储】区域名称region", configKey.concat(".region"), param.getRegion());
return this;
}
/**
* 腾讯云
*
* @param param {@link TencentUpload.Param}
*/
public GenUtils uploadTencent(TencentUpload.Param param) {
String configKey = getSceneConfigKey("upload.tencent");
insert("【腾讯云存储】secretId", configKey.concat(".secretId"), param.getSecretId());
insert("【腾讯云存储】secretKey", configKey.concat(".secretKey"), param.getSecretKey());
insert("【腾讯云存储】region", configKey.concat(".region"), param.getRegion());
insert("【腾讯云存储】bucket", configKey.concat(".bucket"), param.getBucket());
insert("【腾讯云存储】fileUrl", configKey.concat(".fileUrl"), param.getFileUrl());
return this;
}
/**
* configKey
* @param configKey configKey
* @return String
*/
private String getSceneConfigKey(String configKey){
if (StrUtil.isNotBlank(scene)) {
configKey = configKey.concat("-").concat(scene);
}
return configKey;
}
/**
* 增加sql {@link HnConfigUtils#CONFIG_TABLE }
*
* @param configName 配置注释
* @param configKey 配置key
* @param configValue 配置value
*/
private void insert(String configName, String configKey, String configValue) {
if (db == null) {
map.put(configKey, configValue);
return;
}
//默认数据源
Entity entity = Entity.create(HnConfigUtils.CONFIG_TABLE)
.set("config_name", configName)
.set("config_key", configKey)
.set("config_value", configValue)
.set("create_time", new Date());
try {
db.insert(entity);
} catch (SQLException e) {
log.debug("{}已存在配置,更新配置config_name={},config_value={}", configKey, configName, configValue);
// 已经存在记录,就更新
entity = Entity.create().set("config_name", configName)
.set("config_value", configValue)
.set("update_time", new Date());
Entity whereEntity = Entity.create(HnConfigUtils.CONFIG_TABLE)
.set("config_key", configKey);
try {
db.update(entity, whereEntity);
} catch (SQLException e1) {
log.error("{}已存在配置,更新配置config_name={},config_value={}失败",
configKey, configName, configValue);
}
}
}
public Map getMap() {
return map;
}
}