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>
    ///  环形库分配货位
    /// </summary>
    [BelongTo(typeof(SortingBranchWorld))]
    [Description("环形库分配货位")]
    public class 环形库分配货位 : DeviceSystem<Device<IStation520, IStation521, IStation523>>
    {
        protected override bool ParallelDo => true;

        public override void Do(Device<IStation520, IStation521, IStation523> obj)
        {
            if (obj.Data.VoucherNo != obj.Data2.VoucherNo)
            {
                World.Log($"凭证号不一致,DB520:{obj.Data.VoucherNo}-DB521:{obj.Data2.VoucherNo}");
                return;
            }

            if (obj.Data3.Status.HasFlag(StationStatus.Run))
            {
                World.Log("设备运行中");
                return;
            }
            ;
            if (!obj.Data3.Status.HasFlag(StationStatus.OT_Status))
            {
                World.Log("站台货物信息与实际占用不一致");
                return;
            }

            if (!obj.Data3.Status.HasFlag(StationStatus.PH_Status))
            {
            }
            ;
            if (obj.Data2.Request != 1)
            {
                World.Log("无请求");
                return;
            }

            WCS_TaskInfo taskInfo = null;
            int sideNum = 0;
            //开始获取货位号
            SqlSugarHelper.Do(db =>
            {
                var task = db.Default.Queryable<WCS_TaskInfo>().First(v => obj.Data2.TaskNumber == v.ID);
                if (task == null)
                {
                    World.Log($"未找到任务[{obj.Data2.TaskNumber}]，请人工接入处理！！！！");
                    return;
                }

                if (task.Type != TaskType.SetPlate)
                {
                    World.Log($"任务[{obj.Data2.TaskNumber}]任务类型错误，应该是组盘任务，请人工接入处理！！！！");
                    return;
                }
                if (task.Status != TaskStatus.FinishOfShunt)
                {
                    World.Log($"任务[{obj.Data2.TaskNumber}]任务状态错误，当前状态应该市分流结束，，请人工接入处理！！！！");
                    return;
                }

                var res = WmsApi.RingApplyStockInLoc(task.ID, task.Device, obj.Entity.Code, task.GoodsType);
                if (res.ResCode != ResponseStatusCodeEnum.Sucess)
                {
                    World.Log(res.ResMsg,ServiceCenter.Logs.LogLevelEnum.Mid);
                    return;
                }
                task.AddrTo = res.ResData.CellNo;
                task.Line = res.ResData.Row;
                task.Col = res.ResData.Colomn;
                task.Layer = res.ResData.Layer;
                task.Depth = res.ResData.Row;
                task.LastInteractionPoint = obj.Entity.Code;
                task.Device = obj.Entity.Code;
                task.EditWho = "WCS";
                db.Default.UpdateableRowLock(task).UpdateColumns(x => new { x.Status, x.AddrTo, x.Line, x.Col, x.Layer, x.Depth, x.LastInteractionPoint, x.SrmStation, x.Device, x.EditWho }).ExecuteCommand();
                task.AddWCS_TASK_DTL(db.Default, obj.Entity.Code, task.AddrTo, $"任务分配货位{task.AddrTo}");
                var invNow = db.Default.Queryable<BillInvnow>().NoLock().First(x => x.ContGrpBarCode == task.BarCode);
                sideNum = invNow.SideNum;
                taskInfo = task;
            });
            if (taskInfo == null) return;
            var next = (taskInfo.AddrNext.ToShort() - 2).ToShort();
            obj.Data.TaskNumber = taskInfo.ID;
            obj.Data.TaskNumber = taskInfo.ID;
            obj.Data.GoodsStart = obj.Entity.Code.ToShort();
            obj.Data.GoodsStart = obj.Entity.Code.ToShort();
            obj.Data.GoodsEnd = next;
            if (sideNum == 2) //反面
            {
                obj.Data.CmdType = StationCmd.Res6;
                obj.Data.CmdType = StationCmd.Res6;
            }

            obj.Data.GoodsEnd = next;
            obj.Data.SetVoucherNo();
        }

        public override bool Select(Device dev)
        {
            var codes = new List<string>()
            {
                "444","644","844","1044","1244","1444",
                "426","626","826","1026","1226","1426",
                "435","635","835","1035","1235","1435"
            };
            return codes.Any(x => x == dev.Code);
        }
    }
}