GoodsVarietiesViewController.swift 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. //
  2. // GoodsVarietiesViewController.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 SwiftyAttributes
  14. import Popover
  15. /// 商品信息视图容器控制类
  16. class GoodsVarietiesViewController: BaseViewController {
  17. // MARK: - 属性列表
  18. /// new
  19. @IBOutlet weak var new: UIBarButtonItem!
  20. /// info1
  21. @IBOutlet weak var info1: UIButton!
  22. /// info1
  23. @IBOutlet weak var info2: UIButton! {
  24. didSet {
  25. info2.isHidden = true
  26. }
  27. }
  28. /// 类目
  29. @IBOutlet weak var segmentedView: JXSegmentedView! {
  30. didSet {
  31. segmentedView.indicators = [indicator]
  32. segmentedView.delegate = self
  33. }
  34. }
  35. /// 列表
  36. @IBOutlet weak var tableView: UITableView!
  37. /// CellIdentifier
  38. let CellIdentifier = "GoodsVarieties_Cell"
  39. /// ArborescenceCellIdentifier
  40. let ArborescenceCellIdentifier = "Arborescence_Cell"
  41. /// 选中的位置
  42. var selectedIndex: (row: Int, isExpland: Bool)? {
  43. didSet {
  44. if let index = selectedIndex {
  45. /// 刷新某一行
  46. tableView.reloadRows(at: [IndexPath(row: index.row, section: 0)], with: .automatic)
  47. }
  48. }
  49. }
  50. /// 树状数据
  51. var objs: [Any] = [] {
  52. didSet {
  53. /// 刷新列表数据
  54. tableView.reloadData()
  55. /// 无数据按钮
  56. noDataButton.isHidden = objs.count != 0
  57. }
  58. }
  59. /// 权限功能
  60. var roleTitles: [String] {
  61. get {
  62. var titles: [String] = []
  63. /// 权限控制
  64. guard let commonManager = MTP2BusinessCore.shared.commonManager else {
  65. return titles
  66. }
  67. if commonManager.isContainTraderMenu(key: "client_deliverygoods_spot") {
  68. titles.append("现货品种")
  69. }
  70. if commonManager.isContainTraderMenu(key: "client_deliverygoods_hedge") {
  71. titles.append("套保品种")
  72. }
  73. return titles
  74. }
  75. }
  76. // MARK: - 生命周期
  77. override func viewDidLoad() {
  78. super.viewDidLoad()
  79. // Do any additional setup after loading the view.
  80. }
  81. override func loadView() {
  82. super.loadView()
  83. /// addLoadingView
  84. addLoadingView()
  85. /// title
  86. dataSource.titles = roleTitles
  87. segmentedView.dataSource = dataSource
  88. segmentedView.reloadData()
  89. if roleTitles.count != 0 {
  90. /// 添加下拉刷新控件
  91. tableView.gtm_addRefreshHeaderView(refreshHeader: DefaultGTMRefreshHeader()) {
  92. switch self.roleTitles[self.segmentedView.selectedIndex] {
  93. case "现货品种":
  94. /// 现货品种
  95. self.queryDeliveryGoodsDetails()
  96. default:
  97. /// 套保品种
  98. self.queryMiddleGoodsDetails()
  99. }
  100. }
  101. /// 默认选中第一个
  102. segmentedView.defaultSelectedIndex = 0
  103. segmentedView.delegate?.segmentedView(segmentedView, didSelectedItemAt: 0)
  104. }
  105. }
  106. /// onButtonPressed
  107. /// - Parameter sender: sender
  108. @IBAction fileprivate func onBarButtonItemPressed(_ sender: UIBarButtonItem) {
  109. switch sender {
  110. case new:
  111. /// 新增
  112. /// 新增现货品种
  113. self.performSegue(withIdentifier: "ShowNewGoodsVarieties", sender: OperatorType.add)
  114. default: break
  115. }
  116. }
  117. /// onButtonPressed
  118. /// - Parameter sender: sender
  119. @IBAction fileprivate func onButtonPressed(_ sender: UIButton) {
  120. let start = sender.convert(CGPoint.zero, to: self.view)
  121. let startPoint = CGPoint(x: start.x+22.0, y: start.y+25.0)
  122. let options: [PopoverOption] = [.type(.down), .cornerRadius(10), .showBlackOverlay(true)]
  123. var infos = ""
  124. switch sender {
  125. case info1:
  126. /// 信息1
  127. infos = "1.商品对应的是标仓系数,是非标商品转换成现货品种的系数;\n 现货品种数量=现货商品数量*标仓系数;\n 2.套保品种对应的是套保系数,是现货品种转换成套保品种的系数;\n套保品种数量=现货品种数量*套保系数;\n3.期货品种对应的是期货品种系数,是期货品种转换成套保品种的系数;\n套保品种数量=期货品种数量*期货品种系数;"
  128. default:
  129. /// 信息2
  130. infos = "1.商品对应的是标仓系数,是非标商品转换成现货品种的系数;\n 现货品种数量=现货商品数量*标仓系数;\n 2.套保品种对应的是套保系数,是现货品种转换成套保品种的系数;\n 套保品种数量=现货品种数量*套保系数;\n 3.期货品种对应的是期货品种系数,是期货品种转换成套保品种的系数;\n 套保品种数量=期货品种数量*期货品种系数;\n 4.期货合约对应的是期货合约乘数,是期货合约转换成期货品种的系数;\n 期货品种数量=期货合约手数*期货合约乘数"
  131. }
  132. info.text = infos
  133. let popover = Popover(options: options, showHandler: nil, dismissHandler: nil)
  134. popover.show(info, point: startPoint)
  135. }
  136. // MARK: - 接口请求
  137. /// 查询现货品种详细信息
  138. fileprivate func queryDeliveryGoodsDetails() {
  139. /// 异常
  140. guard let contractManager = MTP2BusinessCore.shared.contractManager else {
  141. return
  142. }
  143. /// startAnimating
  144. _anim?.startAnimating()
  145. /// 发送查询请求
  146. contractManager.queryDeliveryGoodsDetail(deliverygoodsid: nil, qrytradegoods: nil) { (isComplete, er, details) in
  147. DispatchQueue.main.async {
  148. /// dismisss loding.....
  149. self._anim?.stopAnimating()
  150. /// endRefreshing
  151. self.tableView.endRefreshing()
  152. /// 查询失败
  153. if !isComplete {
  154. self.objs = []
  155. WHToast.showMessage("数据查询失败,原因:\(er?.retMsg ?? "查询超时")", duration: 1.5, finishHandler: {})
  156. return
  157. }
  158. /// 数据赋值
  159. self.objs = details?.sorted(by: { (obj1, obj2) -> Bool in
  160. if obj1.data?.isvalid == obj2.data?.isvalid {
  161. return (obj1.data?.deliverygoodsid ?? 0)>(obj2.data?.deliverygoodsid ?? 0)
  162. }
  163. return (obj1.data?.isvalid ?? 0)>(obj2.data?.isvalid ?? 0)
  164. }) ?? []
  165. }
  166. }
  167. }
  168. /// 查询套保品种(菜单:套保品种)
  169. /// - Parameter status: 状态 0-停用 1-正常
  170. fileprivate func queryMiddleGoodsDetails() {
  171. /// 异常
  172. guard let planManager = MTP2BusinessCore.shared.planManager else {
  173. return
  174. }
  175. /// startAnimating
  176. _anim?.startAnimating()
  177. /// 发送请求
  178. planManager.queryMiddleGoodsDetail(middlegoodsid: nil) { (isComplete, er, details) in
  179. DispatchQueue.main.async {
  180. /// dismisss loding.....
  181. self._anim?.stopAnimating()
  182. /// endRefreshing
  183. self.tableView.endRefreshing()
  184. /// 查询失败
  185. if !isComplete {
  186. self.objs = []
  187. WHToast.showMessage("数据查询失败,原因:\(er?.retMsg ?? "查询超时")", duration: 1.5, finishHandler: {})
  188. return
  189. }
  190. /// 数据赋值
  191. self.objs = details?.sorted(by: { (obj1, obj2) -> Bool in
  192. if obj1.mg?.isvalid == obj2.mg?.isvalid {
  193. return (obj1.mg?.modifytime ?? "")>(obj2.mg?.modifytime ?? "")
  194. }
  195. return (obj1.mg?.isvalid ?? 0)>(obj2.mg?.isvalid ?? 0)
  196. }) ?? []
  197. }
  198. }
  199. }
  200. // MARK: - Navigation
  201. // In a storyboard-based application, you will often want to do a little preparation before navigation
  202. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  203. // Get the new view controller using segue.destination.
  204. // Pass the selected object to the new view controller.
  205. if segue.identifier == "ShowNewGoodsVarieties", let new = segue.destination as? NewGoodsVarietiesViewController {
  206. /// 新增现货品种
  207. if (sender as? OperatorType) == .update {
  208. new.goodsDetail = objs[selectedIndex?.row ?? 0] as? MoDeliveryGoodsDetail
  209. new.operatorType = .update
  210. new.navigationItem.title = "修改现货品种"
  211. }
  212. new.popBlock = { (_ takeInfo: Any?) in
  213. self.tableView.triggerRefreshing()
  214. }
  215. } else if segue.identifier == "ShowNewHedge", let new = segue.destination as? NewHedgeViewController {
  216. /// 新增套保品种
  217. if (sender as? OperatorType) == .update {
  218. new.moMiddleGoods = objs[selectedIndex?.row ?? 0] as? MoErmcpMiddleGoodsDetail
  219. new.operatorType = .update
  220. new.navigationItem.title = "修改套保品种"
  221. }
  222. new.popBlock = { (_ takeInfo: Any?) in
  223. self.tableView.triggerRefreshing()
  224. }
  225. } else if segue.identifier == "ShowGoodsVarietiesDetail", let detail = segue.destination as? GoodsVarietiesDetailViewController {
  226. /// 现货品种详情
  227. detail.deliveryGoodsDetail = objs[selectedIndex?.row ?? 0] as? MoDeliveryGoodsDetail
  228. detail.popBlock = { (_ takeInfo: Any?) in
  229. self.tableView.triggerRefreshing()
  230. }
  231. } else if segue.identifier == "ShowHedgeDetail",let detail = segue.destination as? HedgeDetailViewController {
  232. /// 套保品种详情
  233. detail.middleGoods = objs[selectedIndex?.row ?? 0] as? MoErmcpMiddleGoodsDetail
  234. detail.popBlock = { (_ takeInfo: Any?) in
  235. self.tableView.triggerRefreshing()
  236. }
  237. }
  238. }
  239. }
  240. // MARK: - JXSegmentedViewDelegate
  241. extension GoodsVarietiesViewController: JXSegmentedViewDelegate {
  242. func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
  243. switch roleTitles[index] {
  244. case "现货品种":
  245. /// 查询现货商品数据
  246. queryDeliveryGoodsDetails()
  247. /// 该账户没权限
  248. guard let commonManager = MTP2BusinessCore.shared.commonManager else { return }
  249. /// 没有权限
  250. if !commonManager.isContainTraderMenu(key: "client_deliverygoods_add") {
  251. self.new.image = nil
  252. self.new.isEnabled = false
  253. } else {
  254. self.new.image = UIImage(named: "new_contract")
  255. self.new.isEnabled = true
  256. }
  257. default:
  258. /// 查询套保品种
  259. queryMiddleGoodsDetails()
  260. new.image = nil
  261. new.isEnabled = false
  262. }
  263. info1.isHidden = index != 0
  264. info2.isHidden = index != 1
  265. }
  266. }
  267. extension GoodsVarietiesViewController: UITableViewDelegate, UITableViewDataSource {
  268. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  269. return objs.count
  270. }
  271. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  272. let cell = tableView.dequeueReusableCell(withIdentifier: ArborescenceCellIdentifier, for: indexPath) as! ArborescenceCell
  273. cell.model = objs[indexPath.row]
  274. cell.indexPath = indexPath
  275. cell.block = { (_ model: Any?, _ sender: Any?) in
  276. if let type = sender as? OperatorType {
  277. switch type {
  278. case .detail:
  279. /// 详情
  280. self.performSegue(withIdentifier: self.segmentedView.selectedIndex == 0 ? "ShowGoodsVarietiesDetail" : "ShowHedgeDetail", sender: nil)
  281. case .stop:
  282. /// 停用
  283. self.performSegue(withIdentifier: self.segmentedView.selectedIndex == 0 ? "ShowGoodsVarietiesDetail" : "ShowHedgeDetail", sender: model)
  284. case .reused:
  285. /// 停用
  286. self.performSegue(withIdentifier: self.segmentedView.selectedIndex == 0 ? "ShowGoodsVarietiesDetail" : "ShowHedgeDetail", sender: model)
  287. case .update:
  288. /// 修改
  289. if self.segmentedView.selectedIndex == 0 {
  290. /// 新增现货品种
  291. self.performSegue(withIdentifier: "ShowNewGoodsVarieties", sender: OperatorType.update)
  292. } else {
  293. /// 新增套保品种
  294. self.performSegue(withIdentifier: "ShowNewHedge", sender: OperatorType.update)
  295. }
  296. default:
  297. /// 展开
  298. tableView.delegate?.tableView?(tableView, didSelectRowAt: indexPath)
  299. }
  300. }
  301. }
  302. return cell
  303. }
  304. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  305. self.selectedIndex = (row: indexPath.row, true)
  306. if self.segmentedView.selectedIndex == 0 {
  307. /// 新增现货品种
  308. var obj = self.objs[indexPath.row] as! MoDeliveryGoodsDetail
  309. obj.expland = !obj.expland
  310. self.objs.replaceSubrange(indexPath.row...indexPath.row, with: [obj])
  311. } else {
  312. /// 新增套保品种
  313. var obj = self.objs[indexPath.row] as! MoErmcpMiddleGoodsDetail
  314. obj.expland = !obj.expland
  315. self.objs.replaceSubrange(indexPath.row...indexPath.row, with: [obj])
  316. }
  317. tableView.reloadData()
  318. /// 滚动到底部
  319. self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
  320. }
  321. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  322. var height: CGFloat = 45.0
  323. switch objs[indexPath.row].self {
  324. case is MoDeliveryGoodsDetail:
  325. /// 现货品种
  326. let obj = objs[indexPath.row] as! MoDeliveryGoodsDetail
  327. if obj.expland {
  328. /// 操作项高度
  329. height += 40.0
  330. if obj.mgExpland {
  331. height += CGFloat(obj.mgList.count)*45.0
  332. obj.mgList.forEach { mg in
  333. height += CGFloat(mg.gpExpland ? mg.gplist.count : 0)*45.0
  334. }
  335. }
  336. /// count+1是因为列头高度
  337. height += CGFloat((obj.mgExpland ? obj.mgList.count : 0)+1)*45.0
  338. height += CGFloat((obj.gbExpland ? obj.gblist.count : 0)+1)*45.0
  339. height += CGFloat((obj.gmExpland ? obj.gmlist.count : 0)+1)*45.0
  340. }
  341. default:
  342. /// 期货品种
  343. let obj = objs[indexPath.row] as! MoErmcpMiddleGoodsDetail
  344. if obj.expland {
  345. /// 操作项高度
  346. height += 40.0
  347. /// count+1是因为列头高度
  348. height += CGFloat((obj.gpExpland ? obj.gplist.count : 0)+1)*45.0
  349. height += CGFloat((obj.dgExpland ? obj.dglist.count : 0)+1)*45.0
  350. if obj.gpExpland {
  351. height += CGFloat(obj.gplist.count)*45.0
  352. obj.gplist.forEach { gp in
  353. height += CGFloat(gp.gexpland ? gp.glist.count : 0)*45.0
  354. }
  355. }
  356. if obj.dgExpland {
  357. height += CGFloat(obj.dglist.count)*45.0
  358. obj.dglist.forEach { dg in
  359. height += CGFloat(dg.gmExpland ? dg.gmlist.count : 0)*45.0
  360. }
  361. }
  362. }
  363. }
  364. return height
  365. }
  366. }
  367. /// 树状Cell
  368. class ArborescenceCell: BaseTableViewCell<Any> {
  369. /// 树状按钮
  370. @IBOutlet weak var arborescence: UIImageView!
  371. /// 标题
  372. @IBOutlet weak var titleLabel: UILabel!
  373. /// 恢复
  374. @IBOutlet weak var restore: UIButton!
  375. /// 修改
  376. @IBOutlet weak var update: UIButton!
  377. /// 停用
  378. @IBOutlet weak var stop: UIButton!
  379. /// 详情
  380. @IBOutlet weak var detail: UIButton!
  381. /// 子树
  382. @IBOutlet weak var tableView: UITableView!
  383. /// 所处位置
  384. var indexPath: IndexPath?
  385. /// SubArborescenceCellIdentifier
  386. let SubArborescenceCellIdentifier = "SubArborescence_Cell"
  387. /// 数据
  388. override var model: Any? {
  389. didSet {
  390. switch model.self {
  391. case is MoDeliveryGoodsDetail:
  392. /// 现货品种信息
  393. if let obj = (model as? MoDeliveryGoodsDetail)?.data {
  394. /// 单位
  395. let enumdicname = (try? DatabaseHelper.getEnumInfo(enumdiccode: "goodsunit")?.first(where: {$0.enumitemname == obj.goodsunitid})?.enumdicname) ?? "--"
  396. /// 标题
  397. titleLabel.attributedText = "\(obj.deliverygoodsname.isBlank()) (\(enumdicname))".withFont(.font_16).withTextColor(.hex333())+" \(obj.isvalid == 0 ? "停用" : "正常")".withFont(.font_13).withTextColor(obj.isvalid == 0 ? .FF3E3E() : .hex2FBB4D())
  398. /// 修改按钮
  399. update.isHidden = !(obj.isvalid == 1)
  400. /// 停用按钮
  401. stop.isHidden = !(obj.isvalid == 1)
  402. /// 恢复
  403. restore.isHidden = !(obj.isvalid == 0)
  404. }
  405. default:
  406. /// 套保品种
  407. if let obj = (model as? MoErmcpMiddleGoodsDetail)?.mg {
  408. /// 套保比例
  409. let needarbitrageratio = (obj.needarbitrageratio*100.0).toString(reserve: 2, false)+"%"
  410. /// 套利比例
  411. let needhedgeratio = (obj.needhedgeratio*100.0).toString(reserve: 2, false)+"%"
  412. titleLabel.attributedText = "\(obj.middlegoodsname.isBlank()) (\(obj.enumdicname.isBlank()))".withFont(.font_16).withTextColor(.hex333())+" 套保比例:\(needhedgeratio) 套利比例:\(needarbitrageratio)".withFont(.font_13).withTextColor(obj.isvalid == 0 ? .FF3E3E() : .hex2FBB4D())
  413. /// 修改按钮
  414. update.isHidden = !(obj.isvalid == 1)
  415. /// 停用按钮
  416. stop.isHidden = true
  417. /// 恢复
  418. restore.isHidden = true
  419. }
  420. }
  421. /// 刷新数据
  422. tableView.reloadData()
  423. }
  424. }
  425. /// onButtonPressed
  426. /// - Parameter sender: sender
  427. @IBAction func onButtonPressed(_ sender: UIButton) {
  428. switch sender {
  429. case detail:
  430. /// 详情
  431. if let bk = self.block { bk(self.model, OperatorType.detail) }
  432. case stop:
  433. /// 停用
  434. if let bk = self.block { bk(self.model, OperatorType.stop) }
  435. case restore:
  436. /// 恢复
  437. if let bk = self.block { bk(self.model, OperatorType.reused) }
  438. case update:
  439. /// 修改
  440. if let bk = self.block { bk(self.model, OperatorType.update) }
  441. default:
  442. if let bk = self.block { bk(self.model, OperatorType.expland) }
  443. }
  444. }
  445. }
  446. extension ArborescenceCell: UITableViewDelegate, UITableViewDataSource {
  447. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  448. switch model.self {
  449. case is MoDeliveryGoodsDetail:
  450. /// 现货商品
  451. return 3
  452. default:
  453. /// 套保商品
  454. return 2
  455. }
  456. }
  457. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  458. let cell = tableView.dequeueReusableCell(withIdentifier: SubArborescenceCellIdentifier, for: indexPath) as! SubArborescenceCell
  459. switch model.self {
  460. case is MoDeliveryGoodsDetail:
  461. /// 现货品种
  462. let obj = model as! MoDeliveryGoodsDetail
  463. switch indexPath.row {
  464. case 0:
  465. /// 商品
  466. cell.model = obj.gmlist
  467. /// 单位
  468. cell.unit = (try? DatabaseHelper.getEnumInfo(enumdiccode: "goodsunit")?.first(where: {$0.enumitemname == obj.data?.goodsunitid})?.enumdicname) ?? "--"
  469. case 1:
  470. /// 品牌
  471. cell.model = obj.gblist
  472. default:
  473. /// 套保品种
  474. cell.model = obj.mgList
  475. }
  476. default:
  477. /// 套保品种
  478. let obj = model as! MoErmcpMiddleGoodsDetail
  479. switch indexPath.row {
  480. case 0:
  481. /// 期货品种
  482. cell.isEx = true
  483. cell.model = obj.gplist
  484. default:
  485. /// 现货品种
  486. cell.isEx = false
  487. cell.model = obj.dglist
  488. }
  489. }
  490. cell.indexPath = (tableView.superview?.superview as? ArborescenceCell)?.indexPath
  491. cell.rowNum = indexPath.row
  492. cell.block = { (_ model: Any?, _ sender: Any?) in
  493. if let index = cell.indexPath?.row {
  494. switch cell.parent.self {
  495. case is MoDeliveryGoodsDetail:
  496. var obj = cell.parent as! MoDeliveryGoodsDetail
  497. switch model.self {
  498. case is [MoErmcpMiddleGoodsDetail]:
  499. /// 套保品种
  500. obj.mgExpland = !obj.mgExpland
  501. /// 设置图标
  502. cell.arborescence.setImage(UIImage(named: !obj.mgExpland ? "unsubarborescence" : "subarborescence"), for: .normal)
  503. case is [MoGoodsBrand]:
  504. /// 品牌
  505. obj.gbExpland = !obj.gbExpland
  506. /// 设置图标
  507. cell.arborescence.setImage(UIImage(named: !obj.gbExpland ? "unsubarborescence" : "subarborescence"), for: .normal)
  508. default:
  509. /// 商品
  510. obj.gmExpland = !obj.gmExpland
  511. /// 设置图标
  512. cell.arborescence.setImage(UIImage(named: !obj.gmExpland ? "unsubarborescence" : "subarborescence"), for: .normal)
  513. }
  514. cell.parent = obj
  515. (self.superController() as? GoodsVarietiesViewController)?.objs.replaceSubrange(index...index, with: [obj])
  516. default:
  517. var obj = cell.parent as! MoErmcpMiddleGoodsDetail
  518. switch model.self {
  519. case is [MoGoodsGroupEX]:
  520. /// 期货品种
  521. obj.gpExpland = !obj.gpExpland
  522. /// 设置图标
  523. cell.arborescence.setImage(UIImage(named: !obj.gpExpland ? "unsubarborescence" : "subarborescence"), for: .normal)
  524. default:
  525. /// 现货品种
  526. obj.dgExpland = !obj.dgExpland
  527. /// 设置图标
  528. cell.arborescence.setImage(UIImage(named: !obj.dgExpland ? "unsubarborescence" : "subarborescence"), for: .normal)
  529. }
  530. cell.parent = obj
  531. (self.superController() as? GoodsVarietiesViewController)?.objs.replaceSubrange(index...index, with: [obj])
  532. }
  533. /// 刷新列表
  534. self.tableView.reloadData()
  535. }
  536. }
  537. if let index = cell.indexPath?.row {
  538. cell.parent = (self.superController() as? GoodsVarietiesViewController)?.objs[index]
  539. }
  540. return cell
  541. }
  542. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  543. var height: CGFloat = 0.0
  544. switch model.self {
  545. case is MoDeliveryGoodsDetail:
  546. /// 现货品种
  547. let obj = model as! MoDeliveryGoodsDetail
  548. switch indexPath.row {
  549. case 0:
  550. /// 商品
  551. height += CGFloat((obj.gmExpland ? obj.gmlist.count : 0)+1)*45.0
  552. case 1:
  553. /// 品牌
  554. height += CGFloat((obj.gbExpland ? obj.gblist.count : 0)+1)*45.0
  555. default:
  556. /// 套保品种
  557. height += CGFloat((obj.mgExpland ? obj.mgList.count : 0)+1)*45.0
  558. if obj.mgExpland {
  559. height += CGFloat(obj.mgList.count)*45.0
  560. obj.mgList.forEach { mg in
  561. height += CGFloat(mg.gpExpland ? mg.gplist.count : 0)*45.0
  562. }
  563. }
  564. }
  565. default:
  566. /// 套保品种
  567. let obj = model as! MoErmcpMiddleGoodsDetail
  568. switch indexPath.row {
  569. case 0:
  570. /// 期货品种
  571. height += CGFloat((obj.gpExpland ? obj.gplist.count : 0)+1)*45.0
  572. if obj.gpExpland {
  573. height += CGFloat(obj.gplist.count)*45.0
  574. obj.gplist.forEach { gp in
  575. height += CGFloat(gp.gexpland ? gp.glist.count : 0)*45.0
  576. }
  577. }
  578. default:
  579. /// 现货品种
  580. height += CGFloat((obj.dgExpland ? obj.dglist.count : 0)+1)*45.0
  581. if obj.dgExpland {
  582. height += CGFloat(obj.dglist.count)*45.0
  583. obj.dglist.forEach { dg in
  584. height += CGFloat(dg.gmExpland ? dg.gmlist.count : 0)*45.0
  585. }
  586. }
  587. }
  588. }
  589. return height
  590. }
  591. }
  592. /// 第二层视图Cell
  593. class SubArborescenceCell: BaseTableViewCell<Any> {
  594. /// 树状按钮
  595. @IBOutlet weak var arborescence: UIButton!
  596. /// 标题
  597. @IBOutlet weak var leftTitle: UILabel!
  598. /// 标题
  599. @IBOutlet weak var midleTitle: UILabel!
  600. /// 标题
  601. @IBOutlet weak var rightTitle: UILabel!
  602. /// 列表
  603. @IBOutlet weak var tableView: UITableView!
  604. /// ThirdArborescencCellIdentifier
  605. let ThirdArborescencCellIdentifier = "ThirdArborescenc_Cell"
  606. /// 父数据
  607. var parent: Any?
  608. /// 所处位置
  609. var indexPath: IndexPath?
  610. /// 商品单位
  611. var unit: String = ""
  612. /// 数据
  613. override var model: Any? {
  614. didSet {
  615. if let selectIndex = (self.superController() as? GoodsVarietiesViewController)?.segmentedView.selectedIndex {
  616. if selectIndex == 0 {
  617. switch model.self {
  618. case is [MoErmcpMiddleGoodsDetail]:
  619. /// 套保品种
  620. let obj = model as! [MoErmcpMiddleGoodsDetail]
  621. leftTitle.attributedText = "套保品种".withFont(.font_16).withTextColor(.hex333())+" (\(obj.count))".withFont(.font_14).withTextColor(.hex999())
  622. midleTitle.text = "单位"
  623. rightTitle.text = "套保系数"
  624. case is [MoWrStandard]:
  625. /// 商品
  626. let types = model as! [MoWrStandard]
  627. leftTitle.attributedText = "商品".withFont(.font_16).withTextColor(.hex333())+" (\(types.count))".withFont(.font_14).withTextColor(.hex999())
  628. midleTitle.text = "单位"
  629. rightTitle.text = "标仓系数"
  630. case is [MoGoodsBrand]:
  631. /// 品牌
  632. let brands = model as! [MoGoodsBrand]
  633. leftTitle.attributedText = "品牌".withFont(.font_16).withTextColor(.hex333())+" (\(brands.count))".withFont(.font_14).withTextColor(.hex999())
  634. midleTitle.text = ""
  635. rightTitle.text = ""
  636. default: break
  637. }
  638. } else {
  639. if isEx {
  640. /// 期货品种
  641. let converts = model as! [MoGoodsGroupEX]
  642. leftTitle.attributedText = "期货品种".withFont(.font_16).withTextColor(.hex333())+" (\(converts.count))".withFont(.font_14).withTextColor(.hex999())
  643. midleTitle.text = "单位"
  644. rightTitle.text = "品种系数"
  645. } else {
  646. /// 现货品种
  647. let standards = model as! [MoDeliveryGoodsDetail]
  648. leftTitle.attributedText = "现货品种".withFont(.font_16).withTextColor(.hex333())+" (\(standards.count))".withFont(.font_14).withTextColor(.hex999())
  649. midleTitle.text = "单位"
  650. rightTitle.text = "套保系数"
  651. }
  652. }
  653. /// 刷新数据
  654. tableView.reloadData()
  655. }
  656. }
  657. }
  658. /// 造虐啊 由于数据特殊情况 为空的时候特殊处理
  659. var isEx: Bool = true
  660. /// onButtonPressed
  661. /// - Parameter sender: sender
  662. @IBAction func onButtonPressed(_ sender: UIButton) {
  663. if let bk = self.block { bk(self.model, OperatorType.expland) }
  664. }
  665. }
  666. extension SubArborescenceCell: UITableViewDelegate, UITableViewDataSource {
  667. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  668. return (model as? [Any])?.count ?? 0
  669. }
  670. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  671. let cell = tableView.dequeueReusableCell(withIdentifier: ThirdArborescencCellIdentifier, for: indexPath) as! ThirdArborescenceCell
  672. switch model.self {
  673. case is [MoErmcpMiddleGoodsDetail]:
  674. /// 套保品种
  675. let obj = (model as! [MoErmcpMiddleGoodsDetail])[indexPath.row].mg!
  676. let ratio = (model as! [MoErmcpMiddleGoodsDetail])[indexPath.row].convertratio
  677. cell.model = (key: "\(obj.middlegoodsname.isBlank())", middle: "\(obj.enumdicname.isBlank())", value: "\(ratio.toString(reserve: 2))")
  678. cell.subObj = (model as! [MoErmcpMiddleGoodsDetail])[indexPath.row]
  679. case is [MoWrStandard]:
  680. /// 商品
  681. let obj = (model as! [MoWrStandard])[indexPath.row]
  682. cell.model = (key: "\(obj.wrstandardname.isBlank())", middle: "\(obj.enumdicname.isBlank())", value: "\(obj.convertfactor)")
  683. case is [MoGoodsBrand]:
  684. /// 品牌
  685. let obj = (model as! [MoGoodsBrand])[indexPath.row]
  686. cell.model = (key: "\(obj.brandname.isBlank())", middle: "", value: "")
  687. case is [MoGoodsGroupEX]:
  688. /// 期货品种
  689. let obj = (model as! [MoGoodsGroupEX])[indexPath.row]
  690. cell.model = (key: "\(obj.exexchangecode.isBlank())-\(obj.goodsgroupname.isBlank())", middle: "\(obj.enumdicname.isBlank())", value: "\(obj.convertratio.toString(reserve: 2))")
  691. cell.subObj = obj
  692. case is [MoDeliveryGoodsDetail]:
  693. /// 现货品种
  694. let obj = (model as! [MoDeliveryGoodsDetail])[indexPath.row].data!
  695. let convertratio = (model as! [MoDeliveryGoodsDetail])[indexPath.row].convertratio
  696. cell.model = (key: "\(obj.deliverygoodsname.isBlank())", middle: "\(obj.enumdicname.isBlank())", value: "\(convertratio)")
  697. cell.subObj = (model as! [MoDeliveryGoodsDetail])[indexPath.row]
  698. default: break
  699. }
  700. cell.rowNum = indexPath.row
  701. /// 父类
  702. cell.parent = self.parent
  703. cell.callBack = { (_ mo: Any?, _ takeInfo: Any?) in
  704. if let obj = mo,
  705. let topRow = (self.superview?.superview?.superview as? ArborescenceCell)?.indexPath?.row {
  706. /// 最顶层的位置
  707. switch obj.self {
  708. case is MoErmcpMiddleGoodsDetail:
  709. /// 现货品种-套保品种-期货品种
  710. var middle = obj as! MoErmcpMiddleGoodsDetail
  711. middle.gpExpland = !middle.gpExpland
  712. var good = (self.superController() as! GoodsVarietiesViewController).objs[topRow] as! MoDeliveryGoodsDetail
  713. good.mgList.replaceSubrange(cell.rowNum!...cell.rowNum!, with: [middle])
  714. (self.superController() as! GoodsVarietiesViewController).objs.replaceSubrange(topRow...topRow, with: [good])
  715. case is MoGoodsGroupEX:
  716. /// 套保品种-期货品种-期货合约
  717. var group = obj as! MoGoodsGroupEX
  718. group.gexpland = !group.gexpland
  719. var good = (self.superController() as! GoodsVarietiesViewController).objs[topRow] as! MoErmcpMiddleGoodsDetail
  720. good.gplist.replaceSubrange(cell.rowNum!...cell.rowNum!, with: [group])
  721. (self.superController() as! GoodsVarietiesViewController).objs.replaceSubrange(topRow...topRow, with: [good])
  722. case is MoDeliveryGoodsDetail:
  723. /// 套保品种-现货品种-商品
  724. var goods = obj as! MoDeliveryGoodsDetail
  725. goods.gmExpland = !goods.gmExpland
  726. var good = (self.superController() as! GoodsVarietiesViewController).objs[topRow] as! MoErmcpMiddleGoodsDetail
  727. good.dglist.replaceSubrange(cell.rowNum!...cell.rowNum!, with: [goods])
  728. (self.superController() as! GoodsVarietiesViewController).objs.replaceSubrange(topRow...topRow, with: [good])
  729. default: break
  730. }
  731. }
  732. }
  733. return cell
  734. }
  735. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  736. switch model.self {
  737. case is [MoErmcpMiddleGoodsDetail]:
  738. /// 套保品种
  739. let obj = (model as! [MoErmcpMiddleGoodsDetail])[indexPath.row]
  740. return CGFloat((obj.gpExpland ? obj.gplist.count : 0)+2)*45.0
  741. case is [MoGoodsGroupEX]:
  742. /// 期货品种
  743. let obj = (model as! [MoGoodsGroupEX])[indexPath.row]
  744. return CGFloat((obj.gexpland ? obj.glist.count : 0)+2)*45.0
  745. case is [MoDeliveryGoodsDetail]:
  746. /// 现货品种
  747. let obj = (model as! [MoDeliveryGoodsDetail])[indexPath.row]
  748. return CGFloat((obj.gmExpland ? obj.gmlist.count : 0)+2)*45.0
  749. default:
  750. /// 商品、 品牌
  751. return 45.0
  752. }
  753. }
  754. }
  755. /// ThirdArborescenceCell
  756. class ThirdArborescenceCell: BaseTableViewCell<(key: String, middle: String, value: String)> {
  757. /// 背景视图
  758. @IBOutlet weak var bgView: AnimatableView!
  759. /// 右边标题
  760. @IBOutlet weak var rightTitle: UILabel!
  761. /// 中间标题
  762. @IBOutlet weak var middleTitle: UILabel!
  763. /// 左边标题
  764. @IBOutlet weak var leftTitle: UILabel!
  765. /// 左边标题
  766. @IBOutlet weak var tableView: UITableView!
  767. /// 树状按钮
  768. @IBOutlet weak var arborescence: UIButton!
  769. /// 标题
  770. @IBOutlet weak var headerLeftTitle: UILabel!
  771. /// 标题
  772. @IBOutlet weak var headerMidleTitle: UILabel!
  773. /// 标题
  774. @IBOutlet weak var headerRightTitle: UILabel!
  775. /// 回调
  776. var callBack: ((_ model: Any?, _ sender: Any?) -> Void)?
  777. /// 数据
  778. override var model: (key: String, middle: String, value: String)? {
  779. didSet {
  780. guard let obj = model else { return }
  781. /// 左边值
  782. leftTitle.text = obj.key
  783. /// 中间标题
  784. middleTitle.text = obj.middle
  785. /// 右边值
  786. rightTitle.text = obj.value
  787. }
  788. }
  789. /// 子数据
  790. var subObj: Any? {
  791. didSet {
  792. /// 异常
  793. guard let obj = subObj else {
  794. return
  795. }
  796. switch obj.self {
  797. case is MoErmcpMiddleGoodsDetail:
  798. /// 现货品种-套保品种-期货品种
  799. let sub = obj as! MoErmcpMiddleGoodsDetail
  800. headerLeftTitle.text = "期货品种" + " (\(sub.gplist.count))"
  801. headerMidleTitle.text = "单位"
  802. headerRightTitle.text = "品种系数"
  803. /// 设置图标
  804. arborescence.setImage(UIImage(named: !sub.gpExpland ? "unsubarborescence" : "subarborescence"), for: .normal)
  805. case is MoGoodsGroupEX:
  806. /// 套保品种-期货品种-期货合约
  807. let sub = obj as! MoGoodsGroupEX
  808. headerLeftTitle.text = "期货合约" + " (\((obj as! MoGoodsGroupEX).glist.count))"
  809. headerMidleTitle.text = "单位"
  810. headerRightTitle.text = "合约乘数"
  811. /// 设置图标
  812. arborescence.setImage(UIImage(named: !sub.gexpland ? "unsubarborescence" : "subarborescence"), for: .normal)
  813. case is MoDeliveryGoodsDetail:
  814. /// 套保品种-现货品种-商品
  815. let sub = obj as! MoDeliveryGoodsDetail
  816. headerLeftTitle.text = "商品" + " (\((obj as! MoDeliveryGoodsDetail).gmlist.count))"
  817. headerMidleTitle.text = "单位"
  818. headerRightTitle.text = "标仓系数"
  819. /// 设置图标
  820. arborescence.setImage(UIImage(named: !sub.gmExpland ? "unsubarborescence" : "subarborescence"), for: .normal)
  821. default: break
  822. }
  823. /// 刷新数据
  824. tableView.reloadData()
  825. }
  826. }
  827. /// CellIdentifier
  828. let CellIdentifier = "Four_Cell"
  829. /// 父数据
  830. var parent: Any? {
  831. didSet {
  832. switch parent.self {
  833. /// 现货品种
  834. case is MoDeliveryGoodsDetail:
  835. let obj = (parent as! MoDeliveryGoodsDetail).data
  836. bgView.borderColor = obj?.isvalid == 1 ? .hex90D7FF() : .hexCCC()
  837. bgView.backgroundColor = obj?.isvalid == 1 ? .E8F3F6() : .F4F4F4()
  838. leftTitle.textColor = obj?.isvalid == 1 ? .hex333() : .hexCCC()
  839. middleTitle.textColor = obj?.isvalid == 1 ? .hex333() : .hexCCC()
  840. rightTitle.textColor = obj?.isvalid == 1 ? .hex333() : .hexCCC()
  841. default:
  842. /// 套保品种
  843. let obj = (parent as! MoErmcpMiddleGoodsDetail).mg
  844. bgView.borderColor = obj?.isvalid == 1 ? .hex90D7FF() : .hexCCC()
  845. bgView.backgroundColor = obj?.isvalid == 1 ? .E8F3F6() : .F4F4F4()
  846. leftTitle.textColor = obj?.isvalid == 1 ? .hex333() : .hexCCC()
  847. middleTitle.textColor = obj?.isvalid == 1 ? .hex333() : .hexCCC()
  848. rightTitle.textColor = obj?.isvalid == 1 ? .hex333() : .hexCCC()
  849. }
  850. }
  851. }
  852. @IBAction func onButtonPressed(_ sender: UIButton) {
  853. /// 执行回调
  854. if let bk = callBack { bk(subObj, nil) }
  855. }
  856. }
  857. extension ThirdArborescenceCell: UITableViewDelegate, UITableViewDataSource {
  858. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  859. switch subObj.self {
  860. case is MoErmcpMiddleGoodsDetail:
  861. /// 现货品种-套保品种-期货品种
  862. return (subObj as! MoErmcpMiddleGoodsDetail).gplist.count
  863. case is MoGoodsGroupEX:
  864. /// 套保品种-期货品种-期货合约
  865. return (subObj as! MoGoodsGroupEX).glist.count
  866. case is MoDeliveryGoodsDetail:
  867. /// 套保品种-现货品种-商品
  868. return (subObj as! MoDeliveryGoodsDetail).gmlist.count
  869. default: return 0
  870. }
  871. }
  872. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  873. let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath) as! FourArborescenceCell
  874. switch subObj.self {
  875. case is MoErmcpMiddleGoodsDetail:
  876. /// 现货品种-套保品种-期货品种
  877. let obj = (subObj as! MoErmcpMiddleGoodsDetail).gplist[indexPath.row]
  878. cell.model = (key: "\(obj.exexchangecode.isBlank())-\(obj.goodsgroupname.isBlank())", middle: "\(obj.enumdicname.isBlank())", value: "\(obj.convertratio)")
  879. case is MoGoodsGroupEX:
  880. /// 套保品种-期货品种-期货合约
  881. let obj = (subObj as! MoGoodsGroupEX).glist[indexPath.row]
  882. cell.model = (key: "\(obj.goodsname)", middle: "\(obj.enumdicname)", value: "\(obj.agreeunit)")
  883. case is MoDeliveryGoodsDetail:
  884. /// 套保品种-现货品种-商品
  885. let obj = (subObj as! MoDeliveryGoodsDetail).gmlist[indexPath.row]
  886. cell.model = (key: "\(obj.wrstandardname.isBlank())", middle: "\(obj.enumdicname.isBlank())", value: "\(obj.convertfactor)")
  887. default: break
  888. }
  889. cell.parent = parent
  890. return cell
  891. }
  892. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  893. return 45.0
  894. }
  895. }
  896. /// ThirdArborescenceCell
  897. class FourArborescenceCell: BaseTableViewCell<(key: String, middle: String, value: String)> {
  898. /// 背景视图
  899. @IBOutlet weak var bgView: AnimatableView!
  900. /// 右边标题
  901. @IBOutlet weak var rightTitle: UILabel!
  902. /// 中间标题
  903. @IBOutlet weak var middleTitle: UILabel!
  904. /// 左边标题
  905. @IBOutlet weak var leftTitle: UILabel!
  906. /// 数据
  907. override var model: (key: String, middle: String, value: String)? {
  908. didSet {
  909. guard let obj = model else { return }
  910. /// 左边值
  911. leftTitle.text = obj.key
  912. /// 中间标题
  913. middleTitle.text = obj.middle
  914. /// 右边值
  915. rightTitle.text = obj.value
  916. }
  917. }
  918. /// 父数据
  919. var parent: Any? {
  920. didSet {
  921. switch parent.self {
  922. /// 现货品种
  923. case is MoDeliveryGoodsDetail:
  924. let obj = (parent as! MoDeliveryGoodsDetail).data
  925. bgView.borderColor = obj?.isvalid == 1 ? .hex90D7FF() : .hexCCC()
  926. bgView.backgroundColor = obj?.isvalid == 1 ? .E8F3F6() : .F4F4F4()
  927. leftTitle.textColor = obj?.isvalid == 1 ? .hex333() : .hexCCC()
  928. middleTitle.textColor = obj?.isvalid == 1 ? .hex333() : .hexCCC()
  929. rightTitle.textColor = obj?.isvalid == 1 ? .hex333() : .hexCCC()
  930. default:
  931. /// 套保品种
  932. let obj = (parent as! MoErmcpMiddleGoodsDetail).mg
  933. bgView.borderColor = obj?.isvalid == 1 ? .hex90D7FF() : .hexCCC()
  934. bgView.backgroundColor = obj?.isvalid == 1 ? .E8F3F6() : .F4F4F4()
  935. leftTitle.textColor = obj?.isvalid == 1 ? .hex333() : .hexCCC()
  936. middleTitle.textColor = obj?.isvalid == 1 ? .hex333() : .hexCCC()
  937. rightTitle.textColor = obj?.isvalid == 1 ? .hex333() : .hexCCC()
  938. }
  939. }
  940. }
  941. }