// // NewWareHouseViewController.swift // MTP2_iOS // // Created by Muchinfo on 2021/2/26. // Copyright © 2021 Muchinfo. All rights reserved. // import UIKit import WHToast import ActionSheetPicker_3_0 import NVActivityIndicatorView /// 新增仓库信息视图容器控制类 class NewWareHouseViewController: BaseViewController { // MARK: - 属性列表 /// 仓库简称 @IBOutlet weak var wareHouseSimpleName: UITextField! /// 仓库名称 @IBOutlet weak var wareHouseName: UITextField! /// 仓库类型 @IBOutlet weak var wareHouseType: UIButton! /// 联系人 @IBOutlet weak var contractName: UITextField! /// 联系电话 @IBOutlet weak var contractNum: UITextField! /// 详细地址 @IBOutlet weak var detailAddress: UITextField! /// 所在地区 @IBOutlet weak var address: UIButton! /// 提交按钮 @IBOutlet weak var submit: UIButton! /// 仓库类型 var type: WareHouseType? { didSet { /// 异常 guard let obj = type else { return } wareHouseType.setTitle(obj.description, for: .normal) wareHouseType.setTitleColor(.hex333(), for: .normal) } } /// 国家 var country: MoDivisions? /// 省 var province: MoDivisions? /// 市 var city: MoDivisions? /// 区 var district: MoDivisions? /// 地址信息 var addressStr: String? { didSet { /// 异常 guard let obj = addressStr else { return } address.setTitle(obj, for: .normal) address.setTitleColor(.hex333(), for: .normal) } } /// 操作 var operatorType: OperatorType? = .add { didSet { self.navigationItem.title = (operatorType?.description ?? "")+"仓库" } } /// 仓库信息 var moWareHouse: MoWarehouse? // MARK: - 生命周期 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func buildView() { super.buildView() /// 没有仓库数据 guard let obj = moWareHouse else { return } /// 仓库简称 wareHouseSimpleName.text = obj.warehousecode /// 仓库名称 wareHouseName.text = obj.warehousename /// 仓库类型 type = obj.warehousetype /// 联系电话 contractNum.text = obj.contactnum /// 联系人 contractName.text = obj.contactname /// 详细地址 detailAddress.text = obj.address /// 查询对应的省市区 queryProvincesAndCitys() } // MARK: - 交互相关 /// onButtonPressed /// - Parameter sender: sender @IBAction fileprivate func onButtonPressed(_ sender: UIButton) { switch sender { case address: /// 地址切换 self.view.endEditing(true) /// 弹出地址选择视图 let selectAreaPopupView = Bundle.main.loadNibNamed("SelectArea", owner: nil, options: nil)?.first as! SelectAreaPopupView selectAreaPopupView.delegate = self selectAreaPopupView.show() case submit: /// 提交按钮 /// 仓库提交 doSubmit() default: /// 仓库类型 ActionSheetStringPicker.show(withTitle: "请选择仓库类型", rows: ["厂库", "自有库", "合作库"], initialSelection: 0, doneBlock: { picker, value, index in self.type = WareHouseType(rawValue: value+1) }, cancel: { picker in return }, origin: sender) } } // MARK: - 接口请求 /// 查询对应的省市区 func queryProvincesAndCitys() { /// 异常 guard let obj = moWareHouse else { return } /// 对应的省 province = (try? DatabaseHelper.getAllDivisions())?.first(where: {$0.autoid == obj.provinceid}) /// 对应的市 city = (try? DatabaseHelper.getAllDivisions())?.first(where: {$0.autoid == obj.cityid}) /// 对应的区 district = (try? DatabaseHelper.getAllDivisions())?.first(where: {$0.autoid == obj.districtid}) /// 地址信息 addressStr = "\(province?.divisionname ?? "") \(city?.divisionname ?? "") \(district?.divisionname ?? "")" } /// 新增仓库提交请求 fileprivate func doSubmit() { /// 异常 guard let wareHouseManager = MTP2BusinessCore.shared.wareHouseManager, let warehousetype = type, check() else { return } /// 仓库简称 let simple = wareHouseSimpleName.text /// 仓库名称 let houseName = wareHouseName.text /// 联系人 let name = contractName.text /// 联系电话 let num = contractNum.text /// 详细地址 let detail = detailAddress.text /// 市 let cityid = self.city?.autoid /// 省 let provinceid = self.province?.autoid /// 区 let disrictid = self.district?.autoid /// 新增、修改 let type = operatorType == .add ? 1 : 2 /// ID let id = operatorType == .add ? nil : moWareHouse?.autoid /// 开启Loading NVActivityIndicatorPresenter.sharedInstance.startAnimating(animActivityData("请求中...")) /// 新增申请 wareHouseManager.requestWarehouseApplyReq(type: type, warehouseid: id, warehousecode: simple, warehousename: houseName, warehousetype: warehousetype.rawValue, provinceid: provinceid, cityid: cityid, districtid: disrictid, address: detail, contactname: name, contactnum: num) { (isComplete, er) in DispatchQueue.main.async { /// dismisss loding..... NVActivityIndicatorPresenter.sharedInstance.stopAnimating() /// 失败 if !isComplete { WHToast.showMessage("请求失败,原因:\(er?.retMsg ?? "未知错误")", duration: 1.5, finishHandler: {}) return } WHToast.showSuccess(withMessage: "请求成功", duration: 1.5, finishHandler: { if let bolck = self.popBlock { /// 执行回调 bolck(self.operatorType) } /// 返回上层视图 self.navigationController?.popViewController(animated: true) }) } } } /// 合规性校验 /// - Returns: Bool fileprivate func check() -> Bool { /// 请输入仓库简称 guard let simple = wareHouseSimpleName.text, simple != "" else { WHToast.showMessage("请输入仓库简称", duration: 1.5, finishHandler: {}) return false } /// 请选择仓库类型! guard let _ = type else { WHToast.showMessage("请选择仓库类型", duration: 1.5, finishHandler: {}) return false } /// 请输入仓库名称 guard let name = wareHouseName.text, name != "" else { WHToast.showMessage("请输入仓库名称", duration: 1.5, finishHandler: {}) return false } return true } } // MARK: - SelectAreaPopupViewDelegate extension NewWareHouseViewController: SelectAreaPopupViewDelegate { /// selectAreaPopupView /// - Parameters: /// - sender: sender /// - countryModel: countryModel /// - provinceModel: provinceModel /// - cityModel: cityModel /// - districtModel: districtModel func selectAreaPopupView(_ sender: SelectAreaPopupView, country: MoDivisions, province: MoDivisions, city: MoDivisions?, district: MoDivisions?) { /// 隐藏视图 sender.hidePopupView() /// 地址拼接 self.country = country self.province = province self.city = city self.district = district /// 地址拼接 var areaAddress = province.divisionname if let cityname = city?.divisionname, cityname != "null" { areaAddress.append(" \(cityname)") } if let districtname = district?.divisionname, districtname != "null" { areaAddress.append(" \(districtname)") } self.addressStr = areaAddress } }