分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-03-25 65151e7ac63e10113ecbeeabf302a4240bf08e97
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using WIDESEA_Comm;
using WIDESEA_Comm.LogInfo;
using WIDESEA_Core.BaseProvider;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Entity.DomainModels;
using WIDESEA_WCS.IRepositories;
using WIDESEA_WCS.Repositories;
using WIDESEA_WCS.WCSClient;
using WIDESEA_WMS.IRepositories;
using WIDESEA_WMS.IServices;
using WIDESEA_WMS.Repositories;
 
namespace WIDESEA_WCS.JobsPart.Common
{
    public class RestockHCJ
    {
        /// <summary>
        /// 更新队列任务
        /// </summary>
        public static void HCJGetBarcode()
        {
            try
            {
                VOLContext Context = new VOLContext();
                Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(Context);
                Idt_agvtaskRepository agvtaskService = new dt_agvtaskRepository(Context);
                var tasks = agvtaskService.Find(x => x.agv_taskstate == "Queue").OrderBy(x => x.agv_createtime).ToList();
                foreach (var task in tasks)
                {
                    if (task.agv_tasktype == "TaskType_EmptyPallet")//空托任务
                    {
                        if (task.agv_fromaddress == "")
                        {
                            var Pipeline_client = PLCClient.Clients.FirstOrDefault(t => t.PLCName == "链条机");
                            if (Pipeline_client == null) throw new Exception("链条机调度服务未开启!");
                            if (!Pipeline_client.IsConnected) throw new Exception("与链条机连接超时!");
                            var area = task.agv_Traytype == "SmallTray" ? "11" : "10";
                            //找1库区的空托位
                            var EmptyStation = stationinfoRepository.Find(x => x.area == area && x.quantity > 0 && x.stationCode.Contains("A") && x.location_state == "Stroge"/*LocationStateEnum.Stroge.ToString()*/ && x.enable)
                                .OrderBy(x => x.line).OrderByDescending(x => x.column).FirstOrDefault();
 
                            #region 一个区域只能有一个空托任务
                            //bool ok = false;
                            //if (EmptyStation != null)
                            //    ok = agvtaskService.Find(x => x.agv_fromaddress == EmptyStation.stationCode || x.agv_toaddress == EmptyStation.stationCode).Any();
                            //if (EmptyStation == null || ok)
                            //    EmptyStation = stationinfoRepository.Find(x => x.area == area && x.quantity > 0 && x.stationCode.Contains(area == "10" ? "D" : "C") && x.location_state == "Stroge" /*LocationStateEnum.Stroge.ToString()*/ && x.enable)
                            //    .OrderBy(x => x.line).OrderByDescending(x => x.column).FirstOrDefault();
                            #endregion
                            if (EmptyStation == null)
                                EmptyStation = stationinfoRepository.Find(x => x.area == area && x.quantity > 0 && x.stationCode.Contains(area == "10" ? "D" : "C") && x.enable).OrderByDescending(x => x.column).FirstOrDefault();
                            #region 库内存在任务占用
                            if (EmptyStation.location_state == LocationStateEnum.Busy.ToString())
                                continue;
                            #endregion
 
                            if (EmptyStation == null)
                                EmptyStation = stationinfoRepository.Find(x => x.area == area && x.quantity > 0 && x.stationCode.Contains(area == "10" ? "D" : "C") && x.location_state == "Stroge" && x.enable).OrderByDescending(x => x.column).FirstOrDefault();
                            if (EmptyStation != null)
                            {
                                #region 库内存在任务占用
                                //if (EmptyStation.location_state == LocationStateEnum.Busy.ToString())
                                //    continue;
                                #endregion
 
                                //if (agvtaskService.Find(x => x.agv_fromaddress == EmptyStation.stationCode || x.agv_toaddress == EmptyStation.stationCode).Any())
                                //    return;
                                #region 补库内空托盘,不需要光电判断
                                var PalletSignal = Pipeline_client.ReadByOrder<Int16>("R_PalletSignal", task.agv_toaddress);//读取托盘信号:1:有,2无
                                var MaterialSignal = Pipeline_client.ReadByOrder<Int16>("R_MaterialSignal", task.agv_toaddress);//读取货物信号:1:有,2无
                                if (PalletSignal == 2 && MaterialSignal == 2)
                                {
                                    task.agv_fromaddress = EmptyStation.stationCode;
                                    task.StarQuantity = EmptyStation.quantity - 1;
                                    task.agv_taskstate = "Create";
                                    agvtaskService.Update(task, true);
                                    EmptyStation.location_state = LocationStateEnum.Busy.ToString();
                                    stationinfoRepository.Update(EmptyStation, true);
                                    WriteDBLog.Success("更新空盘队列任务", $"任务编号:{task.agv_tasknum}", "PCS");
                                }
                                #endregion
                            }
                        }
                    }
                    else if (task.agv_tasktype == "TaskType_OutsourceOutbound" || task.agv_tasktype == "TaskType_Outbound")//外协库出库/AB库出库
                    {
                        if (task.agv_toaddress == "")
                        {
                            var EmptyStations = stationinfoRepository.Find(x => x.stationCode.Contains("S01001") && x.location_state == "Empty" && x.enable).ToList();
                            foreach (var EmptyStation in EmptyStations)
                            {
                                if (agvtaskService.Find(x => x.agv_toaddress == EmptyStation.stationCode).Any()) continue;
                                task.agv_taskstate = "Create";
                                task.agv_toaddress = EmptyStation.stationCode;
                                EmptyStation.location_state = LocationStateEnum.Busy.ToString();
                                stationinfoRepository.Update(EmptyStation, true);
                                agvtaskService.Update(task, true);
                                WriteDBLog.Success("更新出库队列任务", $"任务编号:{task.agv_tasknum}", "PCS");
                                return;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                WriteDBLog.Error("更新队列任务", $"错误信息:{ex.Message}", "PCS");
            }
        }
    }
}