using DbCommon.BusinessCore.BaseCore;
using DbCommon.Enties.DbModels;
using DbCommon.Extenions;
using PmsSecondaryPackaging.Interface.Model.Carrier;
using PmsSecondaryPackaging.Interface.Model.StepBook;
using PmsSecondaryPackaging.Interface.Model.TaskBook;
using PmsSecondaryPackaging.TaskManager.TaskBookBehavior;
using ProjectManagementSystem.Common.Log;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

        public TaskBookBehavior_63002() : base(63002)
        {
        }

        public override bool TaskBookOperator(TaskDetailInfoModel taskDetailInfo, List<StepDetailInfoModel> taskStepInfoList, CarrierInfoModel carrierInfo)
        {
            try
            {
                var stepInfo = TaskBookRegister.GetCurrentStepDetailInfo(taskDetailInfo, taskStepInfoList);
                var data = locationManager.QueryData(stepInfo.WareHouseID);

                MaterialBinding materialBinding = new MaterialBinding();
                materialBinding.TaskID = taskDetailInfo.TaskID;
                materialBinding.CarrierID = carrierInfo.AgvID;
                materialBinding.MaterialBarcode = data.MaterialBarcode;
                materialBinding.MaterialID = data.MaterialId;
                materialBinding.MaterialName = data.MaterialName;
                materialBinding.MaterialNumber = data.MaterialNumber.ToInt();
                materialBinding.MaterialWeight = data.MaterialWeight.ToDouble();
                materialBinding.BatchNumber = data.BatchNumber;
                if(!materialBindingManager.Save(materialBinding))
                {
                    return false;
                }

                data.Status = LocationStatus.Empty;
                data.MaterialId = string.Empty;
                bool result = locationManager.Update(data);
                TaskBookRegister.Log(taskDetailInfo, stepInfo, $"库位取货，操作结果:{result}");
                return result;
            }
            catch (Exception ex)
            {
                TaskBookRegister.LogException(ex);
            }
            return false;
        }
    }
}
