using ServiceCenter.Extensions;
using ServiceCenter.SqlSugars;
using System.ComponentModel;
using WCS.Core;
using WCS.Entity;
using WCS.Entity.Protocol.Station;
using WCS.WorkEngineering.Extensions;
using WCS.WorkEngineering.WebApi.Controllers;
using WCS.WorkEngineering.WebApi.Models.WMS.Response;
using WCS.WorkEngineering.Worlds;
using TaskStatus = WCS.Entity.TaskStatus;

namespace WCS.WorkEngineering.Systems
{
    /// <summary>
    ///  UpLoadSystems
    /// </summary>
    [BelongTo(typeof(UpLoadWorld))]
    [Description("UpLoadSystems")]
    public class UpLoadSystems : DeviceSystem<Device<IStation520>>
    {
        protected override bool ParallelDo => true;

        public override void Do(Device<IStation520> obj)
        {
            //更新出库放货完成状态
            SqlSugarHelper.Do(_db =>
            {
                var taskIdList = new List<WCS_TaskInfo>();
                var nowTime = DateTime.Now;

                var db = _db.Default;
                var taskList = db.Queryable<WCS_TaskInfo>().UpdLock().Where(x => x.Uploaded != TaskStatus.ConveyorExecution && x.Status == TaskStatus.StackerCompleted && x.Type == TaskType.OutDepot).ToList();

                var isEnd = false;

                //一次就同步一条数据即可
                foreach (var task in taskList)
                {
                    if (isEnd) continue;

                    var res = WmsApi.SrmPickOutCompleted(task.ID);
                    if (res.ResCode == ResponseStatusCodeEnum.Sucess)
                    {
                        taskIdList.Add(task); //更新成功
                        isEnd = true;
                    }
                    World.Log($"出库放货完成状态同步失败:{task.ID}[{res.ResMsg}]");
                }

                //拆分防止锁表
                foreach (var task in taskIdList)
                {
                    task.Uploaded = TaskStatus.ConveyorExecution;
                    task.Status = TaskStatus.ConveyorExecution;
                    task.EditTime = nowTime;
                    db.UpdateableRowLock(task).UpdateColumns(x => new { x.Uploaded, x.EditTime, x.Status }).ExecuteCommand();
                    task.AddWCS_TASK_DTL(db, "", $"同步出库任务放货完成状态同步至WMS");
                }
            });

            //环形库组盘任务/立库入库任务/二楼立库出库//芯股叫料 更新完成
            SqlSugarHelper.Do(_db =>
            {
                var taskIdList = new List<WCS_TaskOld>();
                var nowTime = DateTime.Now;
                var db = _db.Default;
                var taskList = db.Queryable<WCS_TaskOld>().UpdLock()
                    .Where(x => ((x.Type == TaskType.SetPlate && x.WarehouseCode.Contains("R"))
                                || ((x.Type == TaskType.EnterDepot || x.Type == TaskType.OutDepot) && !x.WarehouseCode.Contains("R"))
                                || (x.Type == TaskType.OutDepot && x.AddrFrom.Contains("NSTJ") && x.AddrTo.Contains("NSTJ")))
                               && x.Status == TaskStatus.Finish && x.Uploaded != TaskStatus.Finish)
                    .SplitTable(x => x.Take(2)).ToList();

                var isEnd = false;
                foreach (var task in taskList)
                {
                    if (isEnd) continue;
                    var res = WmsApi.CompleteTask(task.Id);
                    if (res.ResCode == ResponseStatusCodeEnum.Sucess)
                    {
                        taskIdList.Add(task);
                        isEnd = true;
                    }
                    World.Log($"入库完成状态同步失败:{task.Id}[{res.ResMsg}]");
                }

                //拆分防止锁表
                foreach (var task in taskIdList)
                {
                    task.Uploaded = TaskStatus.Finish;
                    task.EditTime = nowTime;
                    db.UpdateableRowLock(task).SplitTable(x => x.Take(2)).ExecuteCommand();
                    task.AddWCS_TASK_DTL(db, "", $"同步入库完成状态同步至WMS");
                }
            });

            ////立库二楼出库更新完成
            //SqlSugarHelper.Do(_db =>
            //{
            //    var taskIdList = new List<int>();
            //    var nowTime = DateTime.Now;
            //    var db = _db.Default;
            //    var taskList = db.Queryable<WCS_TaskOld>().SplitTable(x => x.Take(2))
            //        .Where(x => ((x.Type == TaskType.SetPlate && x.WarehouseCode.Contains("R")) || (x.Type == TaskType.EnterDepot && !x.WarehouseCode.Contains("R")))
            //                    && x.Status == TaskStatus.Finish
            //                    && x.Uploaded != TaskStatus.Finish).ToList();

            //    foreach (var task in taskList)
            //    {
            //        var res = WmsApi.CompleteTask(task.Id);
            //        if (res.ResCode == ResponseStatusCodeEnum.Sucess) taskIdList.Add(task.Id);
            //        else World.Log($"入库完成状态同步失败:{task.Id}[{res.ResMsg}]");
            //    }

            //    //拆分防止锁表
            //    foreach (var taskId in taskIdList)
            //    {
            //        var task = db.Queryable<WCS_TaskOld>().SplitTable(x => x.Take(2)).First(x => x.Id == taskId);
            //        task.Uploaded = TaskStatus.Finish;
            //        task.EditTime = nowTime;
            //        db.Updateable(task).SplitTable(x => x.Take(2)).ExecuteCommand();
            //        task.AddWCS_TASK_DTL(db, "", $"同步入库完成状态同步至WMS");
            //    }
            //});
        }

        public override bool Select(Device dev)
        {
            return dev.Code == nameof(UpLoadSystems);
        }
    }
}