using MaxCommunication.DataBase.Document;
using PmsSecondaryPackaging.Interface.Model.Carrier;
using PmsSecondaryPackaging.Interface.Model.StepBook;
using PmsSecondaryPackaging.Interface.Model.TaskBook;
using PmsSecondaryPackaging.TaskManager.TaskBookBehavior;
using ProjectManagementSystem.NHibernateDBHelper;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using System;

namespace ProjectManagementSystem.TaskBookEvent
{
    public class TaskBookBehavior_61005 : BaseTaskBookBehavior
    {
        public TaskBookBehavior_61005() : base(61005)
        {

        }
        public override bool TaskBookSetting(TaskDetailInfoModel taskDetailInfo, List<StepDetailInfoModel> taskStepInfoList, CarrierInfoModel carrierInfo)
        {
            //IsEnable = false; Asynchronous = true;
            return base.TaskBookSetting(taskDetailInfo, taskStepInfoList, carrierInfo);
        }


        public override bool TaskBookTermination(TaskDetailInfoModel taskDetailInfo, List<StepDetailInfoModel> taskStepInfoList, CarrierInfoModel carrierInfo)
        {
            return base.TaskBookTermination(taskDetailInfo, taskStepInfoList, carrierInfo);
        }

        public override bool TaskBookOperator(TaskDetailInfoModel taskDetailInfo, List<StepDetailInfoModel> taskStepInfoList, CarrierInfoModel carrierInfo)
        {
            bool successed = false;
            StepDetailInfoModel currentStep = taskStepInfoList.Find(p => p.StepId == taskDetailInfo.CurrentStepID);
            string strCurWareHouse = currentStep.WareHouseID;
            try
            {
                bool pass = false;
                string curWareHouse = "";
                using (var session = WMSDBModelHelper.Session)
                {
                    //int curColumn = 0;
                    var result = session.QueryOver<WareHouseLocation>().Where(p => p.WareHouseCode == "003" && p.Status == "1").List().OrderBy(p => p.Attribute2).ThenByDescending(p=>p.Attribute3);//.;//
                    List<WareHouseLocation> list = result.ToList();
                    if (list.Count() > 0)
                    {
                        var columns = list.Select(p => p.Attribute2).Distinct();
                        foreach (string c in columns)
                        {
                            if (pass) break;
                            List<WareHouseLocation> listSub = list.FindAll(p => p.Attribute2 == c);
                            listSub = listSub.OrderByDescending(p => p.Attribute3).ToList();
                            string warehouse = listSub.First().LocationCode;
                            var ress = session.QueryOver<WareHouseLocation>().Where(p => p.WareHouseCode == "003" && p.Attribute2 == c && p.Status != "1").List();
                            if (ress.Count() <= 0)
                            {
                                //EFailCode failcode;
                                //if (controller.SetBookWarehouse(e.TaskBook.NextDefaultBookID, warehouse, out failcode))
                                //{
                                //    pass = true;
                                //    break;
                                //}
                                pass = true;
                                curWareHouse = warehouse;
                                break;
                                
                            }
                            else
                            {
                                string fullWareHouse = ress.First().LocationCode;//行号最小的非空库位
                                foreach (var sub in listSub)
                                {
                                    pass = true;
                                    curWareHouse = warehouse;
                                    break;
                                }
                                if (pass)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
                if(pass)
                {
                    AddTask(taskDetailInfo.TaskID, curWareHouse);
                    successed = true;
                    TextDocument.WriteFile(@"PMS/Log", "选择1楼空库位下料61005", string.Format("选择目标库位：{0}", curWareHouse));

                }
                //using (var session = WMSDBModelHelper.Session)
                //{
                //    var result = session.QueryOver<WareHouseLocation>().Where(p => p.WareHouseCode == "003"&& p.Status == "1").List();
                //    if (result.Count > 0)
                //    {
                //        WareHouseLocation order = result[0];
                //        string nextWareHouseLocation = order.LocationCode;
                //        AddTask(taskDetailInfo.TaskID, nextWareHouseLocation);
                //        session.Flush();
                //        successed = true;
                //        TextDocument.WriteFile(@"PMS/Log", "选择1楼空库位下料61005", string.Format("选择目标库位：{0}", nextWareHouseLocation));

                //    }

                //}
                bool resultTask = InterfaceService.GetTask(taskDetailInfo.TaskID, out TaskDetailInfoModel taskDetailInfo1, out string message);
                if (string.IsNullOrEmpty(message) && taskDetailInfo1 == null)
                {
                    successed = true;
                }
            }
            catch(Exception ex)
            {
                TextDocument.WriteFile(@"PMS/Log", "选择1楼空库位下料61005", string.Format("异常：{0}", ex.ToString()));
            }
            return successed;
        }

        /// <summary>
        /// 添加流程一的子流程2
        /// </summary>
        private void AddTask(string TaskID,string EmptyWareHouse)
        {
            TaskAddInfoModel taskAddInfoModel = new TaskAddInfoModel();
            taskAddInfoModel.TaskID = System.DateTime.Now.ToString("MMddhhmmssffff");
            taskAddInfoModel.TemplateName = "StandardTemplate21";
            taskAddInfoModel.AgvType = "0";
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            dictionary.Add("M", EmptyWareHouse);
            //dictionary.Add("vWrappingMachine01", "WrappingMachine01");
            taskAddInfoModel.ParametersDic = dictionary;
            taskAddInfoModel.ParentTaskId = TaskID;

            if (!InterfaceService.TaskAdd(taskAddInfoModel, out string Message))
            {
                TextDocument.WriteFile(@"PMS/Log", "选择1楼空库位下料61005", Message);
            }
            else
            {
                TextDocument.WriteFile(@"PMS/Log", "选择1楼空库位下料61005", "1楼下料任务添加成功！");
            }
            Thread.Sleep(1000);
        }
    }
}

