using Aspose.Cells; using AutoMapper; using Microsoft.Extensions.Caching.Memory; using SqlSugar; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using Txgy.RBS.DbModel.Models; using Txgy.RBS.DTO; using Txgy.RBS.Framework; using Txgy.RBS.Framework.Api; using Txgy.RBS.IServices; namespace Txgy.RBS.Services { public class ProjectInfoService : BaseService, IProjectInfoService { private readonly IMapper _iMapper; private readonly ProcessManagerService _processManager; private readonly IMemoryCache _cache; public ProjectInfoService(ISqlSugarClient client, IMapper iMapper, ProcessManagerService processManager) : base(client) { this._iMapper = iMapper; this._processManager = processManager; } public ApiResult AddProjectInfo(ProjectInfoDTO project) { try { _Client.Ado.BeginTran(); var projectInfo = _iMapper.Map(project); //project_id要手动赋值 projectInfo.project_id= _Client.Queryable().Max(p=>p.project_id)+1; projectInfo.station_file.project_id= projectInfo.project_id; projectInfo.time_tab.project_id = projectInfo.project_id; var pro = _Client.InsertNav(projectInfo).Include(p => p.station_file).ThenInclude(t => t.stations) .Include(time => time.time_tab).ExecuteCommand(); _Client.Ado.CommitTran(); } catch (Exception ex) { _Client.Ado.RollbackTran(); return new ApiResult() { Message = $"添加失败,{ex}" }; } return new ApiResult(); } public ApiResult DeleteProjectInfo(int id) { var res = _Client.DeleteNav(p => p.id == id).Include(p => p.station_file).ThenInclude(t => t.stations) .Include(time => time.time_tab).ExecuteCommand(); if (res) { return new ApiResult(); } return new ApiResult() { Message = "删除失败" }; } public List GetAllProjectInfo() { var projects = _Client.Queryable().Includes(x => x.station_file, t=>t.stations.OrderBy(n=>n.num).ToList()) .Includes(ti=>ti.time_tab).ToList(); var projectDTOs= _iMapper.Map>(projects); return projectDTOs; } public ProjectUsedDTO GetCurrentProjectUsed(string clientName) { var curProject = _Client.Queryable().Where(c=>c.client_id== clientName).First(); var projectUse=_iMapper.Map(curProject); if (projectUse != null) { var pro = _Client.Queryable().Where(p => p.project_id == curProject.current_project_id).First(); projectUse.project_name = pro.project_name; } return projectUse; } public ProjectUsedDTO GetCurrentProjectUsed(int projectId) { var curProject = _Client.Queryable().Where(c => c.current_project_id == projectId).First(); var projectUse = _iMapper.Map(curProject); if (projectUse != null) { var pro = _Client.Queryable().Where(p => p.project_id == curProject.current_project_id).First(); projectUse.project_name = pro.project_name; } return projectUse; } public ApiResult UpdateProjectInfo(ProjectInfoDTO project) { project_info projectInfo = _iMapper.Map(project); var res = _Client.UpdateNav(projectInfo).Include(p => p.station_file).ThenInclude(t => t.stations) .Include(time => time.time_tab).ExecuteCommand(); if (res) { return new ApiResult(); } return new ApiResult() { Message = "更新失败" }; } public ProjectInfoDTO GetProjectInfo(int id) { var projectInfo = _Client.Queryable().Where(p => p.project_id == id).Includes(p=>p.station_file, s=>s.stations).Includes(p=>p.time_tab).ToList(); if (projectInfo != null && projectInfo.Count() > 0) { ProjectInfoDTO projectInfoDTO = _iMapper.Map(projectInfo[0]); return projectInfoDTO; } return null; } public ApiResult UpdateCurrentProjectUsed(ProjectUsedDTO projectUsed) { int res = 0; var curUser=_iMapper.Map(projectUsed); var user= _Client.Queryable().Where(u => u.client_id == curUser.client_id).First(); if (user != null) { user.current_project_id = curUser.current_project_id; res = _Client.Updateable(user).ExecuteCommand(); } else { res = _Client.Insertable(curUser).ExecuteCommand(); } if (res>0) { return new ApiResult(); } return new ApiResult() { Message = "更新失败" }; } public ApiResult DeleteCurrentProjectUsed(int projectId) { int res = 0; var use = _Client.Queryable().Where(u => u.current_project_id == projectId).First(); if (use != null) { res = _Client.Deleteable().Where(u => u.id == use.id).ExecuteCommand(); if (res > 0) { return new ApiResult(); } } return new ApiResult() { Message = "更新失败" }; } public ApiResult StartProject(string projectName, ProcessConfig processConfig) { var res = _processManager.StartProject(projectName, processConfig); return res; } public ApiResult StopProject(string projectName) { var res = _processManager.StopProject(projectName); return res; } public ApiResult GetState(string projectName, int id) { return _processManager.GetState(projectName, id); } public ApiResult ExportCSV(string fileName, List stations) { return _processManager.ExportCSV(fileName, stations); } } }