NewHedgeViewController.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // NewHedgeViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Muchinfo on 2021/1/22.
  6. // Copyright © 2021 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. import IBAnimatable
  10. import WHToast
  11. import ActionSheetPicker_3_0
  12. import NVActivityIndicatorView
  13. /// 新增套保品种视图容器控制类
  14. class NewHedgeViewController: BaseViewController {
  15. // MARK: - 属性列表
  16. @IBOutlet weak var ggConfig: UIButton!
  17. /// 套保比例
  18. @IBOutlet weak var needhedgeratioField: UITextField!
  19. /// 提交按钮
  20. @IBOutlet weak var submit: UIButton!
  21. /// 单位
  22. @IBOutlet weak var srcunitidname: UILabel!
  23. /// 当前套保比例
  24. @IBOutlet weak var currenthedgeratio: UILabel!
  25. /// 备注
  26. @IBOutlet weak var remark: AnimatableTextField!
  27. /// 套保品种信息
  28. var moMiddleGoods: MoErmcpMiddleGoodsDetail?
  29. /// 套保品种详细信息
  30. var moMiddleGoodsDetail: MoErmcpMiddleGoodsDetail? {
  31. didSet {
  32. /// 异常
  33. guard let obj = moMiddleGoodsDetail?.mg else { return }
  34. /// 套保品种名称
  35. ggConfig.setTitle(obj.middlegoodsname, for: .normal)
  36. /// 当前套保比例
  37. currenthedgeratio.text = "\((obj.needhedgeratio*100.0).toString(reserve: 2, false))%"
  38. /// 单位
  39. srcunitidname.text = obj.enumdicname.isBlank()
  40. /// 备注
  41. remark.text = obj.remark
  42. }
  43. }
  44. /// 新增
  45. var operatorType: OperatorType = .update
  46. // MARK: - 生命周期
  47. override func viewDidLoad() {
  48. super.viewDidLoad()
  49. // Do any additional setup after loading the view.
  50. }
  51. override func buildView() {
  52. super.buildView()
  53. /// moveView
  54. moveView(views: [needhedgeratioField, remark])
  55. /// loding...
  56. self.addLoadingView()
  57. /// 显示套保品种数据信息
  58. self.showMiddleGoodsInfo()
  59. }
  60. /// 显示套保品种数据信息
  61. func showMiddleGoodsInfo() {
  62. /// 异常
  63. guard let planManager = MTP2BusinessCore.shared.planManager,
  64. let obj = moMiddleGoods else { return }
  65. /// startAnimating
  66. self._anim?.startAnimating()
  67. /// 查询套保品种信息
  68. planManager.queryMiddleGoodsDetail(middlegoodsid: obj.mg?.middlegoodsid) { (isComplete, error, detail) in
  69. DispatchQueue.main.async {
  70. /// stopAnimating
  71. self._anim?.stopAnimating()
  72. if !isComplete {
  73. WHToast.showMessage("套保品种详情查询失败", duration: 1.5, finishHandler: {})
  74. return
  75. }
  76. guard let obj = detail else { return }
  77. /// 详细信息
  78. self.moMiddleGoodsDetail = obj.first
  79. }
  80. }
  81. }
  82. // MARK: - 交互相关
  83. /// onButtonPressed
  84. /// - Parameter sender: sender
  85. @IBAction func onButtonPressed(_ sender: UIButton) {
  86. doEditMiddleGoods()
  87. }
  88. // MARK: - 接口请求
  89. /// 修改套保品种
  90. fileprivate func doEditMiddleGoods() {
  91. /// 异常
  92. guard let planManager = MTP2BusinessCore.shared.planManager,
  93. let mg = moMiddleGoodsDetail?.mg,
  94. check() else {
  95. return
  96. }
  97. /// 备注
  98. let rk = remark.text
  99. /// 商品名称
  100. let ratio = (Double((needhedgeratioField.text ?? "").replacingOccurrences(of: "%", with: "")) ?? 0.0)/100.0
  101. /// 开启Loading
  102. NVActivityIndicatorPresenter.sharedInstance.startAnimating(animActivityData("请求中..."))
  103. /// 发送请求
  104. planManager.requestErmsMiddelGoodsEdit(middlegoodsid: UInt64(mg.middlegoodsid), goodsunitid: nil, relatedgoodsid: nil, evaluateratio: nil, qtydecimalplace: nil, relatedgoodstype: nil, needhedgeratio: ratio, goodsgroupid: UInt64(mg.goodsgroupid), remark: rk) { (isComplete, er, rsp) in
  105. DispatchQueue.main.async {
  106. /// dismisss loding.....
  107. NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
  108. /// 请求失败
  109. if !isComplete {
  110. WHToast.showMessage("请求失败,原因:\(ErrorUtils.desc(er?.retCode))", duration: 1.5, finishHandler: {})
  111. return
  112. }
  113. /// 请求成功
  114. WHToast.showSuccess(withMessage: "请求成功", duration: 1.5, finishHandler: {
  115. /// 返回上层视图
  116. self.navigationController?.popViewController(animated: true)
  117. /// 执行回调
  118. if let block = self.popBlock { block(self.operatorType) }
  119. })
  120. }
  121. }
  122. }
  123. /// 合规性校验
  124. /// - Returns: Bool
  125. fileprivate func check() -> Bool {
  126. /// 请输入套保比例
  127. guard let ratio = needhedgeratioField.text,
  128. ratio != "" else {
  129. WHToast.showMessage("请输入修改后套保比例", duration: 1.5, finishHandler: {})
  130. return false
  131. }
  132. return true
  133. }
  134. }
  135. extension NewHedgeViewController: UITextFieldDelegate {
  136. func textFieldDidEndEditing(_ textField: UITextField) {
  137. if textField == needhedgeratioField,
  138. let text = needhedgeratioField.text,
  139. !text.contains("%") {
  140. needhedgeratioField.text = text+"%"
  141. }
  142. }
  143. }