WMS/HHWMS.Web/DataContext.cs
zhougf 48c63bb15d feat: 新增库存批次和Lot查询功能,优化入库界面和报表处理
refactor: 重构SimpleReceiptIn界面,增加历史记录和自动完成功能

fix: 修复报表参数处理和SQL解码问题

perf: 优化WCS接口调用和库存计算逻辑

style: 清理无用代码和调整格式

chore: 更新数据库连接配置和调试模式设置
2025-09-27 23:10:21 +08:00

134 lines
5.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using HHWMS.Web.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
namespace HHWMS.Web
{
public class DataContext : FrameworkContext
{
public DataContext(CS cs)
: base(cs)
{
}
public DataContext(string cs, DBTypeEnum dbtype)
: base(cs, dbtype)
{
}
public DataContext(string cs, DBTypeEnum dbtype, string version = null)
: base(cs, dbtype, version)
{
}
public DataContext(DbContextOptions<DataContext> options) : base(options) { }
public DbSet<FrameworkUser> FrameworkUsers { get; set; }
public DbSet<TaskHeader> TaskHeaders { get; set; }
public DbSet<TaskDetail> TaskDetails { get; set; }
public DbSet<TaskHeaderHistory> TaskHeaderHistories { get; set; }
public DbSet<TaskDetailHistory> TaskDetailHistories { get; set; }
public DbSet<SysCount> SysCounts { get; set; }
public DbSet<SysJob> SysJobs { get; set; }
public DbSet<SysDictType> SysDictTypes { get; set; }
public DbSet<SysDictData> SysDictDatas { get; set; }
public DbSet<Location> Locations { get; set; }
public DbSet<Container> Containers { get; set; }
public DbSet<Inventory> Inventories { get; set; }
public DbSet<InventoryTransaction> InventoryTransactions { get; set; }
public DbSet<ReceiptHeader> ReceiptHeaders { get; set; }
public DbSet<ReceiptDetail> ReceiptDetails { get; set; }
public DbSet<ShipmentHeader> ShipmentHeaders { get; set; }
public DbSet<ShipmentDetail> ShipmentDetails { get; set; }
public DbSet<ReceiptHeaderHistory> ReceiptHeaderHistories { get; set; }
public DbSet<ReceiptDetailHistory> ReceiptDetailHistories { get; set; }
public DbSet<ShipmentHeaderHistory> ShipmentHeaderHistories { get; set; }
public DbSet<ShipmentDetailHistory> ShipmentDetailHistories { get; set; }
public DbSet<ReportDesign> ReportDesigns { get; set; }
public DbSet<Material> Materials { get; set; }
public DbSet<MaterialProcess> MaterialProcesses { get; set; }
public DbSet<PrintConfig> PrintConfigs { get; set; }
public DbSet<LocationZone> LocationZones { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<ProcessConfig> ProcessConfigs { get; set; }
public override async Task<bool> DataInit(object allModules, bool IsSpa)
{
var state = await base.DataInit(allModules, IsSpa);
bool emptydb = false;
try
{
emptydb = Set<FrameworkUser>().Count() == 0 && Set<FrameworkUserRole>().Count() == 0;
}
catch { }
if (state == true || emptydb == true)
{
//when state is true, means it's the first time EF create database, do data init here
//当state是true的时候,表示这是第一次创建数据库,可以在这里进行数据初始化
var user = new FrameworkUser
{
ITCode = "admin",
Password = Utils.GetMD5String("000000"),
IsValid = true,
Name = "Admin",
TenantCode = TenantCode
};
var userrole = new FrameworkUserRole
{
UserCode = user.ITCode,
RoleCode = "001",
TenantCode= TenantCode
};
Set<FrameworkUser>().Add(user);
Set<FrameworkUserRole>().Add(userrole);
await SaveChangesAsync();
}
return state;
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// ef生成表 添加字段注释,只有本项目的表才添加,需要在DisplayAttribute,添加Description
var ddd = modelBuilder.Model.GetEntityTypes();
ddd = ddd.Where(x => x.ClrType.FullName.Contains("HHWMS.Web")).ToList();
foreach (var item in ddd)
{
var props = item.ClrType.GetProperties();
var descriptionAttrtable = item.ClrType.GetCustomAttributes(typeof(DisplayAttribute), true);
if (descriptionAttrtable.Length > 0)
{
modelBuilder.Entity(item.Name).HasComment(((DisplayAttribute)descriptionAttrtable[0]).Description ?? "");
}
foreach (var prop in props)
{
var descriptionAttr = prop.GetCustomAttributes(typeof(DisplayAttribute), true);
if (descriptionAttr.Length > 0)
{
modelBuilder.Entity(item.Name).Property(prop.Name).HasComment(((DisplayAttribute)descriptionAttr[0]).Description ?? "");
}
}
}
}
}
public class DataContextFactory : IDesignTimeDbContextFactory<DataContext>
{
public DataContext CreateDbContext(string[] args)
{
return new DataContext("Server=127.0.0.1;Port=3306;Database=wms3;Uid=root;Password=abc123*;Character Set=utf8mb4;", DBTypeEnum.MySql);
}
}
}