// // ActiveDetailViewController.swift // xingchuangke // // Created by 李晓飞 on 2020/8/31. // Copyright © 2020 Virgil. All rights reserved. // import UIKit class ActiveDetailViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource { var activityId: String = "" var userId: String = "" var canEdit: Bool = false var isActive: Bool = false // let topicArr: NSArray = ["产品政策", "分润规则", "活动奖励分润"] let topicArr: NSArray = ["产品政策", "分润规则", "活动奖励分润"] // 产品政策 var activationAmountReward: String = "" // 激活奖励 var productRulesConfigs: NSArray = [] // 产品政策 // 分润规则 var distributionRuleConfigs: NSArray = [] // 分润规则 var productDatas: NSMutableArray = NSMutableArray() // 产品政策数据 var ruleDatas: NSMutableArray = NSMutableArray() // 分润规则数据 var rewardMoneyOr: String = "" // 奖励分润 var placeAmountReward: String = "" var placeProductDates: NSMutableArray = NSMutableArray() var placeRuleDates: NSMutableArray = NSMutableArray() var placeRewardMoneyId: String = "" var placeRewardMoney: String = "" let navTitleV: ActiveNavCustomView = { let view = CommonViewUntils.getViewForXIB(xibName: "ActiveNavCustomView") as! ActiveNavCustomView return view }() @IBOutlet weak var tableView: UITableView! override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) if canEdit { initNavRightButtonForTitle(title: "保存", color: UIColor.white) } } override func viewDidLoad() { super.viewDidLoad() navInit() tableView.delegate = self tableView.dataSource = self tableView.tableFooterView = UIView.init() tableView.separatorStyle = .none tableView.register(UINib(nibName: "ActiveDetailCell", bundle: nil), forCellReuseIdentifier: "activeDetailCell") loadData() } func navInit() { navTitleV.canEdit = isActive if isActive == false { navTitleV.titleLabel.text = "我的政策" } else { let tapG: UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(tap(tap:))) navTitleV.addGestureRecognizer(tapG) } self.navigationItem.titleView = navTitleV initNavLeftBackButton() } // MARK: - request func loadData() { // 查询 let url = RequestURL.activityEdit let params = NSMutableDictionary() params.setValue(modelJson(), forKey: "model") params.setValue("0003", forKey: "code") loadDataInfoPost(url: url, params: params, tag: 1001) } func modelJson() -> String { let tempDic = NSMutableDictionary() tempDic.setValue(activityId, forKey: "activityId") tempDic.setValue(userId, forKey: "userId") let data = (try? JSONSerialization.data(withJSONObject: tempDic, options: .prettyPrinted))! let strJson = NSString(data: data, encoding: String.Encoding.utf8.rawValue) return strJson! as String } func loadDataForUpdate() { // 修改 let url = RequestURL.activityEdit let params = NSMutableDictionary() let modelStr = modelJsonForUpdate() if modelStr == "" { return } params.setValue(modelStr, forKey: "model") params.setValue("0005", forKey: "code") loadDataInfoPost(url: url, params: params, tag: 1002) } func modelJsonForUpdate() -> String { let tempDic = NSMutableDictionary() tempDic.setValue(activityId, forKey: "activityId") tempDic.setValue(userId, forKey: "userId") // 产品政策 let curProRulesConfigs: NSMutableArray = NSMutableArray() for i in 0...(productRulesConfigs.count - 1) { let dic: NSDictionary = productRulesConfigs[i] as! NSDictionary let mDic: NSMutableDictionary = NSMutableDictionary(dictionary: dic) let productC: String = productDatas[i] as! String if productC.length() == 0 { SVProgressHUD.showError(withStatus: "请先填写\(getString(key: "timeRangeMin", dic: dic))至\(getString(key: "timeRangeMax", dic: dic))天档位的金额") return "" } mDic.setValue(productDatas[i], forKey: "rewardMoney") curProRulesConfigs.add(mDic) } let policys: NSArray = dicData["productPolicys"] as! NSArray let policyDic: NSDictionary = policys.firstObject as! NSDictionary let productPolicysDic: NSMutableDictionary = NSMutableDictionary() if activationAmountReward.length() == 0 { SVProgressHUD.showError(withStatus: "请先填写激活奖励的金额") return "" } productPolicysDic.setValue(activationAmountReward, forKey: "activationAmountReward") productPolicysDic.setValue(policyDic["id"], forKey: "id") productPolicysDic.setValue(curProRulesConfigs, forKey: "productRulesConfigs") tempDic.setValue([productPolicysDic], forKey: "productPolicys") // 分润规则 let curDisRulesConfigs: NSMutableArray = NSMutableArray() for i in 0...(distributionRuleConfigs.count - 1) { let dic: NSDictionary = distributionRuleConfigs[i] as! NSDictionary let mDic: NSMutableDictionary = NSMutableDictionary(dictionary: dic) let curStr: String = ruleDatas[i] as! String let curAm = Float(curStr) let defStr: String = placeRuleDates[i] as! String let defAm = Float(defStr) let curF = CGFloat.init(curAm ?? 0) let defF = CGFloat.init(defAm ?? 0) if curStr.length() == 0 { SVProgressHUD.showError(withStatus: "请先填写\(getString(key: "cardName", dic: dic))价格") return "" } if curF < defF { SVProgressHUD.showError(withStatus: "填写\(getString(key: "cardName", dic: dic))价格应大于或等于\(getString(key: "costRate", dic: dic))") return "" } mDic.setValue(ruleDatas[i], forKey: "costRate") curDisRulesConfigs.add(mDic) } let rules: NSArray = dicData["distributionRules"] as! NSArray let ruleDic: NSDictionary = rules.firstObject as! NSDictionary let distributionRule = ["distributionRuleConfig": curDisRulesConfigs, "id": ruleDic["id"],"debitCardCostRate":ruleDic["debitCardCostRate"],"debitCardCapping":ruleDic["debitCardCapping"]] tempDic .setValue(distributionRule, forKey: "distributionRule") if getString(key: "rewardMoneyOrId").length() > 0 && placeRewardMoney.length() > 0 { // if rewardMoneyOr.length() == 0 { // SVProgressHUD.showError(withStatus: "请先填写奖励分润") // return "" // } let curAm = Float(rewardMoneyOr) let defAm = Float(placeRewardMoney) let curF = CGFloat.init(curAm ?? 0) let defF = CGFloat.init(defAm ?? 0) if curF > defF { SVProgressHUD.showError(withStatus: "填写奖励分润应大于或等于\(getString(key: "rewardMoneyOr"))") return "" } tempDic.setValue(rewardMoneyOr, forKey: "rewardMoneyOr") tempDic.setValue(dicData["rewardMoneyOrId"], forKey: "rewardMoneyOrId") } let data = (try? JSONSerialization.data(withJSONObject: tempDic, options: .prettyPrinted))! let strJson = NSString(data: data, encoding: String.Encoding.utf8.rawValue) return strJson! as String } override func returnData(tag: Int) { if tag == 1001 { //查询 // 产品政策 let policys: NSArray = dicData["productPolicys"] as! NSArray if policys.count != 0 { let policyDic: NSDictionary = policys.firstObject as! NSDictionary activationAmountReward = policyDic["activationAmountReward"] as! String if canEdit == false { placeAmountReward = policyDic["activationAmountReward"] as! String } productRulesConfigs = policyDic["productRulesConfigs"] as! NSArray for dic in productRulesConfigs { productDatas.add(getString(key: "rewardMoney", dic: dic as! NSDictionary)) if canEdit == false { placeProductDates.add(getString(key: "rewardMoney", dic: dic as! NSDictionary)) } } } else { productRulesConfigs = [] } // 分润规则 let rules: NSArray = dicData["distributionRules"] as! NSArray if rules.count != 0 { let ruleDic: NSDictionary = rules.firstObject as! NSDictionary distributionRuleConfigs = ruleDic["distributionRuleConfigs"] as! NSArray ruleDatas.removeAllObjects() for dic in distributionRuleConfigs { ruleDatas.add(getString(key: "costRate", dic: dic as! NSDictionary)) if canEdit == false { placeRuleDates.add(getString(key: "costRate", dic: dic as! NSDictionary)) } } } else { distributionRuleConfigs = [] } rewardMoneyOr = getString(key: "rewardMoneyOr") if canEdit == false { placeRewardMoney = getString(key: "rewardMoneyOr") placeRewardMoneyId = getString(key: "rewardMoneyOrId") } tableView.reloadData() } else if tag == 1002 { // 修改 self.navigationController?.popViewController(animated: true) } } override func returnError(tag: Int, type: String) { } // MARK: - action @objc func tap(tap: UITapGestureRecognizer) { let listVc = DelegateListForActiveViewController() listVc.selectBlock = { (dic) in if self.getString(key: "realName", dic: dic) == "" { self.navTitleV.titleLabel.text = "未实名" } else { self.navTitleV.titleLabel.text = "\(self.getString(key: "realName", dic: dic)) " } self.canEdit = true self.userId = dic["id"] as! String self.loadData() } self.navigationController?.pushViewController(listVc, animated: true) } override func btnRightMenuClick() { if productRulesConfigs.count == 0 && distributionRuleConfigs.count == 0 { return } loadDataForUpdate() } // MARK: - delegate func numberOfSections(in tableView: UITableView) -> Int { if dicData.allKeys.count == 0 || (productRulesConfigs.count == 0 && distributionRuleConfigs.count == 0) { return 0 } if getString(key: "rewardMoneyOrId").length() > 0 && placeRewardMoney.length() > 0 { return 2 } return 2 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if section == 0 { return productRulesConfigs.count>0 ? 3 : 1 } else if section == 1 { return distributionRuleConfigs.count + 1 } return 1 } func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 60 } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 60 } func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let headerV = CommonViewUntils.getViewForXIB(xibName: "ActiveDetailHeaderView") as! ActiveDetailHeaderView headerV.topic = topicArr[section] as! String return headerV } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "activeDetailCell", for: indexPath) as! ActiveDetailCell cell.selectionStyle = .none cell.trailingTextField.isUserInteractionEnabled = canEdit cell.textFieldBlock = { [weak self] (str) in if indexPath.section == 0 { if indexPath.row == 0 { self?.activationAmountReward = str } else if indexPath.row > 1 { self?.productDatas.replaceObject(at: indexPath.row - 2, with: str) } } else if indexPath.section == 1 { if indexPath.row > 0 { self?.ruleDatas.replaceObject(at: indexPath.row - 1, with: str) } } else if indexPath.section == 2 { self?.rewardMoneyOr = str } } if indexPath.section == 0 { if indexPath.row == 0 { cell.isNeedSep = true cell.bgView.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xD7ECF8) cell.leadingLabel.text = "激活奖励(元)" cell.centerLabel.text = "" cell.trailingTextField.text = activationAmountReward cell.trailingTextField.placeholder = "输入≤\(placeAmountReward)的金额" } else if indexPath.row == 1 { cell.isNeedSep = true cell.bgView.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xDEE9F8) cell.leadingLabel.text = "时间档位(天)" cell.centerLabel.text = "消费额度(元)" cell.trailingTextField.text = "交易达标(元/户)" cell.trailingTextField.isUserInteractionEnabled = false } else { cell.isNeedSep = false cell.bgView.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xEBEEF0) let configDic: NSDictionary = productRulesConfigs[indexPath.row - 2] as! NSDictionary let timeMax: String = configDic["timeRangeMax"] as! String let timeMin: String = configDic["timeRangeMin"] as! String cell.leadingLabel.text = "\(timeMin)至\(timeMax)天" cell.centerLabel.text = (configDic["costTotal"] as! String) cell.trailingTextField.text = (configDic["rewardMoney"] as! String) // cell.trailingTextField.placeholder = "输入≤\(configDic["rewardMoney"] ?? "0")的金额" let earn: String = (configDic["rewardMoney"] as! String) cell.trailingTextField.placeholder = "输入≤\(earn)的金额" } } else if indexPath.section == 1 { if indexPath.row == 0 { cell.isNeedSep = true cell.trailingTextField.isUserInteractionEnabled = false cell.bgView.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xDEE9F8) cell.leadingLabel.text = "卡类型" cell.centerLabel.text = "" cell.trailingTextField.text = "结算价格(%)" } else { cell.isNeedSep = false cell.bgView.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xEBEEF0) let ruleDic: NSDictionary = distributionRuleConfigs[indexPath.row - 1] as! NSDictionary cell.leadingLabel.text = (ruleDic["cardName"] as! String) cell.centerLabel.text = "" cell.trailingTextField.text = (ruleDic["costRate"] as! String) let rate: String = (ruleDic["costRate"] as! String) cell.trailingTextField.placeholder = "输入≥\(rate)的价格" } } else if indexPath.section == 2 { cell.isNeedSep = false cell.bgView.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xD7ECF8) cell.leadingLabel.text = "奖励分润(%)" cell.centerLabel.text = "" cell.trailingTextField.text = getString(key: "rewardMoneyOr") // cell.trailingTextField.placeholder = "输入≤\(getString(key: "rewardMoneyOr"))的分润" cell.trailingTextField.placeholder = "输入≤\(placeRewardMoney)的分润" } if indexPath.section == 0 { if indexPath.row == 0 { cell.type = "1" } else if indexPath.row == productRulesConfigs.count + 1 { cell.type = "2" } else { cell.type = "0" } } else if indexPath.section == 1 { if indexPath.row == 0 { cell.type = "1" } else if indexPath.row == distributionRuleConfigs.count { cell.type = "2" } else { cell.type = "0" } } else if indexPath.section == 2 { if indexPath.row == 0 { cell.type = "3" } } return cell } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destination. // Pass the selected object to the new view controller. } */ } //extension UITableView { // // func addNotifications() { // // NotificationCenter.default.addObserver(self, selector: #selector(boardWillShow(not:)), name: UIResponder.keyboardWillShowNotification, object: nil) // // NotificationCenter.default.addObserver(self, selector: #selector(boardDidHide(not:)), name: UIResponder.keyboardWillHideNotification, object: nil) // // } // // // // @objc func boardWillShow(not: NSNotification) { // // if let userInfo = not.userInfo { // // if let keyBoardRect = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect { // // UIView.animate(withDuration: 0.25) { // // self.contentInset = UIEdgeInsetsMake(0, 0, keyBoardRect.size.height, 0) // // } // // } // // } // // } // // // // @objc func boardDidHide(not: NSNotification) { // // UIView.animate(withDuration: 0.25) { // // self.contentInset = .zero // // } // // } // // func `deinit`() { // // NotificationCenter.default.removeObserver(self) // // } // //}