1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4:
5: namespace Ia.Cl.Model.Business.Nfc
6: {
7: ////////////////////////////////////////////////////////////////////////////
8:
9: /// <summary publish="true">
10: /// Inventory NFC Near-Field Communication (NFC) Support Business functions
11: /// </summary>
12: /// <remarks>
13: /// Copyright © 2020-2021 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
14: ///
15: /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
16: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
17: ///
18: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
19: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20: ///
21: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
22: ///
23: /// Copyright notice: This notice may not be removed or altered from any source distribution.
24: /// </remarks>
25: public partial class Inventory
26: {
27: ////////////////////////////////////////////////////////////////////////////
28:
29: /*
30: * Don't remove items that are obsolete, just mark them with obsolete="true"
31: * Insertable is true if not explicitly specified
32: */
33:
34: ////////////////////////////////////////////////////////////////////////////
35:
36: /// <summary>
37: ///
38: /// </summary>
39:
40: public enum CreateOrUpdateCondition
41: {
42: /// <summary/>
43: IgnoreLatitudeAndLongitude,
44:
45: /// <summary/>
46: None
47: };
48:
49: ////////////////////////////////////////////////////////////////////////////
50:
51: /// <summary>
52: ///
53: /// </summary>
54: public class Type
55: {
56: /// <summary/>
57: public int Id { get; set; }
58:
59: /// <summary/>
60: public bool Obsolete { get; set; }
61:
62: /// <summary/>
63: public bool Insertable { get; set; }
64:
65: /// <summary/>
66: public string Name { get; set; }
67:
68: /// <summary/>
69: public string ArabicName { get; set; }
70: }
71:
72: ////////////////////////////////////////////////////////////////////////////
73:
74: /// <summary>
75: ///
76: /// </summary>
77: public static List<Type> TypeList(int projectId)
78: {
79: List<Type> list;
80:
81: list = new List<Type>();
82:
83: list.Add(new Type { Id = 1, Name = "Tree", ArabicName = "", Obsolete = false, Insertable = true });
84: list.Add(new Type { Id = 2, Name = "Engine", ArabicName = "", Obsolete = false, Insertable = true });
85: list.Add(new Type { Id = 3, Name = "Filter", ArabicName = "", Obsolete = false, Insertable = true });
86: list.Add(new Type { Id = 4, Name = "Tire", ArabicName = "", Obsolete = false, Insertable = true });
87: list.Add(new Type { Id = 5, Name = "Cat", ArabicName = "", Obsolete = false, Insertable = true });
88: list.Add(new Type { Id = 6, Name = "Vehicle", ArabicName = "", Obsolete = false, Insertable = true });
89:
90: if (list.Select(u => u.Id).Distinct().Count() != list.Count())
91: {
92: throw new Exception("Duplicate Id in Ia.Cl.Model.Business.Nfc.Default.NfcTypeList.");
93: }
94:
95: return list;
96: }
97:
98: ////////////////////////////////////////////////////////////////////////////
99:
100: /// <summary>
101: ///
102: /// </summary>
103: public class State
104: {
105: /// <summary/>
106: public int Id { get; set; }
107:
108: /// <summary/>
109: public bool Obsolete { get; set; }
110:
111: /// <summary/>
112: public bool Insertable { get; set; }
113:
114: /// <summary/>
115: public string Name { get; set; }
116:
117: /// <summary/>
118: public string ArabicName { get; set; }
119: }
120:
121: ////////////////////////////////////////////////////////////////////////////
122:
123: /// <summary>
124: ///
125: /// </summary>
126: public static List<State> StateList(int projectId)
127: {
128: List<State> list;
129:
130: list = new List<State>();
131:
132: list.Add(new State { Id = 1, Name = "Available", ArabicName = "", Obsolete = false, Insertable = true });
133: list.Add(new State { Id = 2, Name = "Stolen", ArabicName = "", Obsolete = false, Insertable = true });
134: list.Add(new State { Id = 3, Name = "Lost", ArabicName = "", Obsolete = false, Insertable = true });
135: list.Add(new State { Id = 4, Name = "Missing", ArabicName = "", Obsolete = false, Insertable = true });
136: list.Add(new State { Id = 5, Name = "Damaged", ArabicName = "", Obsolete = false, Insertable = true });
137: list.Add(new State { Id = 6, Name = "Recovered", ArabicName = "", Obsolete = false, Insertable = true });
138: list.Add(new State { Id = 7, Name = "Displayed", ArabicName = "", Obsolete = false, Insertable = true });
139: list.Add(new State { Id = 8, Name = "Spoiled", ArabicName = "", Obsolete = false, Insertable = true });
140:
141: if (list.Select(u => u.Id).Distinct().Count() != list.Count())
142: {
143: throw new Exception("Duplicate Id in Ia.Cl.Model.Business.Nfc.Default.NfcStateList.");
144: }
145:
146: return list;
147: }
148:
149: ////////////////////////////////////////////////////////////////////////////
150:
151: /// <summary>
152: ///
153: /// </summary>
154: public static string TypeNameByTypeId(int projectId, int typeId)
155: {
156: string name;
157:
158: name = (from t in Ia.Cl.Model.Business.Nfc.Inventory.TypeList(projectId) where t.Id == typeId select t.Name).SingleOrDefault();
159:
160: return name;
161: }
162:
163: ////////////////////////////////////////////////////////////////////////////
164:
165: /// <summary>
166: ///
167: /// </summary>
168: public static string StateNameListByStateIdList(int projectId, string stateIdList)
169: {
170: string name;
171: var nameList = new List<string>();
172:
173: foreach (string stateId in stateIdList.Split(','))
174: {
175: if (int.TryParse(stateId, out int i))
176: {
177: name = (from t in Ia.Cl.Model.Business.Nfc.Inventory.StateList(projectId) where t.Id == i select t.Name).SingleOrDefault();
178:
179: if (!string.IsNullOrEmpty(name)) nameList.Add(name);
180: }
181: else
182: {
183: }
184: }
185:
186: return string.Join(", ", nameList);
187: }
188:
189: ////////////////////////////////////////////////////////////////////////////
190:
191: /// <summary>
192: ///
193: /// </summary>
194: public Inventory() { }
195:
196: ////////////////////////////////////////////////////////////////////////////
197:
198: /// <summary>
199: ///
200: /// </summary>
201: public static void CreateOrUpdate(string tagId, string name, string description, string content, string note, int typeId, string stateIdList, Guid userId)
202: {
203: CreateOrUpdate(tagId, name, description, content, note, typeId, stateIdList, 0, 0, userId, CreateOrUpdateCondition.IgnoreLatitudeAndLongitude);
204: }
205:
206: ////////////////////////////////////////////////////////////////////////////
207:
208: /// <summary>
209: ///
210: /// </summary>
211: public static void CreateOrUpdate(string tagId, string name, string description, string content, string note, int typeId, string stateIdList, double latitude, double longitude, Guid userId)
212: {
213: CreateOrUpdate(tagId, name, description, content, note, typeId, stateIdList, latitude, longitude, userId, CreateOrUpdateCondition.None);
214: }
215:
216: ////////////////////////////////////////////////////////////////////////////
217:
218: /// <summary>
219: ///
220: /// </summary>
221: public static void CreateOrUpdate(string tagId, string name, string description, string content, string note, int typeId, string stateIdList, double latitude, double longitude, Guid userId, CreateOrUpdateCondition createOrUpdateCondition)
222: {
223: var tag = Ia.Cl.Model.Data.Nfc.Tag.Read(tagId);
224:
225: if (tag != null)
226: {
227: var dateTime = DateTime.UtcNow.AddHours(3);
228:
229: var id = tagId;
230:
231: using (var db = new Ia.Cl.Model.IaDbContext())
232: {
233: Ia.Cl.Model.Nfc.Inventory newInventory;
234:
235: if (createOrUpdateCondition == CreateOrUpdateCondition.IgnoreLatitudeAndLongitude)
236: {
237: newInventory = new Ia.Cl.Model.Nfc.Inventory
238: {
239: Id = id,
240: ProjectId = tag.ProjectId,
241: Name = name,
242: Description = description,
243: Content = content,
244: Note = note,
245: TypeId = typeId,
246: StateIdList = stateIdList,
247: UserId = userId,
248: Created = dateTime,
249: Updated = dateTime,
250: Deleted = null
251: };
252: }
253: else
254: {
255: newInventory = new Ia.Cl.Model.Nfc.Inventory
256: {
257: Id = id,
258: ProjectId = tag.ProjectId,
259: Name = name,
260: Description = description,
261: Content = content,
262: Note = note,
263: TypeId = typeId,
264: StateIdList = stateIdList,
265: Latitude = latitude,
266: Longitude = longitude,
267: UserId = userId,
268: Created = dateTime,
269: Updated = dateTime,
270: Deleted = null
271: };
272: }
273:
274: var persistentStorageState = Ia.Cl.Model.Data.Nfc.Inventory.CreateOrUpdate(newInventory);
275: }
276: }
277: else
278: {
279: //
280: }
281: }
282:
283: ////////////////////////////////////////////////////////////////////////////
284: ////////////////////////////////////////////////////////////////////////////
285: }
286:
287: ////////////////////////////////////////////////////////////////////////////
288: ////////////////////////////////////////////////////////////////////////////
289: }