using Admin.Core.IRepository;
using Admin.Core.IService;
using Admin.Core.Model;
using System.Collections.Generic;
using System.Linq.Expressions;
using System;
using log4net;
using StackExchange.Profiling.Internal;
using Admin.Core.Common;
using System.Threading.Tasks;

namespace Admin.Core.Service
{
    public class BaseSpaceDetailServices : BaseServices<BaseSpaceDetail>, IBaseSpaceDetailServices
    {
        private static readonly log4net.ILog logHelper = LogManager.GetLogger(typeof(BaseSpaceDetailServices));
        private readonly IBaseRepository<BaseSpaceDetail> _dal;
        public BaseSpaceDetailServices(IBaseRepository<BaseSpaceDetail> dal)
        {
            this._dal = dal;
            base.BaseDal = dal;
        }

        /// <summary>
        /// 根据物料编号删除货道明细
        /// </summary>
        /// <param name="materialCode"></param>
        /// <returns></returns>
        public async Task<bool> DeleteSpaceDetailByMaterialCode(string materialCode)
        {
            bool result = false;
            try
            {
                BaseSpaceDetail spaceDetail =await this.GetSpaceDetailByMaterialCode(materialCode);
                result = _dal.Delete(spaceDetail);

            }
            catch (Exception ex)
            {
                logHelper.Error("根据物料编号删除货道明细异常", ex);
            }
            return result;
        }

        /// <summary>
        /// 通过物料编号获取货道明细
        /// </summary>
        /// <param name="materialCode"></param>
        /// <returns></returns>
        public async Task<BaseSpaceDetail> GetSpaceDetailByMaterialCode(string materialCode)
        {
            BaseSpaceDetail spaceDetail = null;
            try
            {
                Expression<Func<BaseSpaceDetail, bool>> exp = s1 => true;
                exp = exp.And(x => x.MaterialCode == materialCode);

                spaceDetail =await _dal.FirstAsync(exp);

                logHelper.Info($"根据物料编号{materialCode};获取到的货道明细信息:{spaceDetail.ToJson()}");
            }
            catch (Exception ex)
            {
                logHelper.Error("通过物料编号获取货道明细", ex);
            }
            return spaceDetail;
        }

        /// <summary>
        /// 根据物料类型获取可用的货道明细
        /// </summary>
        /// <param name="storeCode"></param>
        /// <param name="materialType"></param>
        /// <returns></returns>
        public async Task<List<BaseSpaceDetail>> GetSpaceDetailsByMaterialTypeAsync(string storeCode, string materialType)
        {
            List<BaseSpaceDetail> spaceDetails = null;
            try
            {
                Expression<Func<BaseSpaceDetail, bool>> exp = s1 => true;
                exp = exp.And(x => x.StoreCode == storeCode && x.MaterialType == materialType && x.IsFlag != 1);
                spaceDetails = await _dal.QueryAsync(exp);

                logHelper.Info($"根据仓库编号:{storeCode};物料类型:{materialType};获取到的货道明细:{spaceDetails}");
            }
            catch (Exception ex)
            {
                logHelper.Error("通过物料类型获取货道明细异常", ex);
            }
            return spaceDetails;
        }

        /// <summary>
        /// 通过货道号获取货道明细
        /// </summary>
        /// <param name="storeCode"></param>
        /// <param name="spaceCode"></param>
        /// <returns></returns>
        public async Task<List<BaseSpaceDetail>> GetSpaceDetailsBySpaceCode(string storeCode, string spaceCode)
        {
            List<BaseSpaceDetail> spaceDetails = null;
            try
            {
                spaceDetails =await _dal.QueryAsync(x => x.StoreCode == storeCode && x.SpaceCode == spaceCode);

                logHelper.Info($"根据仓库编号:{storeCode};货道编号:{spaceCode};获取到的执货道明细:{spaceDetails.ToJson()}");
            }
            catch (Exception ex)
            {
                logHelper.Error("通过货道号获取货道明细异常", ex);
            }
            return spaceDetails;
        }

        /// <summary>
        /// 添加货道明细
        /// </summary>
        /// <param name="spaceDetail"></param>
        /// <returns></returns>
        public async Task<bool> InsertSpaceDetail(BaseSpaceDetail spaceDetail)
        {
            bool result = false;
            try
            {
                int r=await _dal.AddAsync(spaceDetail);
                if (r > 0) { result = true; }
            }
            catch (Exception ex)
            {
                logHelper.Error("添加货道明细异常", ex);
            }
            return result;
        }

        /// <summary>
        /// 更新货道明细
        /// </summary>
        /// <param name="spaceDetail"></param>
        /// <returns></returns>
        public async Task<bool> UpdateSpaceDetail(BaseSpaceDetail spaceDetail)
        {
            bool result = false;
            try
            {
                result =await _dal.UpdateAsync(spaceDetail);
            }
            catch (Exception ex)
            {
                logHelper.Error("更新货道明细异常", ex);
            }
            return result;
        }
    }
}