using PlcSiemens.Core.Extension;
using ServiceCenter.Extensions;
using ServiceCenter.SqlSugars;
using System.ComponentModel;
using WCS.Core;
using WCS.Entity;
using WCS.Entity.Protocol.Station;
using WCS.WorkEngineering.Worlds;
using TaskStatus = WCS.Entity.TaskStatus;

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

        protected override bool SaveLogsToFile => true;

        public override void Do(Device<IStation520> obj)
        {
            WCS_Palletizing palletizing = null;
            SqlSugarHelper.Do(_db =>
            {
                var db = _db.Default;
                //取一个时间最靠前的,没有绑定码垛工位的码垛记录
                var palletizingInfo = db.Queryable<WCS_Palletizing>().First(x => x.PalletizingStation == null);
                if (palletizingInfo == null) return;
                var palleTask = db.Queryable<WCS_TaskInfo>().First(x => x.ID == palletizingInfo.TaskId && (x.Status == TaskStatus.FinishOfShunt || x.Status == TaskStatus.ConveyorExecution));
                if (palleTask == null) return;
                //获取可以使用的放货站台信息
                var taskAddNext = Device.All.FirstOrDefault(x => x.Code == palleTask.AddrNext).Targets.FirstOrDefault().Targets.FirstOrDefault().Targets;
                if (taskAddNext == null) return;
                var devs = Device.All.Where(x => taskAddNext.Contains(x)).Select(x => new Device<IStation520, IStation521, IStation523>(x, World)).ToList();

                //获取所有的未结束且有码垛工位的码垛记录
                var palletizingStation = db.Queryable<WCS_Palletizing>().Where(x => !x.Finish && x.PalletizingStation != null).Select(x => x.PalletizingStation).ToList();

                //取一个可用用于码垛的地方
                var dev = devs.Where(x => !palletizingStation.Contains(x.Entity.Code)).FirstOrDefault(x => !x.Data3.Status.HasFlag(StationStatus.PH_Status));
                if (dev == null) return;
                var endDev = Device.All.Where(x => x.Code == "1606").Select(x => new Device<IStation520>(x, World)).FirstOrDefault();
                if (endDev.Data.TaskNumber != 0 || endDev.Data.GoodsEnd != 0) return;

                palletizingInfo.PalletizingStation = dev.Entity.Code;
                db.Updateable(palletizingInfo).ExecuteCommand();
                palletizing = palletizingInfo;
            });
            if (palletizing == null) return;
            //TODO:取货地址暂时写死，后续依靠任务进行计算
            var dev = Device.All.Where(x => x.Code == "1606").Select(x => new Device<IStation520>(x, World)).FirstOrDefault();
            dev.Data.TaskNumber = palletizing.Id;
            dev.Data.GoodsEnd = palletizing.PalletizingStation!.ToShort();
        }

        public override bool Select(Device dev)
        {
            return dev.Code == nameof(桁架码垛区补空托盘任务生成);
        }
    }
}
