using DbCommon.BusinessCore.BaseCore;
using DbCommon.Enties.DbModels;
using Pms.DataLibrary.Order;
using ProjectManagementSystem.Common.Core;
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_63003 : BaseTaskBookBehaviorV2
    {
        LocationPropertyManager locationManager = new LocationPropertyManager();
        MaterialBindingManager materialBindingManager = new MaterialBindingManager();

        public TaskBookBehavior_63003() : base(63003, "库位放货")
        {
        }

        public override bool TaskBookOperator(TaskData taskDetailInfo, StepData stepInfo)
        {
            try
            {
                var materialBinding = materialBindingManager.GetById(taskDetailInfo.TaskID);
                if (materialBinding == null)
                {
                    Log(taskDetailInfo, stepInfo, $"获取物料绑定数据失败");
                    return false;
                }

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

                data.MaterialBarcode = materialBinding.MaterialBarcode;
                data.MaterialId = materialBinding.MaterialID;
                data.MaterialName = materialBinding.MaterialName;
                data.MaterialNumber = materialBinding.MaterialNumber;
                data.MaterialWeight = materialBinding.MaterialWeight;
                data.BatchNumber = materialBinding.BatchNumber;
                data.CurrentTaskID = string.Empty;

                data.Status = LocationStatus.Filled;
                //记录入库时间
                data.ModifyTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                bool result = locationManager.Update(data);
                if (!result)
                {
                    Log(taskDetailInfo, stepInfo, $"更新库位数据失败");
                    return false;
                }

                materialBinding.EndWareHouse = data.WareHouseCode;
                materialBinding.EndLocation = data.LocationCode;
                materialBinding.PlaceTime = DateTime.Now;
                result = materialBindingManager.Update(materialBinding);
                if (!result)
                {
                    Log(taskDetailInfo, stepInfo, $"更新物料绑定数据失败");
                    return false;
                }

                materialBindingManager.DeleteOldData(30);
                Log(taskDetailInfo, stepInfo, $"库位放货，{materialBinding.MaterialID} 操作结果:{result}");
                return result;
            }
            catch (Exception ex)
            {
                LogException(ex);
            }
            return false;
        }
    }
}
