using Tiger.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Rhea.Common;
using System.Net;
using System.Linq;
using Newtonsoft.Json;
using Tiger.IBusiness;
using Microsoft.AspNetCore.Http;
namespace Tiger.Business
{
public partial class Biz
{
///
/// 工艺路线
///
public partial class Route : IRoute
{
///
/// 获取工艺路线
///
///
///
public async Task>> GetRoute(string routerCode)
{
var res = new ApiAction>();
List list = new List();
try
{
list = await Db.Queryable()
.WhereIF(!string.IsNullOrEmpty(routerCode), x => x.ROT_CODE.Equals(routerCode))
.OrderBy(x => x.CREATE_TIME).ToListAsync();
}
catch (Exception ex)
{
res.CatchExceptionWithLog(ex, "查询异常");
}
res.Data = list;
return res;
}
///
/// 获取工艺路线图形数据
///
///
///
public async Task> GetRouteData(string routerId)
{
var res = new ApiAction();
RouteData rotData = new RouteData();
try
{
if (string.IsNullOrEmpty(routerId))
{
res.IsSuccessed = false;
res.LocaleMsg = new($"传入的工艺路线ID为空,不能查找工艺路线!");
return res;
}
//工艺路线
var route = await Db.Queryable()
.WhereIF(!string.IsNullOrEmpty(routerId), x => x.ID.Equals(routerId))
.OrderBy(x => x.CREATE_TIME).FirstAsync();
rotData.route = route;
//节点
var nodes = await Db.Queryable()
.WhereIF(!string.IsNullOrEmpty(routerId), x => x.ROT_ID.Equals(routerId))
.ToListAsync();
foreach (var node in nodes)
{
node.node = new()
{
id = node.ID,
type = node.GPH_TYPE,
x = node.GPH_X,
y = node.GPH_Y,
properties = node.GPH_PROP,
text = new()
{
x = node.GPH_X,
y = node.GPH_Y,
value = node.GPH_TEXT
}
};
}
rotData.nodes = nodes;
var acts = await Db.Queryable()
.WhereIF(!string.IsNullOrEmpty(routerId), x => x.ROT_ID.Equals(routerId))
.ToListAsync();
foreach (var act in acts)
{
act.node = new()
{
id = act.ID,
type = act.GPH_TYPE,
x = act.GPH_X,
y = act.GPH_Y,
properties = act.GPH_PROP,
text = new()
{
x = act.GPH_X,
y = act.GPH_Y,
value = act.GPH_TEXT
}
};
}
rotData.acts = acts;
//边
var edges = await Db.Queryable()
.WhereIF(!string.IsNullOrEmpty(routerId), x => x.ROT_ID.Equals(routerId))
.ToListAsync();
foreach (var edge in edges)
{
edge.edge = new()
{
id = edge.ID,
type = edge.GPH_TYPE,
sourceNodeId = edge.SRC_NODE,
targetNodeId = edge.TGT_NODE,
properties = edge.GPH_PROP,
startPoint = new()
{
x = edge.GPH_SRC_X,
y = edge.GPH_SRC_Y,
},
endPoint = new()
{
x = edge.GPH_TGT_X,
y = edge.GPH_TGT_Y,
},
pointsList = new()
{
new(){
x = edge.GPH_SRC_X,
y = edge.GPH_SRC_Y,
},
new(){
x = edge.GPH_TGT_X,
y = edge.GPH_TGT_Y,
}
}
};
}
rotData.edges = edges;
}
catch (Exception ex)
{
res.CatchExceptionWithLog(ex, "查询异常");
}
res.Data = rotData;
return res;
}
///
/// 保存边数据
///
///
///
public async Task SaveEdges(List routeEdge)
{
var result = new ApiAction();
try
{
var db = Db;
var dbTran = db.UseTran(() =>
{
var y = db.Storageable(routeEdge)
.WhereColumns(t => new { t.ID, t.GHOST_ROW })
.ToStorage();
y.AsInsertable.ExecuteCommand();
y.AsUpdateable.ExecuteCommand();
});
if (!dbTran.IsSuccess)
{
result.IsSuccessed = false;
result.LocaleMsg = new($"保存边数据异常");
}
}
catch (Exception ex)
{
result.CatchExceptionWithLog(ex, "保存边数据异常");
}
return await Task.FromResult(result);
}
///
/// 保存节点数据
///
///
///
public async Task SaveNodes(List routeNode)
{
var result = new ApiAction();
try
{
var db = Db;
var dbTran = db.UseTran(() =>
{
var y = db.Storageable(routeNode)
.WhereColumns(t => new { t.ID, t.GHOST_ROW })
.ToStorage();
y.AsInsertable.ExecuteCommand();
y.AsUpdateable.ExecuteCommand();
});
if (!dbTran.IsSuccess)
{
result.IsSuccessed = false;
result.LocaleMsg = new($"保存节点数据异常");
}
}
catch (Exception ex)
{
result.CatchExceptionWithLog(ex, "保存节点数据异常");
}
return await Task.FromResult(result);
}
///
/// 保存行为
///
///
///
public async Task SaveActs(List routeAct)
{
var result = new ApiAction();
try
{
var db = Db;
var dbTran = db.UseTran(() =>
{
var y = db.Storageable(routeAct)
.WhereColumns(t => new { t.ID, t.GHOST_ROW })
.ToStorage();
y.AsInsertable.ExecuteCommand();
y.AsUpdateable.ExecuteCommand();
});
if (!dbTran.IsSuccess)
{
result.IsSuccessed = false;
result.LocaleMsg = new($"保存行为数据异常");
}
}
catch (Exception ex)
{
result.CatchExceptionWithLog(ex, "保存行为数据异常");
}
return await Task.FromResult(result);
}
///
/// 保存
///
///
///
public async Task SaveRoute(MES_ROUTE route)
{
var result = new ApiAction();
try
{
var _route = Db.Queryable().Where(x=>x.ROT_CODE == route.ROT_CODE).First();
if (_route!=null && Db.Queryable().Where(x=>x.ROT_ID == _route.ID).Any())
{
result.IsSuccessed = false;
result.LocaleMsg = new($"工艺路线已经有设计记录,不能保存!");
return result;
}
var db = Db;
var dbTran = db.UseTran(() =>
{
var y = db.Storageable(route)
.WhereColumns(t => new { t.ROT_CODE, t.GHOST_ROW })
.ToStorage();
y.AsInsertable.ExecuteCommand();
y.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
});
if (!dbTran.IsSuccess)
{
result.IsSuccessed = false;
result.Message = $"保存工艺路线异常";
}
}
catch (Exception ex)
{
result.CatchExceptionWithLog(ex, "保存工艺路线异常");
}
return await Task.FromResult(result);
}
///
/// 保存工艺路线图形数据
///
///
///
public async Task SaveRouteData(RouteData routeData)
{
var result = new ApiAction();
try
{
var db = Db;
var dbTran = db.UseTran(() =>
{
if (routeData.route != null)
{
var y = db.Storageable(routeData.route)
.WhereColumns(t => new { t.ROT_CODE, t.GHOST_ROW })
.ToStorage();
y.AsInsertable.ExecuteCommand();
y.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
}
db.Deleteable(false).Where(x => x.ROT_ID == routeData.route.ID).ExecuteCommand();
db.Deleteable(false).Where(x => x.ROT_ID == routeData.route.ID).ExecuteCommand();
db.Deleteable(false).Where(x=>x.ROT_ID == routeData.route.ID).ExecuteCommand();
if (routeData.nodes != null)
{
db.Insertable(routeData.nodes).ExecuteCommand();
}
if (routeData.edges != null)
{
db.Insertable(routeData.edges).ExecuteCommand();
}
if (routeData.acts != null)
{
db.Insertable(routeData.acts).ExecuteCommand();
}
});
if (!dbTran.IsSuccess)
{
result.IsSuccessed = false;
result.LocaleMsg = new($"保存工艺路线图形数据异常");
}
}
catch (Exception ex)
{
result.CatchExceptionWithLog(ex, "保存工艺路线图形数据异常");
}
return await Task.FromResult(result);
}
///
/// 删除工艺路线
///
///
///
public async Task DeleteRoute(string routeId) {
var result = new ApiAction();
try
{
//查询是否已经有工单在用或者有绑定产品
var db = Db;
var dbTran = db.UseTran(() =>
{
db.Deleteable(false).Where(x => x.ROT_ID == routeId).ExecuteCommand();
db.Deleteable(false).Where(x => x.ROT_ID == routeId).ExecuteCommand();
db.Deleteable(false).Where(x => x.ROT_ID == routeId).ExecuteCommand();
db.Deleteable().Where(x => x.ID == routeId).ExecuteCommand();
});
if (!dbTran.IsSuccess)
{
result.IsSuccessed = false;
result.LocaleMsg = new($"删除工艺路线异常");
}
}
catch (Exception ex)
{
result.CatchExceptionWithLog(ex, "删除工艺路线异常");
}
return await Task.FromResult(result);
}
}
}
}