203 lines
7.3 KiB
C#
203 lines
7.3 KiB
C#
using Aliyun.OSS;
|
||
using DOAN.Model;
|
||
using DOAN.Model.Business;
|
||
using DOAN.Model.Dto;
|
||
using DOAN.Model.MES.process;
|
||
using DOAN.Repository;
|
||
using DOAN.Service.Business.IBusinessService;
|
||
using Infrastructure.Attribute;
|
||
using Infrastructure.Extensions;
|
||
using NPOI.SS.Formula.Functions;
|
||
using NPOI.XSSF.Streaming.Values;
|
||
using SqlSugar;
|
||
using System;
|
||
using System.Linq;
|
||
|
||
namespace DOAN.Service.Business
|
||
{
|
||
/// <summary>
|
||
/// Service业务层处理
|
||
/// </summary>
|
||
[AppService(ServiceType = typeof(IProcessmodelWorkStepService), ServiceLifetime = LifeTime.Transient)]
|
||
public class ProcessmodelWorkStepService : BaseService<ProcessmodelWorkStep>, IProcessmodelWorkStepService
|
||
{
|
||
/// <summary>
|
||
/// 查询列表
|
||
/// </summary>
|
||
/// <param name="parm"></param>
|
||
/// <returns></returns>
|
||
public PagedInfo<ProcessmodeWorkStepDto> GetList(ProcessmodelWorkStepQueryDto parm)
|
||
{
|
||
var predicate = Expressionable.Create<ProcessmodelWorkStep>()
|
||
.AndIF(!string.IsNullOrEmpty(parm.StepCode), p => parm.StepCode.Contains(p.StepCode))
|
||
.AndIF(!string.IsNullOrEmpty(parm.StepName), p => parm.StepName.Contains(p.StepName));
|
||
|
||
var response = Queryable()
|
||
.Where(predicate.ToExpression())
|
||
.GroupBy(p => p.StepCode)
|
||
.ToPage<ProcessmodelWorkStep, ProcessmodeWorkStepDto>(parm);
|
||
|
||
return response;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 获取详情
|
||
/// </summary>
|
||
/// <param name="Id"></param>
|
||
/// <returns></returns>
|
||
public ProcessmodelWorkStep GetInfo(long Id)
|
||
{
|
||
var response = Queryable()
|
||
.Where(x => x.Id == Id)
|
||
.First();
|
||
|
||
return response;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 添加
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
public ProcessmodelWorkStep AddProcessmodelWorkStep(ProcessmodelWorkStep model)
|
||
{
|
||
return Context.Insertable(model).ExecuteReturnEntity();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 修改
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
public int UpdateProcessmodelWorkStep(ProcessmodelWorkStep model)
|
||
{
|
||
//var response = Update(w => w.Id == model.Id, it => new ProcessmodelOperationStep()
|
||
//{
|
||
// OperationStepCode = model.OperationStepCode,
|
||
// OperationStepName = model.OperationStepName,
|
||
// OperationCode = model.OperationCode,
|
||
// Description = model.Description,
|
||
// IsActive = model.IsActive,
|
||
// UpdateTime = model.UpdateTime,
|
||
// UpdateBy = model.UpdateBy,
|
||
// Remark = model.Remark,
|
||
//});
|
||
//return response;
|
||
return Update(model, true);
|
||
}
|
||
|
||
public List<UniqueValueQueryDto> GetUniqueValueList(string stepCode)
|
||
{
|
||
bool stepCodeExists = Context.Queryable<ProcessmodelWorkStep>()
|
||
.Any(p => p.StepCode == stepCode);
|
||
|
||
if (!stepCodeExists)
|
||
{
|
||
return new List<UniqueValueQueryDto>();
|
||
}
|
||
|
||
List<string> uniqueValues = Context.Queryable<ProcessmodelWorkStep>()
|
||
.Where(p => p.StepCode == stepCode)
|
||
.Select(p => p.UniqueValue)
|
||
.Distinct()
|
||
.Where(uv => !string.IsNullOrEmpty(uv))
|
||
.ToList();
|
||
|
||
var result = Context.Queryable<ProcessmodelDictionary>()
|
||
.Where(d => uniqueValues.Contains(d.UniqueValue)) // 批量匹配,性能优于循环查询
|
||
.Select(d => new UniqueValueQueryDto
|
||
{
|
||
Id = d.Id,
|
||
Value = d.UniqueValue,
|
||
Name = d.Description
|
||
})
|
||
.ToList();
|
||
|
||
return result;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 新增设备
|
||
/// </summary>
|
||
/// <param name="parm"></param>
|
||
/// <returns></returns>
|
||
/// <exception cref="ArgumentException"></exception>
|
||
/// <exception cref="Exception"></exception>
|
||
public int AddUniqueValue(UniqueQueryDto parm)
|
||
{
|
||
// 1. 校验入参
|
||
if (string.IsNullOrEmpty(parm.ProcessCode) || string.IsNullOrEmpty(parm.StepCode) || string.IsNullOrEmpty(parm.Description))
|
||
{
|
||
throw new ArgumentException("ProcessCode、StepCode、Description 不能为空");
|
||
}
|
||
|
||
// 2. 查询字典表中是否存在匹配的 Description
|
||
var dictionary = Context.Queryable<ProcessmodelDictionary>()
|
||
.Where(d => d.Description == parm.Description)
|
||
.First();
|
||
|
||
string uniqueValue;
|
||
if (dictionary != null)
|
||
{
|
||
// 字典表中存在匹配的 Description,使用其 UniqueValue
|
||
uniqueValue = dictionary.UniqueValue;
|
||
}
|
||
else
|
||
{
|
||
// 字典表中不存在,生成新的唯一 UniqueValue
|
||
uniqueValue = GenerateUniqueValue();
|
||
}
|
||
|
||
// 3. 查询工步基础信息
|
||
var workStep = Context.Queryable<ProcessmodelWorkStep>()
|
||
.Where(x => x.StepCode == parm.StepCode && x.ProcessCode == parm.ProcessCode)
|
||
.First();
|
||
|
||
if (workStep == null)
|
||
{
|
||
throw new Exception("未找到对应的工步信息");
|
||
}
|
||
|
||
// 4. 插入新的工步记录
|
||
var newWorkStep = new ProcessmodelWorkStep
|
||
{
|
||
StepCode = workStep.StepCode,
|
||
StepName = workStep.StepName,
|
||
ProcessCode = workStep.ProcessCode,
|
||
Description = workStep.Description,
|
||
IsActive = workStep.IsActive,
|
||
UniqueValue = uniqueValue,
|
||
CreateTime = DateTime.Now
|
||
};
|
||
|
||
return Context.Insertable(newWorkStep).ExecuteCommand();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 生成唯一的 UniqueValue
|
||
/// </summary>
|
||
/// <returns>唯一的 UniqueValue</returns>
|
||
private string GenerateUniqueValue()
|
||
{
|
||
// 生成格式为 "UV" + 时间戳 + 随机数 的唯一值
|
||
var timestamp = DateTime.Now.ToString("yyyyMMddHHmmssfff");
|
||
var random = new Random().Next(1000, 9999);
|
||
return $"UV{timestamp}{random}";
|
||
|
||
// 也可以根据业务规则生成,例如结合 ProcessCode 和 StepCode
|
||
// return $"{parm.ProcessCode}-{parm.StepCode}-{Guid.NewGuid().ToString("N").Substring(0, 8)}";
|
||
}
|
||
|
||
///// <summary>
|
||
///// 删除设备
|
||
///// </summary>
|
||
///// <param name="id"></param>
|
||
///// <returns></returns>
|
||
///// <exception cref="NotImplementedException"></exception>
|
||
//public int DeleteUniqueValue(long id)
|
||
//{
|
||
// Context.Deleteable<ProcessmodelWorkStep>()
|
||
//}
|
||
}
|
||
} |