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

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

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

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

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

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

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

        /// <summary>
        /// 按组删除物料子类别信息
        /// </summary>
        /// <param name="ids">子类别信息ID数组</param>
        /// <returns></returns>
        public bool DeleteGroup(int[] ids)
        {
            try
            {
                dynamic[] dynamicArray = ids.Select(x => (dynamic)x).ToArray();

                return _repository.DeleteByIds(dynamicArray);
            }
            catch (Exception ex)
            {
                log.Error("物料子类别组信息删除异常", ex);
                return false;
            }
        }
    }
}
