MeasureExcute.cs 25.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
using HHECS.Application.Enums;
using HHECS.Application.Service;
using HHECS.BllModel;
using HHECS.Communication.PLC;
using HHECS.Dal;
using HHECS.Dal.Repository;
using HHECS.Infrastructure.CommonHelper;
using HHECS.Infrastructure.Enums;
using HHECS.Infrastructure.Notice;
using HHECS.Model.Dtos;
using HHECS.Model.Entities;
using HHECS.Model.Enums;
using HHECS.Model.Enums.Machine;
using NPOI.SS.Formula.Functions;
using NPOI.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HHECS.Executor.EquipmentHandler.Machine
{
    /// <summary>
    /// 测长设备处理类
    /// </summary>
    public class MeasureExcute : StationExecute
    {
        /// <summary>
        /// 对应的设备类型
        /// </summary>
        public EquipmentType EquipmentType { get; set; }

        /// <summary>
        /// 用于可用存储设备列表
        /// </summary>
        public List<Equipment> Equipments { get; set; }

        public MeasureExcute(EquipmentType equipmentType) : base(equipmentType)
        {
        }
        /// <summary>
        /// 测长的控制指令
        /// </summary>
        /// <param name="station"></param>
        /// <param name="allEquipments"></param>
        /// <param name="plc"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public override BllResult ExcuteWCSControl(Equipment station, List<Equipment> allEquipments, PLCCore plc, User user)
        {
            var controlType = station[StationProps.AllowEntry.ToString()];//判断PLC是否允许放货信号
            if (controlType != null)
            {
                if (controlType.Value == "1")//允许下发数据
                {
                    return ExcuteWCSControlToMeasure(station, allEquipments, plc, user);
                }
                else if (controlType.Value == "2")
                {
                    return BllResultFactory.Error($"处理工位【{station.WorkStationCode}】的设备【{station.Name}】PLC禁止下发测长数据,请检查测长口是否有货!");
                }
                else
                {
                    return BllResultFactory.Error($"处理工位【{station.WorkStationCode}】的设备【{station.Name}】执行WCS发给PLC的控制指令失败,任务类型【{controlType.Value}】没有做对应的处理!");
                }
            }
            else
            {
                return BllResultFactory.Error($"处理工位【{station.WorkStationCode}】的设备【{station.Name}】执行WCS发给PLC的控制指令失败,设备属性中不存在任务类型【ControlType】");
            }
        }
        /// <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
            {
                BaseLoaderTaskRepository baseLoaderTaskRepository = new BaseLoaderTaskRepository();
                var requestBarcode = station[StationProps.RequestBarcode.ToString()];
                var requestNumber = station[StationProps.RequestNumber.ToString()];
                var requestLength = station[StationProps.RequestLength.ToString()];
                if (string.IsNullOrEmpty(requestBarcode.Value))
                {
                    return BllResultFactory.Error($"设备{station.Code}处理测长设备请求失败,请求的信号中无任务号信息{requestBarcode}");
                }
                else
                {
                    var baseloder = baseLoaderTaskRepository.Where(t => t.taskCode == requestBarcode.Value && t.loaderStatus == (int)BaseLoaderTaskStatus.测长上料中 && t.equipmentCode == station.Code).ToList();
                    if (baseloder == null) //如果没有在测长中的数据直接跳出
                    {
                        return BllResultFactory.Error($"设备{station.Code}处理测长设备请求失败,暂无可处理测长信号的数据条码{requestBarcode}");
                    }
                    else//如果存在
                    {
                        using (var uw = DALHelper.GetFreeSql().CreateUnitOfWork())
                        {
                            if (requestNumber.Value == baseloder[0].loaderNum.ToString())
                            {
                                baseloder[0].loaderStatus = (int)BaseLoaderTaskStatus.测长成功;
                                baseloder[0].updateTime = DateTime.Now;
                                baseloder[0].feedbackNum = requestNumber.Value;
                                baseLoaderTaskRepository.UnitOfWork = uw;
                                baseLoaderTaskRepository.Update(baseloder[0]);
                            }
                            if (requestLength.Value == "0")
                            {
                                return BllResultFactory.Error($"设备{station.Code}处理设备测长请求失败,测长长度为{requestLength.Value}");
                            }
                            //响应设备
                            var sendResult = SendAddressReplyToPlc(station, plc, StationMessageFlag.WCSAddressReply, StationLoadStatus.Default, requestNumber.Value,
                                                               "0", station.Code, "", "0", "0", "0", "0");
                            if (sendResult.Success)
                            {
                                WebApiService.SendMeasure(baseloder[0].taskCode.ToString(), requestLength.Value, requestNumber.Value); //同步给mes
                            }
                            return BllResultFactory.Success($"设备{station.Code}处理设备测长请求成功,已完成数量{requestNumber.Value}");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error(ex.ToString(), $"设备{station.Code}处理测长设备请求失败");
                //throw;
            }
        }
        /// <summary>
        /// 到达--s上料只用这个
        /// </summary>
        /// <param name="station"></param>
        /// <param name="allEquipments"></param>
        /// <param name="plc"></param>
        /// <param name="user"></param>
        /// <returns></returns>

        public override BllResult ExcuteArrive(Equipment station, List<Equipment> allEquipments, PLCCore plc, User user)
        {
            try
            {
                //    BaseLoaderTaskRepository baseLoaderTaskRepository = new BaseLoaderTaskRepository();

                //    var backup = station[StationProps.ACKBackup.ToString()]; //反馈第几根
                //    var length = station[StationProps.ACKLength.ToString()];//测量长度
                //    var barcode = station[StationProps.ACKBarcode.ToString()];//条码
                //    string lenghw;
                //    string number;
                //    if (string.IsNullOrEmpty(barcode.Value))
                //    {
                //        return BllResultFactory.Error($"设备{station.Code}处理测长设备请求失败,请求的信号中无任务号信息{barcode}");
                //    }
                //    else
                //    {
                //        var baseloder = baseLoaderTaskRepository.Where(t => t.taskCode == barcode.Value && t.loaderStatus == (int)BaseLoaderTaskStatus.测长上料中 && t.equipmentCode == station.Code).ToList();
                //        if (baseloder == null) //如果没有在测长中的数据直接跳出
                //        {
                //            return BllResultFactory.Error($"设备{station.Code}处理测长设备请求失败,暂无可处理测长信号的数据条码{barcode}");
                //        }
                //        else//如果存在
                //        {
                //            using (var uw = DALHelper.GetFreeSql().CreateUnitOfWork())
                //            {
                //                if (backup.Value == baseloder[0].loaderNum.ToString())
                //                {
                //                    baseloder[0].loaderStatus = (int)BaseLoaderTaskStatus.测长成功;
                //                    baseloder[0].updateTime = DateTime.Now;
                //                }
                //                baseloder[0].consumeNum = backup.Value;
                //                baseLoaderTaskRepository.UnitOfWork = uw;
                //                baseLoaderTaskRepository.Update(baseloder[0]);
                //                if (length.Value == "0")
                //                {
                //                    return BllResultFactory.Error($"设备{station.Code}处理设备测长请求失败,测长长度为{length.Value}");
                //                }
                //                lenghw = length.Value;
                //                number = backup.Value;
                //                //响应设备
                //                var sendResult = SendAckToPlc(station, plc, StationMessageFlag.WCSPLCACK, StationLoadStatus.ArriveReply, "0",
                //                                                   "0", "0", 0, 0, 0, 0, 0);
                //                if (sendResult.Success)
                //                {
                //                    uw.Commit();
                //                    WebApiService.SendMeasure(baseloder[0].taskCode.ToString(), lenghw, number); //同步给mes
                //                    return BllResultFactory.Success($"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】地址请求成功,任务号【{baseloder[0].taskCode.ToString()}】,长度:{length.Value},第【{backup.Value}】根上传中控成功");
                //                }
                //                else
                //                {
                //                    uw.Rollback();
                //                    return BllResultFactory.Error($"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】位置到达失败时候");

                //                }
                //                //return BllResultFactory.Success($"设备{station.Code}处理设备测长请求成功,已完成数量{backup.Value}");
                //            }
                //        }
                //    }

                //}
                //catch (Exception ex)
                //{
                //    return BllResultFactory.Exception(ex, $"处理工位【{station.WorkStationCode}】设备【{station.Name}】位置到达失败时候,异常原因:{ex.Message}");
                //}

                BaseMaterialRepository materialRepository = new BaseMaterialRepository();

                using (var uw = DALHelper.GetFreeSql().CreateUnitOfWork())
                {
                    var cutPlanHeadRepository = new CutPlanHeadRepository();
                    var cutPlanHeads = cutPlanHeadRepository.Where(t => t.State == (int)CutPlanHeadState.初始 && t.LineCode == station.LineCode).OrderBy(t => t.CreateTime).ToList();
                    if (cutPlanHeads.Count == 0)
                    {
                        var msg = cutPlanHeads.Count > 1 ? $"{cutPlanHeads.Count}条未使用的" : $"没有找到已经到达的管材";
                        return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】位置到达失败,切割方案存在{msg}");
                    }
                    cutPlanHeads[0].State = (int)CutPlanHeadState.下料开始;
                    cutPlanHeads[0].measureStartTime = DateTime.Now;
                    cutPlanHeads[0].DischargeEndTime = DateTime.Now;
                    cutPlanHeads[0].ActualstartTime = DateTime.Now;
                    //var result = ExcuteWalkPlan(station, allEquipments, plc, user);
                    var material = materialRepository.Where(t => t.MaterialCode == cutPlanHeads[0].MaterialCode).First();
                    if (material == null)
                    {
                        return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】的设备【{station.Name}】上料请求失败,没有找到物料");
                    }

                    var matreial = materialRepository.Where(t => t.MaterialCode == cutPlanHeads[0].MaterialCode).First();
                    if (matreial != null)
                    {
                        BllResult sendResult = SendAckToPlc(station, plc, StationMessageFlag.WCSPLCACK, StationLoadStatus.ArriveReply, "1", cutPlanHeads[0].Id.ToString(), "1", 1, material.TypesPlcCode, cutPlanHeads[0].Pipelength, material.Diameter, material.Thickness);
                        if (sendResult.Success)
                        {
                            cutPlanHeadRepository.UnitOfWork = uw;
                            cutPlanHeadRepository.Update(cutPlanHeads[0]);
                            uw.Commit();
                            return BllResultFactory.Success($"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】地址请求成功,物料号【{cutPlanHeads[0].MaterialCode.ToString()}】,长度:{cutPlanHeads[0].Pipelength}");
                        }
                        else
                        {
                            uw.Rollback();
                            return BllResultFactory.Error($"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】位置到达回复写入失败,原因:" + sendResult.Msg);

                        }
                    }
                    else
                    {
                        return BllResultFactory.Error($"设备{station.Code}处理测长设备请求失败,当前管材物料编码异常,请检查!!!");
                    }
                }

            }
            catch (Exception ex)
            {
                return BllResultFactory.Exception(ex, $"处理工位【{station.WorkStationCode}】设备【{station.Name}】位置到达失败时候,异常原因:{ex.Message}");
            }
        }
        /// <summary>
        /// 发送测长数量
        /// </summary>
        /// <param name="station"></param>
        /// <param name="allEquipments"></param>
        /// <param name="plc"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public BllResult ExcuteWCSControlToMeasure(Equipment station, List<Equipment> allEquipments, PLCCore plc, User user)
        {
            try
            {
                BaseMaterialRepository baseMaterialRepository = new BaseMaterialRepository();

                using (var uw = DALHelper.GetFreeSql().CreateUnitOfWork())
                {
                    var cutPlanHeadRepository = new CutPlanHeadRepository();
                    ///已经套料完毕,已经有了切割方案,只需要复测就开始喷号除锈
                    var cutPlanHeads = cutPlanHeadRepository.Where(t => t.State == (int)CutPlanHeadState.初始 && t.LineCode == station.LineCode).OrderBy(t => t.CreateTime).ToList();
                    if (cutPlanHeads.Count == 0)
                    {
                        var msg = cutPlanHeads.Count > 1 ? $"{cutPlanHeads.Count}条未使用的" : $"没有找到已经到达的管材";
                        // return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】设备【{station.Name}】位置到达失败,切割方案存在{msg}");
                        NoticeBus.Notice($"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】测长请求失败,暂无测长的数据,切割方案存在{msg}", Level.Info);
                        return BllResultFactory.Success("");
                    }
                    cutPlanHeads[0].State = (int)CutPlanHeadState.下料开始;
                    cutPlanHeads[0].measureStartTime = DateTime.Now;
                    cutPlanHeads[0].DischargeEndTime = DateTime.Now;
                    cutPlanHeads[0].ActualstartTime = DateTime.Now;
                    //var result = ExcuteWalkPlan(station, allEquipments, plc, user);
                    var material = baseMaterialRepository.Where(t => t.MaterialCode == cutPlanHeads[0].MaterialCode).First();
                    if (material == null)
                    {
                        return BllResultFactory.Error($"处理打码工位【{station.WorkStationCode}】的设备【{station.Name}】上料请求失败,没有找到物料");
                    }

                    var matreial = baseMaterialRepository.Where(t => t.MaterialCode == cutPlanHeads[0].MaterialCode).First();
                    if (matreial != null)
                    {

                        //var sendResult = SendAckToPlc(station, plc, StationMessageFlag.WCSPLCACK, StationLoadStatus.ArriveReply, "0",
                        //    "0", "0", 0, 0, 0, 0, 0);

                        //  var sendResult = SendAckToPlc(station, plc, StationMessageFlag.WCSPLCACK, StationLoadStatus.ArriveReply, "0", cutPlanHeads[0].Id.ToString(), "1", 0, matreial.Id, (decimal)cutPlanHeads[0].Pipelength, matreial.Diameter, matreial.Thickness);
                        //  var sendResult = SendAckToPlc(station, plc, StationMessageFlag.WCSPLCACK, StationLoadStatus.ArriveReply, cutPlanHeads[0].Id.ToString(), matreial.Id, (decimal)cutPlanHeads[0].Pipelength, matreial.Diameter, matreial.Thickness);

                        //  var sendResult = SendAckToPlc11(station, plc, StationMessageFlag.WCSPLCACK, StationLoadStatus.ArriveReply, cutPlanHeads[0].Id.ToString(), matreial.Id, (decimal)cutPlanHeads[0].Pipelength, matreial.Diameter, matreial.Thickness);
                        BllResult sendResult = SendAckToPlc(station, plc, StationMessageFlag.WCSPLCACK, StationLoadStatus.ArriveReply, "1", cutPlanHeads[0].Id.ToString(), "1", 1, material.TypesPlcCode, cutPlanHeads[0].Pipelength, material.Diameter, material.Thickness);
                        if (sendResult.Success)
                        {
                            cutPlanHeadRepository.UnitOfWork = uw;
                            cutPlanHeadRepository.Update(cutPlanHeads[0]);
                            uw.Commit();
                            return BllResultFactory.Success($"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】地址请求成功,物料号【{cutPlanHeads[0].MaterialCode.ToString()}】,长度:{cutPlanHeads[0].Pipelength}");
                        }
                        else
                        {
                            uw.Rollback();
                            return BllResultFactory.Error($"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】位置到达回复写入失败,原因:" + sendResult.Msg);

                        }
                    }
                    else
                    {
                        return BllResultFactory.Error($"设备{station.Code}处理测长设备请求失败,当前管材物料编码异常,请检查!!!");
                    }
                }

                #region
                //BaseLoaderTaskRepository baseLoaderRepository = new BaseLoaderTaskRepository();
                //BaseMaterialRepository baseMaterialRepository = new BaseMaterialRepository();
                //var baselodertask = baseLoaderRepository.Where(t => t.loaderStatus == (int)BaseLoaderTaskStatus.初始 && t.equipmentCode == station.Code).OrderBy(t => t.createTime).ToList();
                //var baseloadertaskToPLC = baseLoaderRepository.Where(t => t.loaderStatus == (int)BaseLoaderTaskStatus.测长成功 && t.equipmentCode == station.Code).ToList();
                //if (baselodertask.Any())
                //{
                //    var matercode = baseMaterialRepository.Where(t => t.MaterialCode == baselodertask[0].materialCode).First(); //获取管径、壁厚、材质
                //    var isbaseloder = baselodertask.GroupBy(t => t.taskCode).ToList();
                //    if (isbaseloder.Count > 1)
                //    {
                //        var msg = isbaseloder.Count > 1 ? $"存在不同测长计划" : $"没有找到可以测长的计划";
                //        return BllResultFactory.Error($"处理工位【{station.WorkStationCode}】设备【{station.Name}】测长请求失败,{msg}");
                //    }
                //    baselodertask[0].loaderStatus = (int)BaseLoaderTaskStatus.测长上料中;
                //    baselodertask[0].updateTime = DateTime.Now;
                //    using (var uw = DALHelper.GetFreeSql().CreateUnitOfWork())
                //    {
                //        try
                //        {
                //            var sendResult = SendControlToPLC(station, plc, StationMessageFlag.WCSControl, "1", "1", Convert.ToInt32(matercode.Diameter * 10).ToString(), Convert.ToInt32(matercode.Thickness * 10).ToString(), baselodertask[0].taskCode.ToString(), baselodertask[0].loaderNum.ToString());
                //            baseLoaderRepository.UnitOfWork = uw;
                //            baseLoaderRepository.Update(baselodertask[0]);
                //            if (sendResult.Success)
                //            {
                //                uw?.Commit();
                //                return BllResultFactory.Success($"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】测长请求成功,条码{baselodertask[0].taskCode},测长数量{baselodertask[0].loaderNum}");
                //            }
                //            else
                //            {
                //                uw?.Rollback();
                //                return BllResultFactory.Error($"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】测长请求失败,下发数据给PLC失败原因:{sendResult.Msg}");
                //            }
                //        }
                //        catch (Exception ex)
                //        {
                //            uw.Rollback();
                //            return BllResultFactory.Exception(ex, $"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】测长请求失败,下发测长数据的时候发生异常:{ex.Message}");
                //        }
                //    }
                //}
                //else if (baseloadertaskToPLC.Any())
                //{
                //    using (var uw = DALHelper.GetFreeSql().CreateUnitOfWork())
                //    {
                //        var isbaseloder = baseloadertaskToPLC.GroupBy(t => t.taskCode).ToList();
                //        if (isbaseloder.Count > 1)
                //        {
                //            var msg = isbaseloder.Count > 1 ? $"存在不同测长完成计划" : $"没有找到可以测长完成的计划";
                //            return BllResultFactory.Error($"处理工位【{station.WorkStationCode}】设备【{station.Name}】测长请求完成失败,{msg}");
                //        }
                //        foreach (var item in baseloadertaskToPLC)
                //        {
                //            item.loaderStatus = (int)BaseLoaderTaskStatus.测长发送PLC成功;
                //            item.updateTime = DateTime.Now;
                //        }
                //        try
                //        {
                //            baseLoaderRepository.UnitOfWork = uw;
                //            baseLoaderRepository.Update(baseloadertaskToPLC[0]);
                //            var sendResult = SendControlToPLC(station, plc, StationMessageFlag.WCSControl, "2", "0", "0", "0", "0", "0");
                //            if (sendResult.Success)
                //            {
                //                uw?.Commit();
                //                return BllResultFactory.Success($"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】测长全部完成请求成功");
                //            }
                //            else
                //            {
                //                uw?.Rollback();
                //                return BllResultFactory.Error($"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】测长请求完成失败,下发数据给PLC失败原因:{sendResult.Msg}");
                //            }
                //        }
                //        catch (Exception ex)
                //        {
                //            uw.Rollback();
                //            return BllResultFactory.Exception(ex, $"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】测长请求完成失败,下发测长数据的时候发生异常:{ex.Message}");
                //        }
                //    }
                //}
                //else
                //{
                //    // return BllResultFactory.Error($"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】测长请求失败,暂无测长的数据,请检查数据");
                //    NoticeBus.Notice($"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】测长请求失败,暂无测长的数据,请检查数据",Level.Info);
                //    return BllResultFactory.Success("");
                //}

                #endregion
                //return BllResultFactory.Error($"处理工位【{station.WorkStationCode}】设备【{station.Name}】测长请求失败,下发测长数据的时候发生异常");
            }
            catch (Exception ex)
            {
                // uw.Rollback();
                return BllResultFactory.Exception(ex, $"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】测长请求失败,下发测长数据的时候发生异常:{ex.Message}");
            }
        }

    }
}