-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmployeeClient.cs
169 lines (148 loc) · 5.33 KB
/
EmployeeClient.cs
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
using Phase3Databases.DatabaseModels;
namespace Phase3Databases;
public class EmployeeClient : Client
{
private Employee user;
public bool IsPicking
{
get
{
using (Phase3Context db = new Phase3Context(this.connstring))
{
if (db.PickLists.Any(pl => pl.EmployeeId == uid))
{
return true;
}
else
{
return false;
}
}
}
}
public EmployeeClient(string connstring) : base(connstring)
{
//Queries here? https://learn.microsoft.com/en-us/ef/core/querying/
try
{
using (Phase3Context db = new Phase3Context(this.connstring))
{
user = db.Employees.Single(e => (e.EmployeeId == uid));
}
}
catch (InvalidOperationException e)
{
//We either got more than one user for id (not possible) or got 0 results (id doesn't exist)
throw new UserNotFoundException("Couldn't find user with id " + this.uid);
}
Console.WriteLine(user.FirstName);
}
private PickWalk getCurrentWalk()
{
using (Phase3Context db = new Phase3Context(this.connstring))
{
List<PickList> list = db.PickLists.Where(pl => pl.EmployeeId == uid).ToList();
if (list.Count() > 0)
{
PickWalk walk = db.PickWalks.Where(pw =>
((pw.EmployeeId == list[0].EmployeeId) && (pw.StartTimestamp == list[0].StartTimestamp))).Single();
return walk;
}
else
{
throw new InvalidOperationException("Didn't find any items associated with this employee in the picking queue.");
}
}
}
//Returns bool representing whether a pickwalk was successfully ended or not.
private bool EndPick_Walk()
{
PickWalk walk;
if (!IsPicking)
{
Console.WriteLine("You dont have any items to be picked right now!");
return false;
}
else
{
using (Phase3Context db = new Phase3Context(this.connstring))
{
List<PickList> list = db.PickLists.Where(pl => pl.EmployeeId == uid).ToList();
if (list.Count() > 0)
{
walk = db.PickWalks.Where(pw =>
((pw.EmployeeId == list[0].EmployeeId) && (pw.StartTimestamp == list[0].StartTimestamp))).Single();
}
else
{
throw new InvalidOperationException("Didn't find any items associated with this employee in the picking queue.");
}
//db.Update(user);
db.Update(walk);
// db.Attach(user);
walk.EndTimestamp = DateTime.Now;
walk.WalkDuration = (float) (walk.EndTimestamp - walk.StartTimestamp).Value.TotalHours;
walk.PickRate = (walk.PickLists.Count / walk.WalkDuration);
walk.TotalQuantityPicked = walk.PickLists.Count;
db.SaveChanges();
//Get number of walks in the system and divide their pickrates
int numwalks = db.PickWalks.Count(pw => (pw.EmployeeId == uid));
user.CumulativePickrate += (walk.PickRate / numwalks).Value;
//Mark order as fulfilled
db.Orders.Single(order => (order.OrderId == list[0].OrderId)).OrderStatus = 2;
db.PickLists.RemoveRange(walk.PickLists);
db.SaveChanges();
}
return true;
}
return false;
}
//Returns bool representing whether a pickwalk was successfully done or not.
private bool StartPick_Walk()
{
if (IsPicking)
{
Console.WriteLine("You already have items to pick! Finish that order first!");
return false;
}
using (Phase3Context db = new Phase3Context(this.connstring))
{
List<PickList> queue = db.PickLists.Where(pl => (pl.PickWalk == null)).ToList();
if (queue.Count <= 0)
{
Console.WriteLine("No items are in the picking queue right now!");
return false;
}
queue = queue.GroupBy(pl => pl.Order).First().ToList();
db.Attach(user);
PickWalk newWalk = new PickWalk(DateTime.Now, this.uid);
foreach (PickList k in queue)
{
newWalk.PickLists.Add(k);
}
db.PickWalks.Add(newWalk);
db.SaveChanges();
}
return true;
}
public override void MainLoop()
{
int option = -1;
while (option != 3)
{
Console.WriteLine("What would you like to do?\n");
option = Client.OptionsMenu("Start a Pickwalk", "End Pickwalk", "Log-out");
switch (option)
{
case 1:
//Start PickWalk
StartPick_Walk();
break;
case 2:
//End Pickwalk
EndPick_Walk();
break;
}
}
}
}