using System;
using System.Collections.Generic;
using System.Runtime.ConstrainedExecution;
using System.Web.Http;
using HH.WCS.Mobox3.DSZSH.core;
using HH.WCS.Mobox3.DSZSH.device;
using HH.WCS.Mobox3.DSZSH.models;
using HH.WCS.Mobox3.DSZSH.util;
using HH.WCS.Mobox3.DSZSH.wms;
using Newtonsoft.Json;
using SqlSugar;
using static HH.WCS.Mobox3.DSZSH.api.ApiModel;
using static HH.WCS.Mobox3.DSZSH.api.OtherModel;
using static HH.WCS.Mobox3.DSZSH.Config;
using static HH.WCS.Mobox3.DSZSH.core.Monitor;
namespace HH.WCS.Mobox3.DSZSH.api {
///
/// 测试用:如果项目中要和设备对接,前期设备无法测试,用接口模拟
///
[RoutePrefix("api")]
public class DebugController : ApiController {
///
/// AGV状态一键回报134562
///
/// 容器号
///
[HttpPost]
[Route("AGVSeriesReports")]
public ReturnResults AGVSeriesReports(UpdateTaskState model) {
ReturnResults returnResult = new ReturnResults();
returnResult.ResultList = new List();
var agvTaskState = new AgvTaskState() {
task_no = model.TaskID,
forklift_no = model.ForkliftNo,
};
agvTaskState.state = 1;
var temp1 = WCSCore.OperateAgvTaskStatus(agvTaskState);
returnResult.ResultList.Add(temp1);
agvTaskState.state = 3;
var temp3 = WCSCore.OperateAgvTaskStatus(agvTaskState);
returnResult.ResultList.Add(temp3);
agvTaskState.state = 4;
var temp4 = WCSCore.OperateAgvTaskStatus(agvTaskState);
returnResult.ResultList.Add(temp4);
agvTaskState.state = 5;
var temp5 = WCSCore.OperateAgvTaskStatus(agvTaskState);
returnResult.ResultList.Add(temp5);
agvTaskState.state = 6;
var temp6 = WCSCore.OperateAgvTaskStatus(agvTaskState);
returnResult.ResultList.Add(temp6);
agvTaskState.state = 2;
var temp2 = WCSCore.OperateAgvTaskStatus(agvTaskState);
returnResult.ResultList.Add(temp2);
return returnResult;
}
///
/// 模拟AGV回报任务状态(指定下一个状态,用于需要分段测试的场景)
/// 性能相对较差,仅用于测试,一般不使用
///
///
///
[HttpPost]
[Route("AgvReports")]
public ReturnResults AgvReports(AgvReportsInfo model) {
var db = new SqlHelper().GetInstance();
ReturnResults returnResult = new ReturnResults();
returnResult.ResultList = new List();
var agvTaskState = new AgvTaskState() {
task_no = model.TaskId,
forklift_no = model.ForkliftNo,
};
var taskAction = db.Queryable()
.Where(t => t.S_TASK_CODE == model.TaskId)
.OrderBy(t => new { create = SqlFunc.Desc(t.T_CREATE) }).First(); // 最后一个Action状态
var lastState = taskAction?.N_ACTION_CODE ?? 0;
if (model.NextState <= 0 || model.NextState >= 7) {
LogHelper.Info("小车回报状态不在134562的范围内");
}
// 0 ==> 1,3,4,5,6,2
if (lastState == 0 && model.NextState >= 1) {
agvTaskState.state = 1;
var temp = WCSCore.OperateAgvTaskStatus(agvTaskState);
returnResult.ResultList.Add(temp);
}
// 0,1 ==> 3,4,5,6,2
if (lastState < 2 && model.NextState >= 2) {
agvTaskState.state = 3;
var temp = WCSCore.OperateAgvTaskStatus(agvTaskState);
returnResult.ResultList.Add(temp);
}
// 0,1,3 ==> 4,5,6,2
if (lastState < 4 && lastState != 2 && (model.NextState >= 3 || model.NextState == 2)) {
agvTaskState.state = 4;
var temp = WCSCore.OperateAgvTaskStatus(agvTaskState);
returnResult.ResultList.Add(temp);
}
// 0,1,3,4 ==> 5,6,2
if (lastState < 5 && lastState != 2 && (model.NextState >= 4 || model.NextState == 2)) {
agvTaskState.state = 5;
var temp = WCSCore.OperateAgvTaskStatus(agvTaskState);
returnResult.ResultList.Add(temp);
}
// 0,1,3,4,5 ==> 6,2
if (lastState < 6 && lastState != 2 && (model.NextState >= 5 || model.NextState == 2)) {
agvTaskState.state = 6;
var temp = WCSCore.OperateAgvTaskStatus(agvTaskState);
returnResult.ResultList.Add(temp);
}
// 0,1,3,4,5,6 ==> 2
if (lastState != 2 && model.NextState == 2) {
agvTaskState.state = 2;
var temp = WCSCore.OperateAgvTaskStatus(agvTaskState);
returnResult.ResultList.Add(temp);
}
return returnResult;
}
///
/// AGV状态一键回报134
///
/// 容器号
///
[HttpPost]
[Route("AGVSeriesReports14")]
public ReturnResults AGVSeriesReports14(UpdateTaskState model) {
var agvTaskState = new AgvTaskState() {
task_no = model.TaskID,
forklift_no = model.ForkliftNo,
state = 1
};
ReturnResults returnResult = new ReturnResults();
returnResult.ResultList = new List();
var temp1 = WCSCore.OperateAgvTaskStatus(agvTaskState);
returnResult.ResultList.Add(temp1);
agvTaskState.state = 3;
var temp3 = WCSCore.OperateAgvTaskStatus(agvTaskState);
returnResult.ResultList.Add(temp3);
agvTaskState.state = 4;
var temp4 = WCSCore.OperateAgvTaskStatus(agvTaskState);
returnResult.ResultList.Add(temp4);
return returnResult;
}
///
/// AGV状态一键回报562
///
/// 容器号
///
[HttpPost]
[Route("AGVSeriesReports62")]
public ReturnResults AGVSeriesReports62(UpdateTaskState model) {
var agvTaskState = new AgvTaskState() {
task_no = model.TaskID,
forklift_no = model.ForkliftNo,
state = 5
};
ReturnResults returnResult = new ReturnResults();
returnResult.ResultList = new List();
var temp5 = WCSCore.OperateAgvTaskStatus(agvTaskState);
returnResult.ResultList.Add(temp5);
agvTaskState.state = 6;
var temp6 = WCSCore.OperateAgvTaskStatus(agvTaskState);
returnResult.ResultList.Add(temp6);
agvTaskState.state = 2;
var temp2 = WCSCore.OperateAgvTaskStatus(agvTaskState);
returnResult.ResultList.Add(temp2);
return returnResult;
}
///
/// 初始化数据库
///
///
[HttpPost]
[Route("CreateDatabase")]
public string CreateDatabase() {
try {
var db = new SqlHelper().GetInstance();
var entityTypes = new Type[] { };
//db.CodeFirst.InitTables(entityTypes);
}
catch (Exception ex) {
LogHelper.Info($"发生了异常");
return "初始化数据库错误" + ex.Message;
}
return "成功";
}
///
/// DEBUG:模拟输送线产线满托盘下线流程
///
///
///
[HttpPost]
[Route("AddInboundTask")]
public string AddInboundTask(AddInboundTaskInfo model) {
return WCSCore.CreateInboundTask(model.StartLoc, model.CntrCode).Content;
}
///
/// DEBUG:模拟人工将料箱搬运到产线上线口(直接修改数据库)
///
///
///
[HttpPost]
[Route("GoodpackToProdLine")]
public string GoodpackToProdLine(GoodpackToProdLineInfo model) {
var db = new SqlHelper().GetInstance();
try {
// 查询起点货位:数量=0
var startLoc = db.Queryable().LeftJoin((l, c) => l.S_CODE == c.S_LOC_CODE)
.Where((l,c) => l.N_LOCK_STATE == 0 && l.S_LOCK_STATE == "无" && l.C_ENABLE == "Y" && l.S_CODE == model.StartLoc && l.N_CURRENT_NUM == 1 && c.S_CNTR_CODE == model.CntrCode).First();
if (startLoc == null) {
return $"没有找到起点货位'{model.StartLoc}'!要求:锁状态='无',当前容器数量=1";
}
// 查询终点货位
// Order:按货位层数,从小到大排列
var endLoc = db.Queryable().Where(l => l.N_LOCK_STATE == 0 && l.S_LOCK_STATE == "无" && l.C_ENABLE == "Y" && l.N_CURRENT_NUM == 0).First();
if (endLoc == null) {
return $"没有找到合适的终点货位!要求:锁状态='无',当前容器数量=0";
}
var locCntrRel = db.Queryable().Where(c => c.S_CNTR_CODE == model.CntrCode).First() ;
if (locCntrRel == null) {
return $"该容器不存在绑定的货位!";
}
locCntrRel.S_LOC_CODE = model.StartLoc;
startLoc.N_CURRENT_NUM = 0;
endLoc.N_CURRENT_NUM = 1;
using (var tran = db.Ado.UseTran()) {
if (db.Updateable(startLoc).ExecuteCommand() <= 0 &&
db.Updateable(endLoc).ExecuteCommand() <= 0 &&
db.Updateable(locCntrRel).ExecuteCommand() <= 0) {
tran.RollbackTran();
return "数据库操作失败!";
}
tran.CommitTran() ;
return "数据库操作成功";
}
}
catch (Exception ex) {
return ex.Message;
}
}
///
/// DEBUG:模拟Erp下发出库计划单
///
///
///
[HttpPost]
[Route("TestErpSendOutboundPlan")]
public ErpResult TestErpSendOutboundPlan(TestErpSendOutboundPlanInfo model) {
var apiName = "ERP下发出库计划单";
//LogHelper.InfoApi(apiName, model);
var erpModel = new ErpSendOutboundPlanInfo();
erpModel.jhdh = model.PlanNo;
erpModel.cpdm = model.ItemCode;
erpModel.pzjs = model.ItemNum;
LogHelper.InfoApi(apiName, erpModel);
return ApiHelper.ErpSendOutboundPlan(erpModel);
}
///
/// (内部方法请勿调用)模拟取货完成反馈Erp回报结果(默认为success)
///
///
///
[HttpPost]
[Route("PickUpReturn")]
public TestErpResult PickUpReturn(PickUpReturnErpInfo model) {
if (model.sfjs == 0) {
return new TestErpResult {
code = 1,
message = "实发件数未确定"
};
}
return new TestErpResult {
code = 0,
message = "success"
};
}
///
/// (内部方法请勿调用)模拟任务创建完成反馈Erp回报结果(默认为success)
///
///
///
[HttpPost]
[Route("CreateTaskReturn")]
public TestErpResult CreateTaskReturn(CreateTaskReturnErpInfo model) {
if (model.hw == "") {
return new TestErpResult {
code = 1,
message = "货位信息未提供"
};
}
return new TestErpResult {
code = 0,
message = "success"
};
}
}
public class GoodpackToProdLineInfo {
public string CntrCode { get; set; }
public string StartLoc { get; set; }
public string EndLoc { get; set; }
}
public class TestErpSendOutboundPlanInfo {
///
/// 出库计划单号(计划单号 jhdh)
///
public string PlanNo { get; set; } = string.Empty;
///
/// 物料编码(产品代码 cpdm)
///
public string ItemCode { get; set; } = string.Empty;
///
/// 物料数量(派装件数 pzjs)
///
public float ItemNum { get; set; } = 0;
}
public class TestErpResult {
public int code { get; set; }
public string message { get; set; }
}
public class AddInboundTaskInfo {
///
/// 物料编码(暂时没用)
///
public string ItemCode { get; set; }
///
/// 批次号(暂时没用)
///
public string BatchNo { get; set; }
///
/// 容器编码
///
public string CntrCode { get; set; }
///
/// 起始货位
///
public string StartLoc { get; set; }
}
///
/// 模拟 AGV 传递信号,用于更改任务状态
///
public class UpdateTaskState {
///
/// 任务ID
///
public string TaskID { set; get; }
///
/// AGV 小车号
///
public string ForkliftNo { set; get; }
}
public class AgvReportsInfo {
///
/// 任务号
///
public string TaskId { set; get; }
///
/// AGV 小车号
///
public string ForkliftNo { get; set; }
///
/// AGV 下一状态(任务回报号)
///
public int NextState { set; get; } = 0;
}
///
///
///
public class ReturnResults {
public List ResultList { set; get; }
}
}