NewGoodsVarietiesViewController.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. //
  2. // NewGoodsVarietiesViewController.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 WHToast
  10. import IBAnimatable
  11. import ActionSheetPicker_3_0
  12. import NVActivityIndicatorView
  13. /// 新增现货品种视图容器控制类
  14. class NewGoodsVarietiesViewController: BaseViewController {
  15. // MARK: - 属性列表
  16. /// 现货品种名称
  17. @IBOutlet weak var goodsName: AnimatableTextField!
  18. /// 现货品种代码
  19. @IBOutlet weak var goodCode: AnimatableTextField!
  20. /// 单位
  21. @IBOutlet weak var unit: UIButton!
  22. /// 备注
  23. @IBOutlet weak var remark: AnimatableTextField!
  24. /// 套保品种列表
  25. @IBOutlet weak var hedgeTableView: UITableView! {
  26. didSet {
  27. hedgeDelegate = HedgeDelegate(self)
  28. hedgeTableView.dataSource = hedgeDelegate
  29. hedgeTableView.delegate = hedgeDelegate
  30. }
  31. }
  32. /// 套保品种列表高度约束
  33. @IBOutlet weak var hedgeLayoutConstraint: NSLayoutConstraint! {
  34. didSet {
  35. hedgeLayoutConstraint.constant = 35.0
  36. }
  37. }
  38. /// 品牌商品列表
  39. @IBOutlet weak var brandTableView: UITableView! {
  40. didSet {
  41. brandDelegate = BrandDelegate(self)
  42. brandTableView.dataSource = brandDelegate
  43. brandTableView.delegate = brandDelegate
  44. }
  45. }
  46. /// 品牌商品列表高度约束
  47. @IBOutlet weak var brandLayoutConstraint: NSLayoutConstraint! {
  48. didSet {
  49. brandLayoutConstraint.constant = 35.0
  50. }
  51. }
  52. /// 规格商品列表
  53. @IBOutlet weak var specTableView: UITableView! {
  54. didSet {
  55. specDelegate = SpecDelegate(self)
  56. specTableView.dataSource = specDelegate
  57. specTableView.delegate = specDelegate
  58. }
  59. }
  60. /// 品牌商品列表高度约束
  61. @IBOutlet weak var specLayoutConstraint: NSLayoutConstraint! {
  62. didSet {
  63. specLayoutConstraint.constant = 35.0
  64. }
  65. }
  66. /// 添加套保品种按钮
  67. @IBOutlet weak var addHedge: UIButton!
  68. /// 添加品牌按钮
  69. @IBOutlet weak var addBrand: UIButton!
  70. /// 添加规格按钮
  71. @IBOutlet weak var addSpec: UIButton!
  72. /// 提交按钮
  73. @IBOutlet weak var submit: UIButton!
  74. /// 新增
  75. var operatorType: OperatorType = .add
  76. /// BrandCellIdentifier
  77. let BrandCellIdentifier = "Brand_Cell"
  78. var brandDelegate: BrandDelegate?
  79. /// SpecCellIdentifier
  80. let SpecCellIdentifier = "Spec_Cell"
  81. var specDelegate: SpecDelegate?
  82. /// HedgeCellIdentifier
  83. let HedgeCellIdentifier = "Hedge_Cell"
  84. var hedgeDelegate: HedgeDelegate?
  85. /// 商品商品数据
  86. var wrStandardExs: [GLDWRStandardEx] = [] {
  87. didSet {
  88. /// 高度约束
  89. specLayoutConstraint.constant = (CGFloat(wrStandardExs.count)*145.0)+35.0
  90. /// 刷新列表
  91. specTableView.reloadData()
  92. }
  93. }
  94. /// 商品品牌数据
  95. var itemExs: [GLDDGFactoryItemEx] = [] {
  96. didSet {
  97. /// 高度约束
  98. brandLayoutConstraint.constant = (CGFloat(itemExs.count)*55.0)+35.0
  99. /// 刷新列表
  100. brandTableView.reloadData()
  101. }
  102. }
  103. /// 套保品种数据
  104. var convertDetailExs: [WRSConvertDetailEx] = [] {
  105. didSet {
  106. /// 高度约束
  107. hedgeLayoutConstraint.constant = (CGFloat(convertDetailExs.count)*145.0)+35.0
  108. /// 刷新列表
  109. hedgeTableView.reloadData()
  110. }
  111. }
  112. /// 商品单位数据信息
  113. var goodsUnits: [MoEnum] {
  114. return try! DatabaseHelper.getEnumInfo(enumdiccode: "goodsunit") ?? []
  115. }
  116. /// 商品单位
  117. var goodsUnit: MoEnum? {
  118. didSet {
  119. unit.setTitle(goodsUnit?.enumdicname, for: .normal)
  120. unit.setTitleColor(.hex333(), for: .normal)
  121. }
  122. }
  123. /// 现货品种详细信息
  124. var goodsDetail: MoDeliveryGoodsDetail?
  125. /// 商品组信息
  126. var goodsGroups: [MoGoodsGroup] = []
  127. // MARK: - 生命周期
  128. override func viewDidLoad() {
  129. super.viewDidLoad()
  130. // Do any additional setup after loading the view.
  131. }
  132. /// UI界面初始化
  133. override func buildView() {
  134. super.buildView()
  135. /// loding.....
  136. self.addLoadingView()
  137. /// 最少要一个数据
  138. wrStandardExs.append(GLDWRStandardEx())
  139. itemExs.append(GLDDGFactoryItemEx())
  140. convertDetailExs.append(WRSConvertDetailEx())
  141. }
  142. override func loadView() {
  143. super.loadView()
  144. /// 查询商品组信息
  145. queryGoodsGroup()
  146. }
  147. // MARK: - 交互相关
  148. /// onButtonPressed
  149. /// - Parameter sender: sender
  150. @IBAction fileprivate func onButtonPressed(_ sender: UIControl) {
  151. switch sender {
  152. case submit: /// 完成
  153. doDeliveryGoodsApply(operatorType == .add ? 1 : 2)
  154. case addSpec: /// 新增商品
  155. let ex = GLDWRStandardEx()
  156. self.wrStandardExs.append(ex)
  157. case addBrand: /// 新增品牌
  158. let ex = GLDDGFactoryItemEx()
  159. self.itemExs.append(ex)
  160. case addHedge: /// 新增套保品种
  161. let ex = WRSConvertDetailEx()
  162. self.convertDetailExs.append(ex)
  163. case unit: /// 商品单位
  164. ActionSheetStringPicker.show(withTitle: "商品单位", rows: self.goodsUnits.map({ (obj) -> String in
  165. return obj.enumdicname
  166. }), initialSelection: 0, doneBlock: { picker, value, index in
  167. self.goodsUnit = self.goodsUnits[value]
  168. },
  169. cancel: { picker in
  170. return
  171. },
  172. origin: sender)
  173. default: break
  174. }
  175. }
  176. // MARK: - 接口请求
  177. /// 查询商品组信息
  178. fileprivate func queryGoodsGroup() {
  179. /// 异常
  180. guard let contractManager = MTP2BusinessCore.shared.contractManager else { return }
  181. /// 查询商品组信息
  182. contractManager.queryGoodsGroup(excludecfg: 1) { (_, _, objs) in
  183. DispatchQueue.main.async {
  184. /// 商品组信息
  185. self.goodsGroups = objs ?? []
  186. /// 显示现货品种数据信息
  187. self.showWrStandardInfo()
  188. }
  189. }
  190. }
  191. /// 显示现货品种数据信息
  192. fileprivate func showWrStandardInfo() {
  193. /// 异常
  194. guard let contractManager = MTP2BusinessCore.shared.contractManager,
  195. let id = goodsDetail?.data?.deliverygoodsid else { return }
  196. /// startAnimating
  197. _anim?.startAnimating()
  198. /// 查询详情
  199. contractManager.queryDeliveryGoodsDetail(deliverygoodsid: id, qrytradegoods: nil) { (isComplete, er, objs) in
  200. DispatchQueue.main.async {
  201. /// stopAnimating
  202. self._anim?.stopAnimating()
  203. if !isComplete {
  204. WHToast.showMessage("现货品种详情查询失败", duration: 1.5, finishHandler: {})
  205. return
  206. }
  207. guard let obj = objs?.first else {
  208. WHToast.showMessage("详细数据为空", duration: 1.5, finishHandler: {})
  209. return
  210. }
  211. /// 商品名称
  212. self.goodsName.text = obj.data?.deliverygoodsname
  213. /// 商品代码
  214. self.goodCode.text = obj.data?.deliverygoodscode
  215. /// 单位
  216. self.goodsUnit = self.goodsUnits.first(where: {$0.enumitemname == obj.data?.goodsunitid})
  217. /// 备注
  218. if let rk = obj.data?.remark, rk != "" {
  219. self.remark.text = rk
  220. }
  221. /// 品牌数据
  222. if obj.gblist.count != 0 {
  223. /// 移出所有数据
  224. var exs: [GLDDGFactoryItemEx] = []
  225. obj.gblist.forEach { (gb) in
  226. var ex = GLDDGFactoryItemEx()
  227. ex.dgfactoryitemvalue = gb.brandname
  228. ex.dgfactoryitemid = UInt64(gb.brandid)
  229. exs.append(ex)
  230. }
  231. self.itemExs = exs
  232. }
  233. /// 商品数据数据
  234. if obj.gmlist.count != 0 {
  235. /// 移出所有数据
  236. var exs: [GLDWRStandardEx] = []
  237. obj.gmlist.forEach { (gm) in
  238. var ex = GLDWRStandardEx()
  239. ex.wrstandardname = gm.wrstandardname
  240. ex.wrstandardid = UInt64(gm.wrstandardid)
  241. ex.convertfactor = gm.convertfactor
  242. ex.unitid = UInt64(gm.unitid)
  243. exs.append(ex)
  244. }
  245. self.wrStandardExs = exs
  246. }
  247. /// 套保信息
  248. if obj.mgList.count != 0 {
  249. /// 移出所有数据
  250. var exs: [WRSConvertDetailEx] = []
  251. obj.mgList.forEach { (mg) in
  252. var ex = WRSConvertDetailEx()
  253. ex.middlegoodsid = UInt64(mg.mg?.goodsgroupid ?? 0)
  254. ex.convertratio = mg.convertratio
  255. ex.unitid = UInt64(mg.mg?.goodsunitid ?? 0)
  256. exs.append(ex)
  257. }
  258. self.convertDetailExs = exs
  259. }
  260. }
  261. }
  262. }
  263. /// 现货品种申请增加请求方法
  264. fileprivate func doDeliveryGoodsApply(_ type: Int32) {
  265. /// 异常
  266. guard let contractManager = MTP2BusinessCore.shared.contractManager,
  267. check() else {
  268. return
  269. }
  270. /// 商品代码
  271. let goodscode = goodCode.text
  272. /// 备注
  273. let rk = remark.text
  274. /// 商品名称
  275. let goodsname = goodsName.text
  276. /// 单位ID
  277. let unitid = goodsUnit?.enumitemname != nil ? UInt64(goodsUnit?.enumitemname ?? 0) : nil
  278. /// 现货品种商品ID
  279. let goodsid = operatorType == .add ? nil : UInt64(goodsDetail?.data?.deliverygoodsid ?? 0)
  280. /// 开启Loading
  281. NVActivityIndicatorPresenter.sharedInstance.startAnimating(animActivityData("请求中..."))
  282. /// 发送请求
  283. contractManager.requestDeliveryGoodsApply(deliverygoodsid: goodsid, deliverygoodscode: goodscode, deliverygoodsname: goodsname, unitid: unitid, type: type, remark: rk, wrstandards: wrStandardExs, items: itemExs, wrsconvertdetails: convertDetailExs) { (isComplete, er, rsp) in
  284. DispatchQueue.main.async {
  285. /// dismisss loding.....
  286. NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
  287. /// 请求失败
  288. if !isComplete {
  289. WHToast.showMessage("请求失败,原因:\(rsp?.retDesc ?? "未知错误")", duration: 1.5, finishHandler: {})
  290. return
  291. }
  292. /// 请求成功
  293. WHToast.showSuccess(withMessage: "请求成功", duration: 1.5, finishHandler: {
  294. /// 执行回调
  295. if let block = self.popBlock { block(self.operatorType) }
  296. /// 返回上层视图
  297. self.navigationController?.popViewController(animated: true)
  298. })
  299. }
  300. }
  301. }
  302. // MARK: - 合规性校验
  303. /// 合规性校验
  304. /// - Returns: Bool
  305. fileprivate func check() -> Bool {
  306. /// 请输入现货品种名称
  307. guard let name = goodsName.text, name.count != 0 else {
  308. WHToast.showMessage("请输入现货品种名称", duration: 1.5, finishHandler: {
  309. self.goodsName.becomeFirstResponder()
  310. })
  311. return false
  312. }
  313. /// 现货品种代码只能为字母、数字组合
  314. guard let code = goodCode.text, !code.isChinese() else {
  315. WHToast.showMessage("现货品种代码只能为字母、数字组合", duration: 1.5, finishHandler: {
  316. self.goodsName.becomeFirstResponder()
  317. })
  318. return false
  319. }
  320. /// 请选择单位
  321. guard let _ = goodsUnit else {
  322. WHToast.showMessage("请选择单位", duration: 1.5, finishHandler: {})
  323. return false
  324. }
  325. /// 请选择品牌
  326. if itemExs.contains(where: {$0.dgfactoryitemvalue == ""}) {
  327. WHToast.showMessage("请输入品牌", duration: 1.5, finishHandler: {})
  328. return false
  329. }
  330. /// 请选择商品
  331. if wrStandardExs.contains(where: {$0.wrstandardname == ""}) {
  332. WHToast.showMessage("请输入商品", duration: 1.5, finishHandler: {})
  333. return false
  334. }
  335. /// 请选择商品对应的单位
  336. if let obj = wrStandardExs.first(where: {$0.unitid == 0}) {
  337. /// 请选择商品
  338. WHToast.showMessage("请选择\(obj.wrstandardname)对应的单位", duration: 1.5, finishHandler: {})
  339. return false
  340. }
  341. /// 请输入\(obj.modelname)对应标仓系数!
  342. if let obj = wrStandardExs.first(where: {$0.convertfactor == 0.0}) {
  343. /// 请选择商品
  344. WHToast.showMessage("请输入\(obj.wrstandardname)对应标仓系数", duration: 1.5, finishHandler: {})
  345. return false
  346. }
  347. /// 请选择套保品种
  348. if convertDetailExs.contains(where: {$0.middlegoodsid == 0}) {
  349. WHToast.showMessage("请选择套保品种", duration: 1.5, finishHandler: {})
  350. return false
  351. }
  352. /// 请输入套保系数
  353. if convertDetailExs.contains(where: {$0.convertratio == 0.0}) {
  354. WHToast.showMessage("请输入套保系数", duration: 1.5, finishHandler: {})
  355. return false
  356. }
  357. return true
  358. }
  359. }
  360. // MARK: - BrandDelegate UITableViewDelegate & UITableViewDataSource
  361. class BrandDelegate: NSCoder, UITableViewDelegate, UITableViewDataSource {
  362. var owner: NewGoodsVarietiesViewController
  363. init(_ owner: NewGoodsVarietiesViewController) {
  364. self.owner = owner
  365. }
  366. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  367. return self.owner.itemExs.count
  368. }
  369. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  370. let cell = tableView.dequeueReusableCell(withIdentifier: self.owner.BrandCellIdentifier, for: indexPath) as! BrandCell
  371. cell.rowNum = indexPath.row
  372. cell.model = self.owner.itemExs[indexPath.row]
  373. cell.block = { (_ model: Any?, _ sender: Any?) in
  374. if let name = model as? String, let index = sender as? Int {
  375. self.owner.itemExs[index].dgfactoryitemvalue = name
  376. } else {
  377. /// 触发删除操作
  378. if let index = sender as? Int {
  379. if self.owner.itemExs.count > 1 {
  380. self.owner.itemExs.remove(at: index)
  381. } else {
  382. WHToast.showMessage("至少需要输入一项品牌", duration: 2, finishHandler: {})
  383. }
  384. }
  385. }
  386. }
  387. return cell
  388. }
  389. }
  390. // MARK: - SpecDelegate UITableViewDelegate & UITableViewDataSource
  391. class SpecDelegate: NSCoder, UITableViewDelegate, UITableViewDataSource {
  392. var owner: NewGoodsVarietiesViewController
  393. init(_ owner: NewGoodsVarietiesViewController) {
  394. self.owner = owner
  395. }
  396. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  397. return self.owner.wrStandardExs.count
  398. }
  399. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  400. let cell = tableView.dequeueReusableCell(withIdentifier: self.owner.SpecCellIdentifier, for: indexPath) as! SpecCell
  401. cell.rowNum = indexPath.row
  402. cell.model = self.owner.wrStandardExs[indexPath.row]
  403. cell.block = { (_ model: Any?, _ sender: Any?) in
  404. if let name = model as? (spec: String, con: String, unit: MoEnum?), let index = sender as? Int {
  405. self.owner.wrStandardExs[index].wrstandardname = name.spec
  406. self.owner.wrStandardExs[index].convertfactor = Double(name.con) ?? 0.0
  407. if let id = name.unit?.enumitemname {
  408. self.owner.wrStandardExs[index].unitid = UInt64(id)
  409. }
  410. } else {
  411. /// 触发删除操作
  412. if let index = sender as? Int {
  413. if self.owner.wrStandardExs.count > 1 {
  414. self.owner.wrStandardExs.remove(at: index)
  415. } else {
  416. WHToast.showMessage("至少需要输入一项商品", duration: 2, finishHandler: {})
  417. }
  418. }
  419. }
  420. }
  421. return cell
  422. }
  423. }
  424. // MARK: - HedgeDelegate UITableViewDelegate & UITableViewDataSource
  425. class HedgeDelegate: NSCoder, UITableViewDelegate, UITableViewDataSource {
  426. var owner: NewGoodsVarietiesViewController
  427. init(_ owner: NewGoodsVarietiesViewController) {
  428. self.owner = owner
  429. }
  430. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  431. return self.owner.convertDetailExs.count
  432. }
  433. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  434. let cell = tableView.dequeueReusableCell(withIdentifier: self.owner.HedgeCellIdentifier, for: indexPath) as! HedgeCell
  435. cell.rowNum = indexPath.row
  436. cell.model = self.owner.convertDetailExs[indexPath.row]
  437. cell.block = { (_ model: Any?, _ sender: Any?) in
  438. if let obj = model as? (Int, Double, Int), let index = sender as? Int {
  439. if obj.0 != 0 { self.owner.convertDetailExs[index].middlegoodsid = UInt64(obj.0) }
  440. self.owner.convertDetailExs[index].convertratio = obj.1
  441. if obj.2 != 0 {
  442. self.owner.convertDetailExs[index].unitid = UInt64(obj.2)
  443. }
  444. } else {
  445. /// 触发删除操作
  446. if let index = sender as? Int {
  447. if self.owner.convertDetailExs.count > 1 {
  448. self.owner.convertDetailExs.remove(at: index)
  449. } else {
  450. WHToast.showMessage("至少需要填入一项套保品种", duration: 2, finishHandler: {})
  451. }
  452. }
  453. }
  454. }
  455. return cell
  456. }
  457. }
  458. class SpecCell: BaseTableViewCell<Any> {
  459. /// 标题
  460. @IBOutlet weak var title: UILabel!
  461. /// 单位标题
  462. @IBOutlet weak var unitTitle: UILabel!
  463. /// 单位
  464. @IBOutlet weak var unit: UIButton!
  465. /// 子标题
  466. @IBOutlet weak var subTitle: UILabel!
  467. /// 商品
  468. @IBOutlet weak var spec: AnimatableTextField!
  469. /// 标仓系数
  470. @IBOutlet weak var coefficient: AnimatableTextField!
  471. /// 删除按钮
  472. @IBOutlet weak var delete: UIButton!
  473. /// 商品单位
  474. var goodsUnit: MoEnum? {
  475. didSet {
  476. if let obj = goodsUnit {
  477. unit.setTitle(obj.enumdicname, for: .normal)
  478. unit.setTitleColor(.hex333(), for: .normal)
  479. } else {
  480. unit.setTitle("请选择单位", for: .normal)
  481. unit.setTitleColor(.hexCCC(), for: .normal)
  482. }
  483. }
  484. }
  485. /// 当前位置
  486. override var rowNum: Int? {
  487. didSet {
  488. title.text = "商品 \((rowNum ?? 0)+1)"
  489. unitTitle.text = "单位 \((rowNum ?? 0)+1)"
  490. subTitle.text = "标仓系数 \((rowNum ?? 0)+1)"
  491. }
  492. }
  493. override var model: Any? {
  494. didSet {
  495. guard let obj = model as? GLDWRStandardEx else {
  496. return
  497. }
  498. /// 商品名称
  499. spec.text = obj.wrstandardname
  500. /// 标仓系数
  501. if obj.convertfactor != 0.0 {
  502. coefficient.text = "\(obj.convertfactor)"
  503. }
  504. /// 执行回调
  505. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.5) {
  506. /// 商品单位
  507. self.goodsUnit = (self.superController() as? NewGoodsVarietiesViewController)?.goodsUnits.first(where: {$0.enumitemname == obj.unitid})
  508. }
  509. }
  510. }
  511. /// onButtonPressed
  512. /// - Parameter sender: sender
  513. @IBAction fileprivate func onButtonPressed(_ sender: UIButton) {
  514. switch sender {
  515. case delete: /// 删除按钮
  516. if let bk = self.block { bk(nil, self.rowNum) }
  517. case unit: /// 单位
  518. if let units = (self.superController() as? NewGoodsVarietiesViewController)?.goodsUnits {
  519. /// 商品单位
  520. ActionSheetStringPicker.show(withTitle: "商品单位", rows: units.map({ (obj) -> String in
  521. return obj.enumdicname
  522. }), initialSelection: 0, doneBlock: { picker, value, index in
  523. self.goodsUnit = units[value]
  524. /// 执行回调
  525. if let bk = self.block {
  526. bk((spec: self.spec.text, con: self.coefficient.text, unit: self.goodsUnit), self.rowNum)
  527. }
  528. },
  529. cancel: { picker in
  530. return
  531. },
  532. origin: sender)
  533. }
  534. default: break
  535. }
  536. }
  537. }
  538. extension SpecCell: UITextFieldDelegate {
  539. /// textFieldDidEndEditing
  540. /// - Parameter textField: textField
  541. func textFieldDidEndEditing(_ textField: UITextField) {
  542. if textField == spec {
  543. coefficient.becomeFirstResponder()
  544. }
  545. if let bk = self.block { bk((spec: spec.text, con: coefficient.text, unit: self.goodsUnit), self.rowNum) }
  546. }
  547. }
  548. class BrandCell: BaseTableViewCell<Any> {
  549. /// 标题
  550. @IBOutlet weak var title: UILabel!
  551. /// 品牌
  552. @IBOutlet weak var brand: AnimatableTextField!
  553. /// 删除按钮
  554. @IBOutlet weak var delete: UIButton!
  555. /// 当前位置
  556. override var rowNum: Int? {
  557. didSet {
  558. title.text = "品牌 \((rowNum ?? 0)+1)"
  559. }
  560. }
  561. /// 数据
  562. override var model: Any? {
  563. didSet {
  564. guard let obj = model as? GLDDGFactoryItemEx else {
  565. return
  566. }
  567. brand.text = obj.dgfactoryitemvalue
  568. }
  569. }
  570. /// onButtonPressed
  571. /// - Parameter sender: sender
  572. @IBAction fileprivate func onButtonPressed(_ sender: UIButton) {
  573. switch sender {
  574. case delete: /// 删除按钮
  575. if let bk = self.block { bk(nil, self.rowNum) }
  576. default: break
  577. }
  578. }
  579. }
  580. extension BrandCell: UITextFieldDelegate {
  581. /// 执行回调
  582. func textFieldDidEndEditing(_ textField: UITextField) {
  583. if let bk = self.block { bk(textField.text, self.rowNum) }
  584. }
  585. }
  586. class HedgeCell: BaseTableViewCell<Any> {
  587. /// 套保品种
  588. @IBOutlet weak var hedge: UIButton!
  589. /// 单位
  590. @IBOutlet weak var unit: UILabel!
  591. /// 套保系数
  592. @IBOutlet weak var coefficient: AnimatableTextField!
  593. /// 删除按钮
  594. @IBOutlet weak var delete: UIButton!
  595. /// 标题1
  596. @IBOutlet weak var title1: UILabel!
  597. /// 标题2
  598. @IBOutlet weak var title2: UILabel!
  599. /// 当前位置
  600. override var rowNum: Int? {
  601. didSet {
  602. title1.text = "套保品种\((rowNum ?? 0)+1)"
  603. title2.text = "套保系数\((rowNum ?? 0)+1)"
  604. }
  605. }
  606. override var model: Any? {
  607. didSet {
  608. guard let obj = model as? WRSConvertDetailEx else { return }
  609. /// 套保系数
  610. if obj.convertratio != 0.0 {
  611. coefficient.text = "\(obj.convertratio)"
  612. }
  613. /// 执行回调
  614. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.5) {
  615. /// 获取对应的商品组信息
  616. self.group = (self.superController() as? NewGoodsVarietiesViewController)?.goodsGroups.first(where: {$0.goodsgroupid == obj.middlegoodsid})
  617. }
  618. }
  619. }
  620. /// 套保品种
  621. var group: MoGoodsGroup? {
  622. didSet {
  623. /// 异常
  624. guard let obj = group else { return }
  625. /// 商品组
  626. hedge.setTitle(obj.goodsgroupname, for: .normal)
  627. hedge.setTitleColor(.hex333(), for: .normal)
  628. /// 单位
  629. unit.text = obj.enumdicname
  630. unit.textColor = .hex333()
  631. }
  632. }
  633. @IBAction fileprivate func onButtonPressed(_ sender: UIButton) {
  634. switch sender {
  635. case delete: /// 删除按钮
  636. if let bk = self.block { bk(nil, self.rowNum) }
  637. case hedge: /// 套保品种
  638. /// 商品组信息
  639. let groups = (self.superController() as? NewGoodsVarietiesViewController)?.goodsGroups ?? []
  640. /// 过滤掉已选择得数据信息
  641. var shows: [MoGoodsGroup] = []
  642. if let exs = (self.superController() as? NewGoodsVarietiesViewController)?.convertDetailExs, exs.count != 0, groups.count != 0 {
  643. groups.forEach { (obj) in
  644. if !exs.contains(where: {$0.middlegoodsid == obj.goodsgroupid}) {
  645. shows.append(obj)
  646. }
  647. }
  648. }
  649. /// 交易所以及交易所下的商品组信息
  650. var exs: [(name: String, id: Int, grs: [MoGoodsGroup])] = []
  651. guard let ges = MTP2BusinessCore.shared.goodsManager?.exchanges, ges.count != 0 else {
  652. WHToast.showMessage("没有对应的交易所配置信息", duration: 1.5, finishHandler: {})
  653. return
  654. }
  655. /// 数据归类
  656. for obj in ges {
  657. exs.append((name: obj.exexchangename, id: obj.autoid, grs: groups.filter({$0.exexchangeid == obj.autoid})))
  658. }
  659. let mutable = MutablePickerViewController.showMutableString(title: "套保品种选择", leftTitles: exs.map({ obj in
  660. return obj.name
  661. }), rightTitles: exs.map({ obj in
  662. return obj.grs.map { obj in
  663. return obj.goodsgroupname
  664. }
  665. }), cancel: "取消", sure: "确定", select: (0,0)) { left, right in
  666. /// 设置选中的商品组信息
  667. self.group = exs[left].grs[right]
  668. /// 执行回调
  669. if let obj = self.group, let bk = self.block {
  670. bk((obj.goodsgroupid, Double(self.coefficient.text ?? "0.0") ?? 0.0, obj.goodunitid), self.rowNum)
  671. }
  672. }
  673. /// 显示
  674. self.superController()?.present(mutable, animated: true, completion: {})
  675. default: break
  676. }
  677. }
  678. }
  679. extension HedgeCell: UITextFieldDelegate {
  680. /// 执行回调
  681. func textFieldDidEndEditing(_ textField: UITextField) {
  682. /// 执行回调
  683. if let bk = self.block, let ratio = Double(textField.text ?? "0.0") {
  684. /// 为空 传-1
  685. bk((0, ratio, 0), self.rowNum)
  686. }
  687. }
  688. }