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

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

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

        /// <summary>
        /// 查询所有称重信息
        /// </summary>
        /// <returns></returns>
        public List<ZxWeightEntity> GetWeightInfos()
        {
            try
            {
                List<ZxWeightEntity> entity = _repository.GetList(x => x.IsDeleted == false);
                return entity;
            }
            catch (Exception ex)
            {
                log.Error("称量信息获取异常", ex);
                return null;
            }
        }

        /// <summary>
        /// 根据配方编号查询称量信息
        /// </summary>
        /// <param name="recipeCode">配方号</param>
        /// <returns></returns>
        public List<ZxWeightEntity> GetWeightInfos(string recipeCode)
        {
            try
            {
                List<ZxWeightEntity> entity = _repository.GetList(x => x.RecipeCode == recipeCode && x.IsDeleted == false);
                return entity;
            }
            catch (Exception ex)
            {
                log.Error("称量信息获取异常", ex);
                return null;
            }
        }

        /// <summary>
        /// 根据配方编号和物料编号和子类型查询称量信息
        /// </summary>
        /// <param name="recipeCode">配方号</param>
        /// <returns></returns>
        public ZxWeightEntity GetWeightInfos(string recipeCode, string materialCode, string materialType)
        {
            try
            {
                ZxWeightEntity entity = _repository
                    .GetSingle(x => 
                    x.RecipeCode == recipeCode 
                    && x.MaterialCode == materialCode 
                    && x.MaterialType == materialType 
                    && x.IsDeleted == false);
                return entity;
            }
            catch (Exception ex)
            {
                log.Error("称量信息获取异常", ex);
                return null;
            }
        }

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

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

        /// <summary>
        /// 根据ID删除称量信息
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns></returns>
        public bool DeleteWeightInfoById(int id)
        {
            try
            {
                ZxWeightEntity e = _repository.GetById(id);
                e.IsDeleted = true;
                return _repository.Update(e);
            }
            catch (Exception ex)
            {
                log.Error("称量信息删除异常", ex);
                return false;
            }
        }

        /// <summary>
        /// 根据RecipeCode删除称量信息
        /// </summary>
        /// <param name="recipeCode">配方号</param>
        /// <returns></returns>
        public bool DeleteWeightInfoByRecipeCode(string recipeCode)
        {
            try
            {
                List<ZxWeightEntity> es = _repository.GetList(x => x.RecipeCode == recipeCode && x.IsDeleted == false);

                es.ForEach(x => x.IsDeleted = true);

                if (es.Count == 0)
                {
                    return true;
                }

                return _repository.UpdateRange(es);
            }
            catch (Exception ex)
            {
                log.Error("称量信息删除异常", ex);
                return false;
            }
        }
    }
}
