using SlnMesnac.Model.domain;
using SlnMesnac.Repository.service;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SlnMesnac.Business.business
{
    public class DatabaseHandleBusniess
    {
        private ProdPlanInfoService _prodPlanInfoService;
        private ProdPlanExecuteUserService _prodPlanExecuteUserService;
        private ProdOrderInfoService _prodOrderInfoService;
        private ProdPlanDetailService _prodPlanDetailService;
        private IRecordStaffRealTimeService _recordStaffRealTimeService;
        private List<ProdPLanInfo> prodPlanInfos;
       
        public DatabaseHandleBusniess(ProdPlanInfoService prodPlanInfoService, ProdOrderInfoService prodOrderInfoService, 
            ProdPlanExecuteUserService prodPlanExecuteUserService, IRecordStaffRealTimeService recordStaffRealTimeService, 
            ProdPlanDetailService prodPlanDetailService)
        {
            _prodPlanInfoService = prodPlanInfoService;
            _prodOrderInfoService = prodOrderInfoService;
            prodPlanInfos = _prodPlanInfoService.GetRecordStaffAttendances();
            _prodPlanExecuteUserService = prodPlanExecuteUserService;
            _recordStaffRealTimeService = recordStaffRealTimeService;
            _prodPlanDetailService = prodPlanDetailService;
        }

        /// <summary>
        ///  根据订单编号查询订单信息
        /// </summary>
        /// <param name="orderCode"></param>
        /// <returns></returns>
        public ProdOrderInfo GetProdOrderInfoByOrderCode(string orderCode)
        {
            return _prodOrderInfoService.GetProdOrderInfoByOrderCode(orderCode);
        }

        /// <summary>
        /// 添加新的工单信息
        /// </summary>
        /// <param name="orderCode"></param>
        /// <param name="stationCode"></param>
        public void AddNewPlanInfo(string? orderCode,string? stationCode,string? deviceCode,string? processCode,string? importFlag)
        {
            var orderInfo = GetProdOrderInfoByOrderCode(orderCode);
            if (orderInfo != null)
            {
                string randomString = GenerateRandomString(14);
                _prodPlanInfoService.Insert(new ProdPLanInfo()
                {
                    PlanCode = orderCode + randomString,
                    OrderCode = orderCode,
                    MaterialCode = orderInfo.MaterialCode,
                    MaterialName = orderInfo.MaterialName,
                    BeginTime =DateTime.Now.ToString(),
                    StationCode = stationCode,
                    DeviceCode = deviceCode,
                    PlanAmount = orderInfo.OrderAmount,
                    CompleteAmount = orderInfo.CompleteAmount,
                    PlanStatus = "4",
                    ProcessCode = processCode,
                    ImportFlag = importFlag
                });
            }           
        }
        
        /// <summary>
        /// 生成随机字符串
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        private string GenerateRandomString(int length)
        {
            const string chars = "abcdefghijklmnopqrstuvwxyz0123456789";
            Random random = new Random();
            StringBuilder result = new StringBuilder();

            for (int i = 0; i < length; i++)
            {
                result.Append(chars[random.Next(chars.Length)]);
            }

            return result.ToString();
        }

        /// <summary>
        /// 更新订单状态
        /// </summary>
        /// <param name="orderCode"></param>
        /// <param name="stationCode"></param>
        /// <param name="deviceCode"></param>
        /// <param name="processCode"></param>
        /// <param name="v"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void UpdatePlanStatus(string orderCode, string stationCode, string deviceCode, string processCode, string status)
        {
            _prodPlanInfoService.UpdatePlanStatus(orderCode, stationCode, deviceCode, processCode, status);
        }

        /// <summary>
        /// 插入工单执行人员信息
        /// </summary>
        public void InsertPlanExecuteUser(ProdPLanInfo prodPlanInfo,ProdPlanDetail prodPlanDetail,List<RecordStaffRealTime> realTimes)
        {
            string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            foreach (var item in realTimes)
            {
                ProdPlanExecuteUser prodPlanExecuteUser = new ProdPlanExecuteUser()
                {
                    PlanCode = prodPlanInfo.PlanCode,
                    OrderCode = prodPlanInfo.OrderCode,
                    ProcessCode = prodPlanInfo.ProcessCode,
                    StationCode = prodPlanInfo.StationCode,
                    StaffId = item.StaffId,
                    CompleteAmount = prodPlanDetail.CompleteAmount,
                    PlanBeginDate = prodPlanDetail.BeginTime,
                    PlanEndDate = prodPlanDetail.EndTime,
                    CreatedBy = prodPlanInfo.CreatedBy,
                    CreatedTime = time,
                    UpdatedBy = prodPlanInfo.UpdatedBy,
                    UpdatedTime = prodPlanInfo.UpdatedTime,
                    BatchNumber = prodPlanDetail.BatchNumber,
                };
                _prodPlanExecuteUserService.Insert(prodPlanExecuteUser);                
            }
            _prodPlanDetailService.DeleteByPlanCodeBatchNumber(prodPlanDetail.PlanCode, 0);
        }

        /// <summary>
        /// 查询所有工单执行人员信息
        /// </summary>
        /// <returns></returns>
        public List<RecordStaffRealTime> GetRecordStaffRealTimes()
        {
            var list = _recordStaffRealTimeService.Query();
            return list;
        }

        /// <summary>
        /// 查询本条工单是否完成
        /// </summary>
        /// <param name="orderCode"></param>
        /// <param name="stationCode"></param>
        /// <param name="deviceCode"></param>
        /// <param name="processCode"></param>
        /// <param name="v"></param>
        /// <returns></returns>
        public ProdPLanInfo SelectPlanStatus(string orderCode, string stationCode, string deviceCode, string processCode, string v)
        {
            return _prodPlanInfoService.GetPlanInfoByConditions(orderCode,"","", stationCode,"0").FirstOrDefault();
        }

        /// <summary>
        /// 更新订单信息状态
        /// </summary>
        /// <param name="orderCode"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void UpdateOrderInfoStatus(string orderCode,string status)
        {
            _prodOrderInfoService.UpdateOrderInfoStatus(orderCode,status);
        }

        /// <summary>
        /// 删除对应批次的明细和执行人信息
        /// </summary>
        /// <param name="planCode"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void DeleteTheBatchDetailAndExecuter(string planCode,int batch)
        {
            if (planCode != "" && batch != 0)
            {
                bool result = _prodPlanDetailService.DeleteTheBatchNumberByPlanCode(planCode, batch);
                _prodPlanExecuteUserService.DeleteByPlanCodeAndTheBatchNumber(planCode, batch);
            }
        }

        /// <summary>
        /// 获取班长
        /// </summary>
        /// <param name="stationCode"></param>
        /// <returns></returns>
        public MonitorInfo GetMonitor(string stationCode)
        {
            MonitorInfo monitorInfo = new MonitorInfo();
            monitorInfo = _recordStaffRealTimeService.GetMonitorInfo(stationCode);
            return monitorInfo;
        }

        /// <summary>
        /// 获取最后一条明细的结束时间
        /// </summary>
        public ProdPlanDetail GetLastDetailEndTime(string planCode)
        {
            ProdPlanDetail prodPlanDetail = new ProdPlanDetail();
            prodPlanDetail = _prodPlanDetailService.GetLastPlanDetailByPlanCode(planCode);
            return prodPlanDetail;
        }
        ///// <summary>
        ///// 设置员工下班时间
        ///// </summary>
        //public void SetOffWorkTime(string staffId,string dateTime)
        //{
        //    _prodPlanExecuteUserService.SetExecuterOffWorkTime(staffId, dateTime);
        //}
        ///// <summary>
        ///// 查询所有计划工位
        ///// </summary>
        ///// <returns></returns>
        //public List<string> GetProductLineCodes()
        //{

        //    List<string> productLineCodes = new List<string>();
        //    foreach (var prodPlanInfo in prodPlanInfos)
        //    {
        //        productLineCodes.Add(prodPlanInfo.StationCode);
        //    }
        //    return productLineCodes;
        //}

        ///// <summary>
        ///// 查询所有工单编号
        ///// </summary>
        ///// <returns></returns>
        //public List<string> GetPlanCodes()
        //{

        //    List<string> planCodes = new List<string>();
        //    foreach (var prodPlanInfo in prodPlanInfos)
        //    {
        //        planCodes.Add(prodPlanInfo.PlanCode);
        //    }
        //    return planCodes;
        //}

        ///// <summary>
        ///// 查询所有订单编号
        ///// </summary>
        ///// <returns></returns>
        //public List<string> GetOrderCodes()
        //{

        //    List<string> orderCodes = new List<string>();
        //    foreach (var prodPlanInfo in prodPlanInfos)
        //    {
        //        orderCodes.Add(prodPlanInfo.OrderCode);
        //    }
        //    return orderCodes;
        //}

        ///// <summary>
        ///// 查询所有物料名称
        ///// </summary>
        ///// <returns></returns>
        //public List<string> GetMaterialNames()
        //{

        //    List<string> materialNames = new List<string>();
        //    foreach (var prodPlanInfo in prodPlanInfos)
        //    {
        //        materialNames.Add(prodPlanInfo.MaterialName);
        //    }
        //    return materialNames;
        //}
    }
}
