GoodsInfoEntity+Extension.swift 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // GoodsInfoEntity+Extension.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Handy_Cao on 2019/6/6.
  6. // Copyright © 2019 Muchinfo. All rights reserved.
  7. //
  8. import Foundation
  9. extension GoodsInfoEntity {
  10. func setupWith(goodsInfo: MoGoodsInfo) {
  11. self.goodsId = Int32(goodsInfo.goodsid)
  12. self.goodsCode = goodsInfo.goodscode
  13. self.goodsName = goodsInfo.goodsname
  14. self.marketID = Int32(goodsInfo.marketid)
  15. self.decimalPlace = Int16(goodsInfo.decimalplace)
  16. self.currency = goodsInfo.currency
  17. self.agreeUnit = goodsInfo.agreeUnit
  18. self.goodsUnit = goodsInfo.goodsUnit
  19. self.groupID = Int32(goodsInfo.groupID)
  20. self.lotSize = Int32(goodsInfo.lotsize)
  21. let searchConditionArray = [goodsInfo.goodsname, goodsInfo.goodscode, getFirstLetterFromString(aString: goodsInfo.goodsname)]
  22. // 用于商品检索
  23. self.searchCondition = searchConditionArray.joined(separator: ",")
  24. self.goodsType = Int32(goodsInfo.goodsType)
  25. self.needUpdate = goodsInfo.needUpdate
  26. }
  27. func toMoGoodsInfo() -> MoGoodsInfo {
  28. let goodsInfo = MoGoodsInfo()
  29. goodsInfo.goodsid = Int(self.goodsId)
  30. goodsInfo.goodscode = self.goodsCode != nil ? self.goodsCode! : ""
  31. goodsInfo.goodsname = self.goodsName != nil ? self.goodsName! : ""
  32. goodsInfo.marketid = Int(self.marketID)
  33. goodsInfo.currency = self.currency != nil ? self.currency! : ""
  34. goodsInfo.agreeUnit = self.agreeUnit
  35. goodsInfo.goodsUnit = self.goodsUnit != nil ? self.goodsUnit! : ""
  36. goodsInfo.groupID = Int(self.groupID)
  37. goodsInfo.lotsize = Int(self.lotSize)
  38. goodsInfo.goodsType = Int(self.goodsType)
  39. goodsInfo.needUpdate = self.needUpdate
  40. return goodsInfo
  41. }
  42. }
  43. // MARK: - 获取联系人姓名首字母(传入汉字字符串, 返回大写拼音首字母)
  44. func getFirstLetterFromString(aString: String) -> String {
  45. /// 多音字处理
  46. func polyphoneStringHandle(nameString:String, pinyinString:String) -> String {
  47. if nameString.contains("长") {return "chang"}
  48. if nameString.contains("沈") {return "shen"}
  49. if nameString.contains("厦") {return "xia"}
  50. if nameString.contains("地") {return "di"}
  51. if nameString.contains("重") {return "chong"}
  52. return pinyinString
  53. }
  54. guard aString.isChinese() else { return "#" }
  55. // 注意,这里一定要转换成可变字符串
  56. let mutableString = NSMutableString.init(string: aString)
  57. // 将中文转换成带声调的拼音
  58. CFStringTransform(mutableString as CFMutableString, nil, kCFStringTransformToLatin, false)
  59. // 去掉声调(用此方法大大提高遍历的速度)
  60. let pinyinString = mutableString.folding(options: [.diacriticInsensitive, .caseInsensitive], locale: NSLocale.current)
  61. let array = pinyinString.components(separatedBy: .whitespaces)
  62. var results = [String]()
  63. for (index,character) in aString.enumerated() {
  64. results.append(polyphoneStringHandle(nameString: String(character), pinyinString: array[index]).uppercased().substing(range: 0..<1) ?? "#")
  65. }
  66. return results.joined(separator: "")
  67. }