using ServiceCenter.Extensions;
using ServiceCenter.Logs;
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;

namespace WCS.WorkEngineering.Systems.桁架码垛
{
    [BelongTo(typeof(NoInteractionWorld))]
    [Description("创建二次码垛出库任务")]
    public class TrussCreateTwoOut : DeviceSystem<Device<IStation520>>
    {
        protected override bool ParallelDo => true;

        

        public override void Do(Device<IStation520> obj)
        {
            SqlSugarHelper.Do(_db =>
            {
                var db = _db.Default;
                var palls = db.Queryable<WCS_Palletizing>().ReadPastUpdLock().Where(x => !x.Finish && x.isItHalf).ToList();
                foreach (var pall in palls)
                {
                    var task = db.Queryable<WCS_TaskInfo>().NoLock().First(x => x.ID == pall.TaskId);
                    if (task != null) continue;

                    try
                    {
                        //没有对应的出库任务
                        var res = WmsApi.GetTwoPallet(pall.WarehouseCode, pall.TaskId);
                        pall.TaskId = res.ResData;
                        db.UpdateableRowLock(pall).UpdateColumns(x => new { x.TaskId }).ExecuteCommand();
                    }
                    catch (Exception e)
                    {
                        World.Log(e.Message, LogLevelEnum.Mid);
                        continue;
                    }
                }
            });
        }

        public override bool Select(Device dev)
        {
            return dev.Code == nameof(TrussCreateTwoOut);
        }
    }
}
