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

namespace ProjectManagementSystem.TaskEvent
{
    public class ProcessDoubleRollerTask : ICustomProcess
    {
        private string plcCallColName = "PLC叫料信号";

        private List<RouteConfig> routeConfigs = ExcelConfig.Instance.RouteConfigList.Where(d => d.WareHouseCode == "双层滚筒").ToList();

        public void CustomProcess(List<TaskData> taskDataList)
        {
            int taskCount = taskDataList.Count(d => d.TaskBook.StartsWith("双层滚筒"));
            if(taskCount >= 2)
            {
                return;
            }

            for (int i = 0; i < routeConfigs.Count; i++)
            {
                var config = routeConfigs[i];

                string location1 = config.LocationCode;
                string location2 = config.LocationCode.GetLocationMember("续接任务放货点");
                if (string.IsNullOrEmpty(location2))
                {
                    continue;
                }

                string plcCallAddr = location1.GetLocationMember(plcCallColName);
                if (string.IsNullOrEmpty(plcCallAddr))
                {
                    continue;
                }

                var plcCalled = DeviceControl.Instance.GetInputIO(config.IOTransID, plcCallAddr);
                if (!plcCalled.Item1
                    || !plcCalled.Item2)
                {
                    continue;
                }

                if(PmsTaskService.Instance.LocationHasStepTask(location1))
                {
                    //已有任务
                    continue;
                }

                PmsTaskInfoDto data = new PmsTaskInfoDto();
                data.LocationCode = location1;
                data.TargetLocationCode = location2;

                PmsTaskService.Instance.TaskAddNoCheck(data, new string[] { });
            }

        }
    }
}
