Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 

215 wiersze
8.0 KiB

  1. //
  2. // mdlWorkOrder.swift
  3. //
  4. //
  5. // Created by Michiel Carman on 1/24/24.
  6. //
  7. import Fluent
  8. import Vapor
  9. import Foundation
  10. import FluentKit
  11. final class mdlWorkOrder: Model, Content {
  12. static let schema = "workOrder"
  13. @ID(custom: "id", generatedBy: .database) var id: Int?
  14. @Field(key: "number") var number: String?
  15. @Field(key: "meterReading") var meterreading: String?
  16. @Field(key: "specialInstructions") var specialinstructions: String?
  17. @Field(key: "scheduledStartDate") var scheduledstartdate: Date?
  18. @Field(key: "scheduledEndDate") var scheduledenddate: Date?
  19. @Field(key: "startDate") var startdate: Date?
  20. @Field(key: "endDate") var enddate: Date?
  21. @Field(key: "approvedDate") var approveddate: Date?
  22. @OptionalParent(key: "customerId") var customer: mdlCustomer?
  23. @OptionalParent(key: "equipmentId") var equipment: mdlEquipment?
  24. @OptionalParent(key: "statusId") var status: mdlStatus?
  25. @OptionalParent(key: "priorityId") var priority: mdlPriority?
  26. @OptionalParent(key: "technicianUserId") var technician: mdlUser?
  27. @OptionalParent(key: "reasonId") var reason: mdlReason?
  28. @OptionalParent(key: "incompleteReasonId") var incompletereason: mdlIncompleteReason?
  29. @OptionalParent(key: "fuelId") var fuel: mdlFuel?
  30. @OptionalParent(key: "workOrderFlowId") var workorderflow: mdlWorkOrderFlow?
  31. @Siblings(through: mdlWorkOrderAttachment.self, from: \.$workorder, to: \.$equipment) var attachments: [mdlEquipment]
  32. @Children(for: \.$workorder) var signatures: [mdlSignature]
  33. @Children(for: \.$workorder) var notes: [mdlNote]
  34. //@Children(for: \.$workorder) var segments: [mdlWorkOrderSegment]
  35. @Children(for: \.$workorder) var inspections: [mdlInspection]
  36. @Timestamp(key: "createDate", on: .create) var createdate: Date?
  37. @Field(key: "createUserId") var createuserid: Int?
  38. @Timestamp(key: "updateDate", on: .update) var updatedate: Date?
  39. @Field(key: "updateUserId") var updateuserid: Int?
  40. init() { }
  41. init(id: Int? = nil
  42. , number: String? = nil
  43. , meterreading: String? = nil
  44. , specialinstructions: String? = nil
  45. , scheduledstartdate: Date? = nil
  46. , scheduledenddate: Date? = nil
  47. , startdate: Date? = nil
  48. , enddate: Date? = nil
  49. , approveddate: Date? = nil
  50. , customerid: Int? = nil
  51. , equipmentid: Int? = nil
  52. , statusid: Int? = nil
  53. , priorityid: Int? = nil
  54. , technicianuserid: Int? = nil
  55. , reasonid: Int? = nil
  56. , incompletereasonid: Int? = nil
  57. , fuelid: Int? = nil
  58. , workorderflowid: Int? = nil
  59. , createuserid: Int? = nil
  60. , updateuserid: Int? = nil
  61. ) {
  62. self.id = id
  63. self.number = number
  64. self.meterreading = meterreading
  65. self.specialinstructions = specialinstructions
  66. self.scheduledenddate = scheduledstartdate
  67. self.scheduledenddate = scheduledenddate
  68. self.startdate = startdate
  69. self.enddate = enddate
  70. self.approveddate = approveddate
  71. if(customerid != nil) { self.$customer.$id.value = customerid! }
  72. if(equipmentid != nil) { self.$equipment.$id.value = equipmentid! }
  73. if(statusid != nil) { self.$status.$id.value = statusid! }
  74. if(priorityid != nil) { self.$priority.$id.value = priorityid! }
  75. if(technicianuserid != nil) { self.$technician.$id.value = technicianuserid! }
  76. if(reasonid != nil) { self.$reason.$id.value = reasonid! }
  77. if(incompletereasonid != nil) { self.$incompletereason.$id.value = incompletereasonid! }
  78. if(fuelid != nil) {self.$fuel.$id.value = fuelid! }
  79. if(workorderflowid != nil) {self.$workorderflow.$id.value = workorderflowid! }
  80. self.createuserid = createuserid ?? -1
  81. self.updateuserid = updateuserid ?? -1
  82. }
  83. }
  84. extension mdlWorkOrder{
  85. struct Create: AsyncMigration {
  86. func prepare(on database: Database) async throws {
  87. try await database.schema("workOrder")
  88. .field("id", .int, .identifier(auto: true))
  89. .field("number", .string)
  90. .field("meterReading", .float)
  91. .field("specialInstructions", .string)
  92. .field("scheduledStartDate", .datetime)
  93. .field("scheduledEndDate", .datetime)
  94. .field("startDate", .datetime)
  95. .field("endDate", .datetime)
  96. .field("approvedDate", .datetime)
  97. .field("customerId", .int)
  98. .field("equipmentId", .int)
  99. .field("statusId", .int)
  100. .field("priorityId", .int)
  101. .field("technicianUserId", .int)
  102. .field("reasonId", .int)
  103. .field("incompleteReasonId", .int)
  104. .field("fuelId", .int)
  105. .field("createDate", .datetime)
  106. .field("createUserId", .int)
  107. .field("updateDate", .datetime)
  108. .field("updateUserId", .int)
  109. .create()
  110. }
  111. func revert(on database: Database) async throws {
  112. try await database.schema("workOrder").delete()
  113. }
  114. }
  115. struct Mod1: AsyncMigration {
  116. func prepare(on database: Database) async throws {
  117. try await database.schema("workOrder")
  118. .field("workOrderFlowId", .int, .references("workOrderFlow", "id"))
  119. .update()
  120. }
  121. func revert(on database: Database) async throws {
  122. try await database.schema("workOrder")
  123. .deleteField("workOrderFlowId")
  124. .update()
  125. }
  126. }
  127. struct Mod2: AsyncMigration {
  128. func prepare(on database: Database) async throws {
  129. try await database.schema("workOrder")
  130. .deleteField("meterReading")
  131. .field("meterReading", .string)
  132. .update()
  133. }
  134. func revert(on database: Database) async throws {
  135. try await database.schema("workOrder")
  136. .deleteField("meterReading")
  137. .update()
  138. }
  139. }
  140. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlWorkOrder>{
  141. let query = mdlWorkOrder.query(on: req.db)
  142. .with(\.$customer)
  143. .with(\.$equipment)
  144. .with(\.$status)
  145. .with(\.$priority)
  146. .with(\.$technician)
  147. .with(\.$reason)
  148. .with(\.$incompletereason)
  149. .with(\.$fuel)
  150. .with(\.$workorderflow)
  151. .with(\.$attachments)
  152. .with(\.$signatures)
  153. .with(\.$notes)
  154. .with(\.$inspections){
  155. ins in ins
  156. .with(\.$level){
  157. l in l.with(\.$inspectiontype)
  158. }
  159. .with(\.$segment)
  160. .with(\.$categories){
  161. c in c.with(\.$inspectionitems)
  162. .with(\.$category)
  163. }
  164. }
  165. //.with(\.$segments){ segment in segment.with(\.$inspections)}
  166. if let x = req.parameters.get("x"){
  167. let decryptedString = try CustomCrypto.DecryptString(input: x)
  168. let array = decryptedString.components(separatedBy: ":")
  169. ///This call is the standard get by id call
  170. if (array.count == 1){
  171. let id = Int(array[0])!
  172. return query.filter(\.$id == id)
  173. }
  174. ///Optional else if if there are multiple parameters in the call
  175. ///You must include an else for each call that has parameters
  176. ///Need to check the endpoint to get the right filter statements
  177. ///else if(x.count == 2){
  178. /// let username = x[0]
  179. /// let password = x[1]
  180. /// return query.filter(\.$username == username).filter(\.$password == password)
  181. ///}
  182. else{
  183. throw Abort(.badRequest)
  184. }
  185. }
  186. else{
  187. return query
  188. }
  189. }
  190. }