// // NewGoodsVarietiesViewController.swift // MTP2_iOS // // Created by Muchinfo on 2021/1/2. // Copyright © 2021 Muchinfo. All rights reserved. // import UIKit import WHToast import IBAnimatable import ActionSheetPicker_3_0 import NVActivityIndicatorView /// 新增现货品种视图容器控制类 class NewGoodsVarietiesViewController: BaseViewController { // MARK: - 属性列表 /// 现货品种名称 @IBOutlet weak var goodsName: AnimatableTextField! /// 现货品种代码 @IBOutlet weak var goodCode: AnimatableTextField! /// 单位 @IBOutlet weak var unit: UIButton! /// 备注 @IBOutlet weak var remark: AnimatableTextField! /// 套保品种列表 @IBOutlet weak var hedgeTableView: UITableView! { didSet { hedgeDelegate = HedgeDelegate(self) hedgeTableView.dataSource = hedgeDelegate hedgeTableView.delegate = hedgeDelegate } } /// 套保品种列表高度约束 @IBOutlet weak var hedgeLayoutConstraint: NSLayoutConstraint! { didSet { hedgeLayoutConstraint.constant = 35.0 } } /// 品牌商品列表 @IBOutlet weak var brandTableView: UITableView! { didSet { brandDelegate = BrandDelegate(self) brandTableView.dataSource = brandDelegate brandTableView.delegate = brandDelegate } } /// 品牌商品列表高度约束 @IBOutlet weak var brandLayoutConstraint: NSLayoutConstraint! { didSet { brandLayoutConstraint.constant = 35.0 } } /// 规格商品列表 @IBOutlet weak var specTableView: UITableView! { didSet { specDelegate = SpecDelegate(self) specTableView.dataSource = specDelegate specTableView.delegate = specDelegate } } /// 品牌商品列表高度约束 @IBOutlet weak var specLayoutConstraint: NSLayoutConstraint! { didSet { specLayoutConstraint.constant = 35.0 } } /// 添加套保品种按钮 @IBOutlet weak var addHedge: UIButton! /// 添加品牌按钮 @IBOutlet weak var addBrand: UIButton! /// 添加规格按钮 @IBOutlet weak var addSpec: UIButton! /// 提交按钮 @IBOutlet weak var submit: UIButton! /// 新增 var operatorType: OperatorType = .add /// BrandCellIdentifier let BrandCellIdentifier = "Brand_Cell" var brandDelegate: BrandDelegate? /// SpecCellIdentifier let SpecCellIdentifier = "Spec_Cell" var specDelegate: SpecDelegate? /// HedgeCellIdentifier let HedgeCellIdentifier = "Hedge_Cell" var hedgeDelegate: HedgeDelegate? /// 商品商品数据 var wrStandardExs: [GLDWRStandardEx] = [] { didSet { /// 高度约束 specLayoutConstraint.constant = (CGFloat(wrStandardExs.count)*145.0)+35.0 /// 刷新列表 specTableView.reloadData() } } /// 商品品牌数据 var itemExs: [GLDDGFactoryItemEx] = [] { didSet { /// 高度约束 brandLayoutConstraint.constant = (CGFloat(itemExs.count)*55.0)+35.0 /// 刷新列表 brandTableView.reloadData() } } /// 套保品种数据 var convertDetailExs: [WRSConvertDetailEx] = [] { didSet { /// 高度约束 hedgeLayoutConstraint.constant = (CGFloat(convertDetailExs.count)*145.0)+35.0 /// 刷新列表 hedgeTableView.reloadData() } } /// 商品单位数据信息 var goodsUnits: [MoEnum] { return try! DatabaseHelper.getEnumInfo(enumdiccode: "goodsunit") ?? [] } /// 商品单位 var goodsUnit: MoEnum? { didSet { unit.setTitle(goodsUnit?.enumdicname, for: .normal) unit.setTitleColor(.hex333(), for: .normal) } } /// 现货品种详细信息 var goodsDetail: MoDeliveryGoodsDetail? /// 商品组信息 var goodsGroups: [MoGoodsGroup] = [] // MARK: - 生命周期 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } /// UI界面初始化 override func buildView() { super.buildView() /// loding..... self.addLoadingView() /// 最少要一个数据 wrStandardExs.append(GLDWRStandardEx()) itemExs.append(GLDDGFactoryItemEx()) convertDetailExs.append(WRSConvertDetailEx()) } override func loadView() { super.loadView() /// 查询商品组信息 queryGoodsGroup() } // MARK: - 交互相关 /// onButtonPressed /// - Parameter sender: sender @IBAction fileprivate func onButtonPressed(_ sender: UIControl) { switch sender { case submit: /// 完成 doDeliveryGoodsApply(operatorType == .add ? 1 : 2) case addSpec: /// 新增商品 let ex = GLDWRStandardEx() self.wrStandardExs.append(ex) case addBrand: /// 新增品牌 let ex = GLDDGFactoryItemEx() self.itemExs.append(ex) case addHedge: /// 新增套保品种 let ex = WRSConvertDetailEx() self.convertDetailExs.append(ex) case unit: /// 商品单位 ActionSheetStringPicker.show(withTitle: "商品单位", rows: self.goodsUnits.map({ (obj) -> String in return obj.enumdicname }), initialSelection: 0, doneBlock: { picker, value, index in self.goodsUnit = self.goodsUnits[value] }, cancel: { picker in return }, origin: sender) default: break } } // MARK: - 接口请求 /// 查询商品组信息 fileprivate func queryGoodsGroup() { /// 异常 guard let contractManager = MTP2BusinessCore.shared.contractManager else { return } /// 查询商品组信息 contractManager.queryGoodsGroup(excludecfg: 1) { (_, _, objs) in DispatchQueue.main.async { /// 商品组信息 self.goodsGroups = objs ?? [] /// 显示现货品种数据信息 self.showWrStandardInfo() } } } /// 显示现货品种数据信息 fileprivate func showWrStandardInfo() { /// 异常 guard let contractManager = MTP2BusinessCore.shared.contractManager, let id = goodsDetail?.data?.deliverygoodsid else { return } /// startAnimating _anim?.startAnimating() /// 查询详情 contractManager.queryDeliveryGoodsDetail(deliverygoodsid: id, qrytradegoods: nil) { (isComplete, er, objs) in DispatchQueue.main.async { /// stopAnimating self._anim?.stopAnimating() if !isComplete { WHToast.showMessage("现货品种详情查询失败", duration: 1.5, finishHandler: {}) return } guard let obj = objs?.first else { WHToast.showMessage("详细数据为空", duration: 1.5, finishHandler: {}) return } /// 商品名称 self.goodsName.text = obj.data?.deliverygoodsname /// 商品代码 self.goodCode.text = obj.data?.deliverygoodscode /// 单位 self.goodsUnit = self.goodsUnits.first(where: {$0.enumitemname == obj.data?.goodsunitid}) /// 备注 if let rk = obj.data?.remark, rk != "" { self.remark.text = rk } /// 品牌数据 if obj.gblist.count != 0 { /// 移出所有数据 var exs: [GLDDGFactoryItemEx] = [] obj.gblist.forEach { (gb) in var ex = GLDDGFactoryItemEx() ex.dgfactoryitemvalue = gb.brandname ex.dgfactoryitemid = UInt64(gb.brandid) exs.append(ex) } self.itemExs = exs } /// 商品数据数据 if obj.gmlist.count != 0 { /// 移出所有数据 var exs: [GLDWRStandardEx] = [] obj.gmlist.forEach { (gm) in var ex = GLDWRStandardEx() ex.wrstandardname = gm.wrstandardname ex.wrstandardid = UInt64(gm.wrstandardid) ex.convertfactor = gm.convertfactor ex.unitid = UInt64(gm.unitid) exs.append(ex) } self.wrStandardExs = exs } /// 套保信息 if obj.mgList.count != 0 { /// 移出所有数据 var exs: [WRSConvertDetailEx] = [] obj.mgList.forEach { (mg) in var ex = WRSConvertDetailEx() ex.middlegoodsid = UInt64(mg.mg?.goodsgroupid ?? 0) ex.convertratio = mg.convertratio ex.unitid = UInt64(mg.mg?.goodsunitid ?? 0) exs.append(ex) } self.convertDetailExs = exs } } } } /// 现货品种申请增加请求方法 fileprivate func doDeliveryGoodsApply(_ type: Int32) { /// 异常 guard let contractManager = MTP2BusinessCore.shared.contractManager, check() else { return } /// 商品代码 let goodscode = goodCode.text /// 备注 let rk = remark.text /// 商品名称 let goodsname = goodsName.text /// 单位ID let unitid = goodsUnit?.enumitemname != nil ? UInt64(goodsUnit?.enumitemname ?? 0) : nil /// 现货品种商品ID let goodsid = operatorType == .add ? nil : UInt64(goodsDetail?.data?.deliverygoodsid ?? 0) /// 开启Loading NVActivityIndicatorPresenter.sharedInstance.startAnimating(animActivityData("请求中...")) /// 发送请求 contractManager.requestDeliveryGoodsApply(deliverygoodsid: goodsid, deliverygoodscode: goodscode, deliverygoodsname: goodsname, unitid: unitid, type: type, remark: rk, wrstandards: wrStandardExs, items: itemExs, wrsconvertdetails: convertDetailExs) { (isComplete, er, rsp) in DispatchQueue.main.async { /// dismisss loding..... NVActivityIndicatorPresenter.sharedInstance.stopAnimating() /// 请求失败 if !isComplete { WHToast.showMessage("请求失败,原因:\(rsp?.retDesc ?? "未知错误")", duration: 1.5, finishHandler: {}) return } /// 请求成功 WHToast.showSuccess(withMessage: "请求成功", duration: 1.5, finishHandler: { /// 执行回调 if let block = self.popBlock { block(self.operatorType) } /// 返回上层视图 self.navigationController?.popViewController(animated: true) }) } } } // MARK: - 合规性校验 /// 合规性校验 /// - Returns: Bool fileprivate func check() -> Bool { /// 请输入现货品种名称 guard let name = goodsName.text, name.count != 0 else { WHToast.showMessage("请输入现货品种名称", duration: 1.5, finishHandler: { self.goodsName.becomeFirstResponder() }) return false } /// 现货品种代码只能为字母、数字组合 guard let code = goodCode.text, !code.isChinese() else { WHToast.showMessage("现货品种代码只能为字母、数字组合", duration: 1.5, finishHandler: { self.goodsName.becomeFirstResponder() }) return false } /// 请选择单位 guard let _ = goodsUnit else { WHToast.showMessage("请选择单位", duration: 1.5, finishHandler: {}) return false } /// 请选择品牌 if itemExs.contains(where: {$0.dgfactoryitemvalue == ""}) { WHToast.showMessage("请输入品牌", duration: 1.5, finishHandler: {}) return false } /// 请选择商品 if wrStandardExs.contains(where: {$0.wrstandardname == ""}) { WHToast.showMessage("请输入商品", duration: 1.5, finishHandler: {}) return false } /// 请选择商品对应的单位 if let obj = wrStandardExs.first(where: {$0.unitid == 0}) { /// 请选择商品 WHToast.showMessage("请选择\(obj.wrstandardname)对应的单位", duration: 1.5, finishHandler: {}) return false } /// 请输入\(obj.modelname)对应标仓系数! if let obj = wrStandardExs.first(where: {$0.convertfactor == 0.0}) { /// 请选择商品 WHToast.showMessage("请输入\(obj.wrstandardname)对应标仓系数", duration: 1.5, finishHandler: {}) return false } /// 请选择套保品种 if convertDetailExs.contains(where: {$0.middlegoodsid == 0}) { WHToast.showMessage("请选择套保品种", duration: 1.5, finishHandler: {}) return false } /// 请输入套保系数 if convertDetailExs.contains(where: {$0.convertratio == 0.0}) { WHToast.showMessage("请输入套保系数", duration: 1.5, finishHandler: {}) return false } return true } } // MARK: - BrandDelegate UITableViewDelegate & UITableViewDataSource class BrandDelegate: NSCoder, UITableViewDelegate, UITableViewDataSource { var owner: NewGoodsVarietiesViewController init(_ owner: NewGoodsVarietiesViewController) { self.owner = owner } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.owner.itemExs.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: self.owner.BrandCellIdentifier, for: indexPath) as! BrandCell cell.rowNum = indexPath.row cell.model = self.owner.itemExs[indexPath.row] cell.block = { (_ model: Any?, _ sender: Any?) in if let name = model as? String, let index = sender as? Int { self.owner.itemExs[index].dgfactoryitemvalue = name } else { /// 触发删除操作 if let index = sender as? Int { if self.owner.itemExs.count > 1 { self.owner.itemExs.remove(at: index) } else { WHToast.showMessage("至少需要输入一项品牌", duration: 2, finishHandler: {}) } } } } return cell } } // MARK: - SpecDelegate UITableViewDelegate & UITableViewDataSource class SpecDelegate: NSCoder, UITableViewDelegate, UITableViewDataSource { var owner: NewGoodsVarietiesViewController init(_ owner: NewGoodsVarietiesViewController) { self.owner = owner } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.owner.wrStandardExs.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: self.owner.SpecCellIdentifier, for: indexPath) as! SpecCell cell.rowNum = indexPath.row cell.model = self.owner.wrStandardExs[indexPath.row] cell.block = { (_ model: Any?, _ sender: Any?) in if let name = model as? (spec: String, con: String, unit: MoEnum?), let index = sender as? Int { self.owner.wrStandardExs[index].wrstandardname = name.spec self.owner.wrStandardExs[index].convertfactor = Double(name.con) ?? 0.0 if let id = name.unit?.enumitemname { self.owner.wrStandardExs[index].unitid = UInt64(id) } } else { /// 触发删除操作 if let index = sender as? Int { if self.owner.wrStandardExs.count > 1 { self.owner.wrStandardExs.remove(at: index) } else { WHToast.showMessage("至少需要输入一项商品", duration: 2, finishHandler: {}) } } } } return cell } } // MARK: - HedgeDelegate UITableViewDelegate & UITableViewDataSource class HedgeDelegate: NSCoder, UITableViewDelegate, UITableViewDataSource { var owner: NewGoodsVarietiesViewController init(_ owner: NewGoodsVarietiesViewController) { self.owner = owner } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.owner.convertDetailExs.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: self.owner.HedgeCellIdentifier, for: indexPath) as! HedgeCell cell.rowNum = indexPath.row cell.model = self.owner.convertDetailExs[indexPath.row] cell.block = { (_ model: Any?, _ sender: Any?) in if let obj = model as? (Int, Double, Int), let index = sender as? Int { if obj.0 != 0 { self.owner.convertDetailExs[index].middlegoodsid = UInt64(obj.0) } self.owner.convertDetailExs[index].convertratio = obj.1 if obj.2 != 0 { self.owner.convertDetailExs[index].unitid = UInt64(obj.2) } } else { /// 触发删除操作 if let index = sender as? Int { if self.owner.convertDetailExs.count > 1 { self.owner.convertDetailExs.remove(at: index) } else { WHToast.showMessage("至少需要填入一项套保品种", duration: 2, finishHandler: {}) } } } } return cell } } class SpecCell: BaseTableViewCell { /// 标题 @IBOutlet weak var title: UILabel! /// 单位标题 @IBOutlet weak var unitTitle: UILabel! /// 单位 @IBOutlet weak var unit: UIButton! /// 子标题 @IBOutlet weak var subTitle: UILabel! /// 商品 @IBOutlet weak var spec: AnimatableTextField! /// 标仓系数 @IBOutlet weak var coefficient: AnimatableTextField! /// 删除按钮 @IBOutlet weak var delete: UIButton! /// 商品单位 var goodsUnit: MoEnum? { didSet { if let obj = goodsUnit { unit.setTitle(obj.enumdicname, for: .normal) unit.setTitleColor(.hex333(), for: .normal) } else { unit.setTitle("请选择单位", for: .normal) unit.setTitleColor(.hexCCC(), for: .normal) } } } /// 当前位置 override var rowNum: Int? { didSet { title.text = "商品 \((rowNum ?? 0)+1)" unitTitle.text = "单位 \((rowNum ?? 0)+1)" subTitle.text = "标仓系数 \((rowNum ?? 0)+1)" } } override var model: Any? { didSet { guard let obj = model as? GLDWRStandardEx else { return } /// 商品名称 spec.text = obj.wrstandardname /// 标仓系数 if obj.convertfactor != 0.0 { coefficient.text = "\(obj.convertfactor)" } /// 执行回调 DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.5) { /// 商品单位 self.goodsUnit = (self.superController() as? NewGoodsVarietiesViewController)?.goodsUnits.first(where: {$0.enumitemname == obj.unitid}) } } } /// onButtonPressed /// - Parameter sender: sender @IBAction fileprivate func onButtonPressed(_ sender: UIButton) { switch sender { case delete: /// 删除按钮 if let bk = self.block { bk(nil, self.rowNum) } case unit: /// 单位 if let units = (self.superController() as? NewGoodsVarietiesViewController)?.goodsUnits { /// 商品单位 ActionSheetStringPicker.show(withTitle: "商品单位", rows: units.map({ (obj) -> String in return obj.enumdicname }), initialSelection: 0, doneBlock: { picker, value, index in self.goodsUnit = units[value] /// 执行回调 if let bk = self.block { bk((spec: self.spec.text, con: self.coefficient.text, unit: self.goodsUnit), self.rowNum) } }, cancel: { picker in return }, origin: sender) } default: break } } } extension SpecCell: UITextFieldDelegate { /// textFieldDidEndEditing /// - Parameter textField: textField func textFieldDidEndEditing(_ textField: UITextField) { if textField == spec { coefficient.becomeFirstResponder() } if let bk = self.block { bk((spec: spec.text, con: coefficient.text, unit: self.goodsUnit), self.rowNum) } } } class BrandCell: BaseTableViewCell { /// 标题 @IBOutlet weak var title: UILabel! /// 品牌 @IBOutlet weak var brand: AnimatableTextField! /// 删除按钮 @IBOutlet weak var delete: UIButton! /// 当前位置 override var rowNum: Int? { didSet { title.text = "品牌 \((rowNum ?? 0)+1)" } } /// 数据 override var model: Any? { didSet { guard let obj = model as? GLDDGFactoryItemEx else { return } brand.text = obj.dgfactoryitemvalue } } /// onButtonPressed /// - Parameter sender: sender @IBAction fileprivate func onButtonPressed(_ sender: UIButton) { switch sender { case delete: /// 删除按钮 if let bk = self.block { bk(nil, self.rowNum) } default: break } } } extension BrandCell: UITextFieldDelegate { /// 执行回调 func textFieldDidEndEditing(_ textField: UITextField) { if let bk = self.block { bk(textField.text, self.rowNum) } } } class HedgeCell: BaseTableViewCell { /// 套保品种 @IBOutlet weak var hedge: UIButton! /// 单位 @IBOutlet weak var unit: UILabel! /// 套保系数 @IBOutlet weak var coefficient: AnimatableTextField! /// 删除按钮 @IBOutlet weak var delete: UIButton! /// 标题1 @IBOutlet weak var title1: UILabel! /// 标题2 @IBOutlet weak var title2: UILabel! /// 当前位置 override var rowNum: Int? { didSet { title1.text = "套保品种\((rowNum ?? 0)+1)" title2.text = "套保系数\((rowNum ?? 0)+1)" } } override var model: Any? { didSet { guard let obj = model as? WRSConvertDetailEx else { return } /// 套保系数 if obj.convertratio != 0.0 { coefficient.text = "\(obj.convertratio)" } /// 执行回调 DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.5) { /// 获取对应的商品组信息 self.group = (self.superController() as? NewGoodsVarietiesViewController)?.goodsGroups.first(where: {$0.goodsgroupid == obj.middlegoodsid}) } } } /// 套保品种 var group: MoGoodsGroup? { didSet { /// 异常 guard let obj = group else { return } /// 商品组 hedge.setTitle(obj.goodsgroupname, for: .normal) hedge.setTitleColor(.hex333(), for: .normal) /// 单位 unit.text = obj.enumdicname unit.textColor = .hex333() } } @IBAction fileprivate func onButtonPressed(_ sender: UIButton) { switch sender { case delete: /// 删除按钮 if let bk = self.block { bk(nil, self.rowNum) } case hedge: /// 套保品种 /// 商品组信息 let groups = (self.superController() as? NewGoodsVarietiesViewController)?.goodsGroups ?? [] /// 过滤掉已选择得数据信息 var shows: [MoGoodsGroup] = [] if let exs = (self.superController() as? NewGoodsVarietiesViewController)?.convertDetailExs, exs.count != 0, groups.count != 0 { groups.forEach { (obj) in if !exs.contains(where: {$0.middlegoodsid == obj.goodsgroupid}) { shows.append(obj) } } } /// 交易所以及交易所下的商品组信息 var exs: [(name: String, id: Int, grs: [MoGoodsGroup])] = [] guard let ges = MTP2BusinessCore.shared.goodsManager?.exchanges, ges.count != 0 else { WHToast.showMessage("没有对应的交易所配置信息", duration: 1.5, finishHandler: {}) return } /// 数据归类 for obj in ges { exs.append((name: obj.exexchangename, id: obj.autoid, grs: groups.filter({$0.exexchangeid == obj.autoid}))) } let mutable = MutablePickerViewController.showMutableString(title: "套保品种选择", leftTitles: exs.map({ obj in return obj.name }), rightTitles: exs.map({ obj in return obj.grs.map { obj in return obj.goodsgroupname } }), cancel: "取消", sure: "确定", select: (0,0)) { left, right in /// 设置选中的商品组信息 self.group = exs[left].grs[right] /// 执行回调 if let obj = self.group, let bk = self.block { bk((obj.goodsgroupid, Double(self.coefficient.text ?? "0.0") ?? 0.0, obj.goodunitid), self.rowNum) } } /// 显示 self.superController()?.present(mutable, animated: true, completion: {}) default: break } } } extension HedgeCell: UITextFieldDelegate { /// 执行回调 func textFieldDidEndEditing(_ textField: UITextField) { /// 执行回调 if let bk = self.block, let ratio = Double(textField.text ?? "0.0") { /// 为空 传-1 bk((0, ratio, 0), self.rowNum) } } }