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 SqlSugar; using JNPF.Common.Contracts; using Tnb.EquipMgr.Entities; using Tnb.EquipMgr.Entities.Dto; using Tnb.EquipMgr.Interfaces; using Senparc.Weixin.MP.AdvancedAPIs.GroupMessage; using Aop.Api.Domain; namespace Tnb.EquipMgr { [ApiDescriptionSettings(Tag = ModuleConsts.Tag, Area = ModuleConsts.Area, Order = 700)] [Route("api/[area]/[controller]/[action]")] public class ToolMoldMaintainGroupService : BaseMoldMaintainService, IToolMoldMaintainGroupService, IDynamicApiController, ITransient { private readonly ISqlSugarRepository _repository; private readonly ISqlSugarClient _db; public ToolMoldMaintainGroupService(ISqlSugarRepository repository) : base(repository.AsSugarClient()) { _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) { try { await _db.Ado.BeginTranAsync(); if (input.ids == null || input.ids.Count == 0) throw new ArgumentException($"parameter {nameof(input.ids)} not be null or count not be zero"); await Relevance(input, nameof(ToolMoldMaintainGroupItem.item_group_id), nameof(ToolMoldMaintainGroupItem.item_id), it => it.item_group_id == input.id); List itemRecords = new(); var grpIds = await _db.Queryable().Where(it => input.ids.Contains(it.item_id)).Select(it => it.item_group_id).Distinct().ToListAsync(); if (grpIds?.Count > 0) { var grps = await _db.Queryable().Where(it => grpIds.Contains(it.id)).ToListAsync(); foreach (var grp in grps) { var molds = await _db.Queryable().Where(it => it.item_group_id == grp.id).ToListAsync(); if (molds?.Count > 0) { foreach (var mold in molds) { var dbItIds = await _db.Queryable().Where(it => it.mold_id == mold.id && it.item_group_id == grp.id && input.ids.Contains(it.id)).Select(it => it.item_id).ToListAsync(); input.ids = input.ids.Except(dbItIds).ToList(); if (input.ids?.Count > 0) { var items = await _db.Queryable().Where(it => input.ids.Contains(it.id)).ToListAsync(); if (items?.Count > 0) { foreach (var item in items) { ToolMoldMaintainItemRecord record = new(); record.mold_id = mold.id; record.item_group_id = grp.id; record.item_group_name = grp.name; record.item_id = item.id; record.item_name = item.name; record.status = 0; itemRecords.Add(record); } } } } } } await _db.Insertable(itemRecords).ExecuteCommandAsync(); } await _db.Ado.CommitTranAsync(); } catch (Exception ex) { await _db.Ado.RollbackTranAsync(); throw; } } /// /// 关联项目组与模具 /// /// /// [HttpPost] public async Task RelevanceMaintianGroupAndMold(MoldMaintainGroupItemRelationInput input) { await Relevance(input, nameof(ToolMoldMaintainGroupRelation.item_group_id), nameof(ToolMoldMaintainGroupRelation.mold_id), it => it.item_group_id == input.id); List itemRecords = new(); foreach (var moldId in input.ids) { var grpIds = await _db.Queryable().Where(it => it.mold_id == moldId).Select(it => it.item_group_id).Distinct().ToListAsync(); if (grpIds?.Count > 0) { var grps = await _db.Queryable().Where(it => grpIds.Contains(it.id)).ToListAsync(); foreach (var grp in grps) { var itemIds = await _db.Queryable().Where(it => it.item_group_id == grp.id).Select(it => it.item_id).ToListAsync(); if (itemIds?.Count > 0) { var dbItemIds = await _db.Queryable().Where(it => it.mold_id == moldId && it.item_group_id == grp.id && itemIds.Contains(it.item_id)).Select(it => it.item_id).ToListAsync(); itemIds = itemIds.Except(dbItemIds).ToList(); if (itemIds?.Count > 0) { var items = await _db.Queryable().Where(it => itemIds.Contains(it.id)).ToListAsync(); foreach (var item in items) { ToolMoldMaintainItemRecord record = new(); record.mold_id = moldId; record.item_group_id = grp.id; record.item_group_name = grp.name; record.item_id = item.id; record.item_name = item.name; record.status = 0; itemRecords.Add(record); } } } } } } await _db.Insertable(itemRecords).ExecuteCommandAsync(); } /// /// 删除项目组与模具检查项信息 /// /// /// [HttpPost] public async Task Delete(MoldMaintainDelInput input) { if (input.table_name == "item") { await Delete(it => it.item_group_id == input.id && input.ids.Contains(it.item_id)); await Delete(it => it.item_group_id == input.id && input.ids.Contains(it.item_id)); } else { await Delete(it => it.item_group_id == input.id && input.ids.Contains(it.mold_id)); await Delete(it => it.item_group_id == input.id && input.ids.Contains(it.mold_id)); } } #endregion private async Task Delete(Expression> deleteExp) where T : BaseEntity, new() { await _db.Deleteable().Where(deleteExp).ExecuteCommandAsync(); } } }