using Admin.NET.Application;
using Admin.NET.Core.EnterWareHouseModule;
using Admin.NET.Core.EnterWareHouseModule.Enum;
using Admin.NET.Core.TaskModule.Enum;
using iWareCommon.Utils;
using iWareModel;
using iWareModel.EnumType.AoSinPublicCommon;
using iWareSql.DbOrm;
using iWareSql.Orm;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace iWareCC
{
public class MoveTaskHandle
{
public static LogType currentLogType = LogType.MainTaskCompleteHandle;
public static string host = ConfigHelper.GetConfigString("WMSHost");
public static string confirm = ConfigHelper.GetConfigString("confirm");
///
/// 任务完成后处理单据状态
///
public static void HandlerMainTaskStatus()
{
List list = new List();
while (true)
{
try
{
using (DbOrm dbOrm = new DbOrm())
{
DateTime nowDate = DateTime.Now;
list = dbOrm.ware_device_wareing.Where(u => u.DeviceCode == "1099" && u.Status == 0 && (DbFunctions.DiffSeconds(u.UpdatedTime,nowDate) > 20 || u.UpdatedTime == null)).ToList();
foreach (var item in list)
{
item.UpdatedTime = nowDate;
item.UpdatedUserName = "WMS";
item.Status = 1; //状态(0:新建 1:已处理)
item.StatusName = "已处理";
item.CloseContent = "WMS关闭";
item.FinishTime = nowDate;
item.OperationRemark = "关闭报警";
var msg = "";
GetTimeDiffer(item.CreatedTime?.DateTime, nowDate, ref msg);
item.DurationTime = msg;
}
dbOrm.SaveChanges();
//var list = dbOrm.v_ware_inventory_by_container.AsNoTracking().Where(u => u.WareLocationCode != u.RealLocationCode && u.StockStatus == 2 && !string.IsNullOrEmpty(u.RealLocationCode)).ToList();
//if (list != null)
//{
// if (list.Count > 0)
// {
// string str = list[0].WareContainerCode;
// string toLocation = "";
// //查看目标库位是不是第2排
// //if (list[0].RealLocationCode.Substring(2).StartsWith("02"))
// //{
// // //查看该1排是否有货
// // var arr = list[0].RealLocationCode.Split('-');
// // string newstr = arr[0] + "-" + "01" + "-" + arr[2] + "-"+ arr[3];
// // var lvc = dbOrm.ware_location_vs_container.AsNoTracking().Where(u => u.WareLocationCode == newstr).FirstOrDefault();
// // if (lvc == null)
// // {
// // //目标库位修改为1排
// // toLocation = newstr;
// // var container = dbOrm.wms_container.Where(u => u.ContainerCode == str).FirstOrDefault();
// // container.RealLocationCode = toLocation;
// // }
// // else
// // {
// // toLocation = list[0].RealLocationCode;
// // }
// //}
// //else
// //{
// // toLocation = list[0].RealLocationCode;
// //}
// toLocation = list[0].RealLocationCode;
// //查看同一个伸位是否有任务
// if (toLocation.Substring(2).StartsWith("01"))
// {
// //查看该1排是否有货
// var arr = toLocation.Split('-');
// string newstr = arr[0] + "-" + "02" + "-" + arr[2] + "-" + arr[3];
// var isExist = dbOrm.ware_task.Any(u => u.ToLocationCode == newstr || u.FromLocationCode == newstr || u.MoveFromLocation == newstr || u.MoveToLocation == newstr);
// if (isExist)
// {
// Thread.Sleep(3000);//休眠2秒
// continue;
// }
// }
// if (toLocation.Substring(2).StartsWith("02"))
// {
// //查看该1排是否有货
// var arr = toLocation.Split('-');
// string newstr = arr[0] + "-" + "01" + "-" + arr[2] + "-" + arr[3];
// var isExist = dbOrm.ware_task.Any(u => u.ToLocationCode == newstr || u.FromLocationCode == newstr || u.MoveFromLocation == newstr || u.MoveToLocation == newstr);
// if (isExist)
// {
// Thread.Sleep(3000);//休眠2秒
// continue;
// }
// }
// //创建移库任务,每次只处理一条
// var cvm = dbOrm.ware_container_vs_material.Where(u => u.WareContainerCode == str ).FirstOrDefault();
// var tasknew = new ware_task
// {
// //OrderNo = input.OrderNo,
// TaskNo = DateTime.Now.ToString("yyyyMMddhhmmssfff"),
// //TaskName = wcsTask.TaskName,
// //TaskCategory = wcsTask.TaskType,
// TaskCategory = (int)TaskCategoryEnum.MoveStock,
// TaskDescribe = "",
// TaskPriority = 99,
// //TaskState = 0,
// TaskState = (int)TaskStateEnum.未执行,
// //TaskType = wcsTask.TaskType,
// FromLocationCode = list[0].WareLocationCode,
// ToLocationCode = toLocation,
// ContainerCode = list[0].WareContainerCode,
// Lane = list[0].Lane,
// ContainerType = 0,
// };
// cvm.StockStatus = (int)StockStatusEnum.移库中;
// dbOrm.ware_task.Add(tasknew);
// dbOrm.SaveChanges();
// }
//}
}
}
catch (Exception ex)
{
Log4NetHelper.WriteErrorLog(currentLogType, "HandlerMainTaskStatus处理库存状态异常" + ex.Message);
}
Thread.Sleep(5000);//休眠2秒
}
}
public static int GetTimeDiffer(DateTime? startTime, DateTime? endTime, ref string msg)
{
if (startTime == null || endTime == null)
{
return 0;
}
TimeSpan ts = ((DateTime)endTime - (DateTime)startTime);
msg = "";
if (ts.Days != 0)
{
msg += ts.Days + "天";
}
if (ts.Hours != 0)
{
msg += ts.Hours + "小时";
}
if (ts.Minutes != 0)
{
msg += ts.Minutes + "分钟";
}
if (ts.Seconds != 0)
{
msg += ts.Seconds + "秒";
}
return (int)ts.TotalSeconds;
}
}
}