181 lines
7.6 KiB
C#
181 lines
7.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Dynamic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Aop.Api.Domain;
|
|
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 JNPF.Logging;
|
|
using JNPF.Systems.Interfaces.System;
|
|
using Mapster;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
using SqlSugar;
|
|
using Tnb.BasicData;
|
|
using Tnb.EquipMgr.Entities;
|
|
using Tnb.EquipMgr.Entities.Dto;
|
|
using Tnb.EquipMgr.Interfaces;
|
|
|
|
namespace Tnb.EquipMgr
|
|
{
|
|
/// <summary>
|
|
/// 模具保养规则定义服务
|
|
/// </summary>
|
|
[ApiDescriptionSettings(Tag = ModuleConsts.Tag, Area = ModuleConsts.Area, Order = 700)]
|
|
[Route("api/[area]/[controller]/[action]")]
|
|
public class ToolMoldMaintainRuleService : IToolMoldMaintainRuleService, IDynamicApiController, ITransient
|
|
{
|
|
private readonly ISqlSugarClient _db;
|
|
private readonly IUserManager _userManager;
|
|
private readonly IDictionaryDataService _dictionaryDataService;
|
|
|
|
public ToolMoldMaintainRuleService(ISqlSugarRepository<ToolMoldMaintainRule> repository, IUserManager userManager, IDictionaryDataService dictionaryDataService)
|
|
{
|
|
_db = repository.AsSugarClient();
|
|
_userManager = userManager;
|
|
_dictionaryDataService = dictionaryDataService;
|
|
}
|
|
/// <summary>
|
|
/// 根据规则Id获取匹配的模具列表
|
|
/// </summary>
|
|
/// <param name="ruleId">规则Id</param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public async Task<dynamic> GetListById([FromRoute] string ruleId)
|
|
{
|
|
var result = new List<MaintainRuleMoldListOutput>();
|
|
var list = await _db.Queryable<ToolMoldMaintainRuleRelation>().Where(it => it.rule_id == ruleId).ToListAsync();
|
|
if (list?.Count > 0)
|
|
{
|
|
var ids = list.Select(it => it.mold_id).ToList();
|
|
result = await _db.Queryable<ToolMolds>().Where(it => ids.Contains(it.id))
|
|
.Select(it => new MaintainRuleMoldListOutput { mold_id = it.id, item_group_id = list.First().item_group_id }, true)
|
|
.Mapper
|
|
(
|
|
it => it.item_group_name = _db.Queryable<ToolMoldMaintainGroup>().First(x => x.id == list.First().item_group_id).name!
|
|
)
|
|
.ToListAsync();
|
|
}
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// 获取模具选择列表
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public async Task<dynamic> GetMoldRuleSelectorList()
|
|
{
|
|
return await _db.Queryable<ToolMolds>().Select(it => new MoldRuleSelectorListOutput
|
|
{
|
|
mold_id = it.id,
|
|
}, true)
|
|
.Mapper(it =>
|
|
{
|
|
var itemGroupIds = _db.Queryable<ToolMoldMaintainGroupRelation>().Where(x => x.mold_id == it.mold_id).Select(x => x.item_group_id).Distinct().ToList();
|
|
it.groupItems = _db.Queryable<ToolMoldMaintainGroup>().Where(x => itemGroupIds.Contains(x.id)).Select(x => new MaintainItemGroupItem { item_group_id = x.id, name = x.name }).ToList();
|
|
})
|
|
.ToListAsync();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 关联模具
|
|
/// </summary>
|
|
/// <param name="input">关联模具输入参数</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="ArgumentNullException"></exception>
|
|
[HttpPost]
|
|
public async Task RelevanceMold(RelevanceMoldInput input)
|
|
{
|
|
if (input == null) throw new ArgumentNullException(nameof(input));
|
|
await _db.Deleteable<ToolMoldMaintainRuleRelation>().Where(it => it.rule_id == input.rule_id).ExecuteCommandAsync();
|
|
if (input.rowIds?.Count > 0)
|
|
{
|
|
List<ToolMoldMaintainRuleRelation> entities = new();
|
|
foreach (var item in input.rowIds)
|
|
{
|
|
ToolMoldMaintainRuleRelation entity = new();
|
|
entity.id = SnowflakeIdHelper.NextId();
|
|
entity.rule_id = input.rule_id;
|
|
entity.mold_id = item.mold_id;
|
|
entity.item_group_id = item.group_id;
|
|
|
|
entities.Add(entity);
|
|
}
|
|
var row = await _db.Insertable(entities).ExecuteCommandAsync();
|
|
if (row < 1) throw Oops.Oh(ErrorCode.COM1000);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 删除模具信息
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task DeleteMoldRelevance(RelevanceMoldInput input)
|
|
{
|
|
if (input.ids?.Count > 0)
|
|
{
|
|
var row = await _db.Deleteable<ToolMoldMaintainRuleRelation>().Where(it => it.rule_id == input.rule_id && input.ids.Contains(it.mold_id)).ExecuteCommandAsync();
|
|
if (row < 1) throw Oops.Oh(ErrorCode.COM1002);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 生成模具保养计划
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task GenMaintainPlan(MaintainPlanCrInput input)
|
|
{
|
|
if (input == null) throw new ArgumentNullException("input");
|
|
|
|
try
|
|
{
|
|
await _db.Ado.BeginTranAsync();
|
|
|
|
var maintainRules = await _db.Queryable<ToolMoldMaintainRule>().Where(it => input.ruleIds.Contains(it.id)).ToListAsync();
|
|
if (maintainRules?.Count > 0)
|
|
{
|
|
List<ToolMoldMaintainPlan> maintainPlans = new();
|
|
List<ToolMoldMaintainPlanRelation> maintainPlanRelations = new();
|
|
foreach (var maintainRule in maintainRules)
|
|
{
|
|
if (maintainRule.cycle.HasValue && maintainRule.cycle.Value > 0)
|
|
{
|
|
ToolMoldMaintainPlan maintainPlan = new();
|
|
maintainPlan.plan_code = $"JHDM{DateTime.Now:yyyyMMddmmss}";
|
|
maintainPlan.mode = maintainRule.mode;
|
|
maintainPlan.status = DictConst.UnMaintainStatusCode;
|
|
maintainPlan.plan_start_date = DateTime.Now;
|
|
maintainPlan.plan_end_date = DateTime.Now.AddDays(maintainRule.cycle.Value);
|
|
maintainPlan.create_id = _userManager.UserId;
|
|
maintainPlan.create_time = DateTime.Now;
|
|
maintainPlans.Add(maintainPlan);
|
|
|
|
ToolMoldMaintainPlanRelation maintainPlanReation = new();
|
|
maintainPlanReation.maintain_plan_id = maintainPlan.id;
|
|
maintainPlanReation.mold_id = (await _db.Queryable<ToolMoldMaintainRuleRelation>().FirstAsync(it => it.rule_id == maintainRule.id))?.mold_id!;
|
|
maintainPlanRelations.Add(maintainPlanReation);
|
|
}
|
|
}
|
|
await _db.Insertable(maintainPlans).ExecuteCommandAsync();
|
|
await _db.Insertable(maintainPlanRelations).ExecuteCommandAsync();
|
|
await _db.Ado.CommitTranAsync();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error("生成保养计划失败", ex);
|
|
await _db.Ado.RollbackTranAsync();
|
|
}
|
|
}
|
|
}
|
|
}
|