HedgePlanViewController.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. //
  2. // HedgePlanViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Muchinfo on 2021/1/2.
  6. // Copyright © 2021 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. import JXSegmentedView
  10. import IBAnimatable
  11. import WHToast
  12. import GTMRefresh
  13. import NVActivityIndicatorView
  14. /// 套保计划视图容器控制类
  15. class HedgePlanViewController: BaseCollectionViewController {
  16. // MARK: - 属性列表
  17. /// 新增合同按钮
  18. @IBOutlet weak var new: UIBarButtonItem!
  19. /// 主滚动视图
  20. @IBOutlet weak var mainScrollView: UIScrollView!
  21. /// 客户资料数据
  22. var plans: [MoHedgePlan] = [] {
  23. didSet {
  24. /// 左边数据
  25. leftDatas = plans.map { (obj) -> String in
  26. /// 计划类型/\n计划名称
  27. return obj.contracttype.description+"\n\(obj.hedgeplanno)"
  28. }
  29. /// 右边数据
  30. rightDatas = plans.map { (obj) -> [String] in
  31. return ["\(obj.deliverygoodsname)\n\(obj.wrstandardname)",
  32. "\(obj.convertfactor.toString(reserve: 2, false))\n\(obj.planqty)\(obj.wrunitidame)",
  33. "\(obj.tradeusername)\n\(obj.hedgeplanstatus.description)",
  34. obj.currencyname]
  35. }
  36. /// 刷新右侧列表数据
  37. rightTableView.reloadData()
  38. /// 无数据按钮
  39. noDataButton.isHidden = plans.count != 0
  40. }
  41. }
  42. /// 类别
  43. var roleTitles: [String] {
  44. get {
  45. var titles: [String] = []
  46. /// 权限控制
  47. guard let commonManager = MTP2BusinessCore.shared.commonManager else {
  48. return titles
  49. }
  50. if commonManager.isContainTraderMenu(key: "client_hedgeplan_unsubmit") {
  51. titles.append("未提交")
  52. }
  53. if commonManager.isContainTraderMenu(key: "client_hedgeplan_unaduit") {
  54. titles.append("待审核")
  55. }
  56. if commonManager.isContainTraderMenu(key: "client_hedgeplan_inprogress") {
  57. titles.append("执行中")
  58. }
  59. return titles
  60. }
  61. }
  62. // MARK: - 生命周期
  63. override func viewDidLoad() {
  64. super.viewDidLoad()
  65. // Do any additional setup after loading the view.
  66. }
  67. /// UI界面初始化
  68. override func buildView() {
  69. super.buildView()
  70. /// addLoadingView
  71. addLoadingView()
  72. /// 需要返回按钮
  73. addBackBarButtonItem(false)
  74. }
  75. override func loadView() {
  76. super.loadView()
  77. /// 判断其权限控制
  78. guard let commonManager = MTP2BusinessCore.shared.commonManager else { return }
  79. /// 没有新增套保计划权限
  80. if !commonManager.isContainTraderMenu(key: "client_hedgeplan_add") {
  81. self.navigationItem.rightBarButtonItem?.image = nil
  82. self.navigationItem.rightBarButtonItem?.isEnabled = false
  83. } else {
  84. self.navigationItem.rightBarButtonItem?.image = UIImage(named: "new_contract")
  85. self.navigationItem.rightBarButtonItem?.isEnabled = true
  86. }
  87. /// titles
  88. dataSource.titles = roleTitles
  89. segmentedView.dataSource = dataSource
  90. segmentedView.reloadData()
  91. /// 操作回调
  92. operatorBlock = { (_ type: OperatorType, _ takeInfo: Any?, _ row: Int?) in
  93. if type == .delete, let index = row {
  94. /// 待提交状态下的套保计划删除操作
  95. self.doHedgePlanOperator(3, self.plans[index])
  96. }
  97. }
  98. /// 分页切换回调
  99. segmentBlock = { (_ index: Int, _ segment: JXSegmentedView) in
  100. switch self.roleTitles[index] {
  101. case "未提交": /// 未提交
  102. self.queryHedgePlans("0,6")
  103. case "待审核": /// 待审核
  104. self.queryHedgePlans("1,4")
  105. default: /// 查询全部
  106. self.queryHedgePlans("2")
  107. }
  108. self.selectedIndex = nil
  109. }
  110. /// 请求数据
  111. if roleTitles.count != 0 {
  112. /// 添加下拉刷新控件
  113. mainScrollView.gtm_addRefreshHeaderView(refreshHeader: DefaultGTMRefreshHeader()) {
  114. switch self.roleTitles[self.segmentedView.selectedIndex] {
  115. case "未提交":
  116. /// 未提交
  117. self.queryHedgePlans("0,6")
  118. case "待审核":
  119. /// 待审核
  120. self.queryHedgePlans("1,4")
  121. default:
  122. /// 执行中
  123. self.queryHedgePlans("2")
  124. }
  125. self.selectedIndex = nil
  126. }
  127. /// 默认选中第一个
  128. segmentedView.defaultSelectedIndex = 0
  129. segmentedView.delegate?.segmentedView(segmentedView, didSelectedItemAt: 0)
  130. }
  131. }
  132. // MARK: - 接口请求
  133. /// 查询套保计划
  134. /// - Parameter status: 套保计划状态 - 0:未提交 1:待审核 2:执行中 3:正常完结 4:审核拒绝 5:异常完结 6:已撤销
  135. fileprivate func queryHedgePlans(_ status: String) {
  136. /// 异常
  137. guard let planManager = MTP2BusinessCore.shared.planManager,
  138. let commonManager = MTP2BusinessCore.shared.commonManager else { return }
  139. /// group
  140. let group = DispatchGroup()
  141. /// startAnimating
  142. _anim?.startAnimating()
  143. /// enter
  144. group.enter()
  145. planManager.queryHedgePlan(status: status) { isComplete, er, objs in
  146. DispatchQueue.main.async {
  147. /// 查询失败
  148. if !isComplete {
  149. self.plans = []
  150. WHToast.showMessage("数据查询失败,原因:\(er?.retMsg ?? "请求超时")", duration: 1.5, finishHandler: {})
  151. return
  152. }
  153. /// 数据赋值
  154. self.plans = objs ?? []
  155. }
  156. group.leave()
  157. }
  158. /// 查询列头
  159. group.enter()
  160. commonManager.queryTableDefine(tableKey: "table_mobile_hedgeplan") { isComplete, er, objs in
  161. DispatchQueue.main.async {
  162. /// 查询失败
  163. if !isComplete {
  164. WHToast.showMessage("数据查询失败,原因:\(ErrorUtils.desc(er?.retCode))", duration: 1.5, finishHandler: {})
  165. return
  166. }
  167. /// 数据
  168. self.columnHeaders = objs?.first?.columns.sorted(by: { (obj1, obj2) -> Bool in
  169. return obj1.orderindex<obj2.orderindex
  170. }).map({ (obj) -> String in
  171. return obj.columntitle.replacingOccurrences(of: "\\n", with: "\n")
  172. }) ?? []
  173. }
  174. group.leave()
  175. }
  176. group.notify(queue: .main) {
  177. /// stopAnimating
  178. self._anim?.stopAnimating()
  179. /// endRefreshing
  180. self.mainScrollView.endRefreshing()
  181. }
  182. }
  183. /// 套保计划取消提交
  184. /// - Parameters:
  185. /// - operateType: operateType
  186. /// - hedgePlan: hedgePlan
  187. fileprivate func doHedgePlanOperator(_ operateType: Int, _ hedgePlan: MoHedgePlan?) {
  188. /// 异常
  189. guard let planManager = MTP2BusinessCore.shared.planManager,
  190. let obj = hedgePlan else { return }
  191. /// id
  192. let id = Int(obj.hedgeplanid)
  193. /// 开启Loading
  194. NVActivityIndicatorPresenter.sharedInstance.startAnimating(animActivityData("请求中..."))
  195. /// 发送请求
  196. planManager.requestErmcpHedgePlan(hedgePlanID: id, hedgePlanNo: nil, contractType: nil, deliveryGoodsID: nil, productType: nil, spotGoodsDesc: nil, planQty: obj.planqty, convertFactor: obj.convertfactor, planTime: nil, operateType: 3, accountid: nil, wrstandardID: nil, remark: nil, tradeuserid: nil, currencyid: nil) { complete, er, rsp in
  197. DispatchQueue.main.async {
  198. /// dismisss loding.....
  199. NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
  200. /// 失败
  201. if !complete {
  202. WHToast.showMessage("请求失败,原因:\(ErrorUtils.desc(er?.retCode))", duration: 2.0, finishHandler: {})
  203. return
  204. }
  205. WHToast.showSuccess(withMessage: "请求成功", duration: 2.0, finishHandler: {
  206. /// 刷新数据
  207. self.mainScrollView.triggerRefreshing()
  208. })
  209. }
  210. }
  211. }
  212. // MARK: - UIScrollViewDegelate
  213. override func scrollViewDidScroll(_ scrollView: UIScrollView) {
  214. super.scrollViewDidScroll(scrollView)
  215. /// 设置滚动位置
  216. headerCollectionView.contentOffset.x = scrollView.contentOffset.x
  217. /// 左侧 防止边界溢出
  218. if scrollView.contentOffset.x<=0 {
  219. headerCollectionView.contentOffset.x = 0.0
  220. }
  221. if let index = selectedIndex,
  222. index.isExpland,
  223. index.row < rightTableView.visibleCells.count,
  224. let cell = rightTableView.visibleCells[index.row] as? RightCell {
  225. cell.stackView.right = self.view.right-12.0-leftTableView.width+headerCollectionView.contentOffset.x
  226. cell.stackView.left = rightTableView.left+12.0+headerCollectionView.contentOffset.x
  227. }
  228. }
  229. // MARK: - Navigation
  230. // In a storyboard-based application, you will often want to do a little preparation before navigation
  231. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  232. // Get the new view controller using segue.destination.
  233. // Pass the selected object to the new view controller.
  234. /// 客户详情信息
  235. if segue.identifier == "ShowHedgePlanDetail",
  236. let plan = segue.destination as? HedgePlanDetailViewController,
  237. let row = selectedIndex?.row {
  238. /// 审核 详情 撤销
  239. plan.hedgePlan = plans[row]
  240. switch (sender as? UIButton)?.currentTitle {
  241. case "审核":
  242. plan.title = "审核"
  243. plan.operatorType = .aduit
  244. case "撤销":
  245. plan.title = "撤销"
  246. plan.operatorType = .cancel
  247. default:
  248. plan.title = "套保计划详情"
  249. plan.operatorType = .detail
  250. }
  251. plan.popBlock = { (_ takeInfo: Any?) in
  252. self.mainScrollView.triggerRefreshing()
  253. }
  254. } else if segue.identifier == "ShowNewHedgePlan",
  255. let new = segue.destination as? NewHedgePlanViewController,
  256. let row = selectedIndex?.row {
  257. /// 新增、修改
  258. if let _ = sender as? UIBarButtonItem {
  259. new.operatorType = .add
  260. } else {
  261. new.operatorType = .update
  262. new.hedgePlan = plans[row]
  263. }
  264. new.popBlock = { (_ takeInfo: Any?) in
  265. self.mainScrollView.triggerRefreshing()
  266. }
  267. }
  268. }
  269. }
  270. class HedgePlanCell: RightCell {
  271. /// 审核
  272. @IBOutlet weak var aduit: UIButton!
  273. /// 重新提交
  274. @IBOutlet weak var reSubmit: UIButton!
  275. /// 修改
  276. @IBOutlet weak var update: UIButton!
  277. /// 撤销
  278. @IBOutlet weak var cancel: UIButton!
  279. /// 删除
  280. @IBOutlet weak var delete: UIButton!
  281. /// 详情
  282. @IBOutlet weak var detail: UIButton!
  283. /// model
  284. override var model: [String]? {
  285. didSet {
  286. /// 异常
  287. guard let commonManager = MTP2BusinessCore.shared.commonManager,
  288. let row = rowNum,
  289. let superVC = self.superController() as? HedgePlanViewController,
  290. row <= superVC.plans.count-1 else {
  291. return
  292. }
  293. let obj = superVC.plans[row]
  294. /// 判断其权限控制
  295. switch obj.hedgeplanstatus {
  296. case .unSubmitted:
  297. /// 未提交
  298. update.isHidden = !commonManager.isContainTraderMenu(key: "client_hedgeplan_update")
  299. cancel.isHidden = true
  300. aduit.isHidden = true
  301. reSubmit.isHidden = true
  302. delete.isHidden = false
  303. case .reviewed:
  304. /// 待审核
  305. update.isHidden = true
  306. cancel.isHidden = !commonManager.isContainTraderMenu(key: "client_hedgeplan_cancel")
  307. aduit.isHidden = !commonManager.isContainTraderMenu(key: "client_hedgeplan_check")
  308. reSubmit.isHidden = true
  309. delete.isHidden = true
  310. case .withDrawn, .reviewRejected:
  311. /// 已撤销 审核拒绝
  312. reSubmit.isHidden = !commonManager.isContainTraderMenu(key: "client_hedgeplan_recommit")
  313. update.isHidden = true
  314. aduit.isHidden = true
  315. cancel.isHidden = true
  316. delete.isHidden = obj.hedgeplanstatus != .withDrawn
  317. case .progress:
  318. /// 执行中
  319. update.isHidden = true
  320. cancel.isHidden = true
  321. aduit.isHidden = true
  322. reSubmit.isHidden = true
  323. delete.isHidden = true
  324. default:
  325. break
  326. }
  327. }
  328. }
  329. // MARK: - 交互相关
  330. /// onButtonPressed
  331. /// - Parameter sender: sender
  332. @IBAction fileprivate func onButtonPressed(_ sender: UIControl) {
  333. switch sender {
  334. case delete:
  335. /// 删除
  336. if let bk = operatorBlock { bk(OperatorType.delete, model, rowNum) }
  337. default: break
  338. }
  339. }
  340. }