using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Text; using System.Threading.Tasks; using Aspose.Cells.Drawing; using JNPF.Common.Core.Manager; using JNPF.Common.Enums; using JNPF.Common.Security; using JNPF.DependencyInjection; using JNPF.DynamicApiController; using JNPF.FriendlyException; using Mapster; using Microsoft.AspNetCore.Mvc; using NPOI.SS.Formula.Functions; using Qiniu.Util; using Spire.Pdf.Widget; using SqlSugar; using Tnb.Common.Contracts; using Tnb.EquipMgr.Entities; using Tnb.EquipMgr.Entities.Dto; using Tnb.EquipMgr.Entities.Entity; using Tnb.EquipMgr.Interfaces; namespace Tnb.EquipMgr { [ApiDescriptionSettings(Tag = ModuleConsts.Tag, Area = ModuleConsts.Area, Order = 700)] [Route("api/[area]/[controller]/[action]")] public class ToolMoldMaintainGroupService : IToolMoldMaintainGroupService, IDynamicApiController, ITransient { private readonly ISqlSugarRepository _repository; private readonly IUserManager _userManager; private readonly ISqlSugarClient _db; public ToolMoldMaintainGroupService(ISqlSugarRepository repository, IUserManager userManager) { _repository = repository; _userManager = userManager; _db = repository.AsSugarClient(); } #region Get /// /// 根据项目组Id获取关联保养项信息 /// /// /// [HttpGet("{itemGroupId}")] public async Task GetMaintianItemListByGroupId(string itemGroupId) { var list = new List(); var itemIds = await _db.Queryable().Where(it => it.item_group_id == itemGroupId).Select(it => it.item_id).ToListAsync(); if (itemIds?.Count > 0) { var items = await _db.Queryable().Where(it => itemIds.Contains(it.id)).ToListAsync(); list = items.Adapt>(); } return list; } /// /// 根据项目组Id获取关联模具信息 /// /// /// [HttpGet("{itemGroupId}")] public async Task GetMoldListByGroupId(string itemGroupId) { var list = new List(); var moldIds = await _db.Queryable().Where(it => it.item_group_id == itemGroupId).Select(it => it.mold_id).ToListAsync(); if (moldIds?.Count > 0) { var items = await _db.Queryable().Where(it => moldIds.Contains(it.id)).ToListAsync(); list = items.Adapt>(); } return list; } #endregion #region Post /// /// 关联保养组与保养项 /// /// /// [HttpPost] public async Task RelevanceMaintianGroupAndItem(MoldMaintainGroupItemInput input) => await Relevance(input, nameof(ToolMoldMaintainGroupItem.item_id), it => it.item_group_id == input.item_group_id); /// /// 关联项目组与模具 /// /// /// [HttpPost] public async Task RelevanceMaintianGroupAndMold(MoldMaintainGroupItemRelationInput input) => await Relevance(input, nameof(ToolMoldMaintainGroupRelation.mold_id), it => it.item_group_id == input.item_group_id); #endregion private async Task Relevance(TSrc input, string name, Expression> deleleExp) where TDest : BaseEntity, new() where TSrc : BaseInput { await _db.Deleteable().Where(deleleExp).ExecuteCommandAsync(); var itemGroupId = nameof(ToolMoldMaintainGroupRelation.item_group_id); if (input == null) throw new ArgumentNullException(nameof(input)); var entities = new List(); if (input.ids?.Count > 0) { foreach (var id in input.ids) { var pk = id; TDest entity = new(); entity.id = SnowflakeIdHelper.NextId(); if (!PropertySet.ValueFactories.TryGetValue(itemGroupId, out Action setGroupIdAction)) { setGroupIdAction = PropertySet.CreateSetPropertyValueAction(itemGroupId); PropertySet.ValueFactories.Add(itemGroupId, setGroupIdAction); } setGroupIdAction(entity, input.item_group_id); if (!PropertySet.ValueFactories.TryGetValue(name, out Action setAction)) { setAction = PropertySet.CreateSetPropertyValueAction(name); PropertySet.ValueFactories.Add(name, setAction); } setAction(entity, pk); entities.Add(entity); } } var row = await _db.Insertable(entities).ExecuteCommandAsync(); if (row < 1) throw Oops.Oh(ErrorCode.COM1000); } } public class PropertySet { public static Dictionary> ValueFactories = new Dictionary>(StringComparer.OrdinalIgnoreCase); public static Action CreateSetPropertyValueAction(string propertyName) { var property = typeof(T).GetProperty(propertyName); var target = Expression.Parameter(typeof(object)); var propertyValue = Expression.Parameter(typeof(object)); var castTarget = Expression.Convert(target, typeof(T)); var castPropertyValue = Expression.Convert(propertyValue, property.PropertyType); var setPropertyValue = Expression.Call(castTarget, property.GetSetMethod(), castPropertyValue); return Expression.Lambda>(setPropertyValue, target, propertyValue).Compile(); } } }