NewWareHouseViewController.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // NewWareHouseViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Muchinfo on 2021/2/26.
  6. // Copyright © 2021 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. import WHToast
  10. import ActionSheetPicker_3_0
  11. import NVActivityIndicatorView
  12. /// 新增仓库信息视图容器控制类
  13. class NewWareHouseViewController: BaseViewController {
  14. // MARK: - 属性列表
  15. /// 仓库简称
  16. @IBOutlet weak var wareHouseSimpleName: UITextField!
  17. /// 仓库名称
  18. @IBOutlet weak var wareHouseName: UITextField!
  19. /// 仓库类型
  20. @IBOutlet weak var wareHouseType: UIButton!
  21. /// 联系人
  22. @IBOutlet weak var contractName: UITextField!
  23. /// 联系电话
  24. @IBOutlet weak var contractNum: UITextField!
  25. /// 详细地址
  26. @IBOutlet weak var detailAddress: UITextField!
  27. /// 所在地区
  28. @IBOutlet weak var address: UIButton!
  29. /// 提交按钮
  30. @IBOutlet weak var submit: UIButton!
  31. /// 仓库类型
  32. var type: WareHouseType? {
  33. didSet {
  34. /// 异常
  35. guard let obj = type else {
  36. return
  37. }
  38. wareHouseType.setTitle(obj.description, for: .normal)
  39. wareHouseType.setTitleColor(.hex333(), for: .normal)
  40. }
  41. }
  42. /// 国家
  43. var country: MoDivisions?
  44. /// 省
  45. var province: MoDivisions?
  46. /// 市
  47. var city: MoDivisions?
  48. /// 区
  49. var district: MoDivisions?
  50. /// 地址信息
  51. var addressStr: String? {
  52. didSet {
  53. /// 异常
  54. guard let obj = addressStr else {
  55. return
  56. }
  57. address.setTitle(obj, for: .normal)
  58. address.setTitleColor(.hex333(), for: .normal)
  59. }
  60. }
  61. /// 操作
  62. var operatorType: OperatorType? = .add {
  63. didSet {
  64. self.navigationItem.title = (operatorType?.description ?? "")+"仓库"
  65. }
  66. }
  67. /// 仓库信息
  68. var moWareHouse: MoWarehouse?
  69. // MARK: - 生命周期
  70. override func viewDidLoad() {
  71. super.viewDidLoad()
  72. // Do any additional setup after loading the view.
  73. }
  74. override func buildView() {
  75. super.buildView()
  76. /// 没有仓库数据
  77. guard let obj = moWareHouse else { return }
  78. /// 仓库简称
  79. wareHouseSimpleName.text = obj.warehousecode
  80. /// 仓库名称
  81. wareHouseName.text = obj.warehousename
  82. /// 仓库类型
  83. type = obj.warehousetype
  84. /// 联系电话
  85. contractNum.text = obj.contactnum
  86. /// 联系人
  87. contractName.text = obj.contactname
  88. /// 详细地址
  89. detailAddress.text = obj.address
  90. /// 查询对应的省市区
  91. queryProvincesAndCitys()
  92. }
  93. // MARK: - 交互相关
  94. /// onButtonPressed
  95. /// - Parameter sender: sender
  96. @IBAction fileprivate func onButtonPressed(_ sender: UIButton) {
  97. switch sender {
  98. case address: /// 地址切换
  99. self.view.endEditing(true)
  100. /// 弹出地址选择视图
  101. let selectAreaPopupView = Bundle.main.loadNibNamed("SelectArea", owner: nil, options: nil)?.first as! SelectAreaPopupView
  102. selectAreaPopupView.delegate = self
  103. selectAreaPopupView.show()
  104. case submit: /// 提交按钮
  105. /// 仓库提交
  106. doSubmit()
  107. default: /// 仓库类型
  108. ActionSheetStringPicker.show(withTitle: "请选择仓库类型", rows: ["厂库", "自有库", "合作库"], initialSelection: 0, doneBlock: { picker, value, index in
  109. self.type = WareHouseType(rawValue: value+1)
  110. }, cancel: { picker in
  111. return
  112. }, origin: sender)
  113. }
  114. }
  115. // MARK: - 接口请求
  116. /// 查询对应的省市区
  117. func queryProvincesAndCitys() {
  118. /// 异常
  119. guard let obj = moWareHouse else { return }
  120. /// 对应的省
  121. province = (try? DatabaseHelper.getAllDivisions())?.first(where: {$0.autoid == obj.provinceid})
  122. /// 对应的市
  123. city = (try? DatabaseHelper.getAllDivisions())?.first(where: {$0.autoid == obj.cityid})
  124. /// 对应的区
  125. district = (try? DatabaseHelper.getAllDivisions())?.first(where: {$0.autoid == obj.districtid})
  126. /// 地址信息
  127. addressStr = "\(province?.divisionname ?? "") \(city?.divisionname ?? "") \(district?.divisionname ?? "")"
  128. }
  129. /// 新增仓库提交请求
  130. fileprivate func doSubmit() {
  131. /// 异常
  132. guard let wareHouseManager = MTP2BusinessCore.shared.wareHouseManager,
  133. let warehousetype = type, check() else { return }
  134. /// 仓库简称
  135. let simple = wareHouseSimpleName.text
  136. /// 仓库名称
  137. let houseName = wareHouseName.text
  138. /// 联系人
  139. let name = contractName.text
  140. /// 联系电话
  141. let num = contractNum.text
  142. /// 详细地址
  143. let detail = detailAddress.text
  144. /// 市
  145. let cityid = self.city?.autoid
  146. /// 省
  147. let provinceid = self.province?.autoid
  148. /// 区
  149. let disrictid = self.district?.autoid
  150. /// 新增、修改
  151. let type = operatorType == .add ? 1 : 2
  152. /// ID
  153. let id = operatorType == .add ? nil : moWareHouse?.autoid
  154. /// 开启Loading
  155. NVActivityIndicatorPresenter.sharedInstance.startAnimating(animActivityData("请求中..."))
  156. /// 新增申请
  157. wareHouseManager.requestWarehouseApplyReq(type: type, warehouseid: id, warehousecode: simple, warehousename: houseName, warehousetype: warehousetype.rawValue, provinceid: provinceid, cityid: cityid, districtid: disrictid, address: detail, contactname: name, contactnum: num) { (isComplete, er) in
  158. DispatchQueue.main.async {
  159. /// dismisss loding.....
  160. NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
  161. /// 失败
  162. if !isComplete {
  163. WHToast.showMessage("请求失败,原因:\(er?.retMsg ?? "未知错误")", duration: 1.5, finishHandler: {})
  164. return
  165. }
  166. WHToast.showSuccess(withMessage: "请求成功", duration: 1.5, finishHandler: {
  167. if let bolck = self.popBlock {
  168. /// 执行回调
  169. bolck(self.operatorType)
  170. }
  171. /// 返回上层视图
  172. self.navigationController?.popViewController(animated: true)
  173. })
  174. }
  175. }
  176. }
  177. /// 合规性校验
  178. /// - Returns: Bool
  179. fileprivate func check() -> Bool {
  180. /// 请输入仓库简称
  181. guard let simple = wareHouseSimpleName.text, simple != "" else {
  182. WHToast.showMessage("请输入仓库简称", duration: 1.5, finishHandler: {})
  183. return false
  184. }
  185. /// 请选择仓库类型!
  186. guard let _ = type else {
  187. WHToast.showMessage("请选择仓库类型", duration: 1.5, finishHandler: {})
  188. return false
  189. }
  190. /// 请输入仓库名称
  191. guard let name = wareHouseName.text, name != "" else {
  192. WHToast.showMessage("请输入仓库名称", duration: 1.5, finishHandler: {})
  193. return false
  194. }
  195. return true
  196. }
  197. }
  198. // MARK: - SelectAreaPopupViewDelegate
  199. extension NewWareHouseViewController: SelectAreaPopupViewDelegate {
  200. /// selectAreaPopupView
  201. /// - Parameters:
  202. /// - sender: sender
  203. /// - countryModel: countryModel
  204. /// - provinceModel: provinceModel
  205. /// - cityModel: cityModel
  206. /// - districtModel: districtModel
  207. func selectAreaPopupView(_ sender: SelectAreaPopupView, country: MoDivisions, province: MoDivisions, city: MoDivisions?, district: MoDivisions?) {
  208. /// 隐藏视图
  209. sender.hidePopupView()
  210. /// 地址拼接
  211. self.country = country
  212. self.province = province
  213. self.city = city
  214. self.district = district
  215. /// 地址拼接
  216. var areaAddress = province.divisionname
  217. if let cityname = city?.divisionname, cityname != "null" {
  218. areaAddress.append(" \(cityname)")
  219. }
  220. if let districtname = district?.divisionname, districtname != "null" {
  221. areaAddress.append(" \(districtname)")
  222. }
  223. self.addressStr = areaAddress
  224. }
  225. }