fix(工单排序): 修复工单上下移动时的排序问题
添加空值检查和使用事务确保排序操作的原子性,同时优化查询逻辑以提高可靠性
This commit is contained in:
@@ -65,35 +65,73 @@ namespace DOAN.Service.MES.product
|
||||
public int MoveWorkorder(string id, int type)
|
||||
{
|
||||
int result = 0;
|
||||
ProWorkorder toMove = Context
|
||||
.Queryable<ProWorkorder>()
|
||||
.Where(it => it.Id == id)
|
||||
.First();
|
||||
var pervious = Context
|
||||
.Queryable<ProWorkorder>()
|
||||
.Where(it => it.WorkorderDate == toMove.WorkorderDate);
|
||||
|
||||
//上移动
|
||||
if (type == 1)
|
||||
try
|
||||
{
|
||||
pervious = pervious
|
||||
.Where(it => it.Sort <= toMove.Sort)
|
||||
.OrderByDescending(it => it.Sort);
|
||||
}
|
||||
//下移
|
||||
else if (type == 2)
|
||||
{
|
||||
pervious = pervious.Where(it => it.Sort >= toMove.Sort).OrderBy(it => it.Sort);
|
||||
}
|
||||
// 获取要移动的工单
|
||||
ProWorkorder toMove = Context
|
||||
.Queryable<ProWorkorder>()
|
||||
.Where(it => it.Id == id)
|
||||
.First();
|
||||
|
||||
ProWorkorder exchange = pervious.Skip(1).Take(1).First();
|
||||
if (exchange != null)
|
||||
// 检查工单是否存在
|
||||
if (toMove == null)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
// 检查Sort值是否存在
|
||||
if (!toMove.Sort.HasValue)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
ProWorkorder exchange = null;
|
||||
|
||||
// 上移动:找到Sort值小于当前工单的最大Sort值的工单
|
||||
if (type == 1)
|
||||
{
|
||||
exchange = Context
|
||||
.Queryable<ProWorkorder>()
|
||||
.Where(it => it.WorkorderDate == toMove.WorkorderDate)
|
||||
.Where(it => it.PlanNum > 0)
|
||||
.Where(it => it.Sort.HasValue && it.Sort < toMove.Sort)
|
||||
.OrderByDescending(it => it.Sort)
|
||||
.First();
|
||||
}
|
||||
// 下移:找到Sort值大于当前工单的最小Sort值的工单
|
||||
else if (type == 2)
|
||||
{
|
||||
exchange = Context
|
||||
.Queryable<ProWorkorder>()
|
||||
.Where(it => it.WorkorderDate == toMove.WorkorderDate)
|
||||
.Where(it => it.PlanNum > 0)
|
||||
.Where(it => it.Sort.HasValue && it.Sort > toMove.Sort)
|
||||
.OrderBy(it => it.Sort)
|
||||
.First();
|
||||
}
|
||||
|
||||
// 检查交换工单是否存在
|
||||
if (exchange != null && exchange.Sort.HasValue)
|
||||
{
|
||||
// 使用事务确保两个工单的Sort值同时更新
|
||||
UseTran2(() =>
|
||||
{
|
||||
// 交换Sort值
|
||||
int temp = toMove.Sort.Value;
|
||||
toMove.Sort = exchange.Sort;
|
||||
exchange.Sort = temp;
|
||||
|
||||
// 更新工单
|
||||
result += Context.Updateable(toMove).ExecuteCommand();
|
||||
result += Context.Updateable(exchange).ExecuteCommand();
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
int temp = toMove.Sort.Value;
|
||||
toMove.Sort = exchange.Sort;
|
||||
exchange.Sort = temp;
|
||||
result += Context.Updateable(toMove).ExecuteCommand();
|
||||
result += Context.Updateable(exchange).ExecuteCommand();
|
||||
// 记录错误日志
|
||||
Console.WriteLine($"移动工单时出错: {ex.Message}");
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user