OOMeetingFormViewController.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. //
  2. // OOMeetingFormViewController.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2020/11/20.
  6. // Copyright © 2020 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import Eureka
  10. class OOMeetingFormViewController: FormViewController {
  11. private lazy var viewModel:OOMeetingCreateViewModel = {
  12. return OOMeetingCreateViewModel()
  13. }()
  14. var fromDetail: Bool = false //是否从OOMeetingDetailViewController来的,如果是,就需要返回两层
  15. var meetingInfo: OOMeetingInfo? = nil
  16. var typeList: [String] = [] // 会议类型
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19. // 返回按钮重新定义
  20. self.navigationItem.hidesBackButton = true
  21. self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "icon_fanhui"), style: .plain, target: self, action: #selector(closeSelf))
  22. self.navigationItem.leftItemsSupplementBackButton = true
  23. if let _ = meetingInfo { //修改会议申请
  24. title = "修改申请"
  25. self.navigationItem.rightBarButtonItems = [UIBarButtonItem(title: "保存", style: .plain, target: self, action: #selector(createOrUpdateMeetingAction)),
  26. UIBarButtonItem(title: "取消会议", style: .plain, target: self, action: #selector(deleteMeeting))]
  27. } else { //申请会议
  28. title = "申请会议"
  29. self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "创建", style: .plain, target: self, action: #selector(createOrUpdateMeetingAction))
  30. self.meetingInfo = OOMeetingInfo()
  31. }
  32. // 从配置文件读取会议类型列表
  33. if let config = O2UserDefaults.shared.meetingConfig, let types = config.typeList {
  34. self.typeList = types
  35. }
  36. self.loadForm()
  37. }
  38. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  39. if segue.identifier == "showPickRoom" {
  40. if let vc = segue.destination as? OOMeetingMeetingRoomManageController {
  41. vc.currentMode = 1 //单选
  42. vc.delegate = self
  43. if let during = sender as? DuringTime {
  44. if let start = during.startTime {
  45. vc.startDate = start
  46. }
  47. if let end = during.endTime {
  48. vc.endDate = end
  49. }
  50. }
  51. }
  52. }
  53. }
  54. /// 关闭
  55. @objc private func closeSelf() {
  56. if fromDetail {
  57. //返回两层
  58. if let index = self.navigationController?.viewControllers.firstIndex(of: self) {
  59. if let secVC = self.navigationController?.viewControllers.get(at: index - 2) {
  60. self.navigationController?.popToViewController(secVC, animated: true)
  61. }
  62. }
  63. }else {
  64. self.navigationController?.popViewController(animated: true)
  65. }
  66. }
  67. /// 保存或者修改会议
  68. @objc private func createOrUpdateMeetingAction() {
  69. if self.validateForm() {
  70. if self.meetingInfo!.id == nil { //新增
  71. self.viewModel.createMeetingActionNew(self.meetingInfo!) { (err, id) in
  72. if let msg = err {
  73. self.showError(title: msg)
  74. }else {
  75. self.closeSelf()
  76. }
  77. }
  78. }else { // 修改
  79. self.viewModel.updateMeetingAction(meeting: self.meetingInfo!) { (err) in
  80. if let msg = err {
  81. self.showError(title: msg)
  82. }else {
  83. self.closeSelf()
  84. }
  85. }
  86. }
  87. }
  88. }
  89. /// 删除会议
  90. @objc private func deleteMeeting() {
  91. if let meeting = self.meetingInfo {
  92. self.showDefaultConfirm(title: "提示", message: "确定要取消当前会议,数据会被删除?") { (action) in
  93. self.viewModel.deleteMeeting(meetingId: meeting.id!) { (err) in
  94. if let message = err {
  95. self.showError(title: message)
  96. }else {
  97. self.closeSelf()
  98. }
  99. }
  100. }
  101. }
  102. }
  103. private func loadForm() {
  104. form +++ Section()
  105. <<< TextRow("subject") { row in
  106. row.title = "会议标题"
  107. row.placeholder = "请输入标题"
  108. row.value = self.meetingInfo?.subject
  109. }
  110. <<< MeetingFormDatePickerRow("meetingDate") { row in
  111. row.title = "会议日期"
  112. if let startTime = self.meetingInfo?.startTime {
  113. let date = Date.date(startTime, formatter: "yyyy-MM-dd HH:mm:ss")
  114. row.value = date
  115. }
  116. }
  117. <<< MeetingFormTimeDuringRow("meetingTimeDuring") { row in
  118. row.title = "会议时间"
  119. let during = DuringTime()
  120. if let startTime = self.meetingInfo?.startTime {
  121. let s = Date.date(startTime, formatter: "yyyy-MM-dd HH:mm:ss")
  122. during.startTime = s
  123. }
  124. if let completedTime = self.meetingInfo?.completedTime {
  125. let c = Date.date(completedTime, formatter: "yyyy-MM-dd HH:mm:ss")
  126. during.endTime = c
  127. }
  128. row.value = during
  129. }
  130. <<< MeetingFormRoomPickerCellRow("room") { row in
  131. row.title = "会议室"
  132. row.onPresent = {
  133. self.openChooseRoom()
  134. }
  135. row.value = self.meetingInfo?.woRoom
  136. }
  137. <<< ActionSheetRow<String>("type") {
  138. $0.title = "会议类型"
  139. $0.selectorTitle = "请选择会议类型"
  140. $0.options = self.typeList
  141. $0.value = self.meetingInfo?.type ?? ""
  142. }
  143. <<< MeetingFormSinglePersonPickerRow("hostPerson") { row in
  144. row.title = "主持人"
  145. row.value = self.meetingInfo?.hostPerson ?? ""
  146. }
  147. <<< MeetingFormSingleUnitPickerRow("hostUnit") { row in
  148. row.title = "承办部门"
  149. row.value = self.meetingInfo?.hostUnit ?? ""
  150. }
  151. <<< MeetingFormChoosePersonCellRow("invitePerson") { row in
  152. row.title = "参会人员"
  153. row.cell.viewModel = self.viewModel
  154. row.cell.isUpdate = self.meetingInfo?.id != nil
  155. let oldPersons = self.meetingInfo?.invitePersonList ?? [] //兼容老字段
  156. let newPersons = self.meetingInfo?.inviteMemberList ?? [] //
  157. var persons = newPersons
  158. if persons.count <= 0 {
  159. persons = oldPersons
  160. }
  161. if persons.count > 0 {
  162. var selectPersons: [OOPersonModel] = []
  163. for person in persons {
  164. let pModel = OOPersonModel()
  165. pModel.distinguishedName = person
  166. pModel.name = person.split("@").first ?? ""
  167. selectPersons.append(pModel)
  168. }
  169. row.value = selectPersons
  170. }
  171. }
  172. <<< TextAreaRow("summary") { row in
  173. row.title = "会议描述"
  174. row.placeholder = "会议描述"
  175. row.value = self.meetingInfo?.summary
  176. }
  177. <<< MeetingFormAttachmentCellRow("attachmentList") { row in
  178. row.cell.editMode = true
  179. row.cell.uploadAction = {
  180. self.uploadFileAction()
  181. }
  182. row.cell.deleteAction = { atta in
  183. self.deleteMeetingFile(file: atta)
  184. }
  185. row.value = self.meetingInfo?.attachmentList
  186. }
  187. }
  188. private func openChooseRoom() {
  189. let meetingDateRow = form.rowBy(tag: "meetingDate") as? MeetingFormDatePickerRow
  190. guard let date = meetingDateRow?.value else {
  191. self.showError(title: "请选择会议日期")
  192. return
  193. }
  194. let meetingTimeDuringRow = form.rowBy(tag: "meetingTimeDuring") as? MeetingFormTimeDuringRow
  195. guard let startTime = meetingTimeDuringRow?.value?.startTime else {
  196. self.showError(title: "请选择开始时间")
  197. return
  198. }
  199. guard let endTime = meetingTimeDuringRow?.value?.endTime else {
  200. self.showError(title: "请选择结束时间")
  201. return
  202. }
  203. let startString = "\(date.toString("yyyy-MM-dd")) \(startTime.toString("HH:mm:ss"))"
  204. let endString = "\(date.toString("yyyy-MM-dd")) \(endTime.toString("HH:mm:ss"))"
  205. let during = DuringTime()
  206. during.startTime = Date.date(startString, formatter: "yyyy-MM-dd HH:mm:ss")
  207. during.endTime = Date.date(endString, formatter: "yyyy-MM-dd HH:mm:ss")
  208. self.performSegue(withIdentifier: "showPickRoom", sender: during)
  209. }
  210. /// 验证表单 同时赋值
  211. private func validateForm() -> Bool {
  212. if self.meetingInfo == nil {
  213. self.meetingInfo = OOMeetingInfo()
  214. }
  215. let subjectRow = form.rowBy(tag: "subject") as? TextRow
  216. guard let subject = subjectRow?.value else {
  217. self.showError(title: "请输入会议标题")
  218. return false
  219. }
  220. self.meetingInfo?.subject = subject
  221. let meetingDateRow = form.rowBy(tag: "meetingDate") as? MeetingFormDatePickerRow
  222. guard let date = meetingDateRow?.value else {
  223. self.showError(title: "请选择会议日期")
  224. return false
  225. }
  226. let meetingTimeDuringRow = form.rowBy(tag: "meetingTimeDuring") as? MeetingFormTimeDuringRow
  227. guard let startTime = meetingTimeDuringRow?.value?.startTime else {
  228. self.showError(title: "请选择开始时间")
  229. return false
  230. }
  231. guard let endTime = meetingTimeDuringRow?.value?.endTime else {
  232. self.showError(title: "请选择结束时间")
  233. return false
  234. }
  235. self.meetingInfo?.startTime = "\(date.toString("yyyy-MM-dd")) \(startTime.toString("HH:mm:ss"))"
  236. self.meetingInfo?.completedTime = "\(date.toString("yyyy-MM-dd")) \(endTime.toString("HH:mm:ss"))"
  237. let roomRow = form.rowBy(tag: "room") as? MeetingFormRoomPickerCellRow
  238. guard let room = roomRow?.value else {
  239. self.showError(title: "请选择会议室")
  240. return false
  241. }
  242. self.meetingInfo?.room = room.id
  243. let personRow = form.rowBy(tag: "invitePerson") as? MeetingFormChoosePersonCellRow
  244. guard let personList = personRow?.value else {
  245. self.showError(title: "请选择参会人员")
  246. return false
  247. }
  248. var personIds : [String] = []
  249. personList.forEach { (p) in
  250. if let dn = p.distinguishedName {
  251. personIds.append(dn)
  252. }
  253. }
  254. self.meetingInfo?.invitePersonList = personIds
  255. self.meetingInfo?.inviteMemberList = personIds //被邀请的人员 这个是新字段
  256. let summaryRow = form.rowBy(tag: "summary") as? TextAreaRow
  257. self.meetingInfo?.summary = summaryRow?.value
  258. //
  259. let typeRow = form.rowBy(tag: "type") as? ActionSheetRow<String>
  260. self.meetingInfo?.type = typeRow?.value ?? ""
  261. let hostPersonRow = form.rowBy(tag: "hostPerson") as? MeetingFormSinglePersonPickerRow
  262. self.meetingInfo?.hostPerson = hostPersonRow?.value ?? ""
  263. let hostUnitRow = form.rowBy(tag: "hostUnit") as? MeetingFormSingleUnitPickerRow
  264. self.meetingInfo?.hostUnit = hostUnitRow?.value ?? ""
  265. return true
  266. }
  267. /// 上传附件到服务器
  268. fileprivate func uploadFile(_ meetingId: String, _ fileName: String, _ imageData: Data) {
  269. self.viewModel.uploadMeetingFile(meetingId: meetingId, fileName: fileName, file: imageData) { (err, list) in
  270. self.hideLoading()
  271. if let aList = list {
  272. self.reloadAttachmentCell(list: aList)
  273. }
  274. if let msg = err {
  275. self.showError(title: msg)
  276. }
  277. }
  278. }
  279. /// 点击附件
  280. private func uploadFileAction() {
  281. if self.validateForm() {
  282. self.choosePhotoWithImagePicker { (fileName, imageData) in
  283. self.showLoading()
  284. if let meetingId = self.meetingInfo?.id { // 直接上传
  285. self.uploadFile(meetingId, fileName, imageData)
  286. } else { // 新表单需要先保存会议 再上传
  287. if let meeting = self.meetingInfo {
  288. self.viewModel.createMeetingActionNew(meeting) { (err, id) in
  289. if let meetingId = id {
  290. self.meetingInfo?.id = meetingId
  291. self.uploadFile(meetingId, fileName, imageData)
  292. }else if let msg = err {
  293. self.showError(title: msg)
  294. }
  295. }
  296. }
  297. }
  298. }
  299. }
  300. }
  301. /// 删除会议材料 刷新附件模块数据
  302. private func deleteMeetingFile(file: OOMeetingAttachmentList) {
  303. guard let meetingId = self.meetingInfo?.id else {
  304. return
  305. }
  306. guard let fileId = file.id else {
  307. return
  308. }
  309. self.showLoading()
  310. self.viewModel.deleteMeetingFile(meetingId: meetingId, fileId: fileId) { (err, list) in
  311. self.hideLoading()
  312. if let attaList = list {
  313. self.reloadAttachmentCell(list: attaList)
  314. }else if let msg = err {
  315. self.showError(title: msg)
  316. }
  317. }
  318. }
  319. /// 刷新附件模块数据
  320. private func reloadAttachmentCell(list: [OOMeetingAttachmentList]) {
  321. let row = form.rowBy(tag: "attachmentList") as? MeetingFormAttachmentCellRow
  322. row?.value = list
  323. row?.updateCell()
  324. print("refesh attaList size : \(list.count)")
  325. }
  326. }
  327. extension OOMeetingFormViewController: OOCommonBackResultDelegate {
  328. func backResult(_ vcIdentifiter: String, _ result: Any?) {
  329. if vcIdentifiter == "OOMeetingMeetingRoomManageController" {
  330. if let rooms = result as? [OOMeetingRoomInfo] {
  331. if !rooms.isEmpty {
  332. if let roomRow = form.rowBy(tag: "room") as? MeetingFormRoomPickerCellRow {
  333. roomRow.value = rooms.first
  334. roomRow.updateCell()
  335. }
  336. }
  337. }
  338. }
  339. }
  340. }