using Furion.DependencyInjection; using Furion.DynamicApiController; using Furion.FriendlyException; using Furion.JsonSerialization; using Mapster; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Myshipping.Application.Entity; using Myshipping.Application.Helper; using Myshipping.Core; using StackExchange.Profiling.Internal; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Myshipping.Application { /// /// 服务项目管理 /// [ApiDescriptionSettings("Application", Name = "ServiceProject", Order = 10)] public class ServiceProjectService : IServiceProjectService, IDynamicApiController, ITransient { private readonly SqlSugarRepository _serviceProjectBaseInfoRepository; private readonly SqlSugarRepository _serviceWorkFlowProjectRelationRepository; private readonly ILogger _logger; public ServiceProjectService(SqlSugarRepository serviceProjectBaseInfoRepository, ILogger logger, SqlSugarRepository serviceWorkFlowProjectRelationRepository) { _serviceProjectBaseInfoRepository = serviceProjectBaseInfoRepository; _logger = logger; _serviceWorkFlowProjectRelationRepository = serviceWorkFlowProjectRelationRepository; } #region 保存 /// /// 保存 /// /// 服务项目详情 /// 返回回执 [HttpPost("/ServiceProject/Save")] public async Task Save([FromBody] ServiceProjectBaseDto info) { TaskManageOrderResultDto result = new TaskManageOrderResultDto(); try { var id = await InnerSave(info); result.succ = true; result.msg = "保存成功"; result.ext = id; } catch (Exception ex) { result.succ = false; result.msg = $"保存服务项目异常,原因:{ex.Message}"; } return result; } #endregion #region 保存内部方法 /// /// 保存内部方法 /// /// 服务项目详情 /// 是否启用 /// 返回派车Id [SqlSugarUnitOfWork] private async Task InnerSave(ServiceProjectBaseDto info,bool isSetEnable = false) { ServiceProjectBaseInfo entity = info.Adapt(); if(isSetEnable) { entity.IS_ENABLE = 1; } if (entity == null) throw Oops.Oh($"服务项目不能为空", typeof(InvalidOperationException)); _logger.LogInformation($"服务项目保存 JSON={JSON.Serialize(entity)} user={UserManager.UserId}"); if (string.IsNullOrWhiteSpace(entity.PK_ID)) { _serviceProjectBaseInfoRepository.Insert(entity); } else { var model = _serviceProjectBaseInfoRepository.AsQueryable().First(a => a.PK_ID == entity.PK_ID); if (model == null) throw Oops.Oh($"服务项目获取失败,服务项目信息不存在或已作废", typeof(InvalidOperationException)); _logger.LogInformation($"更新服务项目前,获取原始记录 JSON={JSON.Serialize(model)}"); /* 需要校验是否已有服务项目对应到服务流程,已对应的不能修改代码 */ if (!entity.SERVICE_PROJECT_CODE.Equals(model.SERVICE_PROJECT_CODE,StringComparison.OrdinalIgnoreCase)) { ValidateServiceProject(entity, true); } entity.UpdatedTime = DateTime.Now; entity.UpdatedUserId = UserManager.UserId; entity.UpdatedUserName = UserManager.Name; await _serviceProjectBaseInfoRepository.AsUpdateable(entity).IgnoreColumns(it => new { it.TenantId, it.TenantName, it.CreatedTime, it.CreatedUserId, it.CreatedUserName, it.IsDeleted, }).ExecuteCommandAsync(); } return entity.PK_ID; } #endregion /// /// 校验 /// /// 服务项目详情 /// 是否校验关系 /// private void ValidateServiceProject(ServiceProjectBaseInfo entity, bool isCheckRelation = false) { if (_serviceWorkFlowProjectRelationRepository.Any(a => a.SERVICE_PROJECT_ID == entity.PK_ID)) { _logger.LogInformation($"当前服务项目已关联服务流程不能修改产品代码,{entity.SERVICE_PROJECT_CODE}"); throw Oops.Oh($"当前服务项目已关联服务流程不能修改产品代码", typeof(InvalidOperationException)); } } #region 保存并启用 /// /// 保存并启用 /// /// 服务项目详情 /// 返回回执 [HttpPost("/ServiceProject/SaveAndEnable")] public async Task SaveAndEnable([FromBody]ServiceProjectBaseDto info) { TaskManageOrderResultDto result = new TaskManageOrderResultDto(); try { var id = await InnerSave(info,true); result.succ = true; result.msg = "保存成功"; result.ext = id; } catch (Exception ex) { result.succ = false; result.msg = $"保存服务项目异常,原因:{ex.Message}"; } return result; } #endregion /// /// 启用 /// /// 服务项目主键 /// 返回回执 [HttpGet("/ServiceProject/SetEnable")] public async Task SetEnable([FromQuery] string pkId) { TaskManageOrderResultDto result = new TaskManageOrderResultDto(); try { if (string.IsNullOrWhiteSpace(pkId)) { throw Oops.Oh($"服务项目主键不能为空", typeof(InvalidOperationException)); } var model = _serviceProjectBaseInfoRepository.AsQueryable().First(a => a.PK_ID == pkId); if (model == null) throw Oops.Oh($"服务项目获取失败,服务项目信息不存在或已作废", typeof(InvalidOperationException)); _logger.LogInformation($"更新服务项目前,获取原始记录 JSON={JSON.Serialize(model)}"); ValidateServiceProject(model, true); model.UpdatedTime = DateTime.Now; model.UpdatedUserId = UserManager.UserId; model.UpdatedUserName = UserManager.Name; model.IS_ENABLE = 1; await _serviceProjectBaseInfoRepository.AsUpdateable(model).UpdateColumns(it => new { it.IS_ENABLE, it.UpdatedTime, it.UpdatedUserId, it.UpdatedUserName }).ExecuteCommandAsync(); result.succ = true; result.msg = "执行成功"; } catch (Exception ex) { result.succ = false; result.msg = $"执行启用异常,原因:{ex.Message}"; } return result; } /// /// 取消启用 /// /// 服务项目主键 /// 返回回执 [HttpGet("/ServiceProject/SetUnEnable")] public async Task SetUnEnable([FromQuery] string pkId) { TaskManageOrderResultDto result = new TaskManageOrderResultDto(); try { if (string.IsNullOrWhiteSpace(pkId)) { throw Oops.Oh($"服务项目主键不能为空", typeof(InvalidOperationException)); } var model = _serviceProjectBaseInfoRepository.AsQueryable().First(a => a.PK_ID == pkId); if (model == null) throw Oops.Oh($"服务项目获取失败,服务项目信息不存在或已作废", typeof(InvalidOperationException)); _logger.LogInformation($"更新服务项目前,获取原始记录 JSON={JSON.Serialize(model)}"); ValidateServiceProject(model, true); model.UpdatedTime = DateTime.Now; model.UpdatedUserId = UserManager.UserId; model.UpdatedUserName = UserManager.Name; model.IS_ENABLE = 0; await _serviceProjectBaseInfoRepository.AsUpdateable(model).UpdateColumns(it => new { it.IS_ENABLE, it.UpdatedTime, it.UpdatedUserId, it.UpdatedUserName }).ExecuteCommandAsync(); result.succ = true; result.msg = "执行成功"; } catch (Exception ex) { result.succ = false; result.msg = $"执行启用异常,原因:{ex.Message}"; } return result; } /// /// 删除 /// /// 服务项目主键 /// 返回回执 [HttpGet("/ServiceProject/Delete")] public async Task Delete([FromQuery] string pkId) { TaskManageOrderResultDto result = new TaskManageOrderResultDto(); try { if (string.IsNullOrWhiteSpace(pkId)) { throw Oops.Oh($"服务项目主键不能为空", typeof(InvalidOperationException)); } var model = _serviceProjectBaseInfoRepository.AsQueryable().First(a => a.PK_ID == pkId); if (model == null) throw Oops.Oh($"服务项目获取失败,服务项目信息不存在或已作废", typeof(InvalidOperationException)); _logger.LogInformation($"更新服务项目前,获取原始记录 JSON={JSON.Serialize(model)}"); if(model.IS_ENABLE == 1) ValidateServiceProject(model, true); model.UpdatedTime = DateTime.Now; model.UpdatedUserId = UserManager.UserId; model.UpdatedUserName = UserManager.Name; model.IsDeleted = true; await _serviceProjectBaseInfoRepository.AsUpdateable(model).UpdateColumns(it => new { it.IsDeleted, it.UpdatedTime, it.UpdatedUserId, it.UpdatedUserName }).ExecuteCommandAsync(); result.succ = true; result.msg = "执行成功"; } catch (Exception ex) { result.succ = false; result.msg = $"保存派车异常,原因:{ex.Message}"; } return result; } #region 获取服务项目详情 /// /// 获取服务项目详情 /// /// 服务项目主键 /// 返回回执 [HttpGet("/ServiceProject/GetInfo")] public async Task GetInfo([FromQuery] string pkId) { TaskManageOrderResultDto result = new TaskManageOrderResultDto(); try { if (string.IsNullOrWhiteSpace(pkId)) { throw Oops.Oh($"服务项目主键不能为空", typeof(InvalidOperationException)); } var model = await _serviceProjectBaseInfoRepository.AsQueryable().FirstAsync(a => a.PK_ID == pkId); if (model == null) throw Oops.Oh($"服务项目获取失败,服务项目信息不存在或已作废", typeof(InvalidOperationException)); var showModel = model.Adapt(); result.succ = true; result.ext = showModel; } catch (Exception ex) { result.succ = false; result.msg = $"获取服务项目详情异常,原因:{ex.Message}"; } return result; } #endregion #region 检索服务项目列表 /// /// 检索服务项目列表 /// /// 检索值 /// 最大返回行数(默认15) /// 返回回执 [HttpGet("/ServiceProject/QueryList")] public async Task QueryList([FromQuery] string queryItem, [FromQuery] int topNum = 15) { TaskManageOrderResultDto result = new TaskManageOrderResultDto(); try { var list = await _serviceProjectBaseInfoRepository.AsQueryable().Where(a => a.IS_ENABLE == 1 && !a.IsDeleted && (a.SERVICE_PROJECT_CODE.Contains(queryItem) || a.SERVICE_PROJECT_NAME.Contains(queryItem))) .Take(topNum).ToListAsync(); result.succ = true; result.ext = list.Adapt>(); } catch (Exception ex) { result.succ = false; result.msg = $"检索服务项目列表异常,原因:{ex.Message}"; } return result; } #endregion #region 服务项目台账查询 /// /// 服务项目台账查询 /// /// 服务项目台账查询请求 /// 返回结果 [HttpGet("/ServiceProject/GetPage")] public async Task> GetPageAsync(QueryServiceProjectBaseDto QuerySearch) { //制单日期 DateTime createBegin = DateTime.MinValue; DateTime createEnd = DateTime.MinValue; //更新日期 DateTime updateBegin = DateTime.MinValue; DateTime updateEnd = DateTime.MinValue; //制单日期 if (!string.IsNullOrWhiteSpace(QuerySearch.CreateBegin)) { if (!DateTime.TryParse(QuerySearch.CreateBegin, out createBegin)) throw Oops.Oh($"创建起始日期格式错误,{QuerySearch.CreateBegin}"); } if (!string.IsNullOrWhiteSpace(QuerySearch.CreateEnd)) { if (!DateTime.TryParse(QuerySearch.CreateEnd, out createEnd)) throw Oops.Oh($"创建结束日期格式错误,{QuerySearch.CreateEnd}"); createEnd = createEnd.AddDays(1); } //更新日期 if (!string.IsNullOrWhiteSpace(QuerySearch.UpdateBegin)) { if (!DateTime.TryParse(QuerySearch.UpdateBegin, out updateBegin)) throw Oops.Oh($"更新起始日期格式错误,{QuerySearch.UpdateBegin}"); } if (!string.IsNullOrWhiteSpace(QuerySearch.UpdateEnd)) { if (!DateTime.TryParse(QuerySearch.UpdateEnd, out updateEnd)) throw Oops.Oh($"更新结束日期格式错误,{QuerySearch.UpdateEnd}"); updateEnd = updateEnd.AddDays(1); } string entityOrderCol = "CreatedTime"; //这里因为返回给前端的台账数据是DTO,所以这里排序时候需要转换成Entity对应的字段 if (!string.IsNullOrWhiteSpace(QuerySearch.SortField)) entityOrderCol = MapsterExtHelper.GetAdaptProperty(QuerySearch.SortField); var entities = await _serviceProjectBaseInfoRepository.AsQueryable() .WhereIF(createBegin != DateTime.MinValue, t => t.CreatedTime >= createBegin) .WhereIF(createEnd != DateTime.MinValue, t => t.CreatedTime < createEnd) .WhereIF(updateBegin != DateTime.MinValue, t => t.UpdatedTime.HasValue && t.UpdatedTime.Value >= updateBegin) .WhereIF(updateEnd != DateTime.MinValue, t => t.UpdatedTime.HasValue && t.UpdatedTime.Value < updateEnd) .WhereIF(!string.IsNullOrWhiteSpace(QuerySearch.IsEnable) && QuerySearch.IsEnable == "1", t => t.IS_ENABLE == 1) .WhereIF(!string.IsNullOrWhiteSpace(QuerySearch.IsEnable) && QuerySearch.IsEnable == "2", t => t.IS_ENABLE == 0) .WhereIF(!string.IsNullOrWhiteSpace(QuerySearch.ServiceProjectName), t => t.SERVICE_PROJECT_NAME.Contains(QuerySearch.ServiceProjectName)|| t.SERVICE_PROJECT_CODE.Contains(QuerySearch.ServiceProjectName)) .WhereIF(!string.IsNullOrWhiteSpace(QuerySearch.CreateUser), t => t.CreatedUserName.Contains(QuerySearch.CreateUser)) .WhereIF(!string.IsNullOrWhiteSpace(QuerySearch.UpdateUser), t => t.UpdatedUserName.Contains(QuerySearch.UpdateUser)) .OrderBy(entityOrderCol + (QuerySearch.descSort ? " asc " : " desc ")) .ToPagedListAsync(QuerySearch.PageNo, QuerySearch.PageSize); return entities.Adapt>(); } #endregion } }