LocationService.cs 6.87 KB
using FreeSql;
using HHECS.Application.Error;
using HHECS.BllModel;
using HHECS.Dal.Repository;
using HHECS.Model.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace HHECS.Application.Service
{
    public class LocationService : BaseService
    {
        private readonly LogService _logService = new LogService();

        #region CRUD
        public BllResult<List<Location>> GetPageLocations(Expression<Func<Location, bool>> exp, int pageIndex, int pageSize, out long totalCount)
        {
            try
            {
                return BllResultFactory.Success(new LocationRepository().Where(exp).Count(out totalCount).Page(pageIndex, pageSize).ToList());
            }
            catch (Exception ex)
            {
                totalCount = 0;
                return BllResultFactory.Error<List<Location>>(ex.Message, ErrorCodeConst.DEZ0001.ToString());
            }
        }

        /// <summary>
        /// 批量删除货位
        /// </summary>
        /// <param name="codes"></param>
        /// <returns></returns>
        public BllResult DeleteLocations(List<string> codes)
        {
            try
            {
                //TaskEntityRepository taskEntityRepository = new TaskEntityRepository();
                //var tasks = taskEntityRepository.Where(t => t.TaskStatus < (int)TaskEntityStatus.TaskCompleted).ToList();
                //if (tasks.Count(t => codes.Exists(a => a == t.FromLocationCode) || codes.Exists(a => a == t.ToLocationCode)) > 0)
                //{
                //    return BllResultFactory.Error($"所选库位中存在正在执行的任务,操作中止,请先完成任务再删除库位", ErrorCodeConst.Loc_E001.ToString());
                //}
                LocationRepository locationRepository = new LocationRepository();
                return BllResultFactory.Success(locationRepository.Delete(t => codes.Contains(t.Code)));
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error(ex.Message, ErrorCodeConst.DEZ0001.ToString());
            }
        }

        public BllResult<List<Location>> GetAllLocations(Expression<Func<Location, bool>> exp)
        {
            try
            {
                return BllResultFactory.Success(new LocationRepository().Where(exp).ToList());
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error<List<Location>>(ex.Message, ErrorCodeConst.DEZ0001.ToString());
            }
        }

        public BllResult<Location> GetLocationCode(string type, string desctinationArea, string warehouseCode)
        {
            try
            {
                LocationRepository locationRepository = new LocationRepository();
                var result = locationRepository.Where(a => a.Type == type && a.DestinationArea == desctinationArea && a.WarehouseCode == warehouseCode && a.ContainerCode == "").ToList();
                if (result.Count == 0)
                {
                    return BllResultFactory.Error<Location>($"无满足条件的库位数据", ErrorCodeConst.Loc_E002.ToString());
                }
                var locationCode = result.OrderByDescending(t => t.Column).FirstOrDefault();
                if (locationCode == null)
                {
                    return BllResultFactory.Error<Location>($"无满足条件的库位数据", ErrorCodeConst.Loc_E002.ToString());
                }
                return BllResultFactory.Success(locationCode);
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error<Location>(ex.Message, ErrorCodeConst.DEZ0001.ToString());
            }
        }


        /// <summary>
        /// 获取所有库位 根据条件
        /// </summary>
        /// <param name="containerCode"></param>
        /// <param name="row"></param>
        /// <param name="column"></param>
        /// <param name="layer"></param>
        /// <param name="roadway"></param>
        /// <param name="status"></param>
        /// <param name="code"></param>
        /// <param name="warehouseCode"></param>
        /// <returns></returns>
        public BllResult<List<Location>> GetAllLocations(string containerCode, string row, string column, string layer, string roadway, string status, string code, string warehouseCode)
        {
            try
            {
                Expression<Func<Location, bool>> exp = t => true;
                if (!String.IsNullOrEmpty(containerCode))
                {
                    exp = exp.And(t => t.ContainerCode.Contains(containerCode));
                }
                if (!String.IsNullOrEmpty(row))
                {
                    exp = exp.And(t => t.Row.ToString() == row);
                }
                if (!String.IsNullOrEmpty(column))
                {
                    exp = exp.And(t => t.Column.ToString() == column);
                }
                if (!String.IsNullOrEmpty(layer))
                {
                    exp = exp.And(t => t.Layer.ToString() == layer);
                }
                if (!String.IsNullOrEmpty(roadway))
                {
                    exp = exp.And(t => t.Roadway.ToString() == roadway);
                }
                if (!String.IsNullOrEmpty(status) && status != "all")
                {
                    exp = exp.And(t => t.Status.ToString() == status);
                }
                if (!String.IsNullOrEmpty(code))
                {
                    exp = exp.And(t => t.Code == code);
                }
                if (!String.IsNullOrEmpty(warehouseCode))
                {
                    exp = exp.And(t => t.WarehouseCode == warehouseCode);
                }
                return BllResultFactory.Success(new LocationRepository().Where(exp).ToList());
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error<List<Location>>(ex.Message, ErrorCodeConst.DEZ0001.ToString());
            }
        }


        /// <summary>
        /// 根据库位编码获取库位信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public BllResult<Location> GetLocationByCode(string code)
        {
            try
            {
                LocationRepository locationRepository = new LocationRepository();
                var resut = locationRepository.Where(t => t.Code == code).First();
                if (resut != null)
                {
                    return BllResultFactory.Success(resut, "成功");
                }
                else
                {
                    return BllResultFactory.Error<Location>("未查询到数据");
                }
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error<Location>(ex.Message, ErrorCodeConst.DEZ0001.ToString());
            }
        }

        #endregion

    }
}