using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_Core;
using WIDESEA_Model.Models;
using WIDESEAWCS_DTO.WCSInfo;
namespace WIDESEA_TaskInfoService
{
public partial class TaskService
{
///
/// 输送线申请入库
///
///
///
public WebResponseContent ConveyorLineRequestInbound(ConveyorLineDTO lineDTO)
{
WebResponseContent content = new WebResponseContent().OK();
try
{
string ConveyorLineID = AppSettings.Configuration[nameof(ConveyorLineID)];
if (!ConveyorLineID.Split(",").Contains(lineDTO.stationCode)) throw new Exception($"未找到输送线编号[{lineDTO.stationCode}]的信息");
switch (lineDTO.stationCode)
{
case "102":
content = TransmissionlineRequest(lineDTO);
break;
case "1001":
content = ProductionlineRequest(lineDTO);
break;
}
}
catch (Exception ex)
{
content.Code = 404;
content.Error(ex.Message);
}
return content;
}
///
/// 验证数据
///
/// 起始地址
/// 托盘编号
/// 是否检查组盘信息--区分物料入库和空托入库
/// 组盘信息--可空
/// 返回处理结果
private (bool, string) CheckRequestInbound(string stationCode, string palletCode, bool isCheckStock = true, Dt_StockInfo? stockInfo = null)
{
//if (BaseDal.QueryFirst(x => x.PalletCode == palletCode) != null)
//{
// return (false, "该托盘号已有任务");
//}
//if (BaseDal.QueryFirst(x => (x.SourceAddress == stationCode || x.CurrentAddress == stationCode) && x.TaskStatus == InTaskStatusEnum.AGV_InFinish.ObjToInt()) != null)
//{
// return (false, "当前入库站台已有一条任务");
//}
if (isCheckStock)
{
if (stockInfo == null)
{
return (false, "未找到组盘信息");
}
if (stockInfo.StockStatus != StockStatusEmun.组盘暂存.ObjToInt())
{
return (false, "该组盘状态不可入库");
}
if (!string.IsNullOrEmpty(stockInfo.LocationCode))
{
return (false, "该托盘已绑定货位");
}
if (stockInfo.Details == null || stockInfo.Details.Count == 0)
{
return (false, "没有库存明细信息");
}
}
else
{
if (_stockService.StockInfoService.Repository.QueryFirst(x => x.PalletCode == palletCode) != null)
{
return (false, "该托盘已存在库内");
}
}
return (true, "成功");
}
///
/// 输送线申请
///
///
///
public WebResponseContent TransmissionlineRequest(ConveyorLineDTO lineDTO)
{
WebResponseContent content = new WebResponseContent().OK();
try
{
//if (BaseDal.QueryFirst(x => (x.SourceAddress == lineDTO.stationCode || x.CurrentAddress == lineDTO.stationCode) && x.TaskStatus == InTaskStatusEnum.AGV_InFinish.ObjToInt()) != null)
//{
// throw new Exception($"当前入库站台[{lineDTO.stationCode}]已有一条任务");
//}
var task = BaseDal.QueryFirst(x => x.PalletCode == lineDTO.Barcode && x.TaskType == TaskTypeEnum.Inbound.ObjToInt());
if (task == null) throw new Exception($"未找到托盘号[{lineDTO.Barcode}]的入库任务");
//if (task.TaskState != (int)InTaskStatusEnum.AGV_InFinish) throw new Exception($"托盘号[{lineDTO.Barcode}]的入库任务状态不匹配");
Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.GetStockInfo(lineDTO.Barcode);
(bool, string) result = CheckRequestInbound(lineDTO.stationCode, lineDTO.Barcode, true, stockInfo);
if (!result.Item1) return content = WebResponseContent.Instance.Error(result.Item2);
Dt_StockInfoDetail stockInfoDetail = stockInfo.Details.FirstOrDefault();
if (lineDTO.Spec != 1 || lineDTO.Weight != 500)//检测条件需更改!!!!!!!!!!
{
task.NextAddress = "101";
task.TaskState = (int)InTaskStatusEnum.InException;
task.Remark = $"托盘[{lineDTO.Barcode}]信息不合格";
//stockInfo.StockStatus = StockStatusEmun.入库撤销.ObjToInt();
}
else
{
task.NextAddress = "104";
task.CurrentAddress = lineDTO.stationCode;
task.Remark=string.Empty;
task.TaskState = (int)InTaskStatusEnum.Line_InExecuting;
stockInfo.StockStatus = StockStatusEmun.入库确认.ObjToInt();
stockInfoDetail.Status = StockStatusEmun.入库确认.ObjToInt();
}
#region 事务
Db.Ado.BeginTran();
BaseDal.UpdateData(task);
_stockService.StockInfoService.Repository.UpdateData(stockInfo);
_stockService.StockInfoDetailService.Repository.UpdateData(stockInfoDetail);
//Db.Updateable(stockInfo).ExecuteCommand();
Db.Ado.CommitTran();
#endregion
if (!string.IsNullOrEmpty(task.Remark)) throw new Exception(task.Remark);
}
catch (Exception ex)
{
Db.Ado.RollbackTran();
content.Error(ex.Message);
}
return content;
}
///
/// 产线申请
///
///
///
public WebResponseContent ProductionlineRequest(ConveyorLineDTO lineDTO)
{
WebResponseContent content = new WebResponseContent();
try
{
var task = BaseDal.QueryFirst(x => x.PalletCode == lineDTO.Barcode);
if (task != null && task.TaskType == TaskTypeEnum.Inbound.ObjToInt())
return content.OK(data: task);
if (task != null) throw new Exception($"托盘号[{lineDTO.Barcode}]已存在任务");
Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.GetStockInfo(lineDTO.Barcode);
(bool, string) result = CheckRequestInbound(lineDTO.stationCode, lineDTO.Barcode, true, stockInfo);
if (!result.Item1) return content = WebResponseContent.Instance.Error(result.Item2);
var StockInfoDetails = stockInfo.Details.Where(x => x.StockId == stockInfo.Id).ToList();
#region 生成入库任务
Dt_Task dt_Task = new Dt_Task()
{
CurrentAddress = lineDTO.stationCode,
NextAddress = "SC01",
SourceAddress = lineDTO.stationCode,
TargetAddress = "SC01",
CreateDate = DateTime.Now,
Creater = "System",
PalletCode = lineDTO.Barcode,
OrderNo = StockInfoDetails.Count() == 1 ? StockInfoDetails.First().OrderNo : null,
Roadway = "SC01",
TaskNum = Convert.ToInt32(DateTime.Now.ToString("HHmmss")),
TaskState = InTaskStatusEnum.InNew.ObjToInt(),
TaskType = TaskTypeEnum.Inbound.ObjToInt(),
};
dt_Task.Dispatchertime = dt_Task.CreateDate;
Db.Ado.BeginTran();
BaseDal.AddData(dt_Task);
task = BaseDal.QueryFirst(x => x.PalletCode == lineDTO.Barcode && x.TaskType == TaskTypeEnum.Inbound.ObjToInt());
//content.Data = dt_Task;
Db.Ado.CommitTran();
if (task != null) return content.OK(data: task);
#endregion
}
catch (Exception ex)
{
Db.Ado.RollbackTran();
content.Error(ex.Message);
}
return content;
}
}
}