using Microsoft.AspNetCore.Mvc;
using ServiceCenter.Redis;
using ServiceCenter.SqlSugars;
using WCS.Entity;
using WCS.WorkEngineering.WebApi.Models.AGV;
using WCS.WorkEngineering.WebApi.Models.AGV.Request;
using WCS.WorkEngineering.WebApi.Models.AGV.Response;

namespace WCS.WorkEngineering.WebApi.Controllers
{
    /// <summary>
    /// AGV相关接口控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class AgvController : ControllerBase
    {
        /// <summary>
        ///  AGV任务下发测试
        /// </summary>
        /// <param name="type">任务类型</param>
        /// <param name="code">RFID</param>
        /// <param name="pos">目标位置</param>
        /// <returns></returns>
        [HttpPost]
        public string AgvDebug(int type, string code, string pos)
        {
            try
            {
                switch (type)
                {
                    case 1:
                        //AgvApi.机台补空(pos, code, "1");
                        break;

                    case 2:
                        //AgvApi.机台补满();
                        break;

                    case 3:
                        //AgvApi.满轮入库(code, pos, Guid.NewGuid().ToString().Replace("-", ""), "1");
                        break;
                }
                return "成功";
            }
            catch (Exception ex)
            {
                return $"Error-----" +
                      $"{ex.Message}------" +
                      $"{ex.StackTrace}";
            }
        }

        /// <summary>
        /// AGV执行回调
        /// </summary>
        /// <param name="reqDto"></param>
        /// <returns></returns>
        [HttpPost]
        public AgvCallbackResponse AgvCallback([FromBody] AgvCallbackRequest reqDto)
        {
            var key = $"WCS:Lock:AGV:{nameof(AgvCallback)}";
            var res = new AgvCallbackResponse() { code = AgvResponseCode.Success, message = "失败" };

            try
            {
                RedisHub.Do(key, redis =>
                {
                    SqlSugarHelper.Do(db =>
                    {
                        //跟据AGVid找到对应的AGV任务
                        var agvTask = db.Default.Queryable<WCS_AgvTaskInfo>().SplitTable(tabs => tabs.Take(2))
                            .First(v => v.AgvID == reqDto.taskCode && v.Status < AGVTaskStatus.MissionCompleted);
                        if (agvTask == null)
                        {
                            res.code = AgvResponseCode.Fail;
                            res.message = "未找到对应的AGV任务";
                            return;
                        }
                        switch (reqDto.method)
                        {
                            case "applyContinue": //表示请求巷道
                                agvTask.AgvStatus = AGVTaskStatus.RequestOrPermission1;
                                break;

                            case "outbin": //小车退出取货位
                                agvTask.AgvStatus = AGVTaskStatus.LeaveGet;
                                break;

                            case "applySecurity": //表示请求放货或取货
                                agvTask.AgvStatus = AGVTaskStatus.RequestOrPermission2;
                                break;

                            case "applySecurityPut": //表示请求放货或取货
                                agvTask.AgvStatus = AGVTaskStatus.PutRequestOrPermission;
                                break;

                            case "FJend": //任务完成
                                agvTask.AgvStatus = AGVTaskStatus.MissionCompleted;
                                break;

                            case "cancel": //取消任务
                                           //agvTask.AgvStatus = AGVTaskStatus.Cancel;
                                break;
                        }

                        db.Default.Updateable(agvTask).SplitTable(x=>x.Take(2)).ExecuteCommand();
                        res.code = AgvResponseCode.Success;
                        res.message = "成功";
                    });
                });
            }
            catch (Exception ex)
            {
                res.code = AgvResponseCode.Error;
                res.message = ex.Message;
            }

            return res;
        }
    }
}
