// // NewHedgeViewController.swift // MTP2_iOS // // Created by Muchinfo on 2021/1/22. // Copyright © 2021 Muchinfo. All rights reserved. // import UIKit import IBAnimatable import WHToast import ActionSheetPicker_3_0 import NVActivityIndicatorView /// 新增套保品种视图容器控制类 class NewHedgeViewController: BaseViewController { // MARK: - 属性列表 @IBOutlet weak var ggConfig: UIButton! /// 套保比例 @IBOutlet weak var needhedgeratioField: UITextField! /// 提交按钮 @IBOutlet weak var submit: UIButton! /// 单位 @IBOutlet weak var srcunitidname: UILabel! /// 当前套保比例 @IBOutlet weak var currenthedgeratio: UILabel! /// 备注 @IBOutlet weak var remark: AnimatableTextField! /// 套保品种信息 var moMiddleGoods: MoErmcpMiddleGoodsDetail? /// 套保品种详细信息 var moMiddleGoodsDetail: MoErmcpMiddleGoodsDetail? { didSet { /// 异常 guard let obj = moMiddleGoodsDetail?.mg else { return } /// 套保品种名称 ggConfig.setTitle(obj.middlegoodsname, for: .normal) /// 当前套保比例 currenthedgeratio.text = "\((obj.needhedgeratio*100.0).toString(reserve: 2, false))%" /// 单位 srcunitidname.text = obj.enumdicname.isBlank() /// 备注 remark.text = obj.remark } } /// 新增 var operatorType: OperatorType = .update // MARK: - 生命周期 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func buildView() { super.buildView() /// moveView moveView(views: [needhedgeratioField, remark]) /// loding... self.addLoadingView() /// 显示套保品种数据信息 self.showMiddleGoodsInfo() } /// 显示套保品种数据信息 func showMiddleGoodsInfo() { /// 异常 guard let planManager = MTP2BusinessCore.shared.planManager, let obj = moMiddleGoods else { return } /// startAnimating self._anim?.startAnimating() /// 查询套保品种信息 planManager.queryMiddleGoodsDetail(middlegoodsid: obj.mg?.middlegoodsid) { (isComplete, error, detail) in DispatchQueue.main.async { /// stopAnimating self._anim?.stopAnimating() if !isComplete { WHToast.showMessage("套保品种详情查询失败", duration: 1.5, finishHandler: {}) return } guard let obj = detail else { return } /// 详细信息 self.moMiddleGoodsDetail = obj.first } } } // MARK: - 交互相关 /// onButtonPressed /// - Parameter sender: sender @IBAction func onButtonPressed(_ sender: UIButton) { doEditMiddleGoods() } // MARK: - 接口请求 /// 修改套保品种 fileprivate func doEditMiddleGoods() { /// 异常 guard let planManager = MTP2BusinessCore.shared.planManager, let mg = moMiddleGoodsDetail?.mg, check() else { return } /// 备注 let rk = remark.text /// 商品名称 let ratio = (Double((needhedgeratioField.text ?? "").replacingOccurrences(of: "%", with: "")) ?? 0.0)/100.0 /// 开启Loading NVActivityIndicatorPresenter.sharedInstance.startAnimating(animActivityData("请求中...")) /// 发送请求 planManager.requestErmsMiddelGoodsEdit(middlegoodsid: UInt64(mg.middlegoodsid), goodsunitid: nil, relatedgoodsid: nil, evaluateratio: nil, qtydecimalplace: nil, relatedgoodstype: nil, needhedgeratio: ratio, goodsgroupid: UInt64(mg.goodsgroupid), remark: rk) { (isComplete, er, rsp) in DispatchQueue.main.async { /// dismisss loding..... NVActivityIndicatorPresenter.sharedInstance.stopAnimating() /// 请求失败 if !isComplete { WHToast.showMessage("请求失败,原因:\(ErrorUtils.desc(er?.retCode))", duration: 1.5, finishHandler: {}) return } /// 请求成功 WHToast.showSuccess(withMessage: "请求成功", duration: 1.5, finishHandler: { /// 返回上层视图 self.navigationController?.popViewController(animated: true) /// 执行回调 if let block = self.popBlock { block(self.operatorType) } }) } } } /// 合规性校验 /// - Returns: Bool fileprivate func check() -> Bool { /// 请输入套保比例 guard let ratio = needhedgeratioField.text, ratio != "" else { WHToast.showMessage("请输入修改后套保比例", duration: 1.5, finishHandler: {}) return false } return true } } extension NewHedgeViewController: UITextFieldDelegate { func textFieldDidEndEditing(_ textField: UITextField) { if textField == needhedgeratioField, let text = needhedgeratioField.text, !text.contains("%") { needhedgeratioField.text = text+"%" } } }