using HighWayIot.Log4net;
using HighWayIot.Repository.domain;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace HighWayIot.Repository.service
{
    public class ZxMaterialService
    {
        private static readonly Lazy<ZxMaterialService> lazy = new Lazy<ZxMaterialService>(() => new ZxMaterialService());

        public static ZxMaterialService Instance
        {
            get
            {
                return lazy.Value;
            }
        }

        private LogHelper log = LogHelper.Instance;
        Repository<ZxMaterialEntity> _repository => new Repository<ZxMaterialEntity>("sqlserver");

        /// <summary>
        /// 查询所有物料信息
        /// </summary>
        /// <param name="materialName">物料名</param>
        /// <param name="materialType">类型</param>
        /// <param name="childType">子类型</param>
        /// <param name="isAvaliableUse">是否按是否使用查询</param>
        /// <param name="isUse">是否使用</param>
        /// <returns></returns>
        public List<ZxMaterialEntity> GetMaterialInfos(string materialName = null, string materialType = null, string childType = null, bool isAvaliableUse = false, bool isUse = false)
        {
            try
            {
                List<ZxMaterialEntity> entity = _repository.GetList(x => x.IsDeleted == false);
                if(!string.IsNullOrEmpty(materialName))
                {
                    entity = entity.Where(x => x.MaterialName.Contains(materialName)).ToList();
                }

                if(!string.IsNullOrEmpty(materialType))
                {
                    if(!string.IsNullOrEmpty(childType))
                    {
                        entity = entity.Where(x => x.MaterialType == materialType && x.ChildType == childType).ToList();
                    }
                    else
                    {
                        entity = entity.Where(x => x.MaterialType == materialType).ToList();
                    }
                }

                if(isAvaliableUse == true)
                {
                    entity = entity.Where(x => x.IsUse == isUse).ToList();
                }

                return entity;
            }
            catch (Exception ex)
            {
                log.Error("物料信息获取异常", ex);
                return null;
            }
        }

        /// <summary>
        /// 物料名称获取单个物料信息
        /// </summary>
        /// <param name="materialName"></param>
        /// <returns></returns>
        public ZxMaterialEntity GetMaterialInfoByMaterialName(string materialName)
        {
            try
            {
                var result = _repository.GetSingle(x => x.MaterialName == materialName);
                return result;
            }
            catch (Exception ex)
            {
                log.Error("物料信息获取异常", ex);
                return null;
            }
        }

        /// <summary>
        /// 根据Id获取单条数据
        /// </summary>
        /// <param name="id">物料编号</param>
        /// <returns></returns>
        public ZxMaterialEntity GetSingleEntityById(int id)
        {
            try
            {
                return _repository.GetById(id);
            }
            catch (Exception ex)
            {
                log.Error("单条物料信息获取异常", ex);
                return null;
            }
        }

        /// <summary>
        /// 根据MaterialCode获取数据
        /// </summary>
        /// <param name="materialCode">物料编号</param>
        /// <returns></returns>
        public List<ZxMaterialEntity> GetEntityByMaterialCode(string materialCode)
        {
            try
            {
                return _repository.GetList(x => x.MaterialCode == materialCode && x.IsDeleted == false);
            }
            catch (Exception ex)
            {
                log.Error("根据MaterialCode获取物料信息获取异常", ex);
                return null;
            }
        }

        /// <summary>
        /// 修改物料信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateMaterialInfo(ZxMaterialEntity entity)
        {
            try
            {
                return _repository.Update(entity);
            }
            catch (Exception ex)
            {
                log.Error("物料信息修改异常", ex);
                return false;
            }
        }

        /// <summary>
        /// 新增物料信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool InsertMaterialInfo(ZxMaterialEntity entity)
        {
            try
            {
                return _repository.Insert(entity);
            }
            catch (Exception ex)
            {
                log.Error("物料信息添加异常", ex);
                return false;
            }
        }

        /// <summary>
        /// 删除物料信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteMaterialInfoById(int id)
        {
            try
            {
                ZxMaterialEntity e = _repository.GetById(id);
                e.IsDeleted = true;
                return _repository.Update(e);
            }
            catch (Exception ex)
            {
                log.Error("物料信息删除异常", ex);
                return false;
            }
        }
    }
}
