using DbCommon.BusinessCore.BaseCore;
using DbCommon.Enties.DbModels;
using Pms.DataLibrary.Order;
using ProjectManagementSystem.Common.Core;
using ProjectManagementSystem.Common.Extenions;
using ProjectManagementSystem.Common.Logger;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectManagementSystem.TaskBookEvent
{
    public class TaskBookBehavior_63002 : BaseTaskBookBehaviorV2
    {
        LocationPropertyManager locationManager = new LocationPropertyManager();
        MaterialBindingManager materialBindingManager = new MaterialBindingManager();

        public TaskBookBehavior_63002() : base(63002, "库位取货")
        {
        }

        public override bool TaskBookOperator(TaskData taskDetailInfo, StepData stepInfo)
        {
            try
            {
                var data = locationManager.GetById(stepInfo.WareHouseID);
                if(data == null)
                {
                    Log(taskDetailInfo, stepInfo, $"获取库位数据失败");
                    return false;
                }

                MaterialBinding materialBinding = new MaterialBinding();
                materialBinding.TaskID = taskDetailInfo.TaskID;
                materialBinding.CarrierID = taskDetailInfo.Carrier;

                materialBinding.MaterialBarcode = data.MaterialBarcode;
                materialBinding.MaterialID = data.MaterialId;
                materialBinding.MaterialName = data.MaterialName;
                materialBinding.MaterialNumber = data.MaterialNumber;
                materialBinding.MaterialWeight = data.MaterialWeight;
                materialBinding.BatchNumber = data.BatchNumber;
                materialBinding.ModifyTime = data.ModifyTime;

                materialBinding.TaskBook = taskDetailInfo.TaskBook;
                materialBinding.CreateTime = taskDetailInfo.CreateTime;
                materialBinding.StartTime = taskDetailInfo.StartTime;
                materialBinding.PickTime = DateTime.Now;
                materialBinding.StartWareHouse = data.WareHouseCode;
                materialBinding.StartLocation = data.LocationCode;

                if (!materialBindingManager.Save(materialBinding))
                {
                    return false;
                }

                data.Status = LocationStatus.Empty;
                data.MaterialBarcode = string.Empty;
                data.MaterialId = string.Empty;
                data.MaterialName = string.Empty;
                data.MaterialNumber = string.Empty;
                data.MaterialWeight = string.Empty;
                data.BatchNumber = string.Empty;
                data.CurrentTaskID = string.Empty;

                data.ModifyTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                bool result = locationManager.Update(data);

                if (result)
                {
                    //灭叫料盒灯
                    var config = Common.Config.ExcelConfig.Instance.GetRouteConfig(stepInfo.WareHouseID);
                    if(config != null
                        && config.Route > 0)
                    {
                        for (int i = 0; i < 3; i++)
                        {
                            Common.Core.DeviceControl.Instance.CallBoxLightOff(config.Route);
                            System.Threading.Thread.Sleep(100);
                        }
                    }

                    if (config != null)
                    {
                        int[] routes = config.LocationCode.GetLocationMember("放行叫料路线").ToValueArray<int>();
                        for (int idx = 0; idx < routes.Length; idx++)
                        {
                            int route = routes[idx];
                            for (int i = 0; i < 3; i++)
                            {
                                Common.Core.DeviceControl.Instance.CallBoxLightOff(route);
                                System.Threading.Thread.Sleep(100);
                            }
                        }

                    }
                }

                Log(taskDetailInfo, stepInfo, $"库位取货，物料：{materialBinding.MaterialID} 操作结果:{result}");
                return result;
            }
            catch (Exception ex)
            {
                LogException(ex);
            }
            return false;
        }
    }
}
