using SlnMesnac.Config;
using SlnMesnac.Model.domain;
using SlnMesnac.Model.dto;
using SlnMesnac.Model.enums;
using SlnMesnac.Repository.service.@base;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;

#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* 版权所有 (c) 2024 WenJY 保留所有权利。
* CLR版本：4.0.30319.42000
* 机器名称：LAPTOP-E0N2L34V
* 命名空间：SlnMesnac.Repository.service.Impl
* 唯一标识：f47ba8ea-bac5-40cb-ab9a-a8f59d782594
*
* 创建者：WenJY
* 电子邮箱：wenjy@mesnac.com
* 创建时间：2024-04-07 16:57:36
* 版本：V1.0.0
* 描述：
*
*--------------------------------------------------------------------
* 修改人：
* 时间：
* 修改说明：
*
* 版本：V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace SlnMesnac.Repository.service.Impl
{
    public class MesProductPlanServiceImpl : BaseServiceImpl<MesProductPlan>, IMesProductPlanService
    {
        private AppConfig _appConfig;

        public MesProductPlanServiceImpl(Repository<MesProductPlan> rep, AppConfig appConfig):base(rep) 
        {
            _appConfig = appConfig;
        }

        /// <summary>
        /// 获取MES生产计划
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public List<MesProductPlan> GetMesProductPlans()
        {
            try
            {
                return base._rep.GetList(x => x.StationId == _appConfig.stationId && x.PlanStatus != PlanStatusEnum.已完成);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"获取MES生产计划异常：{ex.Message}");
            }
        }

        /// <summary>
        /// 根据计划编号获取生产计划
        /// </summary>
        /// <param name="planCode"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public MesProductPlan GetProdPlanByPlanCode(string planCode)
        {
            try
            {
                return base._rep.GetFirst(x => x.PlanCode == planCode && x.StationId == _appConfig.stationId);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"根据计划编号获取生产计划异常：{ex.Message}");
            }
        }

        /// <summary>
        /// 获取已开始的第一条生产计划
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public MesProductPlan GetStartedProdPlan(out MesProductPlanDto productPlanDto)
        {
            try
            {

                productPlanDto = base._rep.AsQueryable()
                            .LeftJoin<MesProductOrder>((plan, order) => plan.ProductOrderId == order.ProductOrderId)
                            .LeftJoin<BaseMaterialInfo>((plan, order, material) => plan.MaterialId == material.MaterialId)
                            .Where(plan => plan.StationId == _appConfig.stationId && plan.PlanStatus == PlanStatusEnum.已开始)
                            .Select((plan, order, material) => new MesProductPlanDto
                            {
                                PlanId = plan.PlanId,
                                PlanCode = plan.PlanCode,
                                MaterialId = plan.MaterialId,
                                PlanAmount = plan.PlanAmount,
                                CompleteAmount = plan.CompleteAmount,
                                PlanBeginTime = plan.PlanBeginTime,
                                PlanEndTime = plan.PlanEndTime,
                                RealBeginTime = plan.RealBeginTime,
                                RealEndTime = plan.RealEndTime,
                                PlanStatus = plan.PlanStatus,
                                OrderCode = order.OrderCode,
                                SaleOrderId = order.SaleOrderId,
                                SaleorderCode = order.SaleorderCode,
                                SaleorderLinenumber = order.SaleorderLinenumber,
                                ProjectNo = order.ProjectNo,
                                MaterialName = material.MaterialName,
                                MaterialCategories = material.MaterialCategories,
                                MaterialSubclass = material.MaterialSubclass,
                                MaterialTypeId = material.MaterialTypeId,
                                BatchFlag = material.BatchFlag,
                                MaterialUnit = material.MaterialUnit,
                                MaterialUnitId = material.MaterialUnitId,
                            }).First();

                return base._rep.GetFirst(x => x.StationId == _appConfig.stationId && x.PlanStatus == PlanStatusEnum.已开始);

            }
            catch (Exception e)
            {
                throw new InvalidOperationException($"获取已开始的第一条生产计划异常：{e.Message}");
            }
        }

        /// <summary>
        /// 根据订单编号获取生产计划
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException"></exception>
        public List<MesProductPlan> GetProdPlansByOrderCode(int orderId)
        {
            try
            {
                return base._rep.GetList(x => x.ProductOrderId == orderId && x.StationId == _appConfig.stationId);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"根据订单编号获取生产计划异常：{ex.Message}");
            }
        }

        /// <summary>
        /// 更新计划信息
        /// </summary>
        /// <param name="prodPlan"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool UpdateProdPlan(MesProductPlan prodPlan)
        {
            try
            {
                return base._rep.Update(prodPlan);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"更新计划信息异常：{ex.Message}");
            }
        }

        /// <summary>
        /// 获取MES计划详细信息
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<MesProductPlanDto> GetPlanDtos()
        {
            try
            {
                var info = base._rep.AsQueryable()
                            .LeftJoin<MesProductOrder>((plan, order) => plan.ProductOrderId == order.ProductOrderId)
                            .LeftJoin<BaseMaterialInfo>((plan, order, material) => plan.MaterialId == material.MaterialId)
                            .Where(plan => plan.StationId == _appConfig.stationId && plan.PlanStatus != PlanStatusEnum.已完成)
                            .Select((plan, order, material) => new MesProductPlanDto
                            {
                                PlanId = plan.PlanId,
                                PlanCode = plan.PlanCode,
                                MaterialId = plan.MaterialId,
                                PlanAmount = plan.PlanAmount,
                                CompleteAmount = plan.CompleteAmount,
                                PlanBeginTime = plan.PlanBeginTime,
                                PlanEndTime = plan.PlanEndTime,
                                RealBeginTime = plan.RealBeginTime,
                                RealEndTime = plan.RealEndTime,
                                PlanStatus = plan.PlanStatus,
                                OrderCode = order.OrderCode,
                                SaleOrderId = order.SaleOrderId,
                                SaleorderCode = order.SaleorderCode,
                                SaleorderLinenumber = order.SaleorderLinenumber,
                                ProjectNo = order.ProjectNo,
                                MaterialName = material.MaterialName,
                                MaterialCategories = material.MaterialCategories,
                                MaterialSubclass = material.MaterialSubclass,
                                MaterialTypeId = material.MaterialTypeId,
                                BatchFlag = material.BatchFlag,
                                MaterialUnit = material.MaterialUnit,
                                MaterialUnitId = material.MaterialUnitId,
                            }).ToList();
                return info;
            }catch(Exception ex)
            {
                throw new InvalidOperationException($"获取MES计划详细信息异常:{ex.Message}");
            }
        }
    }
}
