SPSBPickerViewManager.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. //
  2. // SPSBPickerViewManager.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/5/14.
  6. //
  7. #import "SPSBPickerViewManager.h"
  8. #import "SPSBPickerViewController.h"
  9. #import "SPSBSQLProfile.h"
  10. #import "SPSBBusinessManager.h"
  11. SPSBPickerViewType const SPSBPickerViewTypeMailAddress = @"MailAddress";
  12. @interface SPSBPickerViewManager ()<SPSBPickerViewDelegate> {
  13. NSArray *_provinceIdArray;
  14. NSArray *_provinceArray;
  15. NSArray *_cityArray;
  16. NSArray *_cityIdArray;
  17. NSArray *_districtArray;
  18. void (^_tempComplete)(SPSBPickerViewResult result);
  19. SPSBPickerViewType _currentType;
  20. }
  21. @end
  22. @implementation SPSBPickerViewManager
  23. - (instancetype)initWithTypes:(NSArray<SPSBPickerViewType> *)types {
  24. self = [super init];
  25. if (!self) return nil;
  26. [self createDateWithTypes:types];
  27. return self;
  28. }
  29. - (void)createDateWithTypes:(NSArray<SPSBPickerViewType> *)types {
  30. if ([types containsObject:SPSBPickerViewTypeMailAddress]) {
  31. _provinceIdArray = [JXHFMDBManager searchWithPath:SPSBShanpSocialInsuranceCitySQL backKey:@"id" condition:^id(FMDatabase *database) {
  32. return [database executeQuery:@"select * from city where parentId = ?", @"100000"];
  33. }];
  34. _provinceArray = [JXHFMDBManager searchWithPath:SPSBShanpSocialInsuranceCitySQL backKey:@"name" condition:^id(FMDatabase *database) {
  35. return [database executeQuery:@"select * from city where parentId = ?", @"100000"];
  36. }];
  37. }
  38. }
  39. - (void)showPickerViewWithType:(SPSBPickerViewType)type defaultValue:(nullable NSString *)defaultValue complete:(void (^)(SPSBPickerViewResult result))complete {
  40. SEL action = NSSelectorFromString([NSString stringWithFormat:@"show%@WithValue:", type]);
  41. if (![self respondsToSelector:action]) {
  42. return;
  43. }
  44. _tempComplete = complete;
  45. _currentType = type;
  46. IMP imp = [self methodForSelector:action];
  47. void (*func)(id, SEL, NSString *) = (void *)imp;
  48. func(self, action, defaultValue);
  49. }
  50. #pragma mark - Delegate
  51. - (void)pickerView:(SPSBPickerView *)pickerView didChoseRow:(NSInteger)row inComponent:(NSInteger)component {
  52. if ([_currentType isEqualToString:SPSBPickerViewTypeMailAddress]) {
  53. [self pickerView:pickerView didChoseMailAddressRow:row inComponent:component];
  54. }
  55. }
  56. #pragma mark - MailAddress
  57. - (void)showMailAddressWithValue:(nullable NSString *)value {
  58. if (!_provinceIdArray || !_provinceArray) return;
  59. SPSBPickerViewController *vc = SPSBPickerViewController.new;
  60. vc.spsb_pickerDelegate = self;
  61. vc.spsb_dataArray = @[_provinceArray, @[], @[]];
  62. @weakify(self)
  63. vc.spsb_finishChose = ^(SPSBPickerView * _Nonnull pickerView) {
  64. @strongify(self)
  65. [self completeToChoseMailAddressWithPickerView:pickerView];
  66. };
  67. vc.spsb_willDisplay = ^(SPSBPickerView * _Nonnull pickerView) {
  68. [pickerView chooseRow:0 inComponent:0 animated:false];
  69. [pickerView chooseRow:0 inComponent:1 animated:false];
  70. [pickerView chooseRow:0 inComponent:2 animated:false];
  71. };
  72. [_spsb_controller presentViewController:vc animated:false completion:nil];
  73. }
  74. - (void)pickerView:(SPSBPickerView *)pickerView didChoseMailAddressRow:(NSInteger)row inComponent:(NSInteger)component {
  75. if (component == 0) {
  76. _cityArray = [JXHFMDBManager searchWithPath:SPSBShanpSocialInsuranceCitySQL backKey:@"name" condition:^id(FMDatabase *database) {
  77. return [database executeQuery:@"select * from city where parentId = ?", self->_provinceIdArray[row]];
  78. }];
  79. _cityIdArray = [JXHFMDBManager searchWithPath:SPSBShanpSocialInsuranceCitySQL backKey:@"id" condition:^id(FMDatabase *database) {
  80. return [database executeQuery:@"select * from city where parentId = ?", self->_provinceIdArray[row]];
  81. }];
  82. if (_cityIdArray.count == 0) {
  83. _districtArray = @[];
  84. } else {
  85. _districtArray = [JXHFMDBManager searchWithPath:SPSBShanpSocialInsuranceCitySQL backKey:@"name" condition:^id(FMDatabase *database) {
  86. return [database executeQuery:@"select * from city where parentId = ?", self->_cityIdArray[0]];
  87. }];
  88. }
  89. [pickerView reloadViewWithData:@[_provinceArray, _cityArray, _districtArray]];
  90. [pickerView chooseRow:0 inComponent:1 animated:false];
  91. [pickerView chooseRow:0 inComponent:2 animated:false];
  92. } else if (component == 1) {
  93. if (_cityIdArray.count == 0) {
  94. _districtArray = @[];
  95. } else {
  96. _districtArray = [JXHFMDBManager searchWithPath:SPSBShanpSocialInsuranceCitySQL backKey:@"name" condition:^id(FMDatabase *database) {
  97. return [database executeQuery:@"select * from city where parentId = ?", self->_cityIdArray[row]];
  98. }];
  99. }
  100. [pickerView reloadViewWithData:@[_provinceArray, _cityArray, _districtArray]];
  101. [pickerView chooseRow:0 inComponent:2 animated:false];
  102. }
  103. }
  104. - (void)completeToChoseMailAddressWithPickerView:(SPSBPickerView *)pickerView {
  105. if (!_tempComplete) return;
  106. NSString *province = _provinceArray[[pickerView.spsb_choseArray[0] integerValue]];
  107. NSString *city;
  108. if (_cityArray.count != 0) {
  109. city = _cityArray[[pickerView.spsb_choseArray[1] integerValue]];
  110. } else {
  111. city = @"";
  112. }
  113. if ([city isEqualToString:province]) {
  114. city = @"";
  115. }
  116. NSString *district;
  117. if (_districtArray.count != 0) {
  118. district = _districtArray[[pickerView.spsb_choseArray[2] integerValue]];
  119. } else {
  120. district = @"";
  121. }
  122. NSString *displayString = [NSString stringWithFormat:@"%@%@%@", province, city, district];
  123. SPSBPickerViewResult result;
  124. result.spsb_uploadData = displayString;
  125. result.spsb_showData = displayString;
  126. _tempComplete(result);
  127. _tempComplete = nil;
  128. }
  129. //#pragma mark - Gender
  130. //- (void)showGenderWithValue:(nullable NSString *)value {
  131. // SPSBPickerViewController *vc = SPSBPickerViewController.new;
  132. // vc.spsb_dataArray = @[@[@"男", @"女"]];
  133. // NSUInteger row = [vc.spsb_dataArray[0] indexOfObject:value];
  134. // if (row >= vc.spsb_dataArray[0].count) {
  135. // row = 0;
  136. // }
  137. // @weakify(self)
  138. // vc.spsb_finishChose = ^(SPSBPickerView * _Nonnull pickerView) {
  139. // @strongify(self)
  140. // [self completeToChoseGenderWithPickerView:pickerView];
  141. // };
  142. // vc.spsb_willDisplay = ^(SPSBPickerView * _Nonnull pickerView) {
  143. // [pickerView chooseRow:row inComponent:0 animated:false];
  144. // };
  145. // [_spsb_controller presentViewController:vc animated:false completion:nil];
  146. //}
  147. //
  148. //- (void)completeToChoseGenderWithPickerView:(SPSBPickerView *)pickerView {
  149. // if (!_tempComplete) return;
  150. // NSInteger selectedRow = [pickerView.spsb_choseArray.firstObject integerValue];
  151. // NSString *data;
  152. // NSString *displayData;
  153. // if (selectedRow == 0) {
  154. // data = @"1";
  155. // displayData = spsb_genderArray()[1];
  156. // } else {
  157. // data = @"0";
  158. // displayData = spsb_genderArray()[0];
  159. // }
  160. // SPSBPickerViewResult result;
  161. // result.spsb_uploadData = data;
  162. // result.spsb_showData = displayData;
  163. // _tempComplete(result);
  164. // _tempComplete = nil;
  165. //}
  166. //
  167. //#pragma mark - Age
  168. //- (void)showAgeWithValue:(nullable NSString *)value {
  169. // NSArray *dataArray = [JXHFMDBManager searchWithPath:SPSBFixedDataSQL key:SPSBFixedAgeSQLKey condition:^id(FMDatabase *database) {
  170. // return [database executeQuery:@"select * from data where key = ?", SPSBFixedAgeSQLKey];
  171. // }];
  172. // SPSBPickerViewController *vc = SPSBPickerViewController.new;
  173. // vc.spsb_pickerDelegate = self;
  174. // vc.spsb_dataArray = @[dataArray];
  175. // @weakify(self)
  176. // vc.spsb_finishChose = ^(SPSBPickerView * _Nonnull pickerView) {
  177. // @strongify(self)
  178. // [self completeToAgeWithPickerView:pickerView];
  179. // };
  180. // [_spsb_controller presentViewController:vc animated:false completion:nil];
  181. //}
  182. //
  183. //- (void)completeToAgeWithPickerView:(SPSBPickerView *)pickerView {
  184. // if (!_tempComplete) return;
  185. // NSInteger index = [pickerView.spsb_choseArray[0] integerValue];
  186. // NSString *data = [NSString stringWithFormat:@"%ld", (long)index + 1];
  187. // NSString *displayData = [NSString stringWithFormat:@"%ld岁", (long)index + 1];
  188. // SPSBPickerViewResult result;
  189. // result.spsb_uploadData = data;
  190. // result.spsb_showData = displayData;
  191. // _tempComplete(result);
  192. // _tempComplete = nil;
  193. //}
  194. //
  195. //#pragma mark - Birthday
  196. //- (void)showBirthdayWithValue:(nullable NSString *)value {
  197. // NSArray *lastChoseArr;
  198. // if (value.length > 0) {
  199. // lastChoseArr = @[[value substringToIndex:4], [value substringWithRange:NSMakeRange(5, value.length - 7)], [value substringFromIndex:value.length - 2]];
  200. // } else {
  201. // lastChoseArr = @[@"1990年", @"正月", @"初一"];
  202. // }
  203. // NSInteger year = [lastChoseArr[0] integerValue];
  204. // _leapMonth = spsb_leapMonth(year);
  205. // NSMutableArray *monthArray = [_chineseCalendarArray[1] mutableCopy];
  206. // if (_leapMonth != 0) {
  207. // [monthArray insertObject:_leapMonthArray[_leapMonth - 1] atIndex:_leapMonth];
  208. // }
  209. // _tempMonthArray = monthArray;
  210. // NSInteger monthIndex = [monthArray indexOfObject:lastChoseArr[1]];
  211. // NSInteger days;
  212. // if (_leapMonth != 0) {
  213. // if (monthIndex < _leapMonth) {
  214. // days = spsb_monthDays(year, monthIndex + 1);
  215. // } else if (monthIndex == _leapMonth) {
  216. // days = spsb_leapDays(year);
  217. // } else {
  218. // days = spsb_monthDays(year, monthIndex);
  219. // }
  220. // } else {
  221. // days = spsb_monthDays(year, monthIndex + 1);
  222. // }
  223. // NSArray *dayArray = [_chineseCalendarArray[2] subarrayWithRange:NSMakeRange(0, days)];
  224. // NSInteger dayIndex = [dayArray indexOfObject:lastChoseArr[2]];
  225. //
  226. // SPSBPickerViewController *vc = SPSBPickerViewController.new;
  227. // vc.spsb_pickerDelegate = self;
  228. // vc.spsb_dataArray = @[_chineseCalendarArray[0], monthArray, dayArray];
  229. // @weakify(self)
  230. // vc.spsb_finishChose = ^(SPSBPickerView * _Nonnull pickerView) {
  231. // @strongify(self)
  232. // [self completeToChoseBirthdayWithPickerView:pickerView];
  233. // };
  234. // vc.spsb_willDisplay = ^(SPSBPickerView * _Nonnull pickerView) {
  235. // [pickerView chooseRow:year - 1900 inComponent:0 animated:false];
  236. // [pickerView chooseRow:monthIndex inComponent:1 animated:false];
  237. // [pickerView chooseRow:dayIndex inComponent:2 animated:false];
  238. // };
  239. // [_spsb_controller presentViewController:vc animated:false completion:nil];
  240. //}
  241. //
  242. //- (void)pickerView:(SPSBPickerView *)pickerView didChoseBrithdayRow:(NSInteger)row inComponent:(NSInteger)component {
  243. // switch (component) {
  244. // case 0: {
  245. // NSInteger year = [_chineseCalendarArray[component][row] integerValue];
  246. // _leapMonth = spsb_leapMonth(year);
  247. // if (_leapMonth != 0) {
  248. // NSMutableArray *monthArray = [_chineseCalendarArray[1] mutableCopy];
  249. // [monthArray insertObject:_leapMonthArray[_leapMonth - 1] atIndex:_leapMonth];
  250. // NSArray *dataArray = @[_chineseCalendarArray[0], monthArray, _chineseCalendarArray[2]];
  251. // _tempMonthArray = monthArray;
  252. // [pickerView reloadViewWithData:dataArray];
  253. // } else {
  254. // _tempMonthArray = _chineseCalendarArray[1];
  255. // [pickerView reloadViewWithData:_chineseCalendarArray];
  256. //
  257. // }
  258. // [pickerView chooseRow:0 inComponent:1 animated:true];
  259. // }
  260. // break;
  261. // case 1: {
  262. // NSInteger year = [_chineseCalendarArray[0][[pickerView.spsb_choseArray[0] integerValue]] integerValue];
  263. // NSInteger monthIndex = row;
  264. // NSInteger days;
  265. // if (_leapMonth != 0) {
  266. // if (monthIndex < _leapMonth) {
  267. // days = spsb_monthDays(year, monthIndex + 1);
  268. // } else if (monthIndex == _leapMonth) {
  269. // days = spsb_leapDays(year);
  270. // } else {
  271. // days = spsb_monthDays(year, monthIndex);
  272. // }
  273. // } else {
  274. // days = spsb_monthDays(year, monthIndex + 1);
  275. // }
  276. // NSArray *dayArray = [_chineseCalendarArray[2] subarrayWithRange:NSMakeRange(0, days)];
  277. // NSArray *dataArray = @[_chineseCalendarArray[0], _tempMonthArray, dayArray];
  278. // [pickerView reloadViewWithData:dataArray];
  279. // [pickerView chooseRow:0 inComponent:2 animated:true];
  280. // }
  281. // break;
  282. //
  283. // default:
  284. // break;
  285. // }
  286. //}
  287. //
  288. //- (void)completeToChoseBirthdayWithPickerView:(SPSBPickerView *)pickerView {
  289. // if (!_tempComplete) return;
  290. // NSMutableArray *dateArray = [@[] mutableCopy];
  291. // for (NSInteger i = 0; i < pickerView.spsb_choseArray.count; i ++) {
  292. // NSInteger selectedRow = [pickerView.spsb_choseArray[i] integerValue];
  293. // NSString *date;
  294. // if (i == 1) {
  295. // date = _tempMonthArray[selectedRow];
  296. // } else {
  297. // date = _chineseCalendarArray[i][selectedRow];
  298. // }
  299. //
  300. // [dateArray addObject:date];
  301. // }
  302. // NSString *dateString = [NSString stringWithFormat:@"%@%@%@", dateArray[0], dateArray[1], dateArray[2]];
  303. // debugLog(@"%@", dateString);
  304. // SPSBPickerViewResult result;
  305. // result.spsb_uploadData = dateString;
  306. // result.spsb_showData = dateString;
  307. // _tempComplete(result);
  308. // _tempComplete = nil;
  309. //}
  310. //
  311. //static long lunarInfo[] = {
  312. // 0x4bd8, 0x4ae0, 0xa570, 0x54d5, 0xd260, 0xd950, 0x5554, 0x56af, 0x9ad0, 0x55d2,
  313. // 0x4ae0, 0xa5b6, 0xa4d0, 0xd250, 0xd255, 0xb54f, 0xd6a0, 0xada2, 0x95b0, 0x4977,
  314. // 0x497f, 0xa4b0, 0xb4b5, 0x6a50, 0x6d40, 0xab54, 0x2b6f, 0x9570, 0x52f2, 0x4970,
  315. // 0x6566, 0xd4a0, 0xea50, 0x6a95, 0x5adf, 0x2b60, 0x86e3, 0x92ef, 0xc8d7, 0xc95f,
  316. // 0xd4a0, 0xd8a6, 0xb55f, 0x56a0, 0xa5b4, 0x25df, 0x92d0, 0xd2b2, 0xa950, 0xb557,
  317. // 0x6ca0, 0xb550, 0x5355, 0x4daf, 0xa5b0, 0x4573, 0x52bf, 0xa9a8, 0xe950, 0x6aa0,
  318. // 0xaea6, 0xab50, 0x4b60, 0xaae4, 0xa570, 0x5260, 0xf263, 0xd950, 0x5b57, 0x56a0,
  319. // 0x96d0, 0x4dd5, 0x4ad0, 0xa4d0, 0xd4d4, 0xd250, 0xd558, 0xb540, 0xb6a0, 0x95a6,
  320. // 0x95bf, 0x49b0, 0xa974, 0xa4b0, 0xb27a, 0x6a50, 0x6d40, 0xaf46, 0xab60, 0x9570,
  321. // 0x4af5, 0x4970, 0x64b0, 0x74a3, 0xea50, 0x6b58, 0x5ac0, 0xab60, 0x96d5, 0x92e0,
  322. // 0xc960, 0xd954, 0xd4a0, 0xda50, 0x7552, 0x56a0, 0xabb7, 0x25d0, 0x92d0, 0xcab5,
  323. // 0xa950, 0xb4a0, 0xbaa4, 0xad50, 0x55d9, 0x4ba0, 0xa5b0, 0x5176, 0x52bf, 0xa930,
  324. // 0x7954, 0x6aa0, 0xad50, 0x5b52, 0x4b60, 0xa6e6, 0xa4e0, 0xd260, 0xea65, 0xd530,
  325. // 0x5aa0, 0x76a3, 0x96d0, 0x4afb, 0x4ad0, 0xa4d0, 0xd0b6, 0xd25f, 0xd520, 0xdd45,
  326. // 0xb5a0, 0x56d0, 0x55b2, 0x49b0, 0xa577, 0xa4b0, 0xaa50, 0xb255, 0x6d2f, 0xada0,
  327. // 0x4b63, 0x937f, 0x49f8, 0x4970, 0x64b0, 0x68a6, 0xea5f, 0x6b20, 0xa6c4, 0xaaef,
  328. // 0x92e0, 0xd2e3, 0xc960, 0xd557, 0xd4a0, 0xda50, 0x5d55, 0x56a0, 0xa6d0, 0x55d4,
  329. // 0x52d0, 0xa9b8, 0xa950, 0xb4a0, 0xb6a6, 0xad50, 0x55a0, 0xaba4, 0xa5b0, 0x52b0,
  330. // 0xb273, 0x6930, 0x7337, 0x6aa0, 0xad50, 0x4b55, 0x4b6f, 0xa570, 0x54e4, 0xd260,
  331. // 0xe968, 0xd520, 0xdaa0, 0x6aa6, 0x56df, 0x4ae0, 0xa9d4, 0xa4d0, 0xd150, 0xf252,
  332. // 0xd520};
  333. //
  334. //// ====== 传回农历 y年闰哪个月 1-12 , 没闰传回 0
  335. //NSInteger spsb_leapMonth(NSInteger y) {
  336. // long var = lunarInfo[y - 1900] & 0xf;
  337. // return (NSInteger)(var == 0xf ? 0 : var);
  338. //}
  339. //
  340. //// ====== 传回农历 y年闰月的天数
  341. //NSInteger spsb_leapDays(NSInteger y) {
  342. // if (spsb_leapMonth(y) != 0) {
  343. // if ((lunarInfo[y - 1899] & 0xf) != 0)
  344. // return 30;
  345. // else
  346. // return 29;
  347. // } else
  348. // return 0;
  349. //}
  350. //
  351. //// ====== 传回农历 y年m月的总天数
  352. //NSInteger spsb_monthDays(NSInteger y, NSInteger m) {
  353. // if ((lunarInfo[y - 1900] & (0x10000 >> m)) == 0)
  354. // return 29;
  355. // else
  356. // return 30;
  357. //}
  358. //
  359. @end