@ -1,4 +1,5 @@
using SlnMesnac.RfidUpload.Model ;
using SlnMesnac.RfidUpload.NLog ;
using SqlSugar ;
using System ;
@ -29,49 +30,100 @@ using System.Text;
#endregion << 版 本 注 释 >>
namespace slnmesnac.rfidupload.Repository
{
internal class SqlSugarHelper
public class SqlSugarHelper
{
#region 连接字符串
/ * *
* Sqlite : 获 取 debug 路 径 下 的 数 据 库 文 件 , 不 建 议 直 接 写 死 路 径
* private static string sqliteConnStr = $"Data Source={Path.GetFullPath(" data \ \ data . db ")};Version=3" ;
* /
private static string sqliteConnStr = "Data Source=Z:\\Desktop\\日常代码\\HighWayIot\\HighWayIot\\bin\\Debug\\data\\data.db;Version=3" ;
private static SqlSugarHelper instance = null ;
private static readonly object padlock = new object ( ) ;
/ * *
* Mysql
* /
private static string mysqlConnStr = "Data Source=124.70.63.37;Port=6000;Initial Catalog=ry-cloud;uid=root;pwd=haiwei@123;Charset=utf8mb4;SslMode=none" ;
private SqlSugarHelper ( ) { }
public static SqlSugarHelper Instance
{
get
{
lock ( padlock )
{
if ( instance = = null )
instance = new SqlSugarHelper ( ) ;
return instance ;
}
}
}
private static string oracleConnStr = "Data Source=175.27.215.92/helowin;User ID=aucma_mes;Password=aucma" ;
# endregion
private static LogHelper logHelper = LogHelper . Instance ;
//如果是固定多库可以传 new SqlSugarScope(List<ConnectionConfig>,db=>{}) 文档:多租户
//如果是不固定多库 可以看文档Saas分库
public static string url = System . Environment . CurrentDirectory + "/db/" ;
static string ConnectionString1 = $"Data Source={url}/hw.db" ;
//用单例模式
public static SqlSugarScope Db = new SqlSugarScope ( new List < ConnectionConfig > ( )
public static SqlSugarClient db
{
new ConnectionConfig ( )
get = > new SqlSugarClient ( new ConnectionConfig ( )
{
ConfigId = "sqlite" ,
ConnectionString = sqliteConnStr ,
DbType = DbType . Sqlite ,
InitKeyType = InitKeyType . Attribute ,
IsAutoCloseConnection = true
}
} ,
db = >
{
//(A)全局生效配置点
//调试SQL事件, 可以删掉
db . Aop . OnLogExecuting = ( sql , pars ) = >
{
//logHelper.SqlLog($"{sql};参数:{jsonChange.ModeToJson(pars)}");
} ;
} ) ;
ConnectionString = $"Data Source={url}/hw.db" ,
DbType = SqlSugar . DbType . Sqlite , //必填, 数据库类型
IsAutoCloseConnection = true , //默认false, 时候知道关闭数据库连接, 设置为true无需使用using或者Close操作
InitKeyType = InitKeyType . SystemTable //默认SystemTable, codefist需要使用Attribute
} ) ;
}
/// <summary>
/// 插入记录
/// </summary>
/// <param name="printRecord"></param>
/// <returns></returns>
public int InsertRecord ( UploadRecord record )
{
int result = db . Insertable ( record ) . ExecuteCommand ( ) ;
return result ;
}
/// <summary>
/// 查询记录列表--不包含strList字段
/// </summary>
/// <param name="printRecord"></param>
/// <returns></returns>
public List < UploadRecord > GetRecordList ( )
{
List < UploadRecord > list ;
list = db . SqlQueryable < UploadRecord > ( "select id,UPLOAD_RECORD.ffjhNo,opBatch,ffjhscrq,stationOrgCode,stationOrgName,strList,isSuccess from UPLOAD_RECORD" ) . ToList ( ) ;
return list ;
}
/// <summary>
/// 根据封发计划编号查询记录
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public string GetRecordByFfjhNo ( string ffjhNo )
{
string strList = db . Ado . GetString ( $"select * from UPLOAD_RECORD where ffjhNo='{ffjhNo}'" ) ;
return strList ;
}
/// <summary>
/// 插入记录
/// </summary>
/// <param name="printRecord"></param>
/// <returns></returns>
public int InsertPrintCode ( UploadRecord record )
{
int result = db . Insertable < UploadRecord > ( record ) . ExecuteCommand ( ) ;
return result ;
}
/// <summary>
/// 根据封发计划编号改变上传记录上传结果
/// </summary>
/// <param name="printRecord"></param>
/// <returns></returns>
public int DeletePrintCode ( string ffjhNo , bool uploadResult )
{
int flag = uploadResult ? 1 : 0 ;
string sql = $"UPDATE UPLOAD_RECORD SET isSuccess = {flag} WHERE ffjhNo = {ffjhNo}" ;
int result = db . Updateable < UploadRecord > ( sql ) . ExecuteCommand ( ) ;
return result ;
}
}
}