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

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

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

        /// <summary>
        /// 查询配方字段信息
        /// </summary>
        /// <returns></returns>
        public List<ZxRecipeParaEntity> GetRecipeParaInfos()
        {
            try
            {
                List<ZxRecipeParaEntity> entity = _repository.GetList();
                return entity;
            }
            catch (Exception ex)
            {
                log.Error("配方信息获取异常", ex);
                return null;
            }
        }

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

        /// <summary>
        /// 修改配方字段信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateRecipeParaInfo(ZxRecipeParaEntity entity)
        {
            try
            {
                return _repository.Update(entity);
            }
            catch (Exception ex)
            {
                log.Error("配方字段信息修改异常", ex);
                return false;
            }
        }

        /// <summary>
        /// 修改配方字段信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateRangeRecipeParaInfo(List<ZxRecipeParaEntity> entity)
        {
            try
            {
                return _repository.UpdateRange(entity);
            }
            catch (Exception ex)
            {
                log.Error("配方字段信息修改异常", ex);
                return false;
            }
        }

        /// <summary>
        /// 添加配方字段信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool InsertRecipeParaInfo(ZxRecipeParaEntity entity)
        {
            try
            {
                return _repository.Insert(entity);
            }
            catch (Exception ex)
            {
                log.Error("配方字段信息修改异常", ex);
                return false;
            }
        }

        /// <summary>
        /// ID删除配方字段信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteRecipeParaInfoById(int id)
        {
            try
            {
                return _repository.DeleteById(id);
            }
            catch (Exception ex)
            {
                log.Error("配方字段信息删除异常", ex);
                return false;
            }
        }

        /// <summary>
        /// ID删除配方字段信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteRecipeParaInfoByRecipeCode(string recipeCode)
        {
            try
            {
                return _repository.Delete(x => x.RecipeCode == recipeCode);
            }
            catch (Exception ex)
            {
                log.Error("配方字段信息删除异常", ex);
                return false;
            }
        }

        /// <summary>
        /// 清除脏数据
        /// </summary>
        /// <returns></returns>
        public bool DeleteDirtyData()
        {
            try
            {
                var entities = _repository.GetList();

                entities = _repository.GetList().Where(x => x.RecipeCode == null || x.RecipeCode == string.Empty).ToList();

                foreach (var entity in entities)
                {
                    if (!DeleteRecipeParaInfoById(entity.Id))
                    {
                        return false;
                    }
                }

                entities = _repository.GetList();

                foreach (var e in entities.GroupBy(x => x.RecipeCode).ToList())
                {
                    var l = e.ToList();
                    if (l.Count > 1)
                    {
                        l.Remove(l.Where(x => x.Id == l.Min(y => y.Id)).FirstOrDefault());
                        foreach (var entity in l)
                        {
                            if (!DeleteRecipeParaInfoById(entity.Id))
                            {
                                return false;
                            }
                        }
                    }
                }


                return true;
            }
            catch (Exception ex)
            {
                log.Error("工位配方字段信息删除异常", ex);
                return false;
            }
        }
    }
}
