using SqlSugar; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OrmTest { internal class Unitdsfasdfys { public static void Init() { ConnectionConfig config = new ConnectionConfig(); config.ConnectionString =DbHelper.Connection; config.ConfigId = "0"; config.IsAutoCloseConnection = true; config.DbType = SqlSugar.DbType.SqlServer; config.MoreSettings = new ConnMoreSettings() { SqlServerCodeFirstNvarchar = true }; config.ConfigureExternalServices = new ConfigureExternalServices() { EntityService = (c, p) => { if (c.PropertyType == typeof(object) && p.DataType == "sql_variant") { p.SqlParameterDbType = SqlDbType.Variant; } if (p.IsPrimarykey == false && c.PropertyType.IsGenericType && c.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))//自动可为空 { p.IsNullable = true; } if (p.PropertyName.ToLower() == "id" && p.IsPrimarykey)//默认Id这个为主键 { p.IsPrimarykey = true; if (p.PropertyInfo.PropertyType == typeof(int)) { p.IsIdentity = true;//是id并且是int的是自增 } } }, EntityNameService = (type, entity) => { //entity.DbTableName 修改表名 } }; SqlSugarScope sqlSugar = new SqlSugarScope(new List() { config }, db => { db.GetConnectionScope("0").Aop.OnError = (exp) => { var sql = exp.Sql; var parameters = exp.Parametres; var str = $"0--SqlSugar异常 :{exp}"; }; db.GetConnectionScope("0").Aop.OnLogExecuting = (sql, pars) => { string msg = $"0--SqlSugar 执行了Sql语句:{sql}"; }; }); sqlSugar.GetConnectionScope("0").DbMaintenance.CreateDatabase(); var Db = sqlSugar.AsTenant().GetConnectionScope("0"); Db.CodeFirst.InitTables(new Type[] { typeof(TestDateTime) }); Db.DbMaintenance.TruncateTable(); TestDateTime testDateTime = new TestDateTime() { CreateTime = DateTime.Parse("2025-03-29 09:27:37.9991749") }; Db.Insertable(testDateTime).ExecuteCommand(); var data = Db.Queryable().First().CreateTime.ToString("yyyy-MM-dd HH:mm:ss.fffffff"); if (data != "2025-03-29 09:27:37.9991749") throw new Exception("unit error"); var result = Db.Queryable() .Select(s => (decimal?)s.TotalArrearsAmount) .ToSqlString(); //if(result.Contains("*")) throw new Exception("unit error"); } } /// /// 供应商对账单 /// [SugarIndex($"index_{{table}}_{nameof(SheetNo)}", nameof(SheetNo), OrderByType.Asc)] [SugarIndex($"index_{{table}}_{nameof(SupplierId)}", nameof(SupplierId), OrderByType.Asc)] [SugarIndex($"index_{{table}}_{nameof(BisDate)}", nameof(BisDate), OrderByType.Asc)] [SugarIndex($"index_{{table}}_{nameof(BeginDate)}", nameof(BeginDate), OrderByType.Asc)] [SugarIndex($"index_{{table}}_{nameof(EndDate)}", nameof(EndDate), OrderByType.Asc)] public class SupplierStatementOrder { /// /// 雪花Id /// [SugarColumn(IsNullable = false, IsPrimaryKey = true, IsIdentity = false)] public long Id { get; set; } /// /// 单号 /// public string SheetNo { get; set; } /// /// 供应商 /// public long SupplierId { get; set; } /// /// 对账起始时间 /// public DateTime BeginDate { get; set; } /// /// 对账结束时间 /// public DateTime EndDate { get; set; } /// /// 结算日期 /// public DateTime BisDate { get; set; } /// /// 合计金额 /// [SugarColumn(Length = 18, DecimalDigits = 2, DefaultValue = "0")] public decimal TotalAmount { get; set; } /// /// 仅查询当前交易期业务单据 /// [SugarColumn(DefaultValue = "1")] public bool OnlyCurrentPeriod { get; set; } = true; /// /// 备注 /// [SugarColumn(IsNullable = true)] public string Remark { get; set; } /// /// 审核日期 /// public DateTime? ApproveTime { get; set; } /// /// 审核人 /// public long ApproveBy { get; set; } /// /// 审核人名称 /// [SugarColumn(IsNullable = true)] public string ApproveByName { get; set; } #region 账款信息 /// /// 上期结欠金额
/// 期初+记账+调整-付款【查询账款流水,按交易期间开始日期前的发生金额合计】--》供应商第一次对账时,这样取;后续的上期结欠=该供应商上一张对账单的累计结欠! ///
[SugarColumn(Length = 18, DecimalDigits = 2, DefaultValue = "0")] public decimal LastArrearsAmount { get; set; } /// /// 本期购货金额
/// 本期购货情况的金额合计 ///
[SugarColumn(Length = 18, DecimalDigits = 2, DefaultValue = "0")] public decimal PeriodPurchaseAmount { get; set; } /// /// 本期付款
/// 本期付款情况的金额合计 ///
[SugarColumn(Length = 18, DecimalDigits = 2, DefaultValue = "0")] public decimal PeriodPayAmount { get; set; } /// /// 累计结欠
/// 上期结欠+本期购货金额-本期付款 ///
[SugarColumn(Length = 18, DecimalDigits = 2, DefaultValue = "0")] public decimal TotalArrearsAmount { get; set; } #endregion } public class TestDateTime { [SugarColumn(IsPrimaryKey = true)] public int Id { get; set; } [SugarColumn(SqlParameterDbType=System.Data.DbType.DateTime2 , ColumnDataType = "datetime2(7)")] public DateTime CreateTime { get; set; } } }