// // GoodsInfoEntity+Extension.swift // MTP2_iOS // // Created by Handy_Cao on 2019/6/6. // Copyright © 2019 Muchinfo. All rights reserved. // import Foundation extension GoodsInfoEntity { func setupWith(goodsInfo: MoGoodsInfo) { self.goodsId = Int32(goodsInfo.goodsid) self.goodsCode = goodsInfo.goodscode self.goodsName = goodsInfo.goodsname self.marketID = Int32(goodsInfo.marketid) self.decimalPlace = Int16(goodsInfo.decimalplace) self.currency = goodsInfo.currency self.agreeUnit = goodsInfo.agreeUnit self.goodsUnit = goodsInfo.goodsUnit self.groupID = Int32(goodsInfo.groupID) self.lotSize = Int32(goodsInfo.lotsize) let searchConditionArray = [goodsInfo.goodsname, goodsInfo.goodscode, getFirstLetterFromString(aString: goodsInfo.goodsname)] // 用于商品检索 self.searchCondition = searchConditionArray.joined(separator: ",") self.goodsType = Int32(goodsInfo.goodsType) self.needUpdate = goodsInfo.needUpdate } func toMoGoodsInfo() -> MoGoodsInfo { let goodsInfo = MoGoodsInfo() goodsInfo.goodsid = Int(self.goodsId) goodsInfo.goodscode = self.goodsCode != nil ? self.goodsCode! : "" goodsInfo.goodsname = self.goodsName != nil ? self.goodsName! : "" goodsInfo.marketid = Int(self.marketID) goodsInfo.currency = self.currency != nil ? self.currency! : "" goodsInfo.agreeUnit = self.agreeUnit goodsInfo.goodsUnit = self.goodsUnit != nil ? self.goodsUnit! : "" goodsInfo.groupID = Int(self.groupID) goodsInfo.lotsize = Int(self.lotSize) goodsInfo.goodsType = Int(self.goodsType) goodsInfo.needUpdate = self.needUpdate return goodsInfo } } // MARK: - 获取联系人姓名首字母(传入汉字字符串, 返回大写拼音首字母) func getFirstLetterFromString(aString: String) -> String { /// 多音字处理 func polyphoneStringHandle(nameString:String, pinyinString:String) -> String { if nameString.contains("长") {return "chang"} if nameString.contains("沈") {return "shen"} if nameString.contains("厦") {return "xia"} if nameString.contains("地") {return "di"} if nameString.contains("重") {return "chong"} return pinyinString } guard aString.isChinese() else { return "#" } // 注意,这里一定要转换成可变字符串 let mutableString = NSMutableString.init(string: aString) // 将中文转换成带声调的拼音 CFStringTransform(mutableString as CFMutableString, nil, kCFStringTransformToLatin, false) // 去掉声调(用此方法大大提高遍历的速度) let pinyinString = mutableString.folding(options: [.diacriticInsensitive, .caseInsensitive], locale: NSLocale.current) let array = pinyinString.components(separatedBy: .whitespaces) var results = [String]() for (index,character) in aString.enumerated() { results.append(polyphoneStringHandle(nameString: String(character), pinyinString: array[index]).uppercased().substing(range: 0..<1) ?? "#") } return results.joined(separator: "") }