using Admin.Core.IRepository;
using Admin.Core.IService;
using Admin.Core.Model;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System;
using Aucma.Core.PLc;
using Admin.Core.Common;
using log4net;
using SqlSugar;
using System.Linq.Expressions;
using StackExchange.Profiling.Internal;

namespace Admin.Core.Service
{
    public class ExecutePlanInfoServices : BaseServices<ExecutePlanInfo>, IExecutePlanInfoServices
    {
        private static readonly log4net.ILog logHelper = LogManager.GetLogger(typeof(ExecutePlanInfoServices));
        private readonly IBaseRepository<ExecutePlanInfo> _dal;
        private readonly IExecutePlanInfoRepository  _executePlanInfoRepository;
        private readonly IProductOrderInfoServices _productOrderInfoServices;
        private readonly IProductPlanInfoServices _productPlanInfoServices;

        public ExecutePlanInfoServices(IBaseRepository<ExecutePlanInfo> dal, IProductOrderInfoServices productOrderInfoServices,
            IProductPlanInfoServices productPlanInfoServices, IExecutePlanInfoRepository executePlanInfoRepository)
        {
            this._dal = dal;
            base.BaseDal = dal;
            _productOrderInfoServices = productOrderInfoServices;
            _productPlanInfoServices = productPlanInfoServices;
            _executePlanInfoRepository = executePlanInfoRepository;
        }

        #region 计划删除
        /// <summary>
        /// 计划删除
        /// </summary>
        /// <param name="planCode"></param>
        /// <returns></returns>
        public async Task<bool> ExecPlanDelete(string id)
        {
            return await _dal.DeleteByIdAsync(id);
        }
        #endregion

        #region 执行计划下移
        /// <summary>
        /// 执行计划下移
        /// </summary>
        /// <param name="planInfos"></param>
        /// <param name="planCode"></param>
        /// <returns></returns>
        public async Task<bool> PlanMoveDown(string id,string station)
        {
            List<ExecutePlanInfo> execPlans = new List<ExecutePlanInfo>();
            var list = await _dal.QueryAsync(d=>d.ProductLineCode.Equals(station));
            List<ExecutePlanInfo> planInfos = (list.OrderBy(d => d.ExecuteOrder)).ToList();
            if (planInfos.Count()==0) return false;

            ExecutePlanInfo planInfo = planInfos.FirstOrDefault(x => x.ObjId == int.Parse(id));
            int executeOrder = planInfo.ExecuteOrder;
            int planIndex = planInfos.IndexOf(planInfo);
            if (planIndex == 0 && planInfos.Count == 0) return false;
            if ((planInfos.Count - 1) == planIndex) return false;

            var lastPlanInfo = planInfos[planIndex + 1];
            planInfo.ExecuteOrder = lastPlanInfo.ExecuteOrder;
            lastPlanInfo.ExecuteOrder = executeOrder;
            execPlans.Add(planInfo);
            execPlans.Add(lastPlanInfo);
            var result = await _dal.UpdateAsync(execPlans);
            if (result)
                return true;
            else
                return false;


        }
        #endregion

        #region 计划上移
        /// <summary>
        /// 计划上移
        /// </summary>
        /// <param name="planInfos"></param>
        /// <param name="planCode"></param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public async Task<bool> PlanMoveUp(string id, string station)
        {
            List<ExecutePlanInfo> execPlans = new List<ExecutePlanInfo>();

            var list = await _dal.QueryAsync(d => d.ProductLineCode.Equals(station));
            List<ExecutePlanInfo> planInfos = (list.OrderBy(d => d.ExecuteOrder)).ToList();

            if (planInfos.Count()==0) return false;

            ExecutePlanInfo planInfo = planInfos.FirstOrDefault(x => x.ObjId == int.Parse(id));
            int executeOrder = planInfo.ExecuteOrder;
            int planIndex = planInfos.IndexOf(planInfo);

            if (planIndex == 0) return false;

            var lastPlanInfo = planInfos[planIndex - 1];
            planInfo.ExecuteOrder = lastPlanInfo.ExecuteOrder;
            lastPlanInfo.ExecuteOrder = executeOrder;
            execPlans.Add(planInfo);
            execPlans.Add(lastPlanInfo);
            var result = await _dal.UpdateAsync(execPlans);
            if (result)
                return true;
            else
                return false;
        }

        #endregion

        #region 计划下达
        /// <summary>
        /// 计划下达
        /// </summary>
        /// <param name="planCode"></param>
        /// <param name="nowPlanCode"></param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public async Task<ExecutePlanInfo> PlanNextPass(ExecutePlanInfo sm)
        {
            ExecutePlanInfo planInfo = null;
            try
            {
                List<ExecutePlanInfo> planInfos = new List<ExecutePlanInfo>();
                if (sm != null)
                {
                    //下传计划到PLC,同时更改计划状态
                    var rearPanel = PlcHelper.melsecList.FirstOrDefault(d=>d.EquipName.Equals("后板Plc"));
                    if (rearPanel.plc.IsConnected)
                    {
                        rearPanel.plc.WriteString("M100", sm.ProductPlanCode);
                        rearPanel.plc.WriteInt32("M100", sm.PlanAmount);
                    }
                    var box = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("U壳PLC"));
                    if (box.plc.IsConnected)
                    {
                        box.plc.WriteString("M100", sm.ProductPlanCode);
                        box.plc.WriteInt32("M100", sm.PlanAmount);
                    }
                    //将计划写入拆分计划表中

                    return planInfo;
                }
            }
            catch (Exception ex)
            {
                logHelper.Error(ex);
            }
            return planInfo;
        }
        #endregion

        /// <summary>
        /// 通过产线工位获取执行顺序(默认+1)
        /// </summary>
        /// <param name="productLineCode"></param>
        /// <returns></returns>
        public async Task<int> GetExecuteOrderByProductLineCode(string productLineCode)
        {
            int result = 0;
            try
            {
                Expression<Func<ExecutePlanInfo, bool>> exp = s1 => true;
                exp = exp.And(x => x.ProductLineCode == productLineCode);

                List<ExecutePlanInfo> planInfos =await _dal.QueryAsync(exp);

                int order = planInfos.OrderByDescending(x => x.ExecuteOrder).First().ExecuteOrder;

                result = order + 1;
            }
            catch (Exception ex)
            {
                logHelper.Error("通过产线工位获取执行顺异常", ex);
            }
            return result;
        }

        /// <summary>
        /// 通过执行计划编号获取执行计划
        /// </summary>
        /// <param name="executePlanCode"></param>
        /// <returns></returns>
        public async Task<ExecutePlanInfo> GetExecutePlanInfoByPlanCodeAsync(string executePlanCode)
        {
            ExecutePlanInfo planInfo = null;
            try
            {
                Expression<Func<ExecutePlanInfo, bool>> exp = s1 => true;
                exp = exp.And(x => x.ExecutePlanCode == executePlanCode);

                planInfo = await _dal.FirstAsync(exp);

                logHelper.Info($"根据执行计划编号{executePlanCode};获取到的执行计划信息:{planInfo.ToJson()}");
            }
            catch (Exception ex)
            {
                logHelper.Error("通过执行计划编号获取执行计划异常", ex);
            }
            return planInfo;
        }

        /// <summary>
        /// 通过产线工位获取执行计划
        /// </summary>
        /// <param name="productLineCode"></param>
        /// <returns></returns>
        public async Task<List<ExecutePlanInfo>> GetExecutePlanInfosByProductLineCode(string productLineCode)
        {
            List<ExecutePlanInfo> planInfos = null;
            try
            {
                Expression<Func<ExecutePlanInfo, bool>> exp = s1 => true;
                exp = exp.And(x => x.ProductLineCode == productLineCode);

                planInfos =await _dal.QueryAsync(exp);

                logHelper.Info($"根据产线工位编号:{productLineCode};获取到的执行计划信息:{planInfos.ToJson}");
            }
            catch (Exception ex)
            {
                logHelper.Error("通过产线工位获取执行计划异常", ex);
            }
            return planInfos;
        }

        /// <summary>
        /// 通过生产计划编号获取执行计划
        /// </summary>
        /// <param name="productPlanCode"></param>
        /// <returns></returns>
        public async Task<List<ExecutePlanInfo>> GetExecutePlanInfosByProductPlanCode(string productPlanCode)
        {
            List<ExecutePlanInfo> planInfos = null;
            try
            {
                Expression<Func<ExecutePlanInfo, bool>> exp = s1 => true;
                exp = exp.And(x => x.ProductPlanCode == productPlanCode);

                planInfos =await _dal.QueryAsync(exp);

                logHelper.Info($"根据生产计划编号:{productPlanCode};获取到的执行计划信息:{planInfos.ToJson()}");
            }
            catch (Exception ex)
            {
                logHelper.Error("通过生产计划编号获取执行计划异常", ex);
            }
            return planInfos;
        }

        /// <summary>
        /// 新增执行计划
        /// </summary>
        /// <param name="executePlanInfo"></param>
        /// <returns></returns>
        public async Task<bool> InsertExecutePlanInfo(ExecutePlanInfo executePlanInfo)
        {
            bool result = false;
            try
            {
                int r =await _dal.AddAsync(executePlanInfo);
                if (r > 0) result = true;
            }
            catch (Exception ex)
            {
                logHelper.Error("新增执行计划异常", ex);
            }
            return result;
        }

        /// <summary>
        /// 修改执行计划
        /// </summary>
        /// <param name="executePlanInfo"></param>
        /// <returns></returns>
        public async Task<bool> UpdateExecutePlanInfo(ExecutePlanInfo executePlanInfo)
        {
            bool result = false;
            try
            {
                result =await _dal.UpdateAsync(executePlanInfo);
            }
            catch (Exception ex)
            {
                logHelper.Error("修改执行计划异常", ex);
            }
            return result;
        }

        /// <summary>
        /// 批量修改执行计划
        /// </summary>
        /// <param name="executePlanInfos"></param>
        /// <returns></returns>
        public async Task<bool> UpdateRangeExecutePlanInfo(List<ExecutePlanInfo> executePlanInfos)
        {
            bool result = false;
            try
            {
                result =await _dal.UpdateAsync(executePlanInfos);

            }
            catch (Exception ex)
            {
                logHelper.Error("修改执行计划异常", ex);
            }
            return result;
        }

        /// <summary>
        /// 根据执行计划编号删除执行计划
        /// </summary>
        /// <param name="executePlanCode"></param>
        /// <returns></returns>
        public async Task<bool> DeleteExecutePlanInfoByPlanCode(string executePlanCode)
        {
            bool result = false;
            try
            {
                ExecutePlanInfo planInfo =await this.GetExecutePlanInfoByPlanCodeAsync(executePlanCode);

                result =await _dal.DeleteAsync(planInfo);
            }
            catch (Exception ex)
            {
                logHelper.Error("根据执行计划编号删除执行计划异常", ex);
            }
            return result;
        }

        public async Task<List<dynamic>> GetStationHourAmount(string stationCode)
        {
            List<dynamic> result = null;
            try
            {
                result =await _executePlanInfoRepository.GetStationHourAmount(stationCode);
            }
            catch (Exception ex)
            {
                logHelper.Error("获取小时产量数据异常", ex);
            }
            return result;
        }

        public async Task<List<dynamic>> GetStationMaterialStats(string stationCode)
        {
            List<dynamic> result = null;
            try
            {
                result = await _executePlanInfoRepository.GetStationMaterialStats(stationCode);
            }
            catch (Exception ex)
            {
                logHelper.Error("获取型号统计数据异常", ex);
            }
            return result;
        }
        /// <summary>
        /// 通过执行计划编号获取执行计划
        /// </summary>
        /// <param name="executePlanCode"></param>
        /// <returns></returns>
        public async Task<ExecutePlanInfo> GetExecutePlanInfoByPlanCode(string executePlanCode)
        {
            ExecutePlanInfo planInfos = null;
            try
            {
                Expression<Func<ExecutePlanInfo, bool>> exp = s1 => true;
                exp = exp.And(x => x.ProductPlanCode == executePlanCode);

                planInfos = await _executePlanInfoRepository.FirstAsync(exp);

                logHelper.Info($"根据生产计划编号:{executePlanCode};获取到的执行计划信息:{planInfos.ToJson()}");
            }
            catch (Exception ex)
            {
                logHelper.Error("通过生产计划编号获取执行计划异常", ex);
            }
            return planInfos;
        }

        /// <summary>
        /// 获取钣金生产小时数
        /// </summary>
        /// <param name="stationCode"></param>
        /// <returns></returns>
        public async Task<List<dynamic>> GetStationSheetMetalHourAmountAsync(string stationCode)
        {
            return await _executePlanInfoRepository.GetStationSheetMetalHourAmountAsync(stationCode);
        }
        public  List<dynamic> GetStationSheetMetalStats(string stationCode)
        {
            return  _executePlanInfoRepository.GetStationSheetMetalStats(stationCode);
        }
      
    }
}