271 lines
12 KiB
C#
271 lines
12 KiB
C#
using JNPF.Common.Core.Manager;
|
|
using JNPF.Common.Dtos.VisualDev;
|
|
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 JNPF.TaskScheduler;
|
|
using JNPF.TaskScheduler.Entitys.Dto.TaskScheduler;
|
|
using JNPF.TaskScheduler.Entitys.Model;
|
|
using JNPF.VisualDev;
|
|
using JNPF.VisualDev.Interfaces;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
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]")]
|
|
[OverideVisualDev(ModuleId)]
|
|
public class ToolMoldMaintainRuleService : IOverideVisualDevService, IToolMoldMaintainRuleService, IDynamicApiController, ITransient
|
|
{
|
|
|
|
private const string ModuleId = "26164864904981";
|
|
private readonly ISqlSugarClient _db;
|
|
private readonly IUserManager _userManager;
|
|
private readonly IDictionaryDataService _dictionaryDataService;
|
|
private readonly TimeTaskService _timeTaskService;
|
|
private readonly IVisualDevService _visualDevService;
|
|
private readonly IRunService _runService;
|
|
public OverideVisualDevFunc OverideFuncs { get; } = new OverideVisualDevFunc();
|
|
|
|
[Obsolete]
|
|
public ToolMoldMaintainRuleService(ISqlSugarRepository<ToolMoldMaintainRule> repository, IUserManager userManager, IDictionaryDataService dictionaryDataService, TimeTaskService timeTaskService, IVisualDevService visualDevService, IRunService runService)
|
|
{
|
|
_db = repository.AsSugarClient();
|
|
_userManager = userManager;
|
|
_dictionaryDataService = dictionaryDataService;
|
|
_timeTaskService = timeTaskService;
|
|
_visualDevService = visualDevService;
|
|
_runService = runService;
|
|
OverideFuncs.DeleteAsync = Delete;
|
|
OverideFuncs.CreateAsync = Create;
|
|
}
|
|
|
|
[Obsolete]
|
|
private async Task<dynamic> Create(VisualDevModelDataCrInput input)
|
|
{
|
|
int cycle = int.Parse(input.data["cycle"].ToString()!);
|
|
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddMilliseconds(long.Parse(input.data["startandend_date"].ToString()!));
|
|
ToolMoldMaintainRule toolMoldMaintainRule = new()
|
|
{
|
|
id = SnowflakeIdHelper.NextId(),
|
|
name = input.data["name"].ToString(),
|
|
mode = input.data["mode"].ToString(),
|
|
cycle = cycle,
|
|
startandend_date = startTime.ToString("yyyy-MM-dd HH:mm:ss"),
|
|
create_id = _userManager.UserId,
|
|
create_time = DateTime.Now
|
|
};
|
|
_ = await _db.Insertable(toolMoldMaintainRule).ExecuteCommandAsync();
|
|
if (toolMoldMaintainRule.mode == "27118635748885")
|
|
{
|
|
string id = toolMoldMaintainRule.id;
|
|
ContentModel comtentModel = new()
|
|
{
|
|
cron = "0 0 9 " + startTime.Day + "/" + toolMoldMaintainRule.cycle + " * ?",
|
|
interfaceId = "",
|
|
interfaceName = "",
|
|
parameter = new List<InterfaceParameter>()
|
|
};
|
|
comtentModel.parameter!.Add(new InterfaceParameter() { field = "id", value = id, defaultValue = "" });
|
|
comtentModel.localHostTaskId = "MoldMaintainTask/CreateTask";
|
|
DateTimeOffset dateTimeOffset = new(startTime);
|
|
comtentModel.startTime = dateTimeOffset.ToUnixTimeMilliseconds();
|
|
comtentModel.TenantId = _userManager?.TenantId!;
|
|
comtentModel.TenantDbName = _userManager?.TenantDbName!;
|
|
comtentModel.ConnectionConfig = _userManager?.ConnectionConfig!;
|
|
comtentModel.Token = _userManager?.ToKen!;
|
|
TimeTaskCrInput timeTaskCrInput = new()
|
|
{
|
|
enCode = DateTime.Now.ToString("yyyyMMddHHmmss"),
|
|
fullName = "生成模具保养任务" + id,
|
|
executeType = "3",
|
|
executeContent = comtentModel.ToJsonString(),
|
|
description = "",
|
|
sortCode = 99,
|
|
enabledMark = 1,
|
|
};
|
|
await _timeTaskService.DeleteByName(timeTaskCrInput.fullName);
|
|
await _timeTaskService.Create(timeTaskCrInput, false);
|
|
}
|
|
return await Task.FromResult(true);
|
|
}
|
|
private async Task Delete(string id)
|
|
{
|
|
ToolMoldMaintainRule ToolMoldMaintainRule = await _db.Queryable<ToolMoldMaintainRule>().Where(p => p.id == id).FirstAsync();
|
|
List<ToolMoldMaintainRuleRelation> ToolMoldMaintainRuleRelations = await _db.Queryable<ToolMoldMaintainRuleRelation>().Where(p => p.rule_id == id).ToListAsync();
|
|
await _timeTaskService.DeleteByName("生成模具保养任务" + id);
|
|
await _db.Ado.BeginTranAsync();
|
|
_ = await _db.Deleteable(ToolMoldMaintainRule).ExecuteCommandAsync();
|
|
_ = await _db.Deleteable(ToolMoldMaintainRuleRelations).ExecuteCommandAsync();
|
|
await _db.Ado.CommitTranAsync();
|
|
}
|
|
/// <summary>
|
|
/// 根据规则Id获取匹配的模具列表
|
|
/// </summary>
|
|
/// <param name="ruleId">规则Id</param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public async Task<dynamic> GetListById([FromRoute] string ruleId)
|
|
{
|
|
List<MaintainRuleMoldListOutput> result = new();
|
|
List<ToolMoldMaintainRuleRelation> list = await _db.Queryable<ToolMoldMaintainRuleRelation>().Where(it => it.rule_id == ruleId).ToListAsync();
|
|
if (list?.Count > 0)
|
|
{
|
|
List<string> 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 =>
|
|
{
|
|
List<string> 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 (RowIdItem item in input.rowIds)
|
|
{
|
|
ToolMoldMaintainRuleRelation entity = new()
|
|
{
|
|
id = SnowflakeIdHelper.NextId(),
|
|
rule_id = input.rule_id,
|
|
mold_id = item.mold_id,
|
|
item_group_id = item.group_id
|
|
};
|
|
|
|
entities.Add(entity);
|
|
}
|
|
int 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)
|
|
{
|
|
int 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();
|
|
|
|
List<ToolMoldMaintainRule> maintainRules = await _db.Queryable<ToolMoldMaintainRule>().Where(it => input.ruleIds.Contains(it.id)).ToListAsync();
|
|
List<ToolMoldMaintainRuleRelation> ruleMoldRelations = await _db.Queryable<ToolMoldMaintainRuleRelation>().Where(it => input.ruleIds.Contains(it.rule_id)).ToListAsync();
|
|
if (ruleMoldRelations?.Count > 0)
|
|
{
|
|
List<ToolMoldMaintainPlan> maintainPlans = new();
|
|
List<ToolMoldMaintainPlanRelation> maintainPlanRelations = new();
|
|
foreach (ToolMoldMaintainRuleRelation mrr in ruleMoldRelations)
|
|
{
|
|
ToolMoldMaintainRule rule = await _db.Queryable<ToolMoldMaintainRule>().FirstAsync(it => it.id == mrr.rule_id);
|
|
if (rule != null && rule.cycle.HasValue && rule.cycle.Value > 0)
|
|
{
|
|
ToolMoldMaintainPlan maintainPlan = new()
|
|
{
|
|
plan_code = $"JHDM{DateTime.Now:yyyyMMddmmss}",
|
|
mode = rule.mode,
|
|
status = DictConst.UnMaintainStatusCode,
|
|
plan_start_date = DateTime.Now,
|
|
plan_end_date = DateTime.Now.AddDays(rule.cycle.Value),
|
|
create_id = _userManager.UserId,
|
|
create_time = DateTime.Now
|
|
};
|
|
maintainPlans.Add(maintainPlan);
|
|
|
|
ToolMoldMaintainPlanRelation maintainPlanReation = new()
|
|
{
|
|
maintain_plan_id = maintainPlan.id,
|
|
mold_id = mrr.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();
|
|
}
|
|
}
|
|
}
|
|
}
|