WMS/HHWMS.Web/DataContext.cs
sunwang c76e7bb681 fix:永丰售后修复BUG,
1,物料库存获取不到ERP:重新调整触发查询逻辑
2,工序变更导致物料无法出库:配置旧工序
3,测试出库订单ERP推送,任务下发
2026-10-29 13:12:11 +08:00

140 lines
5.9 KiB
C#
Raw Permalink 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 HHWMS.Web.Models.Agv;
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 DbSet<AgvTask> AgvTasks { get; set; }
public DbSet<AgvTaskHistory> AgvTaskHistories { get; set; }
public DbSet<AgvTaskLog> AgvTaskLogs { get; set; }
public DbSet<AgvTaskLogHistory> AgvTaskLogHistories { get; set; }
public DbSet<AgvTaskSceneConfig> AgvTaskSceneConfigs { 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);
}
}
}