// // NewHedgePlanViewController.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 NewHedgePlanViewController: BaseViewController { // MARK: - 属性列表 /// 计划类型 @IBOutlet weak var hedgePlanType: UIButton! /// 计划名称 @IBOutlet weak var hedgePlanName: AnimatableTextField! /// 单位 @IBOutlet weak var goodsUnit: UILabel! /// 现货品种 @IBOutlet weak var delivery: UIButton! /// 标仓系数 @IBOutlet weak var coefficient: AnimatableTextField! { didSet { /// 产品需求写死默认为1 且不能改 coefficient.isUserInteractionEnabled = false coefficient.text = "1.0" } } /// 计划量 @IBOutlet weak var planQty: AnimatableTextField! /// 备注 @IBOutlet weak var remark: AnimatableTextField! /// 保存草稿 @IBOutlet weak var saveDrafts: UIButton! /// 提交审核 @IBOutlet weak var audit: UIButton! /// 商品 @IBOutlet weak var wrstandard: UIButton! /// 交易员 @IBOutlet weak var trader: UIButton! /// 结算币种 @IBOutlet weak var currency: UIButton! /// 计划类型 var contractType: EContractType? { didSet { hedgePlanType.setTitle(contractType?.description, for: .normal) hedgePlanType.setTitleColor(.hex333(), for: .normal) } } /// 现货品种 var moDeliveryGoods: MoDeliveryGoods? { didSet { /// 异常 guard let obj = moDeliveryGoods else { return } delivery.setTitle(obj.deliverygoodsname, for: .normal) delivery.setTitleColor(.hex333(), for: .normal) /// 异常 guard let contractManager = MTP2BusinessCore.shared.contractManager else { return } let group = DispatchGroup() /// startAnimating _anim?.startAnimating() group.enter() /// 查询现货明细数据信息 contractManager.queryDeliveryGoodsDetail(deliverygoodsid: obj.deliverygoodsid, qrytradegoods: nil) { isComplete, er, objs in DispatchQueue.main.async { if let obj = objs?.first { /// 商品 self.standards = obj.gmlist /// 现货商品 self.moWrStandard = obj.gmlist.first(where: {$0.wrstandardid == self.hedgePlan?.wrstandardid}) } } group.leave() } group.notify(queue: DispatchQueue.main) { /// stopAnimating self._anim?.stopAnimating() } } } /// 计划数据 var hedgePlan: MoHedgePlan? /// 操作 var operatorType: OperatorType? { didSet { self.navigationItem.title = (operatorType?.description ?? "")+"套保计划" } } /// 商品信息 var standards: [MoWrStandard] = [] /// 商品 var moWrStandard: MoWrStandard? { didSet { /// 异常 guard let obj = moWrStandard else { return } wrstandard.setTitle(obj.wrstandardname, for: .normal) wrstandard.setTitleColor(.hex333(), for: .normal) /// 单位 goodsUnit.text = obj.enumdicname /// 标仓系数 coefficient.text = "\(obj.convertfactor)" } } /// 交易员用户信息 var traders: [MoMgrLoginUser] = [] /// 选中的交易员用户信息 var moTraderUser: MoMgrLoginUser? { didSet { /// 异常 guard let obj = moTraderUser else { return } trader.setTitle("\(obj.rolename)", for: .normal) trader.setTitleColor(.hex333(), for: .normal) } } /// 币种枚举信息 var units: [MoEnum] { return try! DatabaseHelper.getEnumInfo(enumdiccode: "paycurrency") ?? [] } /// 选中的币种 var moUnit: MoEnum? { didSet { /// 异常 guard let obj = moUnit else { return } currency.setTitle(obj.enumdicname, for: .normal) currency.setTitleColor(.hex333(), for: .normal) } } // MARK: - 生命周期 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. /// 输入框 moveView(views: [hedgePlanName, coefficient, planQty, remark]) /// loding...... addLoadingView() } /// UI界面初始化 override func buildView() { super.buildView() /// 计划 if let obj = hedgePlan { /// 计划类型 contractType = obj.contracttype /// 名称 hedgePlanName.text = obj.hedgeplanno /// 标仓系数 coefficient.text = obj.convertfactor.toString(reserve: 2, false) /// 计划量 planQty.text = obj.planqty.toString(reserve: 2, false) /// 备注 remark.text = obj.remark /// 单位 goodsUnit.text = obj.wrunitidame.isBlank() /// 币种信息 moUnit = units.first(where: {$0.enumitemname == obj.currencyid}) /// 现货品种 if let goods = MTP2BusinessCore.shared.contractManager?.deliveryGoods?.first(where: {$0.deliverygoodsid == obj.deliverygoodsid}) { /// 现货品种 moDeliveryGoods = goods } } /// 查询交易用户信息 queryTraderLoginUsers() } // MARK: - 交互相关 /// onButtonPressed /// - Parameter sender: sender @IBAction fileprivate func onButtonPressed(_ sender: UIControl) { switch sender { case hedgePlanType: /// 计划类型 ActionSheetStringPicker.show(withTitle: "请选择计划类型", rows: ["采购", "销售"], initialSelection: 0, doneBlock: { picker, value, index in self.contractType = value == 0 ? .Buy : .Sale }, cancel: { picker in return }, origin: sender) case delivery: /// 现货品种 guard let goods = MTP2BusinessCore.shared.contractManager?.deliveryGoods, goods.count != 0 else { WHToast.showMessage("没有现货品种数据", duration: 1.5, finishHandler: {}) return } /// 请选择现货品种 ActionSheetStringPicker.show(withTitle: "请选择现货品种", rows: goods.map({ (obj) -> String in return obj.deliverygoodsname }), initialSelection: 0, doneBlock: { picker, value, index in self.moDeliveryGoods = goods[value] }, cancel: { picker in return }, origin: sender) case trader: /// 交易用户 guard traders.count != 0 else { WHToast.showMessage("请先去添加交易用户信息", duration: 1.5, finishHandler: {}) return } /// 请选择交易用户 ActionSheetStringPicker.show(withTitle: "请选择交易用户", rows: traders.map({ (obj) -> String in return "\(obj.rolename)" }), initialSelection: 0, doneBlock: { picker, value, index in self.moTraderUser = self.traders[value] }, cancel: { picker in return }, origin: sender) case currency: /// 币种 guard units.count != 0 else { WHToast.showMessage("没有对应的结算币种信息", duration: 1.5, finishHandler: {}) return } ///请选择结算币种 ActionSheetStringPicker.show(withTitle: "请选择结算币种", rows: units.map({ (obj) -> String in return obj.enumdicname }), initialSelection: 0, doneBlock: { picker, value, index in self.moUnit = self.units[value] }, cancel: { picker in return }, origin: sender) case wrstandard: /// 商品 guard standards.count != 0 else { WHToast.showMessage("该现货品种没有商品信息", duration: 1.5, finishHandler: {}) return } ActionSheetStringPicker.show(withTitle: "请选择商品", rows: standards.map({ (obj) -> String in return obj.wrstandardname }), initialSelection: 0, doneBlock: { picker, value, index in self.moWrStandard = self.standards[value] }, cancel: { picker in return }, origin: sender) case audit: /// 提交审核 doHedgePlanOperator(2) case saveDrafts: /// 保存草稿 doHedgePlanOperator(1) default: break } } // MARK: - 接口请求 /// 查询交易用户数据信息 fileprivate func queryTraderLoginUsers() { /// 异常 guard let accountManager = MTP2BusinessCore.shared.accountManager else { return } /// 查询交易用户数据信息 accountManager.queryAccMgrLoginUser(querytype: 2) { isComplete, er, objs in DispatchQueue.main.async { /// 查询失败 if !isComplete { self.traders = [] WHToast.showMessage("数据查询失败,原因:\(er?.retMsg ?? "请求超时")", duration: 1.5, finishHandler: {}) return } self.traders = objs ?? [] /// 交易用户信息 if let id = self.hedgePlan, self.traders.count != 0 { self.moTraderUser = objs?.first(where: {$0.roleid == id.tradeuserid}) } } } } /// 套保计划操作提交 fileprivate func doHedgePlanOperator(_ operateType: Int) { /// 异常 guard let planManager = MTP2BusinessCore.shared.planManager, let good = moDeliveryGoods, let type = contractType?.rawValue else { return } /// 提交审核才需要必填项 if operateType == 2, !check() { return } /// 审核拒绝 已撤销 重新提交 生成新的计划 let id = (hedgePlan != nil && hedgePlan?.hedgeplanstatus != .reviewRejected && hedgePlan?.hedgeplanstatus != .withDrawn) ? Int(hedgePlan?.hedgeplanid ?? "0") : nil /// 计划量 let qty = Double(planQty.text ?? "0.0") ?? 0.0 /// 标仓系数 let factor = Double(coefficient.text ?? "0.0") ?? 0.0 /// 期货账户id let tradeuserid = moTraderUser?.roleid /// 商品id let wrstandardid = moWrStandard?.wrstandardid /// 币种id let currencyid = moUnit?.enumitemname /// 开启Loading NVActivityIndicatorPresenter.sharedInstance.startAnimating(animActivityData("请求中...")) /// 发送请求 planManager.requestErmcpHedgePlan(hedgePlanID: id, hedgePlanNo: hedgePlanName.text, contractType: type, deliveryGoodsID: good.deliverygoodsid, productType: 1, spotGoodsDesc: nil, planQty: qty, convertFactor: factor, planTime: Date().getString(), operateType: operateType, accountid: nil, wrstandardID: wrstandardid, remark: remark.text, tradeuserid: tradeuserid, currencyid: currencyid) { (isComplete, er, rsp) in DispatchQueue.main.async { /// dismisss loding..... NVActivityIndicatorPresenter.sharedInstance.stopAnimating() /// 失败 if !isComplete { WHToast.showMessage("请求失败,原因:\(ErrorUtils.desc(er?.retCode ?? -60001))", duration: 1.5, finishHandler: {}) return } WHToast.showSuccess(withMessage: "请求成功", duration: 1.5, finishHandler: { /// 执行回调 if let bk = self.popBlock { bk(operateType == 1 ? OperatorType.saveDrafts : OperatorType.add) } /// 返回上层视图 self.navigationController?.popViewController(animated: true) }) } } } // MARK: - 合规性校验 /// 合规性校验 /// - Returns: Bool fileprivate func check() -> Bool { /// 请选择计划类型 guard let _ = contractType else { WHToast.showMessage("请选择计划类型", duration: 1.5, finishHandler: {}) return false } /// 请选择现货品种 guard let _ = moDeliveryGoods else { WHToast.showMessage("请选择现货品种", duration: 1.5, finishHandler: {}) return false } /// 请输入标仓系数 guard let coefficient = coefficient.text, coefficient != "" else { WHToast.showMessage("请输入标仓系数", duration: 1.5, finishHandler: {}) return false } /// 请输入计划量 guard let qty = planQty.text, qty != "" else { WHToast.showMessage("请输入计划量", duration: 1.5, finishHandler: {}) return false } /// 计划量不能为0 if Double(qty) == 0.0 { WHToast.showMessage("计划量不能为0", duration: 1.5, finishHandler: {}) return false } /// 请选择结算币种 guard let _ = moUnit else { WHToast.showMessage("请选择结算币种", duration: 1.5, finishHandler: {}) return false } return true } }