SelectUploadOrderViewController.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // SelectUploadOrderViewController.m
  3. // iSales-NPD
  4. //
  5. // Created by Ray on 9/3/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "SelectUploadOrderViewController.h"
  9. #import "iSalesDB.h"
  10. #import <sqlite3.h>
  11. #import "SelectOrderTableViewCell.h"
  12. @interface SelectUploadOrderViewController ()<UITableViewDataSource,UITableViewDelegate>
  13. @property (nonatomic,strong) NSMutableArray<NSDictionary *> *sync_data;
  14. @property (strong, nonatomic) IBOutlet UITableView *syncDataTableView;
  15. @property (strong, nonatomic) IBOutlet UIButton *uploadButton;
  16. @end
  17. @implementation SelectUploadOrderViewController
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. // Do any additional setup after loading the view.
  21. // self.syncDataTableView.delegate = self;
  22. // self.syncDataTableView.dataSource = self;
  23. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  24. style:UIBarButtonItemStylePlain
  25. target:self
  26. action:@selector( onCloseClick:)];
  27. self.navigationItem.rightBarButtonItem = closeButton;
  28. }
  29. - (void)didReceiveMemoryWarning {
  30. [super didReceiveMemoryWarning];
  31. // Dispose of any resources that can be recreated.
  32. }
  33. - (NSMutableArray<NSDictionary *> *)sync_data {
  34. if (!_sync_data) {
  35. _sync_data = [NSMutableArray array];
  36. NSString *sql = @"select decrypt(c.company_name),o.so_id,o.sync_data from (select so_id,sync_data,customer_cid from offline_order where sync_data not null) as o join offline_contact as c on c.contact_id = o.customer_cid ;";
  37. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  38. const char *cn = (char *)sqlite3_column_text(stmt,0);
  39. if (cn == NULL) {
  40. cn = "";
  41. }
  42. const char *_id = (char *)sqlite3_column_text(stmt,1);
  43. if (_id == NULL) {
  44. _id = "";
  45. }
  46. const char *data = (char *)sqlite3_column_text(stmt,2);
  47. if (data == NULL) {
  48. data = "";
  49. }
  50. NSString *company_name = [NSString stringWithUTF8String:cn];
  51. NSString *so_id = [NSString stringWithUTF8String:_id];
  52. NSString *sync_data_str = [NSString stringWithUTF8String:data];
  53. NSDictionary *dic = [@{
  54. @"check" : @(1),
  55. @"company_name" : company_name,
  56. @"so_id" : so_id,
  57. @"sync_data" : sync_data_str
  58. } mutableCopy];
  59. [_sync_data addObject:dic];
  60. }];
  61. }
  62. return _sync_data;
  63. }
  64. - (IBAction)uploadButtonClicked:(UIButton *)sender {
  65. NSMutableArray *uploadArray = [NSMutableArray array];
  66. for (NSDictionary *dic in self.sync_data) {
  67. int check = [dic[@"check"] integerValue];
  68. NSString *so_id = dic[@"so_id"];
  69. if (check == 1) {
  70. [uploadArray addObject:so_id];
  71. }
  72. }
  73. [self dismissViewControllerAnimated:true completion:^{
  74. if (self.returnValue ) {
  75. self.returnValue(uploadArray);
  76. } ;
  77. }];
  78. }
  79. #pragma mark - dataSource
  80. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  81. return 1;
  82. }
  83. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  84. return self.sync_data.count;
  85. }
  86. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  87. SelectOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SelectOrderTableViewCell" forIndexPath:indexPath];
  88. NSDictionary *dic = [self.sync_data objectAtIndex:indexPath.row];
  89. int check = [dic[@"check"] integerValue];
  90. NSString *so_id = dic[@"so_id"];
  91. NSString *company_name = dic[@"company_name"];
  92. if (check)
  93. cell.checkedButton.selected = YES;
  94. cell.labelsoid.text = so_id;
  95. cell.labelcompany.text = company_name;
  96. __weak typeof(self) weakSelf = self;
  97. __weak typeof(tableView) weakTable = tableView;
  98. // NSIndexPath *idxPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
  99. cell.checkBlock = ^{
  100. [weakSelf tableView:weakTable didSelectRowAtIndexPath:indexPath];
  101. };
  102. return cell;
  103. }
  104. #pragma mark - delegate
  105. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  106. return 44;
  107. }
  108. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  109. SelectOrderTableViewCell *cell = (SelectOrderTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
  110. cell.selected = NO;
  111. cell.checkedButton.selected = !cell.checkedButton.selected;
  112. NSMutableDictionary *dic = (NSMutableDictionary *)[self.sync_data objectAtIndex:indexPath.row];
  113. int check = [dic[@"check"] integerValue];
  114. if (cell.checkedButton.selected) {
  115. check = 1;
  116. } else {
  117. check = 0;
  118. }
  119. [dic setValue:[NSNumber numberWithInteger:check] forKey:@"check"];
  120. }
  121. //- (BOOL
  122. - (void)onCloseClick:(UIButton *)sender {
  123. [self dismissViewControllerAnimated:true completion:nil];
  124. }
  125. @end