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;
using Pms.WebHandle;

namespace ProjectManagementSystem.TaskBookEvent
{
    /// <summary>
    /// 上报送货完成
    /// </summary>
    public class TaskBookBehavior_62002 : BaseTaskBookBehavior
    {
        public TaskBookBehavior_62002() : base(62002)
        {

        }
        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;
            string[] values = taskDetailInfo.TaskID.Split('_');
            string tempOrderId = values != null & values.Length > 1 ? values[1] : taskDetailInfo.TaskID;
            try
            {
                bool pass = false;
                using (var session = MiddleDBModelHelper.Session)
                {
                    var result = session.QueryOver<Order>().Where(p => p.OrderID == taskDetailInfo.TaskID && p.OrderState != (int)WCSOrderState.Downloaded).List();
                    if (result.Count > 0)
                    {
                        Order order = result.First();
                        order.OrderState = (int)WCSOrderState.Downloaded;
                        order.Suspend = false;
                        session.Update(order);
                        session.Flush();
                        return false;
                    }
                }
                //int skcarrier = GlobalData.GetSKCarrier(taskDetailInfo.Carrier);
                //TaskState taskState = new TaskState();
                //taskState.taskid = tempOrderId;
                //taskState.taskstate = (int)WCSOrderState.Downloaded;
                //taskState.faultcode = 0;//
                //taskState.agvid = skcarrier.ToString();
                //taskState.updatetime = DateTime.Now.ToString("G");
                //TaskStateResponse taskStateResponse = SKMESWebServiceHelper.UpdateTaskState(taskState);
                //if (taskStateResponse.code == "0")
                {
                    successed = true;
                }

            }
            catch (Exception ex)
            {
                TextDocument.WriteFile(@"PMS/Log", "62002", string.Format("异常：{0}", ex.ToString()));
            }
            return successed;
        }
    }
}


