217 lines
9.9 KiB
C#
217 lines
9.9 KiB
C#
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<ToolMoldMaintainGroup> _repository;
|
|
private readonly ISqlSugarClient _db;
|
|
public ToolMoldMaintainGroupService(ISqlSugarRepository<ToolMoldMaintainGroup> repository) : base(repository.AsSugarClient())
|
|
{
|
|
_db = repository.AsSugarClient();
|
|
}
|
|
|
|
#region Get
|
|
/// <summary>
|
|
/// 根据项目组Id获取关联保养项信息
|
|
/// </summary>
|
|
/// <param name="itemGroupId"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("{itemGroupId}")]
|
|
public async Task<dynamic> GetMaintianItemListByGroupId(string itemGroupId)
|
|
{
|
|
var list = new List<MoldMaintainItemListOutput>();
|
|
var itemIds = await _db.Queryable<ToolMoldMaintainGroupItem>().Where(it => it.item_group_id == itemGroupId).Select(it => it.item_id).ToListAsync();
|
|
if (itemIds?.Count > 0)
|
|
{
|
|
var items = await _db.Queryable<MoldMaintenance>().Where(it => itemIds.Contains(it.id)).ToListAsync();
|
|
list = items.Adapt<List<MoldMaintainItemListOutput>>();
|
|
}
|
|
return list;
|
|
}
|
|
/// <summary>
|
|
/// 根据项目组Id获取关联模具信息
|
|
/// </summary>
|
|
/// <param name="itemGroupId"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("{itemGroupId}")]
|
|
public async Task<dynamic> GetMoldListByGroupId(string itemGroupId)
|
|
{
|
|
var list = new List<RelevanceMoldListOutput>();
|
|
var moldIds = await _db.Queryable<ToolMoldMaintainGroupRelation>().Where(it => it.item_group_id == itemGroupId).Select(it => it.mold_id).ToListAsync();
|
|
if (moldIds?.Count > 0)
|
|
{
|
|
var items = await _db.Queryable<ToolMolds>().Where(it => moldIds.Contains(it.id)).ToListAsync();
|
|
list = items.Adapt<List<RelevanceMoldListOutput>>();
|
|
}
|
|
return list;
|
|
}
|
|
#endregion
|
|
|
|
|
|
#region Post
|
|
/// <summary>
|
|
/// 关联保养组与保养项
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[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<MoldMaintainGroupItemInput, ToolMoldMaintainGroupItem>(input, nameof(ToolMoldMaintainGroupItem.item_group_id), nameof(ToolMoldMaintainGroupItem.item_id), it => it.item_group_id == input.id);
|
|
List<ToolMoldMaintainItemRecord> itemRecords = new();
|
|
var grpIds = await _db.Queryable<ToolMoldMaintainGroupItem>().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<ToolMoldMaintainGroup>().Where(it => grpIds.Contains(it.id)).ToListAsync();
|
|
foreach (var grp in grps)
|
|
{
|
|
var molds = await _db.Queryable<ToolMoldMaintainGroupRelation>().Where(it => it.item_group_id == grp.id).ToListAsync();
|
|
if (molds?.Count > 0)
|
|
{
|
|
foreach (var mold in molds)
|
|
{
|
|
var dbItIds = await _db.Queryable<ToolMoldMaintainItemRecord>().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<ToolMoldMaintainItem>().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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 关联项目组与模具
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task RelevanceMaintianGroupAndMold(MoldMaintainGroupItemRelationInput input)
|
|
{
|
|
await Relevance<MoldMaintainGroupItemRelationInput, ToolMoldMaintainGroupRelation>(input, nameof(ToolMoldMaintainGroupRelation.item_group_id), nameof(ToolMoldMaintainGroupRelation.mold_id), it => it.item_group_id == input.id);
|
|
List<ToolMoldMaintainItemRecord> itemRecords = new();
|
|
foreach (var moldId in input.ids)
|
|
{
|
|
var grpIds = await _db.Queryable<ToolMoldMaintainGroupRelation>().Where(it => it.mold_id == moldId).Select(it => it.item_group_id).Distinct().ToListAsync();
|
|
if (grpIds?.Count > 0)
|
|
{
|
|
var grps = await _db.Queryable<ToolMoldMaintainGroup>().Where(it => grpIds.Contains(it.id)).ToListAsync();
|
|
foreach (var grp in grps)
|
|
{
|
|
var itemIds = await _db.Queryable<ToolMoldMaintainGroupItem>().Where(it => it.item_group_id == grp.id).Select(it => it.item_id).ToListAsync();
|
|
if (itemIds?.Count > 0)
|
|
{
|
|
var dbItemIds = await _db.Queryable<ToolMoldMaintainItemRecord>().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<ToolMoldMaintainItem>().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();
|
|
|
|
}
|
|
/// <summary>
|
|
/// 删除项目组与模具检查项信息
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task Delete(MoldMaintainDelInput input)
|
|
{
|
|
if (input.table_name == "item")
|
|
{
|
|
await Delete<ToolMoldMaintainGroupItem>(it => it.item_group_id == input.id && input.ids.Contains(it.item_id));
|
|
await Delete<ToolMoldMaintainItemRecord>(it => it.item_group_id == input.id && input.ids.Contains(it.item_id));
|
|
}
|
|
else
|
|
{
|
|
await Delete<ToolMoldMaintainGroupRelation>(it => it.item_group_id == input.id && input.ids.Contains(it.mold_id));
|
|
await Delete<ToolMoldMaintainItemRecord>(it => it.item_group_id == input.id && input.ids.Contains(it.mold_id));
|
|
}
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
private async Task Delete<T>(Expression<Func<T, bool>> deleteExp) where T : BaseEntity<string>, new()
|
|
{
|
|
await _db.Deleteable<T>().Where(deleteExp).ExecuteCommandAsync();
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|