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
{
    /// <summary>
    /// 检查电梯是否有货
    /// </summary>
    public class TaskBookBehavior_61009 : BaseTaskBookBehavior
    {
        public TaskBookBehavior_61009() : base(61009)
        {

        }
        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)
                {
                    var result = session.QueryOver<WareHouseLocation>().Where(p => p.WareHouseCode == "002" && p.Status == "1").List().OrderBy(p => p.Attribute2).ThenByDescending(p => p.Attribute3);//.;//
                    List<WareHouseLocation> list = result.ToList();
                    if (list.Count() > 0)
                    {
                        curWareHouse = list.First().LocationCode;
                        pass = true;
                    }
                }
                if (pass)
                {
                    successed = true;
                    TextDocument.WriteFile(@"PMS/Log", "选择5楼空电梯库位下料61009", string.Format("选择目标库位：{0}", curWareHouse));

                }
                
                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", "选择5楼空电梯库位下料61009", string.Format("异常：{0}", ex.ToString()));
            }
            return successed;
        }

       
    }
}

