using Khd.Core.Domain.Models;
using Khd.Core.EntityFramework;

namespace Khd.Core.Wcs
{
    public class SystemData
    {
        public readonly static int maxTray = 5;

        private static long _serialNo { get; set; } = 1;

        private static object serialLock = new object();

        public static long GetSerialNo(DefaultDbContext dbContext)
        {
            lock (serialLock)
            {
                List<long?> list = dbContext.WcsTask.Select(t => t.serialNo).ToList();
                while (list.Contains(_serialNo))
                {
                    _serialNo++;
                    if (_serialNo > 9999)
                    {
                        _serialNo = 1;
                    }
                }
                return _serialNo;
            }
        }
        public static object outStockLock = new object();

        /// <summary>
        /// 出入库锁定浅库位
        /// </summary>
        /// <param name="wmsBaseLocation"></param>
        /// <param name="dbContext"></param>
        public static void LockOutLocation(WmsBaseLocation wmsBaseLocation, DefaultDbContext dbContext)
        {
            if (wmsBaseLocation.locDeep == 1)
            {
                WmsBaseLocation? wmsLocation = dbContext.WmsBaseLocation.Where(t => t.locDeep == 2 && t.locRow == wmsBaseLocation.locRow && t.locColumn == (wmsBaseLocation.locColumn % 2 == 0 ? wmsBaseLocation.locColumn - 1 : wmsBaseLocation.locColumn + 1)).FirstOrDefault();
                if (wmsLocation != null && wmsLocation.locationStatus == "1")
                {
                    wmsLocation.locationStatus = "2";
                    dbContext.Update(wmsLocation);
                }
            }
        }

        /// <summary>
        /// 出入库解锁浅库位
        /// </summary>
        /// <param name="wmsBaseLocation"></param>
        /// <param name="dbContext"></param>
        public static void UnLockOutLocation(WmsBaseLocation wmsBaseLocation, DefaultDbContext dbContext)
        {
            if (wmsBaseLocation.locDeep == 1)
            {
                WmsBaseLocation? wmsLocation = dbContext.WmsBaseLocation.Where(t => t.locDeep == 2 && t.locRow == wmsBaseLocation.locRow && t.locColumn == (wmsBaseLocation.locColumn % 2 == 0 ? wmsBaseLocation.locColumn - 1 : wmsBaseLocation.locColumn + 1)).FirstOrDefault();
                if (wmsLocation != null && wmsLocation.locationStatus != "3" && wmsLocation.locationStatus != "9")
                {
                    wmsLocation.locationStatus = "1";
                    dbContext.Update(wmsLocation);
                }
            }
        }

        /// <summary>
        /// 报警库位
        /// </summary>
        public static void AlarmLocation(WmsBaseLocation wmsBaseLocation, DefaultDbContext dbContext)
        {
            if (wmsBaseLocation.locDeep == 2)
            {
                WmsBaseLocation? wmsLocation = dbContext.WmsBaseLocation.Where(t => t.locDeep == 1 && t.locRow == wmsBaseLocation.locRow && t.locColumn == (wmsBaseLocation.locColumn % 2 == 0 ? wmsBaseLocation.locColumn - 1 : wmsBaseLocation.locColumn + 1)).FirstOrDefault();
                if (wmsLocation != null && wmsLocation.locationStatus != "3")
                {
                    wmsLocation.locationStatus = "8";
                    wmsLocation.ExceptionDesc = "外侧库位异常导致内测库位异常";
                    dbContext.Update(wmsLocation);
                }
            }
        }

        /// <summary>
        /// 解除报警库位
        /// </summary>
        public static void UnAlarmLocation(WmsBaseLocation wmsBaseLocation, DefaultDbContext dbContext)
        {
            if (wmsBaseLocation.locDeep == 2)
            {
                WmsBaseLocation? wmsLocation = dbContext.WmsBaseLocation.Where(t => t.locDeep == 1 && t.locRow == wmsBaseLocation.locRow && t.locColumn == (wmsBaseLocation.locColumn % 2 == 0 ? wmsBaseLocation.locColumn - 1 : wmsBaseLocation.locColumn + 1)).FirstOrDefault();
                if (wmsLocation != null && wmsLocation.locationStatus == "8")
                {
                    wmsLocation.locationStatus = "1";
                    dbContext.Update(wmsLocation);
                }
            }
        }

        public readonly static object SecondTaskLock = new();
        public readonly static object ThirdTaskLock = new();
        public readonly static object FiveTaskLock = new();
    }
}
