Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

200 linhas
7.5 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. @Timestamp(key: "createDate", on: .create) var createdate: Date?
  36. @Field(key: "createUserId") var createuserid: Int?
  37. @Timestamp(key: "updateDate", on: .update) var updatedate: Date?
  38. @Field(key: "updateUserId") var updateuserid: Int?
  39. init() { }
  40. init(id: Int? = nil
  41. , number: String? = nil
  42. , meterreading: String? = nil
  43. , specialinstructions: String? = nil
  44. , scheduledstartdate: Date? = nil
  45. , scheduledenddate: Date? = nil
  46. , startdate: Date? = nil
  47. , enddate: Date? = nil
  48. , approveddate: Date? = nil
  49. , customerid: Int? = nil
  50. , equipmentid: Int? = nil
  51. , statusid: Int? = nil
  52. , priorityid: Int? = nil
  53. , technicianuserid: Int? = nil
  54. , reasonid: Int? = nil
  55. , incompletereasonid: Int? = nil
  56. , fuelid: Int? = nil
  57. , workorderflowid: Int? = nil
  58. , createuserid: Int? = nil
  59. , updateuserid: Int? = nil
  60. ) {
  61. self.id = id
  62. self.number = number
  63. self.meterreading = meterreading
  64. self.specialinstructions = specialinstructions
  65. self.scheduledenddate = scheduledstartdate
  66. self.scheduledenddate = scheduledenddate
  67. self.startdate = startdate
  68. self.enddate = enddate
  69. self.approveddate = approveddate
  70. if(customerid != nil) { self.$customer.$id.value = customerid! }
  71. if(equipmentid != nil) { self.$equipment.$id.value = equipmentid! }
  72. if(statusid != nil) { self.$status.$id.value = statusid! }
  73. if(priorityid != nil) { self.$priority.$id.value = priorityid! }
  74. if(technicianuserid != nil) { self.$technician.$id.value = technicianuserid! }
  75. if(reasonid != nil) { self.$reason.$id.value = reasonid! }
  76. if(incompletereasonid != nil) { self.$incompletereason.$id.value = incompletereasonid! }
  77. if(fuelid != nil) {self.$fuel.$id.value = fuelid! }
  78. if(workorderflowid != nil) {self.$workorderflow.$id.value = workorderflowid! }
  79. self.createuserid = createuserid ?? -1
  80. self.updateuserid = updateuserid ?? -1
  81. }
  82. }
  83. extension mdlWorkOrder{
  84. struct Create: AsyncMigration {
  85. func prepare(on database: Database) async throws {
  86. try await database.schema("workOrder")
  87. .field("id", .int, .identifier(auto: true))
  88. .field("number", .string)
  89. .field("meterReading", .float)
  90. .field("specialInstructions", .string)
  91. .field("scheduledStartDate", .datetime)
  92. .field("scheduledEndDate", .datetime)
  93. .field("startDate", .datetime)
  94. .field("endDate", .datetime)
  95. .field("approvedDate", .datetime)
  96. .field("customerId", .int)
  97. .field("equipmentId", .int)
  98. .field("statusId", .int)
  99. .field("priorityId", .int)
  100. .field("technicianUserId", .int)
  101. .field("reasonId", .int)
  102. .field("incompleteReasonId", .int)
  103. .field("fuelId", .int)
  104. .field("createDate", .datetime)
  105. .field("createUserId", .int)
  106. .field("updateDate", .datetime)
  107. .field("updateUserId", .int)
  108. .create()
  109. }
  110. func revert(on database: Database) async throws {
  111. try await database.schema("workOrder").delete()
  112. }
  113. }
  114. struct Mod1: AsyncMigration {
  115. func prepare(on database: Database) async throws {
  116. try await database.schema("workOrder")
  117. .field("workOrderFlowId", .int, .references("workOrderFlow", "id"))
  118. .update()
  119. }
  120. func revert(on database: Database) async throws {
  121. try await database.schema("workOrder")
  122. .deleteField("workOrderFlowId")
  123. .update()
  124. }
  125. }
  126. struct Mod2: AsyncMigration {
  127. func prepare(on database: Database) async throws {
  128. try await database.schema("workOrder")
  129. .deleteField("meterReading")
  130. .field("meterReading", .string)
  131. .update()
  132. }
  133. func revert(on database: Database) async throws {
  134. try await database.schema("workOrder")
  135. .deleteField("meterReading")
  136. .update()
  137. }
  138. }
  139. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlWorkOrder>{
  140. let query = mdlWorkOrder.query(on: req.db)
  141. .with(\.$customer)
  142. .with(\.$equipment)
  143. .with(\.$status)
  144. .with(\.$priority)
  145. .with(\.$technician)
  146. .with(\.$reason)
  147. .with(\.$incompletereason)
  148. .with(\.$fuel)
  149. .with(\.$workorderflow)
  150. .with(\.$attachments)
  151. .with(\.$signatures)
  152. .with(\.$notes)
  153. .with(\.$segments){ segment in segment.with(\.$inspections)}
  154. if let x = req.parameters.get("x"){
  155. let decryptedString = try CustomCrypto.DecryptString(input: x)
  156. let array = decryptedString.components(separatedBy: ":")
  157. ///This call is the standard get by id call
  158. if (array.count == 1){
  159. let id = Int(array[0])!
  160. return query.filter(\.$id == id)
  161. }
  162. ///Optional else if if there are multiple parameters in the call
  163. ///You must include an else for each call that has parameters
  164. ///Need to check the endpoint to get the right filter statements
  165. ///else if(x.count == 2){
  166. /// let username = x[0]
  167. /// let password = x[1]
  168. /// return query.filter(\.$username == username).filter(\.$password == password)
  169. ///}
  170. else{
  171. throw Abort(.badRequest)
  172. }
  173. }
  174. else{
  175. return query
  176. }
  177. }
  178. }