分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-07-19 7a4c218909936721fe281737491d10efc7378e09
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Comm;
using WIDESEA_Comm.LogInfo;
using WIDESEA_Comm.TaskNo;
using WIDESEA_Common;
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.Repositories;
 
namespace WIDESEA_WCS.JobsPart.Common
{
    public class EmptyTrayTask
    {
 
        /// <summary>
        /// 创建补空托任务
        /// </summary>
        public static void CreateEmptyTrayTask()
        {
            try
            {
                var Pipeline_client = PLCClient.Clients.FirstOrDefault(t => t.PLCName == "链条机");
                if (Pipeline_client == null) throw new Exception("链条机调度服务未开启!");
                if (!Pipeline_client.IsConnected) throw new Exception("与链条机连接超时!");
                VOLContext context = new VOLContext();
                Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(context);
                Idt_agvtaskRepository agvtaskRepository = new dt_agvtaskRepository(context);
                List<string> strings = new List<string>() { };
                var stations = stationinfoRepository.Find(x => x.stationCode.Contains("X") || x.stationCode.Contains("W01001004") || x.stationCode.Contains("W01001005")).ToList();
                stations = stations.Where(x => x.location_state == LocationStateEnum.Empty.ToString() && x.enable).ToList();
                foreach (var station in stations)
                {
                    if (agvtaskRepository.Find(x => x.agv_toaddress == station.stationCode).Any())
                        continue;
                    var PalletSignal = Pipeline_client.ReadByOrder<Int16>("R_PalletSignal", station.stationCode);//读取托盘信号:1:有,2无
                    var MaterialSignal = Pipeline_client.ReadByOrder<Int16>("R_MaterialSignal", station.stationCode);//读取货物信号:1:有,2无
 
                    if (PalletSignal == 2 && MaterialSignal == 2)
                    {
                        //if (station.remark == TrayTypeEnum.SmallTray.ToString() || station.remark == TrayTypeEnum.LargeTray.ToString()) station.tray_type = station.remark;
 
                        var agv_task = agvtaskRepository.Find(x => x.agv_taskstate == AGVTaskStateEnum.Queue.ToString() && x.agv_tasktype == AGVTaskTypeEnum.TaskType_EmptyPallet.ToString() && string.IsNullOrEmpty(x.agv_toaddress) && x.agv_Traytype == station.tray_type).OrderByDescending(x => x.agv_grade).ThenBy(x => x.agv_createtime).FirstOrDefault();
                        if (agv_task != null)
                        {
                            agv_task.agv_taskstate = AGVTaskStateEnum.Create.ToString();
                            //agv_task.EndQuantity = EmptyStation.quantity;
                            agv_task.agv_toaddress = station.stationCode;
                            agvtaskRepository.Update(agv_task, true);
                            station.location_state = LocationStateEnum.Busy.ToString();
                            stationinfoRepository.Update(station, true);
                            WriteDBLog.Success("更新补空托任务", $"任务编号:{agv_task.agv_tasknum}", "PCS");
                        }
                        else
                        {
                            dt_agvtask taskPart = new dt_agvtask()
                            {
                                agv_fromaddress = "",
                                agv_id = Guid.NewGuid(),
                                agv_tasknum = IdenxManager.GetTaskNo("KH-", "WMS"),
                                agv_grade = station.stationCode.Contains("W0100100") ? 3 : 1,
                                agv_createtime = DateTime.Now,
                                agv_taskstate = AGVTaskStateEnum.Queue.ToString(),
                                agv_qty = 1,
                                agv_tasktype = AGVTaskTypeEnum.TaskType_EmptyPallet.ToString(),
                                agv_toaddress = station.stationCode,
                                agv_userid = "系统",
                                agv_TrayStatus = TrayStateEnum.EmptyTray.ToString(),// station.tray_status,
                                agv_Traytype = station.tray_type
                            };
                            station.location_state = LocationStateEnum.Busy.ToString();
                            stationinfoRepository.Update(station, true);
                            agvtaskRepository.Add(taskPart, true);
                            WriteDBLog.Success("创建补空托任务", $"任务编号:{taskPart.agv_tasknum}", "PCS");
                        }
 
                    }
                }
            }
            catch (Exception ex)
            {
                WriteDBLog.Error("创建补空托任务", $"错误信息:{ex.Message}", "PCS");
            }
        }
 
        /// <summary>
        /// 库内补空托任务
        /// </summary>
        public static void InEmptyTrayTask()
        {
            try
            {
                VOLContext context = new VOLContext();
                Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(context);
                Idt_agvtaskRepository agvtaskRepository = new dt_agvtaskRepository(context);
                var stations = stationinfoRepository.Find(x => x.stationCode.Contains("C01004") && x.enable && x.location_state == LocationStateEnum.Stroge.ToString()).OrderByDescending(x => x.column).ToList();
                //if (stations.Count < 1)
                //{
                //    stations = stationinfoRepository.Find(x => x.area == "3" && x.enable && x.location_state == LocationStateEnum.Empty.ToString()).OrderBy(x => x.line).OrderBy(x => x.column).ToList();
                //}
                foreach (var station in stations)
                {
                    if (agvtaskRepository.Find(x => x.agv_fromaddress == station.stationCode).Any())
                        continue;
                    dt_agvtask taskPart = new dt_agvtask()
                    {
                        agv_fromaddress = station.stationCode,
                        agv_id = Guid.NewGuid(),
                        agv_tasknum = IdenxManager.GetTaskNo("KH-", "WMS"),
                        agv_grade = 1,
                        agv_createtime = DateTime.Now,
                        agv_taskstate = "Queue",
                        //agv_materielid = station.stationType,
                        agv_qty = 1,
                        StarQuantity = 0,
                        EndQuantity = 0,// EmptyStation.quantity,
                        agv_tasktype = "TaskType_EmptyPallet",
                        agv_toaddress = "",// EmptyStation.stationCode,
                        agv_userid = "系统",
                        agv_TrayStatus = "EmptyTray",//station.tray_status,
                        agv_Traytype = station.tray_type,
                    };
                    agvtaskRepository.Add(taskPart, true);
                    station.location_state = LocationStateEnum.OutBusy.ToString();
                    stationinfoRepository.Update(station, true);
                    //WriteDBLog.Success("创建空托任务", $"任务编号:{taskPart.agv_tasknum}", "PCS");
                }
            }
            catch (Exception ex)
            {
                //WriteDBLog.Error("创建补库内空托任务", $"错误信息:{ex.Message}", "PCS");
                //throw;
            }
        }
        public static void task()
        {
            VOLContext context = new VOLContext();
            Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(context);
            Idt_agvtaskRepository agvtaskRepository = new dt_agvtaskRepository(context);
            var tasks = agvtaskRepository.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_toaddress == "")
                    {
                        var EmptyStation = stationinfoRepository.Find(x => x.area == "11" && x.stationCode.Contains("A") && x.quantity < 5 && x.enable).OrderByDescending(x => x.line).OrderBy(x => x.column).FirstOrDefault();
                        if (EmptyStation == null) return;
                        if (EmptyStation.location_state == LocationStateEnum.Busy.ToString()) return;
                        task.agv_toaddress = EmptyStation.stationCode;
                        task.EndQuantity = EmptyStation.quantity;
                        task.agv_taskstate = "Create";
                        EmptyStation.location_state = LocationStateEnum.Busy.ToString();
                        stationinfoRepository.Update(EmptyStation, true);
                        agvtaskRepository.Update(task, true);
                    }
                }
            }
        }
    }
}