using JNPF.Common.Core.Manager;
using JNPF.Common.Enums;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.FriendlyException;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using Tnb.EquipMgr.Entities;
using Tnb.PerMgr.Entities;
using Tnb.PerMgr.Entities.Dto;
using Tnb.PerMgr.Interfaces;
namespace Tnb.PerMgr
{
///
/// 工艺参数类型
///
[ApiDescriptionSettings
(Tag = ModuleConsts.Tag, Area = ModuleConsts.Area, Order = 700)]
[Route("api/[area]/[controller]/[action]")]
public class PerProcessParamTypeService : IPerProcessParamTypeService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository _repository;
private readonly IUserManager _userManager;
public PerProcessParamTypeService(ISqlSugarRepository repository, IUserManager userManager)
{
_userManager = userManager;
_repository = repository;
}
[HttpPost]
public async Task GetProcessParamType(Dictionary dic)
{
string equipId = dic["equip_id"];
var db = _repository.AsSugarClient();
var equipment = await db.Queryable().Where(x => x.id == equipId).SingleAsync();
string? equipTypeId = equipment?.equip_type_id;
//List perProcessParamTypes = await _repository.GetListAsync(x => x.equip_type_id == equipTypeId);
var result = await db.Queryable()
.Where(x => x.equip_type_id == equipTypeId)
.OrderBy(x => x.ordinal)
.Select(x => new
{
tab_id = x.id,
tab_name = x.name,
daq_list = SqlFunc.Subqueryable().Where(a => a.equip_id == equipId).ToList(a => new ProcessParamTypeDaqListOutput
{
id = a.id,
label_name = a.label_name,
}),
children = SqlFunc.Subqueryable()
.LeftJoin((y, z) => y.tolerance_category_id == z.id)
.Where(y => y.process_param_type_id == x.id)
.OrderBy((y, z) => y.ordinal)
.ToList((y, z) => new ProcessParamTypeChildrenOutput
{
process_param_id = y.id,
name = y.name,
upper_value = z.upper_value,
lower_value = z.lower_value
}),
}).ToListAsync();
return result;
}
[HttpPost]
public async Task SaveData(List input)
{
var db = _repository.AsSugarClient();
DbResult result = await db.Ado.UseTranAsync(async () =>
{
List list = new List();
List insertIds = new List();
if (input != null && input.Count > 0)
{
foreach (var item in input)
{
PerProcessStandardsD processStandardsD = await db.Queryable().Where(x =>
x.process_param_id == item.process_param_id && x.process_standards_id == item.process_standards_id &&
item.process_param_type_id == x.process_param_type_id).FirstAsync();
if (processStandardsD != null)
{
PerProcessStandardsH perProcessStandardsH = await db.Queryable().SingleAsync(x => x.id == processStandardsD.process_standards_id);
PerProcessParam processParam = await db.Queryable().SingleAsync(x => x.id == processStandardsD.process_param_id);
if (processStandardsD.value != item.value)
{
PerProcessParamEditRecord record = new PerProcessParamEditRecord
{
process_param_id = item?.process_param_id ?? "",
old_value = processStandardsD.value,
new_value = item?.value ?? 0,
modify_id = _userManager.UserId,
modify_time = DateTime.Now,
org_id = _userManager.GetUserInfo().Result.organizeId,
equip_id = perProcessStandardsH.equip_id,
process_param_name = processParam.name
};
await db.Insertable(record).ExecuteCommandAsync();
}
}
if (item == null || string.IsNullOrEmpty(item.id))
{
PerProcessStandardsD insertObj = new PerProcessStandardsD()
{
value = item?.value ?? 0,
process_param_id = item?.process_param_id ?? "",
process_standards_id = item?.process_standards_id ?? "",
process_param_type_id = item?.process_param_type_id ?? "",
daq_id = item?.daq_id ?? "",
};
insertIds.Add(insertObj.id);
await db.Insertable(insertObj).ExecuteCommandAsync();
}
else
{
await db.Updateable()
.SetColumns(x => x.value == item.value)
.SetColumns(x => x.daq_id == item.daq_id)
.Where(x => x.id == item.id).ExecuteCommandAsync();
}
}
List notDeleteIds = input.Select(x => x.id).ToList();
notDeleteIds.AddRange(insertIds);
await db.Deleteable().Where(x => !notDeleteIds.Contains(x.id)).ExecuteCommandAsync();
}
});
if (!result.IsSuccess) throw Oops.Oh(ErrorCode.COM1008);
return result.IsSuccess ? "保存成功" : result.ErrorMessage;
}
[HttpPost]
public async Task GetProcessStandardsChildrenInfo(Dictionary dic)
{
string id = dic["id"];
var db = _repository.AsSugarClient();
var perProcessStandardsH = await db.Queryable().SingleAsync(x => x.id == id);
var equipment = await db.Queryable().Where(x => x.id == perProcessStandardsH.equip_id).SingleAsync();
string? equipTypeId = equipment?.equip_type_id;
var result = await db.Queryable()
.Where(a => a.equip_type_id == equipTypeId)
.OrderBy(a => a.ordinal)
.Select(a => new
{
tab_id = a.id,
tab_name = a.name,
daq_list = SqlFunc.Subqueryable().Where(e => e.equip_id == perProcessStandardsH.equip_id).ToList(e => new ProcessParamTypeDaqListOutput
{
id = e.id,
label_name = e.label_name,
}),
children = SqlFunc.Subqueryable()
.LeftJoin((b, c) => b.process_param_id == c.id)
.LeftJoin((b, c, d) => c.tolerance_category_id == d.id)
.Where((b, c, d) => c.process_param_type_id == a.id && b.process_standards_id == id)
.OrderBy((b, c, d) => c.ordinal)
.ToList((b, c, d) => new ProcessParamTypeChildrenOutput
{
id = b.id,
process_param_id = c.id,
name = c.name,
value = b.value,
upper_value = d.upper_value,
lower_value = d.lower_value,
daq_id = b.daq_id
}),
}).ToListAsync();
return result;
}
}
}