WeldExcute.cs 11 KB

using HHECS.Application.Service;
using HHECS.BLL.EquipmentExcute.Marking;
using HHECS.BllModel;
using HHECS.Communication;
using HHECS.Communication.PLC;
using HHECS.Dal;
using HHECS.Dal.Repository;
using HHECS.DAL.Repository;
using HHECS.Executor.EquipmentHandler;
using HHECS.Infrastructure.CommonHelper;
using HHECS.Model.Entities;
using HHECS.Model.Enums;
using HHHECS.Model.Enums;
using NPOI.Util;
using System;
using System.Collections.Generic;
using System.Linq;

namespace HHECS.BLL.EquipmentExcute.Machine
{
    /// <summary>
    /// 焊接
    /// </summary>
    public class WeldExcute : StationExecute
    {
        public WeldExcute(EquipmentType equipmentType) : base(equipmentType)
        {
        }

        /// <summary>
        /// 执行WCS发送给PLC的控制指令
        /// </summary>
        /// <param name="station"></param>
        /// <param name="allEquipments"></param>
        /// <param name="plc"></param>
        /// <param name="warehouseCode"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public override BllResult ExcuteWCSControl(Equipment station, List<Equipment> allEquipments, PLCCore plc, User user)
        {
            var OperationModel = station[StationProps.OperationModel.ToString()];
            var Status = station[StationProps.Status.ToString()];
            var TotalError = station[StationProps.TotalError.ToString()];
            if (Status.Value != EquipmentStatus.空闲.GetIndexString())
            {
                return BllResultFactory.Success($"");
            }
            var weldTechnologyEquipmentRepository = DALHelper.GetFreeSql().GetRepository<BaseWeldTechnologyEquipment>();
            //该设备 创建时间最新的数据 下发
            var weldTechnologyEquipments = weldTechnologyEquipmentRepository
                                                    .Where(u => u.sendStatus == (int)WeldTechnologyEquipmentStatus.init && u.equipmentCode == station.Code)
                                                    .OrderByDescending(u => u.createTime)
                                                    .ToList();
            var sendData = new BaseWeldTechnologyEquipment();
            if (weldTechnologyEquipments.Count < 1)
            {
                return BllResultFactory.Success();
            }
            //查找最近一条数据是1分钟之内的就发送给PLC,其余数据状态全部修改为“过期”
            foreach (var item in weldTechnologyEquipments)
            {
                item.sendStatus = (int)WeldTechnologyEquipmentStatus.expire;
                item.updateTime = DateTime.Now;
                item.updateBy = "ECS";
            }
            var dateDiff = DateTime.Now.Subtract((DateTime)weldTechnologyEquipments.First().createTime).Duration().TotalSeconds;
            if ((int)dateDiff < 60)
            {
                sendData = weldTechnologyEquipments[0];
                sendData.sendStatus = (int)WeldTechnologyEquipmentStatus.success;
                //移除可以发送的数据,防止更新过期数据的时候把可以发送的数据同步更新成过期数据
            }
            using (var uw = DALHelper.GetFreeSql().CreateUnitOfWork())
            {
                try
                {
                    weldTechnologyEquipmentRepository.UnitOfWork = uw;
                    var issend = weldTechnologyEquipments.Where(t => t.sendStatus == (int)WeldTechnologyEquipmentStatus.success).ToList();

                    if (!issend.Any())
                    {
                        weldTechnologyEquipmentRepository.Update(weldTechnologyEquipments);
                        uw.Commit();
                        return BllResultFactory.Create(BllResultCode.Info, $"工位【{station.WorkStationCode}】的设备【{station.Name}】处理过期数据成功");
                    }
                    //响应设备
                    var sendResult = SendControlToPLC(station, plc, StationMessageFlag.WCSControl, "1", "0", sendData.Id.ToString(), sendData.material,Convert.ToInt32(sendData.pipeLength), sendData.minDiameter, sendData.minThickness, sendData.minWeldingSeam.ToString());
                    if (sendResult.Success)
                    {
                        weldTechnologyEquipmentRepository.Update(weldTechnologyEquipments);
                        uw.Commit();
                        return BllResultFactory.Success($"处理工位【{station.WorkStationCode}】的设备【{station.Name}】下发焊接工艺参数成功,最小管径:[{sendData.minDiameter}],最小壁厚:[{sendData.minThickness}],材质:[{sendData.material}],最小焊缝:[{sendData.minWeldingSeam}]!");
                    }
                    else
                    {
                        weldTechnologyEquipments.ForEach(t => { t.sendStatus = (int)WeldTechnologyEquipmentStatus.fail; });
                        weldTechnologyEquipmentRepository.Update(weldTechnologyEquipments);
                        uw.Commit();
                        return BllResultFactory.Error($"处理工位【{station.WorkStationCode}】的设备【{station.Name}】下发焊接工艺参数失败,工艺参数设备id【{sendData.Id}】的信息写入PLC失败,原因:{sendResult.Msg}");
                    }
                }
                catch (Exception ex)
                {
                    uw?.Rollback();
                    return BllResultFactory.Exception(ex, $"处理工位【{station.WorkStationCode}】的设备【{station.Name}】下发焊接工艺参数时候,处理在工艺参数设备id【{sendData.Id}】发生异常,原因:{ex.Message}");
                }
            }
            
        }
        /// <summary>
        /// 执行地址请求,余料和废料不请求
        /// 注意:allEquipments引用所有设备,此为共享应用
        /// </summary>
        /// <param name="station"></param>
        /// <param name="allEquipments"></param>
        /// <param name="plc"></param>
        /// <returns></returns>
        public override BllResult ExcuteRequest(Equipment station, List<Equipment> allEquipments, PLCCore plc, User user)
        {
            try
            {
                var cutPlanDetailRepository = new CutPlanDetailRepository();
                var taskEquipmentRepository = DALHelper.GetFreeSql().GetRepository<BaseWeldTechnologyEquipment>();
                var requestBarcode = station[StationProps.RequestBarcode.ToString()];
                var requestNumber = station[StationProps.RequestNumber.ToString()];
                if (string.IsNullOrEmpty(requestBarcode.Value))
                {
                    return BllResultFactory.Error($"处理工位【{station.WorkStationCode}】的设备【{station.Name}】 地址请求失败,PLC上传的任务号为空");

                }
                else
                {
                    using (var uw = DALHelper.GetFreeSql().CreateUnitOfWork())
                    {
                        //var convertResult = int.TryParse(requestBarcode.Value, out int BarCode);
                        //if (!convertResult)
                        //{
                        //    return BllResultFactory.Error($"处理工位【{station.WorkStationCode}】的设备【{station.Name}】 地址请求失败,PLC上传的任务号【{requestBarcode.Value}】转化为整数失败!");
                        //}
                        //if (BarCode < 1)
                        //{
                        //    return BllResultFactory.Error($"处理工位【{station.WorkStationCode}】的设备【{station.Name}】 地址请求失败,PLC上传的任务号【{requestBarcode.Value}】不能小于1!");
                        //}
                        //var weld = BusWorkOrderDetail.Where(t => t.BarCode == BarCode.ToString() && t.OprSequenceCode == "Weld").ToList();
                        //foreach (var weldItem in weld) 
                        //{
                        //    weldItem.State = 100;
                        //    weldItem.StationCode = station.Name;
                        //    BusWorkOrderDetail.Update(weld);
                        //}
                        var weld = cutPlanDetailRepository.Where(t=>t.BarCode == requestBarcode.Value).First();
                        var sendResult = SendAddressReplyToPlc(station, plc, StationMessageFlag.WCSAddressReply, StationLoadStatus.Default, "0", "0", "0", "0", "0", "0", "0", "0");
                        if (sendResult.Success)
                        {
                           // uw.Commit();
                            WebApiService.SendEndTask(weld.BarCode, station.Name);
                            return BllResultFactory.Success($"处理工位【{station.WorkStationCode}】设备【{station.Name}】地址请求成功,任务号【{requestBarcode.Value}】上传中控成功");
                        }
                        else
                        {
                           // uw.Rollback();
                            return BllResultFactory.Error($"处理工位【{station.WorkStationCode}】设备【{station.Name}】地址请求失败,任务号【{requestBarcode.Value}】上传中控失败,原因:{sendResult.Msg}");

                        }
                    }
                }
               
            }
            catch (Exception ex)
            {

                return BllResultFactory.Exception(ex, $"处理工位【{station.WorkStationCode}】设备【{station.Name}】地址请求失败,异常原因:{ex.Message}");
            }
        }
        //public override BllResult ExcuteArrive(Equipment station, List<Equipment> allEquipments, PLCCore plc, User user)
        //{
        //    try
        //    {
        //        var weldTechnologyEquipmentRepository = DALHelper.GetFreeSql().GetRepository<BaseWeldTechnologyEquipment>();
        //        using (var uw = DALHelper.GetFreeSql().CreateUnitOfWork())
        //        {
        //            weldTechnologyEquipmentRepository.UnitOfWork = uw;
        //            var weldorPos = weldTechnologyEquipmentRepository.Where(t => t.equipmentCode == station.Code && t.sendStatus == (int)WeldTechnologyEquipmentStatus.success).First();
        //            if (string.IsNullOrEmpty(weldorPos.equipmentCode))
        //            {
        //                return BllResultFactory.Error($"处理工位【{station.WorkStationCode}】的设备【{station.Name}】完成焊接工艺参数失败,工艺参数设备id【{weldorPos.Id}】的信息写入完成失败");
        //            }
        //            weldorPos.sendStatus = 100;
        //            weldorPos.updateTime = DateTime.Now;
        //            weldorPos.updateBy = "ECS";
        //            weldTechnologyEquipmentRepository.Update(weldorPos);
        //            //var sendResult = SendAckToPlc(station,plc,)
        //            return BllResultFactory.Success();
        //        }
        //    }
        //    catch (Exception ex)
        //    {

        //        return BllResultFactory.Exception(ex, $"处理工位【{station.WorkStationCode}】的设备【{station.Name}】完成焊接工艺参数时候,发生异常,原因:{ex.Message}");

        //    }
        //}
    }
}