using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;
using SqlSugar;
using WCS.BLL;
using WCS.BLL.DbModels;
using WCS.BLL.HardWare;
using WCS.BLL.Manager;
using WCS.BLL.Services.IService;
using WCS.BLL.Services.Service;
using WCS.DAL.Db;
using WCS.DAL.DbModels;
using WCS.Model;
using WCS.Model.ApiModel.AGV;
using WCS.Model.ApiModel.Home;
using Mode = WCS.BLL.HardWare.Mode;

namespace WCS.WebApi.Controllers
{
    /// <summary>
    /// 主页面的接口
    /// </summary>
    [ApiController]
    [Route("[controller]")]
    public class AgvCallbackServiceController : ControllerBase
    {

        public AgvCallbackServiceController(IHomerService homerService, ISelfCheckService selfCheckService)
        {

        }

        /// <summary>
        /// agv任务回调
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("agvCallback")]
        [HttpPost(Name = "agvCallback")]
        public async Task<object> agvCallback(AGVCallBackRequest request)
        {
            try
            {
                Logs.Write("收到AGV回调" + JsonConvert.SerializeObject(request));
                //找到任务数据
                var task = await DbHelp.db.Queryable<AgvTask>().Where(t => t.TaskCode == request.taskCode)
                                                               .FirstAsync();
                if (task == null)
                {
                    Logs.Write("不是我们系统发出的任务！！!!");
                    return new
                    {
                        Code = 0,
                        Message = "成功",
                        reqCode = "123",
                    };
                }
                //if (task.TaskStatus == TaskStatusEnum.已结束)
                //{
                //    return new
                //    {
                //        Code = 0,
                //        Message = "成功",
                //        reqCode = "123",
                //    };
                //}
                //RCS先调用结束再调用出库时会遇到先更新为对应位置 然后又将对应位置更新为0的情况 所以加运输中状态限制

                //判断并更新数据
                var shelf = await DbHelp.db.Queryable<ShelfInfo>().Where(t => t.ShelfCode == request.podCode || t.ShelfCode == task.ShelfCode)
                                                                  .FirstAsync();
                //调用的接口不是当前货架正在执行的任务
                if (task.TaskCode != shelf.CurrentTaskCode)
                {
                    Logs.Write($"AGV回调的任务[{task.TaskCode}]，货架当前任务为【{shelf.CurrentTaskCode}】,不是同一个任务 不进行货架状态更新！");
                    return new
                    {
                        Code = 0,
                        Message = "成功",
                        reqCode = "123",
                    };
                }
                Logs.Write($"AGV回调的任务[{task.TaskCode}]，{request.method}！");
                if (shelf != null && request.method == "outbin" && shelf.TransStatus == TransStatusEnum.运输中)
                {
                    
                    shelf.CurrentLocationId = 0;
                    shelf.CurrentLocaiotnCode = string.Empty;
                    DbHelp.db.Updateable(shelf).ExecuteCommand();
                }

                if (shelf != null && request.method == "cancel" && shelf.TransStatus == TransStatusEnum.运输中)
                {

                    //shelf.CurrentLocationId = 0;
                    //shelf.CurrentLocaiotnCode = string.Empty;

                    shelf.DestinationLocationId = 0;
                    shelf.DestinationLocaiotnCode = string.Empty;
                    shelf.TransStatus = TransStatusEnum.静止;
                    shelf.CurrentTaskCode = string.Empty;

                    DbHelp.db.Updateable(shelf).ExecuteCommand();
                }

                if (shelf != null && request.method == "end" && shelf.TransStatus == TransStatusEnum.运输中)
                {
                    task.TaskStatus = TaskStatusEnum.已结束;
                    task.AgvStatus = "-";
                    DbHelp.db.Updateable(task).ExecuteCommand();

                    shelf.CurrentLocationId = shelf.DestinationLocationId;
                    shelf.CurrentLocaiotnCode = shelf.DestinationLocaiotnCode;
                    shelf.DestinationLocationId = 0;
                    shelf.DestinationLocaiotnCode = string.Empty;
                    shelf.TransStatus = TransStatusEnum.静止;
                    shelf.CurrentTaskCode = string.Empty;
                    DbHelp.db.Updateable(shelf).ExecuteCommand();
                }

                return new
                {
                    Code = 0,
                    Message = "成功",
                    reqCode = "123",
                };
            }
            catch (Exception ex)
            {
                return new ResponseBase()
                {
                    Code = 300,
                    Message = "获取失败：" + ex.Message,
                };
            }
        }
    }
}
