using Microsoft.Extensions.Logging;
using SlnMesnac.Model.domain;
using SlnMesnac.Repository.service.@base;
using System;
using System.Collections.Generic;
using System.Text;

namespace SlnMesnac.Repository.service.Impl
{
    public  class ProdPlanDetailServiceImpl : BaseServiceImpl<ProdPlanDetail>, ProdPlanDetailService
    {
        private ILogger<ProdPlanDetailServiceImpl> _logger;
        public ProdPlanDetailServiceImpl(Repository<ProdPlanDetail> repository, ILogger<ProdPlanDetailServiceImpl> logger) : base(repository)
        {
            _logger = logger;
        }

        public List<ProdPlanDetail> GetPlanDetails()
        {
            List<ProdPlanDetail> pLanDetails = null;
            try
            {
                pLanDetails = base._rep.GetList();
                pLanDetails.Reverse();
            }
            catch (Exception ex)
            {
                _logger.LogError($"获取员工打卡信息异常{ex.Message}");
            }
            return pLanDetails;
        }

        public bool InsertPlanDetails(List<ProdPlanDetail> planDetails)
        {
            bool result = false;
            try
            {
                base._rep.AsTenant().BeginTran();
                result = base._rep.InsertRange(planDetails);
                base._rep.AsTenant().CommitTran();
            }
            catch (Exception ex)
            {
                base._rep.AsTenant().RollbackTran();
                _logger.LogError($"员工打卡信息添加异常:{ex.Message}");
            }
            return result;
        }

        public List<ProdPlanDetail> GetPlanDetailsByPlanCode(string planCode)
        {
            List<ProdPlanDetail> prodPlanDetails = new List<ProdPlanDetail>();
            try
            {
                prodPlanDetails = _rep.AsQueryable().WhereIF(!string.IsNullOrEmpty(planCode), x => x.PlanCode == planCode).OrderByDescending(x => x.ObjId).ToList();
            }
            catch (Exception ex)
            {
                _logger.LogError($"查询对应计划的所有明细异常:{ex.Message}");
                throw;
            }
            return prodPlanDetails;
        }

        public ProdPlanDetail GetLastPlanDetailByPlanCode(string planCode)
        {
            ProdPlanDetail prodPlanDetail = new ProdPlanDetail();
            try
            {
                prodPlanDetail = _rep.AsQueryable().WhereIF(!string.IsNullOrEmpty(planCode), x => x.PlanCode == planCode).OrderByDescending(x => x.EndTime).First();
            }
            catch (Exception ex)
            {
                _logger.LogError($"查询对应计划的最新一条明细异常:{ex.Message}");
                throw;
            }
            return prodPlanDetail;
        }

        public bool DeleteByPlanCodeBatchNumber(string planCode, int number)
        {
            bool result = false;
            try
            {
                //_rep.AsQueryable().WhereIF(!string.IsNullOrEmpty(planCode), x => x.PlanCode == planCode && x.BatchNumber == number).OrderByDescending(x => x.ObjId).First();
                List<ProdPlanDetail> tempList = _rep.AsQueryable().Where(x => x.PlanCode == planCode && x.BatchNumber == number).OrderByDescending(x => x.ObjId).ToList();
                result = _rep.Delete(tempList);
            }
            catch (Exception ex)
            {
                _logger.LogError($"根据计划编号删锅数为0的数据异常{ex.Message}");
            }
            return result;
        }

        /// <summary>
        /// 删除工单里的某个批次
        /// </summary>
        /// <param name="planCode"></param>
        /// <param name="batch"></param>
        /// <exception cref="NotImplementedException"></exception>
        public bool DeleteTheBatchNumberByPlanCode(string planCode, int batch)
        {
            bool result = false;
            try
            {
                ProdPlanDetail temp = _rep.AsQueryable().Where(x => x.PlanCode == planCode && x.BatchNumber == batch).OrderByDescending(x => x.ObjId).First();
                result = _rep.Delete(temp);
            }
            catch (Exception ex)
            {
                _logger.LogError($"根据计划编号删指定锅数的数据异常{ex.Message}");
            }
            return result;
        }
    }
}
