NewHedgePlanViewController.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. //
  2. // NewHedgePlanViewController.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 NewHedgePlanViewController: BaseViewController {
  15. // MARK: - 属性列表
  16. /// 计划类型
  17. @IBOutlet weak var hedgePlanType: UIButton!
  18. /// 计划名称
  19. @IBOutlet weak var hedgePlanName: AnimatableTextField!
  20. /// 单位
  21. @IBOutlet weak var goodsUnit: UILabel!
  22. /// 现货品种
  23. @IBOutlet weak var delivery: UIButton!
  24. /// 标仓系数
  25. @IBOutlet weak var coefficient: AnimatableTextField! {
  26. didSet {
  27. /// 产品需求写死默认为1 且不能改
  28. coefficient.isUserInteractionEnabled = false
  29. coefficient.text = "1.0"
  30. }
  31. }
  32. /// 计划量
  33. @IBOutlet weak var planQty: AnimatableTextField!
  34. /// 备注
  35. @IBOutlet weak var remark: AnimatableTextField!
  36. /// 保存草稿
  37. @IBOutlet weak var saveDrafts: UIButton!
  38. /// 提交审核
  39. @IBOutlet weak var audit: UIButton!
  40. /// 商品
  41. @IBOutlet weak var wrstandard: UIButton!
  42. /// 交易员
  43. @IBOutlet weak var trader: UIButton!
  44. /// 结算币种
  45. @IBOutlet weak var currency: UIButton!
  46. /// 计划类型
  47. var contractType: EContractType? {
  48. didSet {
  49. hedgePlanType.setTitle(contractType?.description, for: .normal)
  50. hedgePlanType.setTitleColor(.hex333(), for: .normal)
  51. }
  52. }
  53. /// 现货品种
  54. var moDeliveryGoods: MoDeliveryGoods? {
  55. didSet {
  56. /// 异常
  57. guard let obj = moDeliveryGoods else { return }
  58. delivery.setTitle(obj.deliverygoodsname, for: .normal)
  59. delivery.setTitleColor(.hex333(), for: .normal)
  60. /// 异常
  61. guard let contractManager = MTP2BusinessCore.shared.contractManager else { return }
  62. let group = DispatchGroup()
  63. /// startAnimating
  64. _anim?.startAnimating()
  65. group.enter()
  66. /// 查询现货明细数据信息
  67. contractManager.queryDeliveryGoodsDetail(deliverygoodsid: obj.deliverygoodsid, qrytradegoods: nil) { isComplete, er, objs in
  68. DispatchQueue.main.async {
  69. if let obj = objs?.first {
  70. /// 商品
  71. self.standards = obj.gmlist
  72. /// 现货商品
  73. self.moWrStandard = obj.gmlist.first(where: {$0.wrstandardid == self.hedgePlan?.wrstandardid})
  74. }
  75. }
  76. group.leave()
  77. }
  78. group.notify(queue: DispatchQueue.main) {
  79. /// stopAnimating
  80. self._anim?.stopAnimating()
  81. }
  82. }
  83. }
  84. /// 计划数据
  85. var hedgePlan: MoHedgePlan?
  86. /// 操作
  87. var operatorType: OperatorType? {
  88. didSet {
  89. self.navigationItem.title = (operatorType?.description ?? "")+"套保计划"
  90. }
  91. }
  92. /// 商品信息
  93. var standards: [MoWrStandard] = []
  94. /// 商品
  95. var moWrStandard: MoWrStandard? {
  96. didSet {
  97. /// 异常
  98. guard let obj = moWrStandard else { return }
  99. wrstandard.setTitle(obj.wrstandardname, for: .normal)
  100. wrstandard.setTitleColor(.hex333(), for: .normal)
  101. /// 单位
  102. goodsUnit.text = obj.enumdicname
  103. /// 标仓系数
  104. coefficient.text = "\(obj.convertfactor)"
  105. }
  106. }
  107. /// 交易员用户信息
  108. var traders: [MoMgrLoginUser] = []
  109. /// 选中的交易员用户信息
  110. var moTraderUser: MoMgrLoginUser? {
  111. didSet {
  112. /// 异常
  113. guard let obj = moTraderUser else { return }
  114. trader.setTitle("\(obj.rolename)", for: .normal)
  115. trader.setTitleColor(.hex333(), for: .normal)
  116. }
  117. }
  118. /// 币种枚举信息
  119. var units: [MoEnum] {
  120. return try! DatabaseHelper.getEnumInfo(enumdiccode: "paycurrency") ?? []
  121. }
  122. /// 选中的币种
  123. var moUnit: MoEnum? {
  124. didSet {
  125. /// 异常
  126. guard let obj = moUnit else { return }
  127. currency.setTitle(obj.enumdicname, for: .normal)
  128. currency.setTitleColor(.hex333(), for: .normal)
  129. }
  130. }
  131. // MARK: - 生命周期
  132. override func viewDidLoad() {
  133. super.viewDidLoad()
  134. // Do any additional setup after loading the view.
  135. /// 输入框
  136. moveView(views: [hedgePlanName, coefficient, planQty, remark])
  137. /// loding......
  138. addLoadingView()
  139. }
  140. /// UI界面初始化
  141. override func buildView() {
  142. super.buildView()
  143. /// 计划
  144. if let obj = hedgePlan {
  145. /// 计划类型
  146. contractType = obj.contracttype
  147. /// 名称
  148. hedgePlanName.text = obj.hedgeplanno
  149. /// 标仓系数
  150. coefficient.text = obj.convertfactor.toString(reserve: 2, false)
  151. /// 计划量
  152. planQty.text = obj.planqty.toString(reserve: 2, false)
  153. /// 备注
  154. remark.text = obj.remark
  155. /// 单位
  156. goodsUnit.text = obj.wrunitidame.isBlank()
  157. /// 币种信息
  158. moUnit = units.first(where: {$0.enumitemname == obj.currencyid})
  159. /// 现货品种
  160. if let goods = MTP2BusinessCore.shared.contractManager?.deliveryGoods?.first(where: {$0.deliverygoodsid == obj.deliverygoodsid}) {
  161. /// 现货品种
  162. moDeliveryGoods = goods
  163. }
  164. }
  165. /// 查询交易用户信息
  166. queryTraderLoginUsers()
  167. }
  168. // MARK: - 交互相关
  169. /// onButtonPressed
  170. /// - Parameter sender: sender
  171. @IBAction fileprivate func onButtonPressed(_ sender: UIControl) {
  172. switch sender {
  173. case hedgePlanType:
  174. /// 计划类型
  175. ActionSheetStringPicker.show(withTitle: "请选择计划类型", rows: ["采购", "销售"], initialSelection: 0, doneBlock: { picker, value, index in
  176. self.contractType = value == 0 ? .Buy : .Sale
  177. }, cancel: { picker in
  178. return
  179. }, origin: sender)
  180. case delivery:
  181. /// 现货品种
  182. guard let goods = MTP2BusinessCore.shared.contractManager?.deliveryGoods, goods.count != 0 else {
  183. WHToast.showMessage("没有现货品种数据", duration: 1.5, finishHandler: {})
  184. return
  185. }
  186. /// 请选择现货品种
  187. ActionSheetStringPicker.show(withTitle: "请选择现货品种", rows: goods.map({ (obj) -> String in
  188. return obj.deliverygoodsname
  189. }), initialSelection: 0, doneBlock: { picker, value, index in
  190. self.moDeliveryGoods = goods[value]
  191. }, cancel: { picker in
  192. return
  193. }, origin: sender)
  194. case trader:
  195. /// 交易用户
  196. guard traders.count != 0 else {
  197. WHToast.showMessage("请先去添加交易用户信息", duration: 1.5, finishHandler: {})
  198. return
  199. }
  200. /// 请选择交易用户
  201. ActionSheetStringPicker.show(withTitle: "请选择交易用户", rows: traders.map({ (obj) -> String in
  202. return "\(obj.rolename)"
  203. }), initialSelection: 0, doneBlock: { picker, value, index in
  204. self.moTraderUser = self.traders[value]
  205. }, cancel: { picker in
  206. return
  207. }, origin: sender)
  208. case currency:
  209. /// 币种
  210. guard units.count != 0 else {
  211. WHToast.showMessage("没有对应的结算币种信息", duration: 1.5, finishHandler: {})
  212. return
  213. }
  214. ///请选择结算币种
  215. ActionSheetStringPicker.show(withTitle: "请选择结算币种", rows: units.map({ (obj) -> String in
  216. return obj.enumdicname
  217. }), initialSelection: 0, doneBlock: { picker, value, index in
  218. self.moUnit = self.units[value]
  219. }, cancel: { picker in
  220. return
  221. }, origin: sender)
  222. case wrstandard:
  223. /// 商品
  224. guard standards.count != 0 else {
  225. WHToast.showMessage("该现货品种没有商品信息", duration: 1.5, finishHandler: {})
  226. return
  227. }
  228. ActionSheetStringPicker.show(withTitle: "请选择商品", rows: standards.map({ (obj) -> String in
  229. return obj.wrstandardname
  230. }), initialSelection: 0, doneBlock: { picker, value, index in
  231. self.moWrStandard = self.standards[value]
  232. }, cancel: { picker in
  233. return
  234. }, origin: sender)
  235. case audit:
  236. /// 提交审核
  237. doHedgePlanOperator(2)
  238. case saveDrafts:
  239. /// 保存草稿
  240. doHedgePlanOperator(1)
  241. default: break
  242. }
  243. }
  244. // MARK: - 接口请求
  245. /// 查询交易用户数据信息
  246. fileprivate func queryTraderLoginUsers() {
  247. /// 异常
  248. guard let accountManager = MTP2BusinessCore.shared.accountManager else {
  249. return
  250. }
  251. /// 查询交易用户数据信息
  252. accountManager.queryAccMgrLoginUser(querytype: 2) { isComplete, er, objs in
  253. DispatchQueue.main.async {
  254. /// 查询失败
  255. if !isComplete {
  256. self.traders = []
  257. WHToast.showMessage("数据查询失败,原因:\(er?.retMsg ?? "请求超时")", duration: 1.5, finishHandler: {})
  258. return
  259. }
  260. self.traders = objs ?? []
  261. /// 交易用户信息
  262. if let id = self.hedgePlan, self.traders.count != 0 {
  263. self.moTraderUser = objs?.first(where: {$0.roleid == id.tradeuserid})
  264. }
  265. }
  266. }
  267. }
  268. /// 套保计划操作提交
  269. fileprivate func doHedgePlanOperator(_ operateType: Int) {
  270. /// 异常
  271. guard let planManager = MTP2BusinessCore.shared.planManager,
  272. let good = moDeliveryGoods,
  273. let type = contractType?.rawValue else {
  274. return
  275. }
  276. /// 提交审核才需要必填项
  277. if operateType == 2, !check() { return }
  278. /// 审核拒绝 已撤销 重新提交 生成新的计划
  279. let id = (hedgePlan != nil && hedgePlan?.hedgeplanstatus != .reviewRejected && hedgePlan?.hedgeplanstatus != .withDrawn) ? Int(hedgePlan?.hedgeplanid ?? "0") : nil
  280. /// 计划量
  281. let qty = Double(planQty.text ?? "0.0") ?? 0.0
  282. /// 标仓系数
  283. let factor = Double(coefficient.text ?? "0.0") ?? 0.0
  284. /// 期货账户id
  285. let tradeuserid = moTraderUser?.roleid
  286. /// 商品id
  287. let wrstandardid = moWrStandard?.wrstandardid
  288. /// 币种id
  289. let currencyid = moUnit?.enumitemname
  290. /// 开启Loading
  291. NVActivityIndicatorPresenter.sharedInstance.startAnimating(animActivityData("请求中..."))
  292. /// 发送请求
  293. 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
  294. DispatchQueue.main.async {
  295. /// dismisss loding.....
  296. NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
  297. /// 失败
  298. if !isComplete {
  299. WHToast.showMessage("请求失败,原因:\(ErrorUtils.desc(er?.retCode ?? -60001))", duration: 1.5, finishHandler: {})
  300. return
  301. }
  302. WHToast.showSuccess(withMessage: "请求成功", duration: 1.5, finishHandler: {
  303. /// 执行回调
  304. if let bk = self.popBlock { bk(operateType == 1 ? OperatorType.saveDrafts : OperatorType.add) }
  305. /// 返回上层视图
  306. self.navigationController?.popViewController(animated: true)
  307. })
  308. }
  309. }
  310. }
  311. // MARK: - 合规性校验
  312. /// 合规性校验
  313. /// - Returns: Bool
  314. fileprivate func check() -> Bool {
  315. /// 请选择计划类型
  316. guard let _ = contractType else {
  317. WHToast.showMessage("请选择计划类型", duration: 1.5, finishHandler: {})
  318. return false
  319. }
  320. /// 请选择现货品种
  321. guard let _ = moDeliveryGoods else {
  322. WHToast.showMessage("请选择现货品种", duration: 1.5, finishHandler: {})
  323. return false
  324. }
  325. /// 请输入标仓系数
  326. guard let coefficient = coefficient.text, coefficient != "" else {
  327. WHToast.showMessage("请输入标仓系数", duration: 1.5, finishHandler: {})
  328. return false
  329. }
  330. /// 请输入计划量
  331. guard let qty = planQty.text, qty != "" else {
  332. WHToast.showMessage("请输入计划量", duration: 1.5, finishHandler: {})
  333. return false
  334. }
  335. /// 计划量不能为0
  336. if Double(qty) == 0.0 {
  337. WHToast.showMessage("计划量不能为0", duration: 1.5, finishHandler: {})
  338. return false
  339. }
  340. /// 请选择结算币种
  341. guard let _ = moUnit else {
  342. WHToast.showMessage("请选择结算币种", duration: 1.5, finishHandler: {})
  343. return false
  344. }
  345. return true
  346. }
  347. }