[feature] python兼容层基本完成

This commit is contained in:
lichx 2024-11-03 11:21:47 +08:00
parent d33ff74a51
commit 03edc76960
21 changed files with 488 additions and 15 deletions

View File

@ -0,0 +1 @@


View File

@ -0,0 +1,35 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>2c74bf2c-c988-45c5-8f7e-1822c7358818</ProjectGuid>
<ProjectHome>.</ProjectHome>
<StartupFile>SiteManagementService.py</StartupFile>
<SearchPath>
</SearchPath>
<WorkingDirectory>.</WorkingDirectory>
<OutputPath>.</OutputPath>
<Name>SiteManagementService</Name>
<RootNamespace>SiteManagementService</RootNamespace>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugSymbols>true</DebugSymbols>
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugSymbols>true</DebugSymbols>
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
</PropertyGroup>
<ItemGroup>
<Compile Include="SiteManagementService.py" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets" />
<!-- Uncomment the CoreCompile target to enable the Build command in
Visual Studio and specify your pre- and post-build commands in
the BeforeBuild and AfterBuild targets below. -->
<!--<Target Name="CoreCompile" />-->
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
</Project>

View File

@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.11.35327.3
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SiteManagementSystem(SoftwareEngineering)", "SiteManagementSystem(SoftwareEngineering)\SiteManagementSystem(SoftwareEngineering).csproj", "{CB0750D4-7BC3-4D7D-B9E1-AADA28283DFB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SiteManagementSystem(SoftwareEngineering)", "SiteManagementSystem(SoftwareEngineering)\SiteManagementSystem(SoftwareEngineering).csproj", "{CB0750D4-7BC3-4D7D-B9E1-AADA28283DFB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

View File

@ -1,4 +1,4 @@
namespace IwutMail.Model
namespace SiteManagementSystem_SoftwareEngineering_.Configuration
{
public class SecretConfig
{

View File

@ -1,4 +1,4 @@
namespace SiteManagementSystem_SoftwareEngineering_.Model
namespace SiteManagementSystem_SoftwareEngineering_.Configuration
{
public class TokenFactoryConfiguration
{

View File

@ -1,8 +1,30 @@
using Microsoft.AspNetCore.Mvc;
using SiteManagementSystem_SoftwareEngineering_.Entity;
using SiteManagementSystem_SoftwareEngineering_.Extension;
using SiteManagementSystem_SoftwareEngineering_.Interface;
using SiteManagementSystem_SoftwareEngineering_.Service;
namespace SiteManagementSystem_SoftwareEngineering_.Controllers
{
[Route("Field")]
public class FieldController : ControllerBase
{
IFieldService _fieldService;
SQLService _sql;
public FieldController(IFieldService fieldService,SQLService sql) { _fieldService = fieldService; _sql = sql; }
[HttpPost("AddField")]
public IActionResult AddField()
{
_sql.Fields.Add(new Field());
_sql.SaveChanges();
return Ok();
}
[HttpGet("test")]
public IActionResult test()
{
_fieldService.AddField("", "");
return this.Success();
}
}
}

View File

@ -0,0 +1,15 @@
using SiteManagementSystem_SoftwareEngineering_.Service;
using System.ComponentModel.DataAnnotations;
namespace SiteManagementSystem_SoftwareEngineering_.Entity
{
public class Field
{
[Key]
public Guid Id { get; set; } = Guid.NewGuid();
public string Name { get; set; } = null!;
public string Position { get; set; } = null!;
public string OpenTime { get; set; } = null!;
public FieldType Type { get; set; } = FieldType.Unspecified;
}
}

View File

@ -1,11 +1,8 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data;
using System.Security.Claims;
using System.Xml.Linq;
using Microsoft.AspNetCore.DataProtection;
using SiteManagementSystem_SoftwareEngineering_.Model;
using SiteManagementSystem_SoftwareEngineering_.Model;
using SiteManagementSystem_SoftwareEngineering_.Service;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Security.Claims;
namespace SiteManagementSystem_SoftwareEngineering_.Entity
{

View File

@ -0,0 +1,15 @@
using System.ComponentModel.DataAnnotations;
namespace SiteManagementSystem_SoftwareEngineering_.Entity
{
public class UserField
{
[Key]
public Guid Uid { get; set; }
//[Key]
public Guid Fid { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
// other properties
}
}

View File

@ -1,7 +1,6 @@
using IwutMail.Model;
using SiteManagementSystem_SoftwareEngineering_.Configuration;
using SiteManagementSystem_SoftwareEngineering_.Factory;
using SiteManagementSystem_SoftwareEngineering_.Interface;
using SiteManagementSystem_SoftwareEngineering_.Model;
using SiteManagementSystem_SoftwareEngineering_.Service;
using static SiteManagementSystem_SoftwareEngineering_.Service.UserManagerService;

View File

@ -4,9 +4,9 @@ using System.Text;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;
using SiteManagementSystem_SoftwareEngineering_.Configuration;
using SiteManagementSystem_SoftwareEngineering_.Entity;
using SiteManagementSystem_SoftwareEngineering_.Interface;
using SiteManagementSystem_SoftwareEngineering_.Model;
namespace SiteManagementSystem_SoftwareEngineering_.Factory
{

View File

@ -0,0 +1,8 @@
namespace SiteManagementSystem_SoftwareEngineering_.Interface
{
public interface IFieldService
{
public void AddField(string name, string value);
public void RentField();
}
}

View File

@ -1,8 +1,10 @@
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using SiteManagementSystem_SoftwareEngineering_.Extension;
using SiteManagementSystem_SoftwareEngineering_.Interface;
using SiteManagementSystem_SoftwareEngineering_.Service;
var builder = WebApplication.CreateBuilder(args);
@ -50,7 +52,7 @@ builder
})
.AddUserManager(options =>
options.HashSalt = builder.Configuration.GetValue<string>("SecretSalt")!
);
).AddScoped<IFieldService, FieldService>();
var app = builder.Build();
// Configure the HTTP request pipeline.

View File

@ -0,0 +1,214 @@
using System.Linq.Dynamic.Core;
using IronPython.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.Scripting.Hosting;
using SiteManagementSystem_SoftwareEngineering_.Entity;
using SiteManagementSystem_SoftwareEngineering_.Interface;
namespace SiteManagementSystem_SoftwareEngineering_.Service
{
public enum FieldType
{
Unspecified,
Tennis,
Basketball,
Badminton
}
public class FieldService : IFieldService
{
private readonly EntityFrameworkPythonCompatibilityAndInterpretationLayer _layer;
private readonly ScriptEngine _engine = Python.CreateEngine();
private readonly dynamic _service;
public FieldService(SQLService storageService)
{
_layer = new EntityFrameworkPythonCompatibilityAndInterpretationLayer(storageService);
var _engine = Python.CreateEngine();
var searchPaths = _engine.GetSearchPaths();
searchPaths.Add(@"./Service/PythonServiceFile/");
_engine.SetSearchPaths(searchPaths);
var source = _engine.CreateScriptSourceFromFile(@"./Service/PythonServiceFile/demo.py");
var scope = _engine.CreateScope();
scope.SetVariable("LAYER", _layer);
source.Execute(scope);
_service = scope.GetVariable("Service")();
}
public void AddField(string name, string value)
{
var t = (Field)
_service.AddField(
new Field
{
Name = "t1",
Position = "t2",
OpenTime = "t3"
}
);
Console.WriteLine(
$"{t.Id},{t.Name},{t.Type}"
);
//t();
//Console.WriteLine(_service.AddField());
throw new NotImplementedException();
}
public void RentField()
{
throw new NotImplementedException();
}
}
public class EntityFrameworkPythonCompatibilityAndInterpretationLayer
{
public readonly SQLHelperService<Field> FieldDb;
public readonly SQLHelperService<UserField> RecordDb;
public EntityFrameworkPythonCompatibilityAndInterpretationLayer(SQLService storageService)
{
FieldDb = new SQLHelperService<Field>(storageService, nameof(storageService.Fields));
RecordDb = new SQLHelperService<UserField>(
storageService,
nameof(storageService.UserFieldRecords)
);
}
}
public class SQLHelperService<TEntity>
where TEntity : class, new()
{
private readonly SQLService _sqlService;
private readonly DbSet<TEntity> _dbSet;
public SQLHelperService(SQLService sqlService, string dbName)
{
_sqlService = sqlService;
if (dbName.IsNullOrEmpty())
dbName = typeof(TEntity).Name;
_dbSet =
_sqlService.GetType().GetProperty(dbName)?.GetValue(_sqlService) as DbSet<TEntity>
?? throw new InvalidOperationException(
$"SQLHelper don't have DbSet which named {dbName}."
);
}
public SQLHelperService<TEntity> Add(TEntity entity) =>
Process((dbSet) => dbSet.Add(entity));
private SQLHelperService<TEntity> Update(TEntity oldEntity, TEntity newEntity) =>
Process(
(dbSet) =>
{
dbSet.Remove(oldEntity);
dbSet.Add(newEntity);
}
);
public SQLHelperService<TEntity> Update<TValue>(TEntity entity, string key, TValue value)
{
var t = FindAll(key, value);
switch (t.Count())
{
case 0:
throw new InvalidOperationException("No values match the criteria!");
case 1:
Update(t.First(), entity);
break;
default:
throw new InvalidOperationException("Multiple values match the criteria!");
}
return this;
}
public SQLHelperService<TEntity> AddOrUpdate<TValue>(
TEntity entity,
string key,
TValue value
)
{
var t = FindAll(key, value);
switch (t.Count())
{
case 0:
Add(entity);
break;
case 1:
Update(t.First(), entity);
break;
default:
throw new InvalidOperationException("Multiple values match the criteria!");
}
return this;
}
public TEntity? FindFirst<TValue>(string key, TValue value)
{
TEntity? ans = null;
if (value is null)
Process(key, (dbSet) => ans = dbSet.Where($"{key} == null").FirstOrDefault());
else
Process(key, (dbSet) => ans = dbSet.Where($"{key} == @0", value).FirstOrDefault());
return ans;
}
public IEnumerable<TEntity> FindAll<TValue>(string key, TValue value)
{
IEnumerable<TEntity> ans = null!;
var y = typeof(TEntity).GetType().GetProperty(key);
if (value is null)
Process(key, (dbSet) => ans = dbSet.Where($"{key} == null").ToList());
else
Process(key, (dbSet) => ans = dbSet.Where($"{key} == @0", value).ToList());
return ans;
}
public IEnumerable<TEntity> GetFirstNElements(int n)
{
List<TEntity> ans = null!;
Process((dbSet) => ans = dbSet.Take(n).ToList());
return ans;
}
public SQLHelperService<TEntity> Delete(TEntity entity) =>
Process((dbSet) => dbSet.Remove(entity));
public SQLHelperService<TEntity> TryDelete<TValue>(string key, TValue value)
{
var t = FindFirst(key, value);
if (t is null)
return this;
return Process((dbSet) => dbSet.Remove(t));
}
public SQLHelperService<TEntity> Save()
{
_sqlService.SaveChanges();
return this;
}
public SQLHelperService<TEntity> DeleteAll<TValue>(string key, TValue value) =>
Process((dbSet) => dbSet.RemoveRange(FindAll(key, value)));
public SQLHelperService<TEntity> Process(Action<DbSet<TEntity>> action)
{
action(_dbSet);
_sqlService.SaveChanges();
return this;
}
public TEntity GetDefaultEntity() => new();
public SQLHelperService<TEntity> Process(string key, Action<DbSet<TEntity>> action)
{
if (typeof(TEntity).GetProperty(key) is null)
throw new InvalidOperationException(
$"{typeof(TEntity).Name} does not have the property key: {key}"
);
action(_dbSet);
_sqlService.SaveChanges();
return this;
}
}
}

View File

@ -0,0 +1,17 @@
from layer import Layer
from field import Field
from user import User
from user_field import UserFieldRecord
class Service:
layer = Layer(LAYER)
x = 16
def AddField(self, cs_field) -> str:
field = Field(cs_field)
self.layer.add_field(field)
return ""

View File

@ -0,0 +1,45 @@
import clr
clr.AddReference('System')
from System import Guid
class FieldType:
UNSPECIFIED = 0
TENNIS = 1
BASKETBALL = 2
BADMINTON = 3
class Field:
id = ""
name = ""
position = ""
open_time = ""
type = FieldType.UNSPECIFIED
def __init__(self, *args, **kwargs):
if len(args) == 0:
pass
elif len(args) == 1:
field = args[0]
self.id = field.Id.ToString()
self.name = field.Name
self.position = field.Position
self.open_time = field.OpenTime
self.type = field.Type
elif len(args) == 4:
self.id, self.name, self.position, self.type = args
else:
raise ValueError("Invalid arguments for Field initialization")
def __str__(self):
return f"Field: Id: {self.id}, Name: {self.name}, Position: {self.position}, Type: {self.type}"
def parse_to_csharp_object(self, empty_field):
empty_field.Id = Guid.Parse(self.id)
empty_field.Name = self.name
empty_field.Position = self.position
empty_field.OpenTime = self.open_time
print(FieldType.UNSPECIFIED)
print(self.type)
empty_field.Type = self.type
return empty_field

View File

@ -0,0 +1,37 @@
import clr
from user import User
from field import Field
from user_field import UserFieldRecord
clr.AddReference('System')
from System import Guid, DateTime
class Layer:
def __init__(self, layer):
self._field_db = layer.FieldDb
self._record_db = layer.RecordDb
def add_field(self, field: Field) -> 'Layer':
self._field_db.Add(field.parse_to_csharp_object(self._field_db.GetDefaultEntity()))
return self
# The value property will
def find_field(self, key: str, value: str) -> [Field]:
return [Field(t) for t in self._field_db.FindAll(key, Guid.Parse(value))]
def remove_field(self, field: Field) -> 'Layer':
self._field_db.TryDelete(field.id, field.parse_to_csharp_object(self._field_db.GetDefaultEntity()).Id)
return self
def add_record(self, record: UserFieldRecord) -> 'Layer':
self._record_db.Add(record.parse_to_csharp_object(self._record_db.GetDefaultEntity()))
return self
def find_record(self, key: str, value: str) -> [UserFieldRecord]:
return [UserFieldRecord(t) for t in self._record_db.FindAll(key, Guid.Parse(value))]
# One User only allowed to book one field at a time
def remove_record(self, record: UserFieldRecord) -> 'Layer':
self._record_db.TryDelete("Uid", record.parse_to_csharp_object(self._record_db.GetDefaultEntity()).Uid)
return self

View File

@ -0,0 +1,18 @@

class RoleNames:
NOT_SUPPORT = 0
COMMON_USER = 1
ADMINISTRATOR = 2
class User:
id = ""
role = RoleNames.NOT_SUPPORT
name = ""
# x:
def __init__(self, user):
self.id = user.Id.ToString()
self.role = user.Role
self.role = user.Name

View File

@ -0,0 +1,41 @@
from field import *
from user import *
import time
import clr
clr.AddReference('System')
from System import Guid, Convert
class UserFieldRecord:
fid = ""
uid = ""
start_time: time.struct_time
end_time: time.struct_time
def __init__(self, *args, **kwargs):
if len(args) == 0:
pass
if len(args) == 1:
field_record = args[0]
self.fid = field_record.Fid.ToString()
self.uid = field_record.Uid.ToString()
self.start_time = time.strptime(field_record.StartTime.ToString(), "%Y-%m-%d %H:%M:%S")
self.end_time = time.strptime(field_record.EndTime.ToString(), "%Y-%m-%d %H:%M:%S")
elif len(args) == 4 and isinstance(args[0], User) and isinstance(args[1], Field) and isinstance(args[2],
time.struct_time) and isinstance(
args[3], time.struct_time):
user, field, start_time, end_time = args
self.fid = field.id
self.uid = user.id
self.start_time = start_time
self.end_time = end_time
else:
raise ValueError("Invalid arguments for UserFieldRecord initialization")
def parse_to_csharp_object(self, empty_field_record):
empty_field_record.Fid = Guid.Parse(self.fid)
empty_field_record.Uid = Guid.Parse(self.uid)
empty_field_record.StartTime = Convert.ToDateTime(time.strftime("%Y-%m-%d %H:%M:%S", self.start_time))
empty_field_record.EndTime = Convert.ToDateTime(time.strftime("%Y-%m-%d %H:%M:%S", self.end_time))
return empty_field_record

View File

@ -12,5 +12,7 @@ namespace SiteManagementSystem_SoftwareEngineering_.Service
Database.EnsureCreated();
}
public DbSet<User> Users { get; set; }
public DbSet<Field> Fields { get; set; }
public DbSet<UserField> UserFieldRecords{get;set;}
}
}

View File

@ -15,6 +15,11 @@
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.4.7" />
</ItemGroup>
<ItemGroup>
<Folder Include="Service\PythonServiceFile\" />
</ItemGroup>
</Project>