using ServiceCenter.Extensions;
using ServiceCenter.SqlSugars;
using System.ComponentModel;
using WCS.Core;
using WCS.Entity;
using WCS.Entity.Protocol.Station;
using WCS.Entity.Protocol.Truss;
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(SortingPalletizingWorld))]
    [Description("码垛工位任务结束处理09")]
    public class 桁架码垛工位任务结束处理 : DeviceSystem<Device<IStation520, IStation521, IStation523, ITruss530, ITruss531>>
    {
        protected override bool ParallelDo => true;

        protected override bool SaveLogsToFile => true;

        public override void Do(Device<IStation520, IStation521, IStation523, ITruss530, ITruss531> obj)
        {
            if (!obj.Data5.CmdType.HasFlag(TrussCmdType.End)) obj.Data4.CmdType = 0;
            if (!obj.Data3.Status.HasFlag(StationStatus.PH_Status)) return;
            if (!obj.Data5.CmdType.HasFlag(TrussCmdType.End)) return;
            if (!obj.Data5.CmdType.HasFlag(TrussCmdType.End1))
            {
                if (obj.Data5.MaxQuantity != obj.Data5.Quantity || obj.Data5.Quantity == 0 || obj.Data5.MaxQuantity == 0) return;
            }
            WCS_TaskInfo task = null;
            //找到对应的码垛数据
            SqlSugarHelper.Do(_db =>
            {
                var db = _db.Default;
                var palletizing = db.Queryable<WCS_Palletizing>().Single(x => x.PalletizingStation == obj.Entity.Code && !x.Finish);

                var TaskNoList = obj.Data5.GetTaskNoList().Where(x => x > 0).ToList();
                var taskList = db.Queryable<WCS_TaskInfo>().Where(x => TaskNoList.Contains(x.ID)).ToList();
                var taskCodes = taskList.Select(x => x.BarCode).ToList();
                if (!obj.Data5.CmdType.HasFlag(TrussCmdType.End1))
                {
                    if (taskList.Count != TaskNoList.Count) return;
                }
                var taskInfo = db.Queryable<WCS_TaskInfo>().Single(x => x.BarCode == palletizing.PalleCode && x.Type == TaskType.EnterDepot && x.Status == TaskStatus.WaitingToExecute);
                if (taskInfo == null)
                {
                    if (obj.Data5.CmdType.HasFlag(TrussCmdType.End1))
                    {
                        //开始绑盘
                        WmsApi.BingPallet(palletizing.PalleCode, taskCodes, taskList.FirstOrDefault().WarehouseCode, obj.Entity.Code, true, "强制码垛结束");
                    }
                    else
                    {
                        //开始绑盘
                        WmsApi.BingPallet(palletizing.PalleCode, taskCodes, taskList.FirstOrDefault().WarehouseCode, obj.Entity.Code);
                    }
                    return;
                }
                if (taskInfo.Status != TaskStatus.WaitingToExecute) return;

                foreach (var item in taskList)
                {
                    item.Status = TaskStatus.Finish;
                    item.EditTime = DateTime.Now;
                    db.Updateable(item).ExecuteCommand();
                    item.AddWCS_TASK_DTL(db, obj.Entity.Code, "任务结束");
                }

                palletizing.Finish = true;
                db.Updateable(palletizing).ExecuteCommand();
                task = taskInfo;
            });
            if (task == null) return;
            obj.Data.TaskNumber = task.ID;
            obj.Data.GoodsStart = obj.Entity.Code.ToShort();
            obj.Data.GoodsEnd = task.AddrNext.ToShort();
            obj.Data4.CmdType = TrussCmdType.End;
        }

        public override bool Select(Device dev)
        {
            return dev.HasFlag(DeviceFlags.桁架码垛位);
        }
    }
}
