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

namespace ProjectManagementSystem.TaskBookEvent
{
    public class TaskBookBehavior_63032 : BaseTaskBookBehaviorV2
    {

        public TaskBookBehavior_63032() : base(63032, "空架检查（成品发动机）")
        {
        }

        public override bool TaskBookOperator(TaskData taskDetailInfo, StepData stepInfo)
        {
            try
            {
                var freightList = PmsApi.GetFreights(taskDetailInfo.Carrier);
                if(freightList == null)
                {
                    Log(taskDetailInfo, stepInfo, $"获取freightList失败");
                    return false;
                }
                if (freightList.Count < 7)
                {
                    Log(taskDetailInfo, stepInfo, $"获取freightList数量少于7 {JsonConvert.SerializeObject(freightList)}");
                    return false;
                }

                if(freightList.First().BHasFreight == false)
                {
                    Log(taskDetailInfo, stepInfo, $"未检测到空架 {string.Join("-", freightList.Select(d => d.BHasFreight ? 1:0))}");
                    return false;
                }

                if (freightList.Skip(1).Any(d => d.BHasFreight == true))
                {
                    Log(taskDetailInfo, stepInfo, $"检测到有发动机 {string.Join("-", freightList.Select(d => d.BHasFreight ? 1 : 0))}");
                    return false;
                }

                bool result = true;
                Log(taskDetailInfo, stepInfo, $"空架检查 {result.ToChineseString()} {string.Join("-", freightList.Select(d => d.BHasFreight ? 1 : 0))}");
                return result;
            }
            catch (Exception ex)
            {
                LogException(ex);
            }
            return false;
        }
    }
}
