using ServiceCenter.Extensions;
using ServiceCenter.Logs;
using ServiceCenter.Redis;
using ServiceCenter.SqlSugars;
using SqlSugar;
using WCS.Core;
using WCS.Entity;
using TaskStatus = WCS.Entity.TaskStatus;

namespace WCS.WorkEngineering.Extensions
{
    /// <summary>
    /// 任务扩展
    /// </summary>
    public static class TaskExtension
    {
        /// <summary>
        /// 更新任务执行记录
        /// 同步更新历史任务
        /// </summary>
        /// <param name="task">任务信息</param>
        /// <param name="db">数据库上下文</param>
        /// <param name="curPoint">当前地址</param>
        /// <param name="desc">描述</param>
        public static void AddWCS_TASK_DTL(this WCS_TaskInfo task, SqlSugarScopeProvider db, string curPoint, string desc) => task.AddWCS_TASK_DTL(db, curPoint, "", desc);

        /// <summary>
        /// 更新任务执行记录
        /// 同步更新历史任务
        /// </summary>
        /// <param name="task">任务信息</param>
        /// <param name="db">数据库上下文</param>
        /// <param name="curPoint">当前地址</param>
        /// <param name="nextPoint">下一个地址</param>
        /// <param name="desc">描述</param>
        public static void AddWCS_TASK_DTL(this WCS_TaskInfo task, SqlSugarScopeProvider db, string curPoint, string nextPoint, string desc)
        {
            db.InsertableRowLock(new WCS_TaskDtl
            {
                ID = Guid.NewGuid(),
                ParentTaskCode = task.ID,
                CurPoint = curPoint,
                NextPoint = nextPoint,
                Desc = desc,
                AddWho = "WCS"
            }).SplitTable().ExecuteCommand();
            task.UpdateableOldTask(db);
        }

        /// <summary>
        /// 更新任务执行记录
        /// 同步更新历史任务
        /// </summary>
        /// <param name="task">任务信息</param>
        /// <param name="db">数据库上下文</param>
        /// <param name="curPoint">当前地址</param>
        /// <param name="desc">描述</param>
        public static void AddWCS_TASK_DTL(this WCS_TaskOld task, SqlSugarScopeProvider db, string curPoint, string desc) => task.AddWCS_TASK_DTL(db, curPoint, "", desc);

        /// <summary>
        /// 更新任务执行记录
        /// 同步更新历史任务
        /// </summary>
        /// <param name="task">任务信息</param>
        /// <param name="db">数据库上下文</param>
        /// <param name="curPoint">当前地址</param>
        /// <param name="nextPoint">下一个地址</param>
        /// <param name="desc">描述</param>
        public static void AddWCS_TASK_DTL(this WCS_TaskOld task, SqlSugarScopeProvider db, string curPoint, string nextPoint, string desc)
        {
            db.Insertable(new WCS_TaskDtl
            {
                ID = Guid.NewGuid(),
                ParentTaskCode = task.Id,
                CurPoint = curPoint,
                NextPoint = nextPoint,
                Desc = desc,
                AddWho = "WCS"
            }).SplitTable().ExecuteCommand();
        }

        /// <summary>
        ///  更新历史表数据
        /// </summary>
        /// <param name="taskInfo"></param>
        /// <param name="db"></param>
        public static void UpdateableOldTask(this WCS_TaskInfo taskInfo, SqlSugarScopeProvider db)
        {
            if (taskInfo.Status == TaskStatus.NewBuild) return;

            // 同步任务信息
            var taskOld = db.Queryable<WCS_TaskOld>().Where(v => v.Id == taskInfo.ID).SplitTable(tabs => tabs.Take(2)).ToList().OrderByDescending(v => v.AddTime).First();
            if (taskOld is not null)
            {
                if (taskInfo.Status >= TaskStatus.Finish) taskInfo.CompleteOrCancelTasks(db);
                else
                {
                    taskOld = taskInfo.Mapper<WCS_TaskOld, WCS_TaskInfo>();
                    taskOld.Id = taskInfo.ID;
                    db.UpdateableRowLock(taskOld).Where(x => x.Id == taskOld.Id).SplitTable(tabs => tabs.Take(2)).ExecuteCommand();
                }
            }
            else
            {
                throw new KnownException($"WCS_TaskOld表中不存在任务:{taskInfo.ID}，无法执行WCS_TaskInfo与WCS_TaskOld同步动作", LogLevelEnum.Mid);
            }
        }

        /// <summary>
        /// 完成或取消任务
        /// </summary>
        /// <param name="taskInfo"></param>
        /// <param name="db"></param>
        public static void CompleteOrCancelTasks(this WCS_TaskInfo taskInfo, SqlSugarScopeProvider db)
        {
            if (taskInfo.Status is not Entity.TaskStatus.Finish and not Entity.TaskStatus.Cancel) throw new KnownException("任务未完成或取消，无法执行WCS_TaskInfo与WCS_TaskOld同步动作", LogLevelEnum.Mid);
            // 任务完成或取消,进行相关同步动作
            var taskOld = db.Queryable<WCS_TaskOld>().Where(v => v.Id == taskInfo.ID).SplitTable(tabs => tabs.Take(2)).ToList().OrderByDescending(v => v.AddTime).First();
            if (taskOld is not null)
            {
                taskOld = taskInfo.Mapper<WCS_TaskOld, WCS_TaskInfo>();
                taskOld.Id = taskInfo.ID;
                //更新任务历史表,删除任务当前表
                db.UpdateableRowLock(taskOld).Where(x => x.Id == taskOld.Id).SplitTable(tabs => tabs.Take(2)).ExecuteCommand();
                db.DeleteableRowLock(taskInfo).ExecuteCommand();
            }
            else
            {
                throw new KnownException($"WCS_TaskOld表中不存在任务:{taskInfo.ID}，无法执行WCS_TaskInfo与WCS_TaskOld同步动作", LogLevelEnum.Mid);
            }
        }

        /// <summary>
        ///  更新表数据
        /// </summary>
        /// <param name="taskInfo"></param>
        /// <param name="db"></param>
        public static void Updateable(this WCS_TaskInfo taskInfo, SqlSugarScopeProvider db)
        {
        }

        ///// <summary>
        /////  获取出库任务
        ///// </summary>
        ///// <param name="taskInfo">任务</param>
        ///// <param name="db">db</param>
        ///// <param name="allOutCode">可用出库站台</param>
        ///// <param name="floor">楼层</param>
        ///// <param name="obj">堆垛机</param>
        ///// <param name="index">递归次数</param>
        ///// <returns></returns>
        ///// <exception cref="KnownException"></exception>
        //public static WCS_TaskInfo GetOutTask(this WCS_TaskInfo taskInfo, SqlSugarHelper db, List<string> allOutCode, int floor, SRM obj, int index = 1)
        //{
        //    return task;
        //}

        /// <summary>
        /// 获取AGV任务ID
        /// </summary>
        /// <param name="db">db</param>
        /// <returns></returns>
        public static int GetAgvTaskId(this SqlSugarHelper db)
        {
            //最多任务号不再连续
            var id = db.Default.Queryable<WCS_AgvTaskInfo>().NoLock().SplitTable(v => v.Take(2)).Max(v => v.ID);
            return id + 1;
        }

        /// <summary>
        /// 更新任务执行记录
        /// </summary>
        /// <param name="task">任务信息</param>
        public static void UpdateRedisHash(this WCS_TaskInfo task)
        {
            var key = $"Hash:{task.ID}";
            if (task.Status >= TaskStatus.Finish)
            {
                RedisHub.WMS.Del(key);
            }
            else
            {
                RedisHub.WMS.HMSet(key, task.ToDic());
            }
        }

        #region 工字轮支线分流

        /// <summary>
        ///  初始化码垛信息
        /// </summary>
        /// <param name="task"></param>
        public static void InitStackStructure(this WCS_TaskInfo task, SqlSugarScopeProvider db, World world)
        {
            var billBomsetgrp = db.Queryable<BillBomsetgrp>().Single(x => x.IsStop == 0 && x.BomCode.Contains(task.MatCode));
            if (billBomsetgrp == null)
            {
                world.Log($"物料规格[{task.MatCode}]无可用码垛垛形");
                return;
            };
            var billBomsetinfos = db.Queryable<BillBomsetinfo>().Where(x => x.BomSetHdrId == billBomsetgrp.Id).ToList();

            //开始构造垛形信息
            var palletizing = new WCS_Palletizing()
            {
                Code = billBomsetgrp.Code,
                ShortCode = billBomsetgrp.ShortCode,
                ProMaterCode = billBomsetgrp.ProMaterCode,
                TpTypeCode = billBomsetgrp.TpTypeCode,
                LayerCountQty = 2,
                StampType = billBomsetgrp.StampType,
                Finish = false,
                AddTime = DateTime.Now,
                TaskId = task.ID,
                WarehouseCode = task.WarehouseCode,
                DeviceCode = task.Device
            };
            palletizing = db.InsertableRowLock(palletizing).ExecuteReturnEntity();
            foreach (var item in billBomsetinfos.Where(x => x.IsEmpty == 0).GroupBy(x => x.Row).OrderBy(x => x.Key))
            {
                var layerNo = item.Key <= 6 ? 1 : 2;
                //获取层信息
                var palletizingLayer = db.Queryable<WCS_PalletizingLayer>().Single(x => x.PalletizingId == palletizing.Id && x.LayerNo == layerNo);

                if (palletizingLayer == null)
                {
                    palletizingLayer = new WCS_PalletizingLayer()
                    {
                        LayerNo = layerNo,
                        PalletizingId = palletizing.Id,
                        WarehouseCode = palletizing.WarehouseCode,
                    };
                    palletizingLayer = db.InsertableRowLock(palletizingLayer).ExecuteReturnEntity();
                }

                //获取行信息
                var palletizingRow = db.Queryable<WCS_PalletizingRow>().Single(x => x.PalletizingLayerId == palletizingLayer.Id && x.RowNo == item.Key);
                if (palletizingRow == null)
                {
                    palletizingRow = new WCS_PalletizingRow()
                    {
                        RowNo = item.Key,
                        PalletizingLayerId = palletizingLayer.Id,
                        PalletizingId = palletizing.Id,
                        WarehouseCode = palletizingLayer.WarehouseCode
                    };
                    palletizingRow = db.InsertableRowLock(palletizingRow).ExecuteReturnEntity();
                }

                //重新查询最新的数据
                var layer = palletizingLayer;
                palletizingLayer = db.Queryable<WCS_PalletizingLayer>().Single(x => x.Id == layer.Id);
                var row = palletizingRow;
                palletizingRow = db.Queryable<WCS_PalletizingRow>().Single(x => x.Id == row.Id);

                //构造位信息
                foreach (var loc in item)
                {
                    var palletizingLoc = db.Queryable<WCS_PalletizingLoc>().Single(x => x.PalletizingRowId == palletizingRow.Id && x.XYNo == loc.XYNo);

                    if (palletizingLoc == null)
                    {
                        palletizingLoc = new WCS_PalletizingLoc()
                        {
                            IsEmpty = loc.IsEmpty != 0,
                            XYNo = loc.XYNo,
                            MatCode = loc.MatCode,
                            SideNum = loc.SideNum,
                            SpoolType = loc.SpoolType,
                            TaskId = task.ID,
                            PalletizingRowId = palletizingRow.Id,
                            Finish = false,
                            WarehouseCode = palletizingRow.WarehouseCode
                        };
                        db.InsertableRowLock(palletizingLoc).ExecuteReturnEntity();
                    }

                    //同步是否混合料行
                    palletizingRow.IsMixRow = loc.IsMixRow != 0;
                    db.UpdateableRowLock(palletizingRow).ExecuteCommand();
                }
                //更新行信息
                palletizingRow = db.Queryable<WCS_PalletizingRow>().Includes(x => x.Locs).Single(x => x.Id == row.Id);
                palletizingRow.QtyMaxCount = palletizingRow.Locs.Count(x => !x.IsEmpty);
                palletizingRow.IsEmpty = palletizingRow.QtyMaxCount <= 0;
                palletizingRow.MatCodeList = palletizingRow.Locs.Select(x => x.MatCode).ToList().GetMatList();
                db.UpdateableRowLock(palletizingRow).ExecuteCommand();
                //更新层信息
                palletizingLayer = db.Queryable<WCS_PalletizingLayer>().Includes(x => x.Rows, l => l.Locs).Single(x => x.Id == layer.Id);
                var count = palletizingLayer.Rows.Count(x => !x.IsEmpty); //计算所有不空数量
                palletizingLayer.IsEmpty = count <= 0;
                palletizingLayer.RowCountQty = palletizingLayer.Rows.Count;
                palletizingLayer.Finish = false;
                palletizingLayer.MatCodeList = palletizingLayer.Rows.SelectMany(x => x.Locs).Select(x => x.MatCode).ToList().GetMatList();

                db.UpdateableRowLock(palletizingLayer).ExecuteCommand();
            }

            var palletizing1 = palletizing;
            palletizing = db.Queryable<WCS_Palletizing>().Includes(x => x.Layers, r => r.Rows, l => l.Locs).Single(x => x.Id == palletizing1.Id);
            //计算垛形信息
            var goods = palletizing.Layers.Select(x => x.Rows).SelectMany(x => x).Select(x => x.Locs).SelectMany(x => x).ToList();
            palletizing.CountQty = goods.Count(x => !x.IsEmpty);
            palletizing.MatCodeList = palletizing.Layers.SelectMany(x => x.Rows).SelectMany(x => x.Locs).Select(x => x.MatCode).ToList().GetMatList();
            db.UpdateableRowLock(palletizing).ExecuteCommand();
        }

        public static string GetMatList(this List<string> matList)
        {
            return matList.Distinct().Aggregate("", (current, mat) => current + $"[{mat}]");
        }

        /// <summary>
        ///  去除转义字符
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string RemoveEscapeCharacters(this string? value)
        {
            return value.Trim('\0', '\a', '\b', '\f', '\n', '\r', '\t', '\v').Trim();
        }

        #endregion 工字轮支线分流
    }

    /// <summary>
    ///  垛形位信息
    /// </summary>
    public class StackPosInfo
    {
        /// <summary>
        ///  任务号
        /// </summary>
        public int TaskNumber { get; set; }

        /// <summary>
        /// 是否空置
        /// </summary>
        public bool IsEmpty { get; set; }

        /// <summary>
        /// 坐标号
        /// </summary>
        public string XYNo { get; set; }

        /// <summary>
        /// 物料编码
        /// </summary>
        public string MatCode { get; set; }

        /// <summary>
        /// 正反面
        /// </summary>
        public int SideNum { get; set; }

        /// <summary>
        /// 工字轮类型
        /// </summary>
        public string SpoolType { get; set; }

        /// <summary>
        ///  是否结束
        /// </summary>
        public bool Finish { get; set; }
    }

    /// <summary>
    /// 垛型明细表
    /// </summary>
    [SugarTable("Bill_BomSetInfo", tableDescription: "垛型明细表")]
    public partial class BillBomsetinfo
    {
        /// <summary>
        ///  ID
        /// </summary>
        [SugarColumn(ColumnName = "Id", IsPrimaryKey = true, ColumnDescription = "ID")]
        public virtual long Id { get; set; }

        /// <summary>
        ///  备注
        /// </summary>
        [SugarColumn(ColumnName = "Memo", Length = 500, IsNullable = true, ColumnDataType = "nvarchar", DefaultValue = "", ColumnDescription = "备注")]
        public virtual string Memo { get; set; }

        /// <summary>
        ///  创建用户
        /// </summary>
        [SugarColumn(ColumnName = "AddWho", Length = 50, ColumnDataType = "nvarchar", DefaultValue = "", IsNullable = false, ColumnDescription = "创建用户")]
        public virtual string AddWho { get; set; } = "";

        /// <summary>
        ///  更新用户
        /// </summary>
        [SugarColumn(ColumnName = "EditWho", Length = 50, ColumnDataType = "nvarchar", DefaultValue = "", IsNullable = false, ColumnDescription = "更新用户")]
        public virtual string EditWho { get; set; } = "";

        /// <summary>
        ///  创建时间
        /// </summary>
        [SugarColumn(ColumnName = "AddTime", DefaultValue = "1900-1-1", IsNullable = false, ColumnDescription = "创建时间")]
        public virtual DateTime AddTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 更新时间
        /// </summary>
        [SugarColumn(ColumnName = "EditTime", DefaultValue = "1900-1-1", IsNullable = false, ColumnDescription = "更新时间")]
        public virtual DateTime EditTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 垛型id
        /// </summary>
        [SugarColumn(ColumnDataType = "bigint", IsNullable = false, ColumnDescription = "垛型id")]
        public long BomSetHdrId { get; set; }

        /// <summary>
        /// 是否停用
        /// </summary>
        [SugarColumn(ColumnDataType = "int", IsNullable = false, ColumnDescription = "是否停用")]
        public int IsStop { get; set; }

        /// <summary>
        /// 是否空置
        /// </summary>
        [SugarColumn(ColumnDataType = "int", IsNullable = false, ColumnDescription = "是否空置")]
        public int IsEmpty { get; set; }

        /// <summary>
        /// 坐标号
        /// </summary>
        [SugarColumn(ColumnDataType = "nvarchar", Length = 50, IsNullable = false, ColumnDescription = "坐标号")]
        public string XYNo { get; set; }

        /// <summary>
        /// 物料id
        /// </summary>
        [SugarColumn(ColumnDataType = "bigint", ColumnDescription = "物料id")]
        public long MatId { get; set; }

        /// <summary>
        /// 物料编码
        /// </summary>
        [SugarColumn(ColumnDataType = "nvarchar", IsNullable = true, Length = 50, ColumnDescription = "物料编码")]
        public string MatCode { get; set; }

        /// <summary>
        /// 正反面
        /// </summary>
        [SugarColumn(ColumnDataType = "int", IsNullable = true, ColumnDescription = "正反面")]
        public int SideNum { get; set; }

        /// <summary>
        /// 工字轮类型
        /// </summary>
        [SugarColumn(ColumnDataType = "nvarchar", Length = 50, IsNullable = true, ColumnDescription = "工字轮类型")]
        public string SpoolType { get; set; }

        /// <summary>
        /// 钢丝类型
        /// </summary>
        [SugarColumn(ColumnDataType = "nvarchar", Length = 50, IsNullable = true, ColumnDescription = "钢丝类型")]
        public string SilkType { get; set; }

        /// <summary>
        /// 钢丝直径
        /// </summary>
        [SugarColumn(ColumnDataType = "decimal", Length = 18, IsNullable = true, ColumnDescription = "钢丝直径")]
        public decimal SilkDiam { get; set; }

        /// <summary>
        /// 钢丝直径上限
        /// </summary>
        [SugarColumn(ColumnDataType = "decimal", Length = 18, IsNullable = true, ColumnDescription = "钢丝直径上限")]
        public decimal SilkDiamMaxCount { get; set; }

        /// <summary>
        /// 数量上限
        /// </summary>
        [SugarColumn(ColumnDataType = "int", IsNullable = true, ColumnDescription = "数量上限")]
        public int QtyMaxCount { get; set; }

        /// <summary>
        /// 是否芯股
        /// </summary>
        [SugarColumn(ColumnDataType = "int", IsNullable = false, ColumnDescription = "是否芯股")]
        public int IsCore { get; set; }

        /// <summary>
        /// 09垛型里面的行（1-12）
        /// </summary>
        [SugarColumn(ColumnDataType = "int", IsNullable = true, ColumnDescription = "09垛型里面的行（1-12）")]
        public int Row { get; set; }

        /// <summary>
        /// 是否混合料行（09垛型使用，1是0否）
        /// </summary>
        [SugarColumn(ColumnDataType = "int", IsNullable = false, ColumnDescription = "是否混合料行（09垛型使用，1是0否）")]
        public int IsMixRow { get; set; }

        /// <summary>
        /// 混合料行编码
        /// </summary>
        [SugarColumn(ColumnDataType = "nvarchar", IsNullable = true, Length = 50, ColumnDescription = "混合料行编码")]
        public string MixRowCode { get; set; }

        /// <summary>
        ///  第一种第二种第三种
        /// </summary>
        [SugarColumn(ColumnDataType = "int", IsNullable = false, ColumnDescription = "第一种第二种第三种")]
        public int CategoryId { get; set; }
    }

    /// <summary>
    ///  垛形主表
    /// </summary>
    [Tenant("fj")]
    [SugarTable("Bill_BomSetGrp", tableDescription: "垛形主表")]
    public partial class BillBomsetgrp
    {
        /// <summary>
        ///  ID
        /// </summary>
        [SugarColumn(ColumnName = "Id", IsPrimaryKey = true, ColumnDescription = "ID")]
        public virtual long Id { get; set; }

        /// <summary>
        ///  备注
        /// </summary>
        [SugarColumn(ColumnName = "Memo", Length = 500, IsNullable = true, ColumnDataType = "nvarchar", DefaultValue = "", ColumnDescription = "备注")]
        public virtual string Memo { get; set; }

        /// <summary>
        ///  创建用户
        /// </summary>
        [SugarColumn(ColumnName = "AddWho", Length = 50, ColumnDataType = "nvarchar", DefaultValue = "", IsNullable = false, ColumnDescription = "创建用户")]
        public virtual string AddWho { get; set; } = "";

        /// <summary>
        ///  更新用户
        /// </summary>
        [SugarColumn(ColumnName = "EditWho", Length = 50, ColumnDataType = "nvarchar", DefaultValue = "", IsNullable = false, ColumnDescription = "更新用户")]
        public virtual string EditWho { get; set; } = "";

        /// <summary>
        ///  创建时间
        /// </summary>
        [SugarColumn(ColumnName = "AddTime", DefaultValue = "1900-1-1", IsNullable = false, ColumnDescription = "创建时间")]
        public virtual DateTime AddTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 更新时间
        /// </summary>
        [SugarColumn(ColumnName = "EditTime", DefaultValue = "1900-1-1", IsNullable = false, ColumnDescription = "更新时间")]
        public virtual DateTime EditTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 是否停用
        /// </summary>
        [SugarColumn(ColumnDataType = "int", IsNullable = false, ColumnDescription = "是否停用")]
        public int IsStop { get; set; }

        /// <summary>
        /// 垛型编码
        /// </summary>
        [SugarColumn(ColumnDataType = "nvarchar", Length = 50, IsNullable = false, ColumnDescription = "垛型编码")]
        public string Code { get; set; }

        /// <summary>
        ///  短垛型编码
        /// </summary>
        [SugarColumn(ColumnDataType = "smallint", IsNullable = false, ColumnDescription = "短垛型编码")]
        public short ShortCode { get; set; }

        /// <summary>
        /// 垛型名称
        /// </summary>
        [SugarColumn(ColumnDataType = "nvarchar", Length = 100, IsNullable = false, ColumnDescription = "垛型名称")]
        public string Name { get; set; }

        /// <summary>
        /// BomCode（投料信息）
        /// </summary>
        [SugarColumn(ColumnDataType = "nvarchar", Length = 200, IsNullable = false, ColumnDescription = "BomCode（投料信息）")]
        public string BomCode { get; set; }

        /// <summary>
        /// 帘线物料编码
        /// </summary>
        [SugarColumn(ColumnDataType = "nvarchar", Length = 50, IsNullable = false, ColumnDescription = "帘线物料编码")]
        public string ProMaterCode { get; set; }

        /// <summary>
        /// 工字轮个数
        /// </summary>
        [SugarColumn(ColumnDataType = "int", IsNullable = false, ColumnDescription = "工字轮个数")]
        public int HWCountQty { get; set; }

        /// <summary>
        /// 托盘类型
        /// </summary>
        [SugarColumn(ColumnDataType = "nvarchar", Length = 50, IsNullable = false, ColumnDescription = "托盘类型")]
        public string TpTypeCode { get; set; }

        /// <summary>
        /// 层数
        /// </summary>
        [SugarColumn(ColumnDataType = "int", IsNullable = false, ColumnDescription = "层数")]
        public int LayerCountQty { get; set; }

        /// <summary>
        /// 垛型大类
        /// </summary>
        [SugarColumn(ColumnDataType = "int", IsNullable = true, ColumnDescription = "垛型大类")]
        public int StampType { get; set; }
    }
}
