1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4:
5: namespace Ia.TentPlay.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 { IgnoreLatitudeAndLongitude, None };
41:
42: ////////////////////////////////////////////////////////////////////////////
43:
44: /// <summary>
45: ///
46: /// </summary>
47: public class Type
48: {
49: public int Id { get; set; }
50: public bool Obsolete { get; set; }
51: public bool Insertable { get; set; }
52: public string Name { get; set; }
53: public string ArabicName { get; set; }
54: }
55:
56: ////////////////////////////////////////////////////////////////////////////
57:
58: /// <summary>
59: ///
60: /// </summary>
61: public static List<Type> TypeList(int projectId)
62: {
63: List<Type> list;
64:
65: list = new List<Type>();
66:
67: list.Add(new Type { Id = 1, Name = "Tree", ArabicName = "", Obsolete = false, Insertable = true });
68: list.Add(new Type { Id = 2, Name = "Engine", ArabicName = "", Obsolete = false, Insertable = true });
69: list.Add(new Type { Id = 3, Name = "Filter", ArabicName = "", Obsolete = false, Insertable = true });
70: list.Add(new Type { Id = 4, Name = "Tire", ArabicName = "", Obsolete = false, Insertable = true });
71: list.Add(new Type { Id = 5, Name = "Cat", ArabicName = "", Obsolete = false, Insertable = true });
72: list.Add(new Type { Id = 6, Name = "Vehicle", ArabicName = "", Obsolete = false, Insertable = true });
73:
74: if (list.Select(u => u.Id).Distinct().Count() != list.Count())
75: {
76: throw new Exception("Duplicate Id in Ia.TentPlay.Cl.Model.Business.Nfc.Default.NfcTypeList.");
77: }
78:
79: return list;
80: }
81:
82: ////////////////////////////////////////////////////////////////////////////
83:
84: /// <summary>
85: ///
86: /// </summary>
87: public class State
88: {
89: public int Id { get; set; }
90: public bool Obsolete { get; set; }
91: public bool Insertable { get; set; }
92: public string Name { get; set; }
93: public string ArabicName { get; set; }
94: }
95:
96: ////////////////////////////////////////////////////////////////////////////
97:
98: /// <summary>
99: ///
100: /// </summary>
101: public static List<State> StateList(int projectId)
102: {
103: List<State> list;
104:
105: list = new List<State>();
106:
107: list.Add(new State { Id = 1, Name = "Available", ArabicName = "", Obsolete = false, Insertable = true });
108: list.Add(new State { Id = 2, Name = "Stolen", ArabicName = "", Obsolete = false, Insertable = true });
109: list.Add(new State { Id = 3, Name = "Lost", ArabicName = "", Obsolete = false, Insertable = true });
110: list.Add(new State { Id = 4, Name = "Missing", ArabicName = "", Obsolete = false, Insertable = true });
111: list.Add(new State { Id = 5, Name = "Damaged", ArabicName = "", Obsolete = false, Insertable = true });
112: list.Add(new State { Id = 6, Name = "Recovered", ArabicName = "", Obsolete = false, Insertable = true });
113: list.Add(new State { Id = 7, Name = "Displayed", ArabicName = "", Obsolete = false, Insertable = true });
114: list.Add(new State { Id = 8, Name = "Spoiled", ArabicName = "", Obsolete = false, Insertable = true });
115:
116: if (list.Select(u => u.Id).Distinct().Count() != list.Count())
117: {
118: throw new Exception("Duplicate Id in Ia.TentPlay.Cl.Model.Business.Nfc.Default.NfcStateList.");
119: }
120:
121: return list;
122: }
123:
124: ////////////////////////////////////////////////////////////////////////////
125:
126: /// <summary>
127: ///
128: /// </summary>
129: public static string TypeNameByTypeId(int projectId, int typeId)
130: {
131: string name;
132:
133: name = (from t in Ia.TentPlay.Cl.Model.Business.Nfc.Inventory.TypeList(projectId) where t.Id == typeId select t.Name).SingleOrDefault();
134:
135: return name;
136: }
137:
138: ////////////////////////////////////////////////////////////////////////////
139:
140: /// <summary>
141: ///
142: /// </summary>
143: public static string StateNameListByStateIdList(int projectId, string stateIdList)
144: {
145: string name;
146: var nameList = new List<string>();
147:
148: foreach (string stateId in stateIdList.Split(','))
149: {
150: if (int.TryParse(stateId, out int i))
151: {
152: name = (from t in Ia.TentPlay.Cl.Model.Business.Nfc.Inventory.StateList(projectId) where t.Id == i select t.Name).SingleOrDefault();
153:
154: if (!string.IsNullOrEmpty(name)) nameList.Add(name);
155: }
156: else
157: {
158: }
159: }
160:
161: return string.Join(", ", nameList);
162: }
163:
164: ////////////////////////////////////////////////////////////////////////////
165:
166: /// <summary>
167: ///
168: /// </summary>
169: public Inventory() { }
170:
171: ////////////////////////////////////////////////////////////////////////////
172:
173: /// <summary>
174: ///
175: /// </summary>
176: public static void CreateOrUpdate(string tagId, string name, string description, string content, string note, int typeId, string stateIdList, Guid userId)
177: {
178: CreateOrUpdate(tagId, name, description, content, note, typeId, stateIdList, 0, 0, userId, CreateOrUpdateCondition.IgnoreLatitudeAndLongitude);
179: }
180:
181: ////////////////////////////////////////////////////////////////////////////
182:
183: /// <summary>
184: ///
185: /// </summary>
186: public static void CreateOrUpdate(string tagId, string name, string description, string content, string note, int typeId, string stateIdList, double latitude, double longitude, Guid userId)
187: {
188: CreateOrUpdate(tagId, name, description, content, note, typeId, stateIdList, latitude, longitude, userId, CreateOrUpdateCondition.None);
189: }
190:
191: ////////////////////////////////////////////////////////////////////////////
192:
193: /// <summary>
194: ///
195: /// </summary>
196: 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)
197: {
198: var tag = Ia.TentPlay.Cl.Model.Data.Nfc.Tag.Read(tagId);
199:
200: if (tag != null)
201: {
202: var dateTime = DateTime.UtcNow.AddHours(3);
203:
204: var id = tagId;
205:
206: using (var db = new Ia.TentPlay.Db())
207: {
208: Ia.TentPlay.Cl.Model.Nfc.Inventory newInventory;
209:
210: if (createOrUpdateCondition == CreateOrUpdateCondition.IgnoreLatitudeAndLongitude)
211: {
212: newInventory = new Ia.TentPlay.Cl.Model.Nfc.Inventory
213: {
214: Id = id,
215: ProjectId = tag.ProjectId,
216: Name = name,
217: Description = description,
218: Content = content,
219: Note = note,
220: TypeId = typeId,
221: StateIdList = stateIdList,
222: UserId = userId,
223: Created = dateTime,
224: Updated = dateTime,
225: Deleted = null
226: };
227: }
228: else
229: {
230: newInventory = new Ia.TentPlay.Cl.Model.Nfc.Inventory
231: {
232: Id = id,
233: ProjectId = tag.ProjectId,
234: Name = name,
235: Description = description,
236: Content = content,
237: Note = note,
238: TypeId = typeId,
239: StateIdList = stateIdList,
240: Latitude = latitude,
241: Longitude = longitude,
242: UserId = userId,
243: Created = dateTime,
244: Updated = dateTime,
245: Deleted = null
246: };
247: }
248:
249: var persistentStorageState = Ia.TentPlay.Cl.Model.Data.Nfc.Inventory.CreateOrUpdate(newInventory);
250: }
251: }
252: else
253: {
254: //
255: }
256: }
257:
258: ////////////////////////////////////////////////////////////////////////////
259: ////////////////////////////////////////////////////////////////////////////
260: }
261:
262: ////////////////////////////////////////////////////////////////////////////
263: ////////////////////////////////////////////////////////////////////////////
264: }