LocationTools.cs 4.82 KB
using HHECS.Application.Enums;
using HHECS.Application.Service;
using HHECS.Dal;
using HHECS.Dal.Repository;
using HHECS.Model.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HHECS.Test
{
    public class LocationTools
    {
        /// <summary>
        /// 创建库位
        /// </summary>
        public static void CreateLocations(List<LocationAddModel> locationAddModels, bool isdel)
        {
            using (var um = DALHelper.GetFreeSql().CreateUnitOfWork())
            {
                try
                {
                    LocationRepository locationRepository = new LocationRepository();
                    locationRepository.UnitOfWork = um;
                    List<Location> locations = new List<Location>();
                    LocationService locationService = new LocationService();
                    if (isdel)
                    {
                        var data = locationService.GetAllLocations(t => true).Data.Select(t => t.Code).ToList();
                        locationService.DeleteLocations(data);
                    }
                    foreach (var locationAddModel in locationAddModels)
                    {
                        for (int i = locationAddModel.RowBeginIndex; i <= locationAddModel.RowEndIndex; i++)//行
                        {
                            for (int j = locationAddModel.ColumnBeginIndex; j <= locationAddModel.ColumnEndIndex; j++)//列
                            {
                                for (int k = locationAddModel.LayerBeginIndex; k <= locationAddModel.LayerEndIndex; k++)//层
                                {
                                    Location location = new Location();
                                    location.Code = $"{locationAddModel.Prefix}{i.ToString().PadLeft(2, '0')}{locationAddModel.Connector}{j.ToString().PadLeft(2, '0')}{locationAddModel.Connector}{k.ToString().PadLeft(2, '0')}";
                                    location.Roadway = locationAddModel.Roadway;
                                    location.Row = i;
                                    location.Column = j;
                                    location.Layer = k;
                                    location.RowIndex1 = locationAddModel.RowIndex1;
                                    location.RowIndex2 = locationAddModel.RowIndex2;
                                    location.SRMCode = locationAddModel.SRMCode;
                                    location.Type = locationAddModel.Type;
                                    //location.Type = k <= 3 ? "Small" : "Middle";
                                    location.Status = (int)LocationStatus.Free;
                                    location.ContainerCode = "";
                                    location.WarehouseCode = locationAddModel.WarehouseCode;
                                    location.DestinationArea = locationAddModel.DestinationArea;
                                    locations.Add(location);
                                }
                            }
                        }
                    }
                    locationRepository.Insert(locations);
                    um.Commit();
                }
                catch (Exception ex)
                {
                    um.Rollback();
                    Console.WriteLine(ex.Message);
                }
            }
        }

        /// <summary>
        /// 用于删除缺失库位
        /// </summary>
        /// <param name="prefix"></param>
        /// <param name="connector"></param>
        /// <param name="RowBeging"></param>
        /// <param name="RowEnd"></param>
        /// <param name="ColumnBeging"></param>
        /// <param name="ColumnEnd"></param>
        /// <param name="LayerBeging"></param>
        /// <param name="LayerEnd"></param>
        /// <param name="warehouseCode"></param>
        public static void DeleteLocations(int rowBeging, int rowEnd, int columnBeging, int columnEnd, int layerBeging, int layerEnd, string warehouseCode)
        {
            using (var um = DALHelper.GetFreeSql().CreateUnitOfWork())
            {
                try
                {
                    LocationRepository locationRepository = new LocationRepository();
                    locationRepository.UnitOfWork = um;

                    locationRepository.Delete(t => t.Row >= rowBeging && t.Row <= rowEnd 
                        && t.Column >= columnBeging && t.Column <= columnEnd 
                        && t.Layer >= layerBeging && t.Layer <= layerEnd 
                        && t.WarehouseCode == warehouseCode);

                    um.Commit();
                }
                catch (Exception ex)
                {
                    um.Rollback();
                    Console.WriteLine(ex.Message);
                }
            }
        }

    }
}