3
schangxiang@126.com
2025-08-20 06ee320f1409b77da7767a98c551ba40b70f6093
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
using iWareCc.Conveyor.Entity;
using iWareCc.Conveyor.EnumType;
using iWareCc.DecomposeTask.Entity;
using iWareCc.Properties;
using iWareCommon.Common.Entity;
using iWareCommon.Common.EnumType;
using iWareCommon.Utils;
using iWareDataCore.ORM;
using iWareDataCore.TASK.Entity;
using iWareDataCore.TASK.EnumType;
using iWareLog.ORM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace iWareCc.DoConveyorTaskAuto.Chain
{
   public class 选择一个未执行的输送机的任务 : IHandler
    {
        /// <summary>
        /// 该节点的下一个节点
        /// </summary>
        private IHandler nextHandler = null;
        public IHandler NextHandler
        {
            set { nextHandler = value; }
        }
 
        /// <summary>
        /// WIP任务容器
        /// </summary>
        private PartTaskContainer DecompositionTaskContainer;
 
        private ConveyorEntity Conveyor;
 
 
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="resultContainer">结果容器</param>
        /// <param name="decompositionTaskContainer">WIP任务容器</param>
        public 选择一个未执行的输送机的任务(ConveyorEntity c, PartTaskContainer decompositionTaskContainer)
        {
            this.Conveyor = c;
            this.DecompositionTaskContainer = decompositionTaskContainer;
        }
 
 
        public void Handle()
        {
 
            using (var dbModel = new DbModelCore())
            {
                try
                {
                    var task = dbModel.TASKPartTasks.OrderBy(x=>x.handletimes).FirstOrDefault(x => x.type == (int)EPartTaskType.输送机任务 && x.isreleased == (int)EYesOrNo.否 && x.isfinished == (int)EYesOrNo.否 && x.equipid == Conveyor.Equipment.Id);
                    var gate=Conveyor.Gates.FirstOrDefault(x=>x.Place.PlaceTypeName=="gate11");
                    var alarm=Conveyor.Gates.FirstOrDefault(x=>x.Place.PlaceTypeName=="alarm");
                    var sc = Conveyor.Gates.FirstOrDefault(x => x.Place.PlaceTypeName == "sc");
                    if (task == null)
                    {
                        LogTextHelper.WriteLine(Resources.LogDir + @"/自动执行输送机任务流程/" + Conveyor.Equipment.EquipName, "选择一个未执行的输送机的任务:{0}", "未找到相应的输送机任务");
                        return;
                    }
 
                    task.handletimes += 1;
                    if (task.handletimes >= 200)
                    {
                        task.handletimes = 0;
                    }
                    dbModel.SaveChanges();
                    if (gate != null && gate.Place.Id == task.sourceplaceid && !string.IsNullOrEmpty(sc.RCheckMaterialCode) && !string.IsNullOrWhiteSpace(sc.RCheckMaterialCode) && !sc.RCheckMaterialCode.Contains("ERROR"))
                    {
                        var material = dbModel.BASEMaterialViews.FirstOrDefault(x => x.code == sc.RCheckMaterialCode.Trim());
                        //判断物料是否入库,如果入库直接发送入库任务,不入库生成退库记录
                        if (material != null)
                        {
                            LogTextHelper.WriteLine(Resources.LogDir + @"/物料码信息/", "接收的物料号:{0}", sc.RCheckMaterialCode);
                            var tasknew = dbModel.TASKPartTasks.FirstOrDefault(x => x.type == (int)EPartTaskType.输送机任务 && x.isreleased == (int)EYesOrNo.否 && x.isfinished == (int)EYesOrNo.否 && x.equipid == Conveyor.Equipment.Id && x.materialcode == material.code && x.sourceplaceid == gate.Place.Id);
                            if (tasknew != null)
                            {
                                LogTextHelper.WriteLine(Resources.LogDir + @"/物料码信息/", "发送的物料:{0};任务号;{1}", sc.RCheckMaterialCode, tasknew.id);
                                if (double.Parse(material.wide) >= 250)//退库
                                {
                                    if (gate.RIsEmpty && !gate.ROccupied)//端口有货才发送退库任务
                                    {
                                        LogTextHelper.WriteLine(Resources.LogDir + @"/自动执行输送机任务流程/" + Conveyor.Equipment.EquipName, "获取需要执行任务的输送机:输送机端口{0}上没有检测到物料", gate.Place.PlaceTypeName);
                                        return;
                                    }
                                    int i = 0;
                                    using (var dbModelLog = new DbModelLog())
                                    {
                                        InOutStorageDetail iosd = new InOutStorageDetail()
                                        {
                                            fromplacecode = tasknew.sourceplace,
                                            formplaceid = tasknew.sourceplaceid,
                                            toplacecode = tasknew.toplace,
                                            toplaceid = tasknew.toplaceid,
                                            equipid = tasknew.equipid,
                                            equipname = tasknew.equipname,
                                            updatetime = DateTime.Now,
                                            createtime = tasknew.createtime,
                                            materialcode = tasknew.materialcode,
                                            materialid = material.id,
                                            type = (int)EMainTaskType.退库任务
                                        };
                                        dbModelLog.InOutStorageDetails.Add(iosd);
                                        i = dbModelLog.SaveChanges();
                                    }
                                    if (i > 0)
                                    {
                                        var flag = gate.SendNotIngGateTask();
                                        if (!flag)
                                        {
                                            gate.WPutFinishSymbol = false;
                                            LogTextHelper.WriteLine(Resources.LogDir + @"/自动执行输送机任务流程/" + Conveyor.Equipment.EquipName, "发送输送机退库任务:{0}失败!", tasknew.id, flag);
                                            gate.SendNotIngGateTask();//发送失败,再发一次
                                        }
                                        dbModel.TASKPartTasks.Remove(tasknew);
                                        dbModel.SaveChanges();
                                        sc.RCheckMaterialCode = "";
                                    
                                    }
                             
                                    return;
                                }
                                else//入库
                                {
                                    task = tasknew;
 
                                }
                                gate.RCheckMaterialCode = "";
                            }
                            else 
                            {
                                alarm.SendGateAlarmTask((int)ESendCode.物料不在入库清单中);
                                LogTextHelper.WriteLine(Resources.LogDir + @"/自动执行输送机任务流程/" + Conveyor.Equipment.EquipName, "选择一个未执行的输送机的任务:{0}", "扫描的物料号在入库任务清单中不存在!");
                                return;
                            }
                        }
                        else 
                        {
                            alarm.SendGateAlarmTask((int)ESendCode.物料不存在);
                            LogTextHelper.WriteLine(Resources.LogDir + @"/自动执行输送机任务流程/" + Conveyor.Equipment.EquipName, "选择一个未执行的输送机的任务:{0}", "没有收到扫描的物料号");
                            return;
                        }
                    }
 
                 
                    var fromgate = Conveyor.Gates.FirstOrDefault(x => x.Place.Id == task.sourceplaceid);
                    var togate = Conveyor.Gates.FirstOrDefault(x => x.Place.Id == task.toplaceid);
 
                    if (fromgate == null || togate==null)
                    {
                        LogTextHelper.WriteLine(Resources.LogDir + @"/自动执行输送机任务流程/" + Conveyor.Equipment.EquipName, "选择一个未执行的输送机的任务:未找到名称为{0}的输送机端口", task.sourceplaceid);
                        return;
                    }
                    //确定当前到位的端口上有物料
                    if (fromgate.RIsEmpty || !fromgate.ROccupied)
                    {
                        LogTextHelper.WriteLine(Resources.LogDir + @"/自动执行输送机任务流程/" + Conveyor.Equipment.EquipName, "获取需要执行任务的输送机:输送机端口{0}上没有检测到物料", fromgate.Place.PlaceTypeName);
                        return;
                    }
                    //确定下一个到位的端口上没有物料
                    //if (togate.Place.PlaceTypeName!="gate22")
                    //{
                      if (!togate.RIsEmpty || togate.ROccupied)
                    {
                        LogTextHelper.WriteLine(Resources.LogDir + @"/自动执行输送机任务流程/" + Conveyor.Equipment.EquipName, "获取需要执行任务的输送机:输送机目标端口{0}上有物料", togate.Place.PlaceTypeName);
                        return;
                    }
                    //}
             
                    DecompositionTaskContainer.PartTask = new PartTaskEntity(task);
 
                    LogTextHelper.WriteLine(Resources.LogDir + @"/自动执行输送机任务流程/" + Conveyor.Equipment.EquipName, "选择一个未执行的输送机的任务:{0}", DecompositionTaskContainer.PartTask.Id);
 
 
                    if (nextHandler != null)
                    {
                        fromgate.RIsEmpty = true;
                        fromgate.ROccupied = false;
 
                        nextHandler.Handle();
                    }
 
                }
                catch (Exception ex)
                {
                    LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "Handle", ex.Message);
                }
            }
 
 
        }
    }
}