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.Worlds;
using DeviceFlags = WCS.WorkEngineering.Extensions.DeviceFlags;
using TaskStatus = WCS.Entity.TaskStatus;

namespace WCS.WorkEngineering.Systems
{
    /// <summary>
    ///  环形库码垛区补空托盘任务生成
    /// </summary>
    [BelongTo(typeof(RingWorld))]
    [Description("环形库码垛区补空托盘任务生成")]
    public class 环形库码垛区补空托盘任务生成 : DeviceSystem<Device<IStation520, IStation521, IStation523>>
    {
        protected override bool ParallelDo => true;

        protected override bool SaveLogsToFile => true;

        public override void Do(Device<IStation520, IStation521, IStation523> obj)
        {
            if (obj.Data3.Status.HasFlag(StationStatus.PH_Status)) return;
            WCS_TaskInfo deliveryTask = null;
            SqlSugarHelper.Do(_db =>
            {
                var db = _db.Default;
                //检查是否有待执行状态放货点是自己的出库任务
                var ringTask = db.Queryable<WCS_TaskInfo>().First(x => x.Type == TaskType.OutDepot && x.Status == TaskStatus.WaitingToExecute && x.SrmStation == obj.Entity.Code);
                if (ringTask == null)
                {
                    //开始申请码垛任务
                    WmsApi.ApplyPalletizingStockOut(obj.Entity.Code, obj.Entity.Sources.Single(x => x.HasFlag(DeviceFlags.Robot)).Code);
                    return;
                }
                var task = db.Queryable<WCS_TaskInfo>().First(x => x.Type == TaskType.Delivery && x.AddrTo == obj.Entity.Code);
                if (task != null) return;
                //开始获取当前地址上个设备的上一个设备中的非零九托盘拆机
                var addrFrom = obj.Entity.Sources.SelectMany(x => x.Sources).Single(x => x.HasFlag(DeviceFlags.拆盘机非09));
                var dev = Device.All.Where(x => x.Code == addrFrom.Code).Select(x => new Device<IStation520>(x, World)).FirstOrDefault();
                if (dev.Data.TaskNumber != 0 || dev.Data.GoodsEnd != 0) return;
                //开始创建搬运任务
                task = new WCS_TaskInfo()
                {
                    AddrFrom = addrFrom.Code,
                    AddrTo = obj.Entity.Code,
                    BarCode = "码垛托盘搬运-" + obj.Entity.Code,
                    Status = Entity.TaskStatus.NewBuild,
                    PalletType = ringTask.PalletType,
                    FullQty = ringTask.FullQty,
                    Type = TaskType.Delivery,
                    AddWho = "WCS",
                    AddTime = DateTime.Now
                };
                var res = db.Insertable(task).ExecuteReturnEntity();
                var taskOld = res.Mapper<WCS_TaskOld, WCS_TaskInfo>();
                taskOld.Id = res.ID;
                taskOld.AddTime = DateTime.Now; ;
                db.Insertable(taskOld).SplitTable().ExecuteCommand();
                res.AddWCS_TASK_DTL(db, obj.Entity.Code, task.AddrTo, "初始化码垛托盘搬运任务");
                deliveryTask = res;
            });

            if (deliveryTask == null) return;
            var dev = Device.All.Where(x => x.Code == deliveryTask.AddrFrom).Select(x => new Device<IStation520>(x, World)).FirstOrDefault();
            dev.Data.TaskNumber = deliveryTask.ID;
            dev.Data.GoodsEnd = obj.Entity.Code.ToShort();
        }

        public override bool Select(Device dev)
        {
            return dev.HasFlag(DeviceFlags.环形库码垛工位);
        }
    }
}
