using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WCS.BLL.DbModels;
using WCS.DAL.Db;
using WCS.DAL.DbModels;

namespace WCS.BLL.Manager
{
    /// <summary>
    /// 数据处理
    /// </summary>
    public static class DataProcessManager
    {
        public static object flag = new object();
        public static void UpdateShelfStatus()
        {
            Task.Run(() =>
            {
                lock (flag)
                {
                    var shelfIds = DbHelp.db.Queryable<MatDetailCurrentInfo>()
                                            .Select(t => t.ShelfId)
                                            .Distinct()
                                            .ToList();
                    var shelfInfos = DbHelp.db.Queryable<ShelfInfo>().ToList();

                    var notEmptyToEmpty = shelfInfos.Where(t => t.ShelfStatus == ShelfStatusEnum.非空货架)
                                                 .Where(t => !shelfIds.Contains(t.Id))
                                                 .ToList();
                    notEmptyToEmpty.ForEach(t => t.ShelfStatus = ShelfStatusEnum.空货架);

                    var emptyToNotEmpty = shelfInfos.Where(t => t.ShelfStatus == ShelfStatusEnum.空货架)
                                                   .Where(t => shelfIds.Contains(t.Id))
                                                    .ToList();
                    emptyToNotEmpty.ForEach(t => t.ShelfStatus = ShelfStatusEnum.非空货架);

                    DbHelp.db.Updateable(notEmptyToEmpty)
                             .UpdateColumns(t => new { t.ShelfStatus })
                             .ExecuteCommand();
                    DbHelp.db.Updateable(emptyToNotEmpty)
                             .UpdateColumns(t => new { t.ShelfStatus })
                             .ExecuteCommand();
                }
            });

        }
    }
}
