using ServiceCenter.Logs;
using ServiceCenter.SqlSugars;
using System.ComponentModel;
using WCS.Core;
using WCS.Entity;
using WCS.Entity.Protocol.BCR;
using WCS.Entity.Protocol.Station;
using WCS.WorkEngineering.Extensions;
using WCS.WorkEngineering.WebApi.Controllers;
using WCS.WorkEngineering.WebApi.Models.WMS.Request;
using WCS.WorkEngineering.Worlds;
using DeviceFlags = WCS.WorkEngineering.Extensions.DeviceFlags;

namespace WCS.WorkEngineering.Systems
{
    /// <summary>
    ///  一楼入库工位处理系统
    /// </summary>
    [BelongTo(typeof(MainWorld))]
    [Description("一楼扫码入库")]
    public class 一楼扫码入库 : DeviceSystem<Device<IStation520, IStation521, IStation523, IStation91, IBCR81>>
    {
        protected override bool ParallelDo => true;

        protected override bool SaveLogsToFile => true;

        public override void Do(Device<IStation520, IStation521, IStation523, IStation91, IBCR81> obj)
        {
            obj.入库站点是否被禁止();
            obj.入库站点是否满足执行条件();

            WCS_TaskInfo task = null;//处理完成的任务
            try
            {
                SqlSugarHelper.Do(_db =>
                {
                    var db = _db.Default;
                    //获取托盘条码
                    var barcode = obj.Data5.GetBCRCode().Substring(0, 3);
                    //var barcode = "TAP00001";
                    //验证是否有对应的任务
                    if (!db.Queryable<WCS_TaskInfo>().Any(v => v.BarCode == barcode && v.Type == TaskType.EnterDepot))
                    {
                        var palletType = PalletType.Pallet09;
                        if (barcode == "TPB") palletType = PalletType.PalletNo09;

                        var res = WmsApi.OneFloorWorkerBuildEmptyPalletsStock(new OneFloorWorkerBuildEmptyPalletsStockRequest()
                        {
                            PalletCode = barcode,
                            StartLoc = obj.Entity.Code,
                            PalletType = palletType,
                            Height = 2
                        });
                        if (res.ResCode == WebApi.Models.WMS.Response.ResponseStatusCodeEnum.Sucess) return;
                    }
                    //找到对应的任务
                    var taskInfo = db.Queryable<WCS_TaskInfo>().First(v => v.BarCode == barcode && v.Type == TaskType.EnterDepot);
                    if (taskInfo.Status == Entity.TaskStatus.WaitingToExecute)
                    {
                        var tunnel = obj.Entity.Targets.FirstOrDefault(v => v.HasFlag(DeviceFlags.巷道));
                        //开始对任务进行处理
                        taskInfo.Status = Entity.TaskStatus.ConveyorExecution;
                        taskInfo.StartTime = DateTime.Now;
                        taskInfo.SrmStation = taskInfo.AddrFrom;
                        taskInfo.LastInteractionPoint = obj.Entity.Code;
                        taskInfo.Tunnel = tunnel.Code;
                        taskInfo.Height = 2;
                        taskInfo.Device = obj.Entity.Code switch
                        {
                            "2532" => "SRM1",
                            "2732" => "SRM2",
                            _ => taskInfo.Device
                        };
                        ;
                        taskInfo.AddWCS_TASK_DTL(db, obj.Entity.Code, "开始执行入库任务");
                        db.Updateable(taskInfo).ExecuteCommand();
                        task = taskInfo;
                    }
                    else return;
                });
            }
            catch (Exception ex)
            {
                throw new KnownException(ex.Message, LogLevelEnum.High);
            }
            if (task == null) return;

            World.Log($"任务处理:开始-DB520[{obj.Data.TaskNumber}]-[{obj.Data.VoucherNo}]-DB521[{obj.Data2.TaskNumber}]-[{obj.Data2.VoucherNo}]-[{obj.Data2.Request}]");
            obj.Data.TaskNumber = task.ID;
            obj.Data.VoucherNo++;
            World.Log($"任务处理:结束-DB520[{obj.Data.TaskNumber}]-[{obj.Data.VoucherNo}]-DB521[{obj.Data2.TaskNumber}]-[{obj.Data2.VoucherNo}]");
        }

        public override bool Select(Device dev)
        {
            //return dev.Code == "2532";
            return dev.HasFlag(Extensions.DeviceFlags.一楼扫码);
        }
    }
}
