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 ZxMaterialTypeService
    {
        private static readonly Lazy<ZxMaterialTypeService> lazy = new Lazy<ZxMaterialTypeService>(() => new ZxMaterialTypeService());

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

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

        /// <summary>
        /// 查询所有物料类别信息
        /// </summary>
        /// <returns></returns>
        public List<ZxMaterialTypeEntity> GetMaterialTypeInfos()
        {
            try
            {
                List<ZxMaterialTypeEntity> entity = _repository.GetList();
                return entity;
            }
            catch (Exception ex)
            {
                log.Error("物料类别信息获取异常", ex);
                return null;
            }
        }

        /// <summary>
        /// 查询所有物料类别信息
        /// </summary>
        /// <returns></returns>
        public List<ZxMaterialTypeEntity> GetMaterialTypeInfos(Expression<Func<ZxMaterialTypeEntity, bool>> whereExpression)
        {
            try
            {
                List<ZxMaterialTypeEntity> entity = _repository.GetList(whereExpression);
                return entity;
            }
            catch (Exception ex)
            {
                log.Error("物料类别信息获取异常", ex);
                return null;
            }
        }

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

        /// <summary>
        /// 删除物料类别信息
        /// </summary>
        /// <param name="materialCode"></param>
        /// <returns></returns>
        public bool DeleteMaterialTypeInfoById(int id)
        {
            try
            {
                return _repository.DeleteById(id);
            }
            catch (Exception ex)
            {
                log.Error("物料类别信息删除异常", ex);
                return false;
            }
        }
    }
}
