using HighWayIot.Log4net;
using HighWayIot.Repository.domain;
using Models;
using SqlSugar;
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 ZxRecipeService
    {
        private static readonly Lazy<ZxRecipeService> lazy = new Lazy<ZxRecipeService>(() => new ZxRecipeService());

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

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

        /// <summary>
        /// 查询配方信息
        /// </summary>
        /// <returns></returns>
        public List<ZxRecipeEntity> GetRecipeInfos()
        {
            try
            {
                List<ZxRecipeEntity> 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<ZxRecipeEntity> GetRecipeInfosByRecipeCode(string recipeCode)
        {
            try
            {
                List<ZxRecipeEntity> entity = _repository.GetList(x => x.IsDeleted == false && x.RecipeCode == recipeCode);
                return entity;
            }
            catch (Exception ex)
            {
                log.Error("根据编号查询配方信息获取异常", ex);
                return null;
            }
        }

        /// <summary>
        /// 根据配方名称查询条配方参数
        /// </summary>
        /// <param name="recipeName"></param>
        /// <returns></returns>
        public ZxRecipeEntity GetSingleInfoByRecipeName(string recipeName)
        {
            try
            {
                ZxRecipeEntity entity = _repository.GetSingle(x => x.RecipeName == recipeName && x.IsDeleted == false);
                return entity;
            }
            catch (Exception ex)
            {
                log.Error("根据一个配方名称查询到多条配方信息", ex);
                return null;
            }
        }

        /// <summary>
        /// 根据成品代号查询条配方参数
        /// </summary>
        /// <param name="recipeName"></param>
        /// <returns></returns>
        public ZxRecipeEntity GetSingleInfoByRecipeCode(string recipeCode)
        {
            try
            {
                ZxRecipeEntity entity = _repository.GetSingle(x => x.RecipeCode == recipeCode && x.IsDeleted == false);
                return entity;
            }
            catch (Exception ex)
            {
                log.Error("根据一个成品代号查询到多条配方信息", ex);
                return null;
            }
        }

        /// <summary>
        /// 获取所有配方名称
        /// </summary>
        /// <returns></returns>
        public List<string> GetAllRecipeName()
        {
            try
            {
                List<string> entity = _repository.GetList(x => x.IsDeleted == false).Select(x => x.RecipeName).ToList();
                return entity;
            }
            catch (Exception ex)
            {
                log.Error("根据编号查询配方信息获取异常", ex);
                return null;
            }
        }


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


        /// <summary>
        /// 新增配方信息
        /// </summary>
        /// <param name="recipeEntity"></param>
        /// <returns></returns>
        public bool InsertRecipeInfo(ZxRecipeEntity recipeEntity)
        {
            try
            {
                return _repository.Insert(recipeEntity);
            }
            catch (Exception ex)
            {
                log.Error("配方信息添加异常", ex);
                return false;
            }
        }

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