刘磊
2024-12-17 a040e5ae0239808e51fb78678f9e60922d11b930
同步
已添加8个文件
783 ■■■■■ 文件已修改
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_BasicInfoRepository/Dt_needBarcodeRepository.cs 29 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_BasicInfoService/Dt_needBarcodeService.cs 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_IBasicInfoRepository/IDt_needBarcodeRepository.cs 32 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_IBasicInfoService/IDt_needBarcodeService.cs 29 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Model/Models/BasicInfo/dt_needBarcode.cs 72 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/ConveyorLine/CommonConveyorLine_BZ.cs 342 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Server/Controllers/Task/Task_HtyController.cs 25 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob_BZ/CommonConveyorLine_BZJob.cs 239 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_BasicInfoRepository/Dt_needBarcodeRepository.cs
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,29 @@
#region << ç‰ˆ æœ¬ æ³¨ é‡Š >>
/*----------------------------------------------------------------
 * å‘½åç©ºé—´ï¼šWIDESEAWCS_TaskInfoRepository
 * åˆ›å»ºè€…:胡童庆
 * åˆ›å»ºæ—¶é—´ï¼š2024/8/2 16:13:36
 * ç‰ˆæœ¬ï¼šV1.0.0
 * æè¿°ï¼š
 *
 * ----------------------------------------------------------------
 * ä¿®æ”¹äººï¼š
 * ä¿®æ”¹æ—¶é—´ï¼š
 * ç‰ˆæœ¬ï¼šV1.0.1
 * ä¿®æ”¹è¯´æ˜Žï¼š
 *
 *----------------------------------------------------------------*/
#endregion << ç‰ˆ æœ¬ æ³¨ é‡Š >>
using WIDESEAWCS_Core.BaseRepository;
using WIDESEAWCS_Model.Models;
namespace WIDESEAWCS_BasicInfoRepository
{
    public class Dt_needBarcodeRepository : RepositoryBase<dt_needBarcode>, IDt_needBarcodeRepository
    {
        public Dt_needBarcodeRepository(IUnitOfWorkManage unitOfWorkManage) : base(unitOfWorkManage)
        {
        }
    }
}
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_BasicInfoService/Dt_needBarcodeService.cs
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,15 @@
using WIDESEAWCS_BasicInfoRepository;
using WIDESEAWCS_Core.BaseServices;
using WIDESEAWCS_ISystemServices;
using WIDESEAWCS_Model.Models;
namespace WIDESEAWCS_BasicInfoService
{
    public partial class dt_needBarcodeService : ServiceBase<dt_needBarcode, IDt_needBarcodeRepository>, Idt_needBarcodeService
    {
        public dt_needBarcodeService(IDt_needBarcodeRepository BaseDal) : base(BaseDal)
        {
        }
    }
}
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_IBasicInfoRepository/IDt_needBarcodeRepository.cs
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,32 @@
#region << ç‰ˆ æœ¬ æ³¨ é‡Š >>
/*----------------------------------------------------------------
 * å‘½åç©ºé—´ï¼šWIDESEAWCS_ITaskInfoRepository
 * åˆ›å»ºè€…:胡童庆
 * åˆ›å»ºæ—¶é—´ï¼š2024/8/2 16:13:36
 * ç‰ˆæœ¬ï¼šV1.0.0
 * æè¿°ï¼š
 *
 * ----------------------------------------------------------------
 * ä¿®æ”¹äººï¼š
 * ä¿®æ”¹æ—¶é—´ï¼š
 * ç‰ˆæœ¬ï¼šV1.0.1
 * ä¿®æ”¹è¯´æ˜Žï¼š
 *
 *----------------------------------------------------------------*/
#endregion << ç‰ˆ æœ¬ æ³¨ é‡Š >>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Core.BaseRepository;
using WIDESEAWCS_Model.Models;
namespace WIDESEAWCS_BasicInfoRepository
{
    public interface IDt_needBarcodeRepository : IRepository<dt_needBarcode>
    {
    }
}
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_IBasicInfoService/IDt_needBarcodeService.cs
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,29 @@
#region << ç‰ˆ æœ¬ æ³¨ é‡Š >>
/*----------------------------------------------------------------
 * å‘½åç©ºé—´ï¼šWIDESEAWCS_ITaskInfoService
 * åˆ›å»ºè€…:胡童庆
 * åˆ›å»ºæ—¶é—´ï¼š2024/8/2 16:13:36
 * ç‰ˆæœ¬ï¼šV1.0.0
 * æè¿°ï¼š
 *
 * ----------------------------------------------------------------
 * ä¿®æ”¹äººï¼š
 * ä¿®æ”¹æ—¶é—´ï¼š
 * ç‰ˆæœ¬ï¼šV1.0.1
 * ä¿®æ”¹è¯´æ˜Žï¼š
 *
 *----------------------------------------------------------------*/
#endregion << ç‰ˆ æœ¬ æ³¨ é‡Š >>
using WIDESEAWCS_Core.BaseServices;
using WIDESEAWCS_Model.Models;
namespace WIDESEAWCS_BasicInfoService
{
    public interface Idt_needBarcodeService : IService<dt_needBarcode>
    {
    }
}
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Model/Models/BasicInfo/dt_needBarcode.cs
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,72 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
using WIDESEAWCS_Core.DB.Models;
namespace WIDESEAWCS_Model.Models
{
    public class dt_needBarcode : BaseEntity
    {
        [Key]
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true, ColumnDescription = "主键")]
        public int id { get; set; }
        /// <summary>
        /// ç›®æ ‡åº“区
        /// </summary>
        [SugarColumn(IsNullable = false, Length = 20, ColumnDescription = "目标库区")]
        public string toArea { get; set; }
        /// <summary>
        /// æ¥æºåº“区
        /// </summary>
        [SugarColumn(IsNullable = false, Length = 10, ColumnDescription = "来源库区")]
        public string fromArea { get; set; }
        /// <summary>
        /// æ‰˜ç›˜ç±»åž‹
        /// </summary>
        [SugarColumn(IsNullable = false, Length = 10, ColumnDescription = "托盘类型")]
        public string barcodeType { get; set; }
        /// <summary>
        /// äº§çº¿
        /// </summary>
        [SugarColumn(IsNullable = false, Length = 10, ColumnDescription = "所属产线")]
        public string productLine {  get; set; }
        /// <summary>
        /// åœ¨é€”数量
        /// </summary>
        [SugarColumn(IsNullable = false, Length = 10, ColumnDescription = "在途数量")]
        public int inLineNum { get; set; }
        /// <summary>
        /// ç›®æ ‡ç¼“存数量
        /// </summary>
        [SugarColumn(IsNullable = false, Length = 10, ColumnDescription = "可缓存数量")]
        public int cacheNum { get; set; } = 0;
        /// <summary>
        /// å·²åˆ›å»ºå‡ºåº“任务数量
        /// </summary>
        [NotMapped]
        public int haveOutNum { get; set; } = 0;
        /// <summary>
        /// å¯å…¥æ•°é‡
        /// </summary>
        [NotMapped]
        public int canInNum { get; set; } = 0;
        /// <summary>
        /// å·²åˆ›å»ºå‡ºåº“任务
        /// </summary>
        [NotMapped]
        public List<Dt_Task> haveOutList { get; set; }
    }
}
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/ConveyorLine/CommonConveyorLine_BZ.cs
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,342 @@
#region << ç‰ˆ æœ¬ æ³¨ é‡Š >>
/*----------------------------------------------------------------
 * å‘½åç©ºé—´ï¼šWIDESEAWCS_QuartzJob
 * åˆ›å»ºè€…:胡童庆
 * åˆ›å»ºæ—¶é—´ï¼š2024/8/2 16:13:36
 * ç‰ˆæœ¬ï¼šV1.0.0
 * æè¿°ï¼šä¸€èˆ¬è¾“送线实现类
 *
 * ----------------------------------------------------------------
 * ä¿®æ”¹äººï¼š
 * ä¿®æ”¹æ—¶é—´ï¼š
 * ç‰ˆæœ¬ï¼šV1.0.1
 * ä¿®æ”¹è¯´æ˜Žï¼š
 *
 *----------------------------------------------------------------*/
#endregion << ç‰ˆ æœ¬ æ³¨ é‡Š >>
using HslCommunication;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Communicator;
using WIDESEAWCS_QuartzJob.ConveyorLine.Enum;
using WIDESEAWCS_QuartzJob.DeviceBase;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
namespace WIDESEAWCS_QuartzJob
{
    [Description("输送线")]
    public class CommonConveyorLine_BZ : IConveyorLine
    {
        #region Private Member
        /// <summary>
        /// å †åž›æœºé€šè®¯å¯¹è±¡
        /// </summary>
        private readonly BaseCommunicator _communicator;
        /// <summary>
        /// å †åž›æœºåè®®ä¿¡æ¯
        /// </summary>
        private readonly List<DeviceProDTO> _deviceProDTOs;
        /// <summary>
        /// å †åž›æœºåè®®æ˜Žç»†ä¿¡æ¯
        /// </summary>
        private readonly List<DeviceProtocolDetailDTO> _deviceProtocolDetailDTOs;
        /// <summary>
        /// è®¾å¤‡ç¼–号
        /// </summary>
        public readonly string _deviceCode;
        /// <summary>
        /// è®¾å¤‡åç§°
        /// </summary>
        public readonly string _deviceName;
        private bool _heartStatr = true;
        private bool _isConnected = true;
        #endregion
        #region Public Member
        /// <summary>
        /// è¾“送线通讯对象
        /// </summary>
        public BaseCommunicator Communicator => _communicator;
        /// <summary>
        /// è¾“送线协议信息
        /// </summary>
        public List<DeviceProDTO> DeviceProDTOs => _deviceProDTOs;
        /// <summary>
        /// è¾“送线协议明细信息
        /// </summary>
        public List<DeviceProtocolDetailDTO> DeviceProtocolDetailDTOs => _deviceProtocolDetailDTOs;
        /// <summary>
        /// è®¾å¤‡ç¼–号
        /// </summary>
        public string DeviceCode => _deviceCode;
        /// <summary>
        /// è®¾å¤‡åç§°
        /// </summary>
        public string DeviceName => _deviceName;
        /// <summary>
        /// æ˜¯å¦æœ‰æ•…éšœ
        /// </summary>
        public bool IsFault => false;
        /// <summary>
        /// é€šè®¯æ˜¯å¦å·²è¿žæŽ¥
        /// </summary>
        public bool IsConnected => Communicator.IsConnected && _isConnected;
        /// <summary>
        /// è®¾å¤‡çŠ¶æ€
        /// </summary>
        public DeviceStatus Status => DeviceStatus.Offline;
        #endregion
        #region Constructor Function
        /// <summary>
        /// æž„造函数
        /// </summary>
        /// <param name="communicator">堆垛机通讯对象</param>
        /// <param name="deviceProDTOs">堆垛机协议信息</param>
        /// <param name="deviceProtocolDetailDTOs">堆垛机协议明细信息</param>
        /// <param name="deviceCode">设备编号</param>
        /// <param name="deviceName">设备名称</param>
        public CommonConveyorLine_BZ(BaseCommunicator communicator, List<DeviceProDTO> deviceProDTOs, List<DeviceProtocolDetailDTO> deviceProtocolDetailDTOs, string deviceCode, string deviceName)
        {
            _communicator = communicator;
            _deviceProDTOs = deviceProDTOs;
            _deviceProtocolDetailDTOs = deviceProtocolDetailDTOs;
            _deviceCode = deviceCode;
            _deviceName = deviceName;
            CheckConnect();
        }
        #endregion
        #region Private Method
        private void CheckConnect()
        {
            Task.Run(() =>
            {
                while (_heartStatr)
                {
                    try
                    {
                        DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault();
                        if (devicePro == null)
                            _isConnected = false;
                        else
                            Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType);
                        _isConnected = true;
                    }
                    catch (Exception ex)
                    {
                        _isConnected = false;
                    }
                    Thread.Sleep(500);
                }
            });
        }
        #endregion
        #region Public Method
        /// <summary>
        /// è¯»å–PLC协议地址的数据
        /// </summary>
        /// <typeparam name="TEnum">协议信息的枚举对象信息。</typeparam>
        /// <typeparam name="TRsult">读取数据的类型对象信息。</typeparam>
        /// <param name="value">枚举值</param>
        /// <param name="deviceChildCode">设备子编号</param>
        /// <returns>读取到的数据</returns>
        public TRsult GetValue<TEnum, TRsult>(TEnum value, string deviceChildCode) where TEnum : Enum
        {
            if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
            DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == value.ToString() && x.DeviceChildCode == deviceChildCode);
            return devicePro == null ? throw new Exception() : (TRsult)Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType);
        }
        /// <summary>
        /// ä¸Žè®¾å¤‡çš„心跳
        /// </summary>
        public void Heartbeat()
        {
            throw new NotImplementedException();
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="command"></param>
        /// <param name="deviceChildCode"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public bool SendCommand<T>(T command, string deviceChildCode) where T : IDataTransfer, new()
        {
            if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
            DeviceProDTO? devicePro = _deviceProDTOs.Where(x => x.DeviceProParamType == nameof(DeviceCommand) && x.DeviceChildCode == deviceChildCode).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
            if (devicePro == null)
            {
                return false;
            }
            if (Communicator.WriteCustomer(devicePro.DeviceProAddress, command))
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// è¯»å–PLC数据,返回自定义对象
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="deviceChildCode">子设备编号</param>
        /// <returns>返回自定义对象或抛出异常</returns>
        /// <exception cref="Exception"></exception>
        public T ReadCustomer<T>(string deviceChildCode) where T : IDataTransfer, new()
        {
            if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
            DeviceProDTO? devicePro = _deviceProDTOs.Where(x => x.DeviceProParamType == "DeviceCommand" && x.DeviceChildCode == deviceChildCode).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
            if (devicePro == null)
            {
                throw new Exception($"【{_deviceCode}】--未找到【{deviceChildCode}】协议信息");
            }
            else
            {
                return Communicator.ReadCustomer<T>(devicePro.DeviceProAddress);
            }
        }
        /// <summary>
        /// è¯»å–PLC数据,返回自定义对象
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="deviceChildCode">子设备编号</param>
        /// <param name="deviceProParamType">参数类型</param>
        /// <returns>返回自定义对象或抛出异常</returns>
        /// <exception cref="Exception"></exception>
        public T ReadCustomer<T>(string deviceChildCode, string deviceProParamType) where T : IDataTransfer, new()
        {
            if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
            DeviceProDTO? devicePro = _deviceProDTOs.Where(x => x.DeviceProParamType == deviceProParamType && x.DeviceChildCode == deviceChildCode).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
            if (devicePro == null)
            {
                throw new Exception($"未找到【{deviceChildCode}】协议信息");
            }
            else
            {
                return Communicator.ReadCustomer<T>(devicePro.DeviceProAddress);
            }
        }
        /// <summary>
        /// æ ¹æ®å‚数名称、设备子编号写入对应的数据。
        /// </summary>
        /// <typeparam name="TEnum">参数名称枚举类型。</typeparam>
        /// <typeparam name="TValue">要写入的数据类型。</typeparam>
        /// <param name="enum">参数名称。</param>
        /// <param name="value">要写入的数据。</param>
        /// <param name="deviceChildCode">设备子编号写</param>
        /// <returns>返回写入成功或失败</returns>
        public bool SetValue<TEnum, TValue>(TEnum @enum, TValue value, string deviceChildCode)
            where TEnum : Enum
            where TValue : notnull
        {
            if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
            DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == @enum.ToString() && x.DeviceChildCode == deviceChildCode);
            return devicePro == null ? throw new Exception() : Communicator.WriteObj(devicePro.DeviceProAddress, devicePro.DeviceDataType, value);
        }
        /// <summary>
        /// æ ¹æ®å‚数名称、设备子编号读取对应的数据。
        /// </summary>
        /// <typeparam name="TEnum">参数名称枚举类型。</typeparam>
        /// <param name="enum">参数名称。</param>
        /// <param name="deviceChildCode">设备子编号写</param>
        /// <returns>返回写入成功或失败</returns>
        public object ReadValue<TEnum>(TEnum @enum, string deviceChildCode)
            where TEnum : Enum
        {
            if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
            DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == @enum.ToString() && x.DeviceChildCode == deviceChildCode);
            return devicePro == null ? throw new Exception() : Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType);
        }
        //public bool IsOccupied(string deviceChildCode)
        //{
        //    if (Communicator.IsConnected)
        //    {
        //    }
        //}
        /// <summary>
        ///
        /// </summary>
        /// <param name="deviceChildCode"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public bool IsOccupied(string deviceChildCode)
        {
            if (Communicator.IsConnected)
            {
                List<DeviceProDTO> devicePros = _deviceProDTOs.Where(x => x.DeviceChildCode == deviceChildCode && x.DeviceProParamName == "InteractiveSignal").ToList();
                if (devicePros.Count == 0)
                {
                    //todo åè®®ä¿¡æ¯æœªèŽ·å–åˆ°æ—¶æŠ›å‡ºå¼‚å¸¸
                    throw new Exception();
                }
                for (int i = 0; i < devicePros.Count; i++)
                {
                    object readStatus = Communicator.ReadAsObj(devicePros[i].DeviceProAddress, devicePros[i].DeviceDataType);
                    //todo åè®®æ˜Žç»†ä¿¡æ¯æœªèŽ·å–åˆ°æ—¶æŠ›å‡ºå¼‚å¸¸
                    DeviceProtocolDetailDTO? deviceProtocolDetail = _deviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == devicePros[i].DeviceProParamName) ?? throw new Exception();
                    deviceProtocolDetail = _deviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == "InteractiveSignal" && x.ProtocalDetailValue.Equals(readStatus.ToString()));
                    if (deviceProtocolDetail != null)
                    {
                        return true;
                    }
                    return false;
                }
            }
            //todo é€šè®¯æœªè¿žæŽ¥æ—¶æŠ›å‡ºå¼‚常
            return false;
        }
        public void Dispose()
        {
            _heartStatr = false;
            _communicator.Dispose();
            GC.SuppressFinalize(this);
        }
        #endregion
    }
}
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Server/Controllers/Task/Task_HtyController.cs
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,25 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.BaseController;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_ITaskInfo_HtyService;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
namespace WIDESEAWCS_WCSServer.Controllers.Task
{
    [Route("api/Task_Hty")]
    [ApiController]
    public class Task_HtyController : ApiBaseController<ITask_HtyService, Dt_Task_Hty>
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        public Task_HtyController(ITask_HtyService service, IHttpContextAccessor httpContextAccessor) : base(service)
        {
            _httpContextAccessor = httpContextAccessor;
        }
    }
}
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob_BZ/CommonConveyorLine_BZJob.cs
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,239 @@
#region MyRegion
#region << ç‰ˆ æœ¬ æ³¨ é‡Š >>
/*----------------------------------------------------------------
 * å‘½åç©ºé—´ï¼šWIDESEAWCS_Tasks.ConveyorLineJob
 * åˆ›å»ºè€…:胡童庆
 * åˆ›å»ºæ—¶é—´ï¼š2024/8/2 16:13:36
 * ç‰ˆæœ¬ï¼šV1.0.0
 * æè¿°ï¼š
 *
 * ----------------------------------------------------------------
 * ä¿®æ”¹äººï¼š
 * ä¿®æ”¹æ—¶é—´ï¼š
 * ç‰ˆæœ¬ï¼šV1.0.1
 * ä¿®æ”¹è¯´æ˜Žï¼š
 *
 *----------------------------------------------------------------*/
#endregion << ç‰ˆ æœ¬ æ³¨ é‡Š >>
using AutoMapper;
using HslCommunication;
using Microsoft.AspNetCore.Server.HttpSys;
using Newtonsoft.Json;
using Quartz;
using SqlSugar;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Reflection;
using System.Threading.Tasks;
using WIDESEAWCS_BasicInfoRepository;
using WIDESEAWCS_BasicInfoService;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Caches;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.HttpContextUser;
using WIDESEAWCS_DTO.MOM;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_IProcessRepository;
using WIDESEAWCS_ISystemRepository;
using WIDESEAWCS_ISystemServices;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DeviceBase;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.Service;
using WIDESEAWCS_SignalR;
using WIDESEAWCS_Tasks.ConveyorLineJob;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
using ICacheService = WIDESEAWCS_Core.Caches.ICacheService;
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public partial class CommonConveyorLine_BZJob : JobBase, IJob
    {
        public readonly ITaskService _taskService;
        private readonly ITaskRepository _taskRepository;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly IRouterService _routerService;
        private readonly IPlatFormRepository _platFormRepository;
        private readonly ISys_ConfigService _sys_ConfigService;
        private readonly IMapper _mapper;
        private readonly IDt_StationManagerService _stationManagerService;
        private readonly IDt_StationManagerRepository _stationManagerRepository;
        private readonly ICacheService _cacheService;
        private readonly INoticeService _noticeService;
        private readonly IDt_needBarcodeRepository _needBarcodeRepository;
        public CommonConveyorLine_BZJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IMapper mapper, ITaskRepository taskRepository, IPlatFormRepository platFormRepository, ISys_ConfigService sys_ConfigService, IDt_StationManagerRepository stationManagerRepository, ICacheService cacheService, INoticeService noticeService, IDt_StationManagerService stationManagerService, IDt_needBarcodeRepository needBarcodeRepository)
        {
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _routerService = routerService;
            _mapper = mapper;
            _taskRepository = taskRepository;
            _platFormRepository = platFormRepository;
            _sys_ConfigService = sys_ConfigService;
            _stationManagerRepository = stationManagerRepository;
            _cacheService = cacheService;
            _noticeService = noticeService;
            _stationManagerService = stationManagerService;
            _needBarcodeRepository = needBarcodeRepository;
        }
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                CommonConveyorLine_BZ conveyorLine = (CommonConveyorLine_BZ)context.JobDetail.JobDataMap.Get("JobParams");
                if (conveyorLine != null)
                {
                    #region ç«™å°æ–¹å¼
                    List<Dt_StationManager> stationManagers = _stationManagerService.GetAllStationByDeviceCode(conveyorLine.DeviceCode);
                    foreach (var station in stationManagers)
                    {
                        ConveyorLineTaskCommand_After command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand_After>(station.stationChildCode);
                        DeviceProtocolDetailDTO? deviceProtocolDetails = conveyorLine.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(ConveyorLineTaskCommand_After.InteractiveSignal) && x.ProtocalDetailValue == command.InteractiveSignal.ToString());
                        if (deviceProtocolDetails != null)
                        {
                            MethodInfo? method = GetType().GetMethod(deviceProtocolDetails.ProtocolDetailType);
                            if (method != null)
                            {
                                method.Invoke(this, new object[] { conveyorLine, command, station });
                            }
                        }
                        #region è°ƒç”¨äº‹ä»¶æ€»çº¿é€šçŸ¥å‰ç«¯
                        var tokenInfos = _cacheService.Get<List<UserInfo>>("Cache_UserToken");
                        if (tokenInfos == null || !tokenInfos.Any())
                        {
                            //throw new Exception(conveyorLine.DeviceName + "缓存中未找到Token缓存");
                            continue;
                        }
                        var userTokenIds = tokenInfos?.Select(x => x.Token_ID).ToList();
                        var userIds = tokenInfos?.Select(x => x.UserId).ToList();
                        object obj = new
                        {
                            station.stationChildCode,
                            commandAfter = command,
                        };
                        _noticeService.StackerData(userIds?.FirstOrDefault(), userTokenIds, new { conveyorLine.DeviceName, data = obj });
                        #endregion è°ƒç”¨äº‹ä»¶æ€»çº¿é€šçŸ¥å‰ç«¯
                    }
                    #endregion
                    #region è·¯ç”±æ–¹å¼
                    //List<string> childDeviceCodes = _routerService.QueryAllPositions(conveyorLine.DeviceCode);
                    //foreach (string childDeviceCode in childDeviceCodes)
                    //{
                    //    ConveyorLineTaskCommand_After command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand_After>(childDeviceCode);
                    //    if (command == null) continue;
                    //    if (command.ConveyorLineBarcode.Trim().Contains("\0")) command.ConveyorLineBarcode = "";
                    //    DeviceProtocolDetailDTO? deviceProtocolDetails = conveyorLine.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(ConveyorLineTaskCommand_After.InteractiveSignal) && x.ProtocalDetailValue == command.InteractiveSignal.ToString());
                    //    if (deviceProtocolDetails != null)
                    //    {
                    //        MethodInfo? method = GetType().GetMethod(deviceProtocolDetails.ProtocolDetailType);
                    //        if (method != null)
                    //        {
                    //            method.Invoke(this, new object[] { conveyorLine, command, childDeviceCode });
                    //        }
                    //    }
                    //    if (childDeviceCode == "1670")
                    //    {
                    //        Platform platform = _platFormRepository.QueryFirst(x => x.PLCCode == conveyorLine.DeviceCode && x.PlatCode == childDeviceCode && x.Status == "Active");
                    //        if (platform != null)
                    //        {
                    //            if (command.HasPallet != 1)
                    //            {
                    //                MethodInfo? method = GetType().GetMethod(platform.ExecutionMethod);
                    //                if (method != null)
                    //                {
                    //                    int count = string.IsNullOrEmpty(platform.Location) ? 0 + 1 : platform.Location.Split(',').Count() + 1;
                    //                    method.Invoke(this, new object[] { conveyorLine, command, childDeviceCode, count, platform });
                    //                }
                    //            }
                    //        }
                    //    }
                    //}
                    #endregion
                }
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine(nameof(CommonConveyorLine_BZJob) + ":" + ex.ToString());
            }
            finally
            {
                //WriteDebug("CommonConveyorLineJob", "test");
                //Console.Out.WriteLine(DateTime.Now);
            }
            return Task.CompletedTask;
        }
        /// <summary>
        /// è¾“送线请求出信息
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        /// <param name="station">线体当前bool读取偏移地址</param>
        public void RequestOutbound(CommonConveyorLine_BZ conveyorLine, ConveyorLineTaskCommand_After command, Dt_StationManager station)
        {
            //查询对应产线的在途数据
            dt_needBarcode needBarcode = _needBarcodeRepository.QueryFirst(x => x.productLine == station.stationRemark);
            //needBarcode.haveOutNum
            //若在途数量小于目标位置的缓存数量 åˆ™å¯»æ‰¾å¯¹åº”常温库存中常温3工序的可出库数据 å¹¶å»ºç«‹å‡ºåº“任务
            if (needBarcode.inLineNum <= needBarcode.cacheNum)
            {
                if (_taskService.RequestOutTaskToBZAsync(station).Result.Status)
                {
                    needBarcode.inLineNum++;
                    _needBarcodeRepository.UpdateData(needBarcode);
                }
            }
        }
        /// <summary>
        /// åˆ°è¾¾ç›®æ ‡ä½ç½®åŽ  å°†å¯¹åº”任务清除 å¹¶æ›´æ–°åœ¨é€”数据
        /// </summary>
        /// <param name="conveyorLine"></param>
        /// <param name="command"></param>
        /// <param name="station"></param>
        public void RecivcePalletBarcode(CommonConveyorLine_BZ conveyorLine, ConveyorLineTaskCommand_After command, Dt_StationManager station)
        {
            Dt_Task task = _taskRepository.QueryFirst(x => x.PalletCode == command.ConveyorLineBarcode && x.CurrentAddress == station.stationChildCode);
            dt_needBarcode needBarcode = _needBarcodeRepository.QueryFirst(x => x.toArea == station.stationChildCode);
            if (needBarcode == null) throw new Exception($"未配置相关{station.stationChildCode}的产线在途数据");
            if (task != null)
            {
                conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineTargetAddress, task.NextAddress, station.stationChildCode);
                _taskRepository.DeleteData(task);
            }
            else
            {
                conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineTargetAddress, station.stationNGChildCode, station.stationChildCode);
            }
            needBarcode.inLineNum--;
            _needBarcodeRepository.UpdateData(needBarcode);
        }
    }
}
#endregion