SetCategoryPriceController.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //
  2. // SetCategoryPriceController.m
  3. // iSales-NPD
  4. //
  5. // Created by Jack on 2016/10/11.
  6. // Copyright © 2016年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "SetCategoryPriceController.h"
  9. #import "const.h"
  10. #import "EnumSelectViewController.h"
  11. #import "RANetwork.h"
  12. #define Percent 100
  13. #define Float 101
  14. #define NoChange 102
  15. #define NUMBERS @"0123456789.\n"
  16. #define NEGATIVE_NUMBERS @"0123456789.-\n"
  17. @interface SetCategoryPriceController ()<UITextFieldDelegate>
  18. @property (strong, nonatomic) IBOutlet UIButton *basePriceButton;
  19. @property (strong, nonatomic) IBOutlet UIButton *floatCheckButton;
  20. @property (strong, nonatomic) IBOutlet UIButton *percentageCheckButton;
  21. @property (strong, nonatomic) IBOutlet UITextField *floatTextField;
  22. @property (strong, nonatomic) IBOutlet UITextField *percentageTextField;
  23. @property (nonatomic,assign) NSInteger changePrice;
  24. @property (nonatomic,assign) NSInteger priceType;
  25. @end
  26. @implementation SetCategoryPriceController
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. // Do any additional setup after loading the view.
  30. self.changePrice = NoChange;
  31. self.basePriceButton.enabled = NO;
  32. [self configAppearance];
  33. }
  34. - (void)didReceiveMemoryWarning {
  35. [super didReceiveMemoryWarning];
  36. // Dispose of any resources that can be recreated.
  37. }
  38. - (void)configAppearance {
  39. UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  40. style:UIBarButtonItemStylePlain
  41. target:self
  42. action:@selector( returnBackClick:)];
  43. self.navigationItem.leftBarButtonItem = backButton;
  44. // base price
  45. // NSString *basePrice = [self.categoryPrice objectForKey:@"base_price"];
  46. // [self changePriceType:basePrice.integerValue];
  47. [self changePriceType:1];
  48. // discount
  49. NSString *discount = [self.categoryPrice objectForKey:@"price_discount"];
  50. if ([discount hasSuffix:@"%"]) {
  51. self.percentageTextField.text = [discount substringToIndex:discount.length - 1];
  52. } else {
  53. self.floatTextField.text = discount;
  54. }
  55. }
  56. - (void)returnBackClick:(id)sender {
  57. [self.navigationController popViewControllerAnimated:YES];
  58. }
  59. - (void)changePriceType:(NSInteger)type {
  60. NSString *title = [RASingleton sharedInstance].deliveryString;
  61. switch (type) {
  62. case 0: {
  63. title = [RASingleton sharedInstance].deliveryString;
  64. }
  65. break;
  66. case 1: {
  67. title = flat_price;
  68. }
  69. break;
  70. default:
  71. break;
  72. }
  73. self.priceType = type;
  74. [self.basePriceButton setTitle:title forState:UIControlStateNormal];
  75. }
  76. - (NSString *)checkPriceType:(NSInteger)priceType {
  77. return self.priceType == priceType ? @"1" : @"0";
  78. }
  79. - (NSInteger)checkedCadedate:(NSDictionary *)cadedate {
  80. int priceType = 0;
  81. int count = [[cadedate objectForKey:@"count"] intValue];
  82. for (int i = 0; i < count; i++) {
  83. NSDictionary *dic = [cadedate objectForKey:[NSString stringWithFormat:@"val_%d",i]];
  84. int check = [[dic objectForKey:@"check"] intValue];
  85. if (check) {
  86. priceType = [[dic objectForKey:@"value_code"] intValue];
  87. break;
  88. }
  89. }
  90. return priceType;
  91. }
  92. - (IBAction)basePriceButtonClick:(UIButton *)sender {
  93. EnumSelectViewController* enumvc =[[UIStoryboard storyboardWithName:@"CommonEditor" bundle:nil] instantiateViewControllerWithIdentifier:@"EnumSelectorViewController"];
  94. enumvc.max_select = 1;
  95. enumvc.cadedate = @{
  96. @"count" : @"2",
  97. @"val_0" : @{@"value" : [RASingleton sharedInstance].deliveryString,@"value_code" : @"0",@"check" : [self checkPriceType:0]},
  98. @"val_1" : @{@"value" : flat_price,@"value_code" : @"1",@"check" : [self checkPriceType:1]}
  99. }.mutableCopy;
  100. enumvc.title = @"";
  101. enumvc.single_select =true;
  102. __weak typeof(self) weakSelf = self;
  103. enumvc.returnValue = ^(NSMutableDictionary* value){
  104. if (weakSelf) {
  105. __strong typeof(weakSelf) strongSelf = weakSelf;
  106. [strongSelf changePriceType:[strongSelf checkedCadedate:value]];
  107. }
  108. };
  109. [self.navigationController pushViewController:enumvc animated:true];
  110. }
  111. - (IBAction)percentageCheckButtonClick:(UIButton *)sender {
  112. if (self.changePrice == NoChange || self.changePrice == Percent) {
  113. sender.selected = !sender.selected;
  114. if (sender.selected) {
  115. self.changePrice = Percent;
  116. } else {
  117. self.changePrice = NoChange;
  118. }
  119. } else if (self.changePrice == Float) {
  120. sender.selected = YES;
  121. self.changePrice = Percent;
  122. self.floatCheckButton.selected = NO;
  123. self.floatTextField.enabled = NO;
  124. }
  125. self.percentageTextField.enabled = sender.selected;
  126. }
  127. - (IBAction)floatCheckButtonClick:(UIButton *)sender {
  128. if (self.changePrice == NoChange || self.changePrice == Float) {
  129. sender.selected = !sender.selected;
  130. if (sender.selected) {
  131. self.changePrice = Float;
  132. } else {
  133. self.changePrice = NoChange;
  134. }
  135. } else if (self.changePrice == Percent) {
  136. sender.selected = YES;
  137. self.changePrice = Float;
  138. self.percentageCheckButton.selected = NO;
  139. self.percentageTextField.enabled = NO;
  140. }
  141. self.floatTextField.enabled = sender.selected;
  142. }
  143. - (IBAction)saveButtonClick:(UIButton *)sender {
  144. // 向服务器提交数据
  145. // http://192.168.0.126:8080/site/isales/setSeeGivenPrice.htm
  146. NSString *discount = @"";
  147. BOOL rightDiscount = YES;
  148. switch (self.changePrice) {
  149. case Float:{
  150. discount = self.floatTextField.text;
  151. if (!discount || !discount.length) {
  152. rightDiscount = NO;
  153. }
  154. }
  155. break;
  156. case Percent:{
  157. discount = [self.percentageTextField.text stringByAppendingString:@"%"];
  158. if (!discount || discount.length <= 1) {
  159. rightDiscount = NO;
  160. }
  161. }
  162. break;
  163. default:
  164. break;
  165. }
  166. if (rightDiscount) {
  167. // __block UIAlertController* waitalert = [RAUtils waiting_alert:self title:@"Set Price" completion:^{
  168. PopWaitAlert * pop = [RAUtils waiting_pop:@"Set Price" completion:nil];
  169. [RANetwork request_update_npd_shop_givenprice:@{@"base_price" : [NSString stringWithFormat:@"%ld",self.priceType],@"discount" : discount,@"category_id" : @(self.categoryID)}.mutableCopy completionHandler:^(NSMutableDictionary *result) {
  170. NSDictionary *dic = result;
  171. // [waitalert dismissViewControllerAnimated:YES completion:^{
  172. [pop hide];
  173. // [waitalert dismissWithClickedButtonIndex:0 animated:YES];
  174. if ([[dic objectForKey:@"result"] integerValue] == 2) {
  175. [self.categoryPrice setObject:[NSString stringWithFormat:@"%ld",self.priceType] forKey:@"base_price"];
  176. [self.categoryPrice setObject:discount forKey:@"price_discount"];
  177. // 将数据返回
  178. if (self.returnBlock)
  179. self.returnBlock(self.categoryPrice);
  180. }
  181. [self.navigationController popViewControllerAnimated:YES];
  182. // }];
  183. }];
  184. // }];
  185. // UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Set Price"];
  186. // NSDictionary *dic = [RANetwork set_npd_shop_givenPrice:@{@"base_price" : [NSString stringWithFormat:@"%ld",self.priceType],@"discount" : discount,@"category_id" : @(self.categoryID)}.mutableCopy];
  187. //
  188. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  189. //// [waitalert dismissWithClickedButtonIndex:0 animated:YES];
  190. //
  191. // if ([[dic objectForKey:@"result"] integerValue] == 2) {
  192. //
  193. // [self.categoryPrice setObject:[NSString stringWithFormat:@"%ld",self.priceType] forKey:@"base_price"];
  194. // [self.categoryPrice setObject:discount forKey:@"price_discount"];
  195. //
  196. // // 将数据返回
  197. // if (self.returnBlock)
  198. // self.returnBlock(self.categoryPrice);
  199. // }
  200. //
  201. // [self.navigationController popViewControllerAnimated:YES];
  202. } else {
  203. [RAUtils message_box:@"Warning" message:@"Please write a correct value" completion:nil];
  204. }
  205. }
  206. #pragma mark - text field delegate
  207. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  208. NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string];
  209. if (newText.length == 0)
  210. return YES;
  211. // 正则表达式
  212. NSString *match = @"-[1-9]{1,1}[0-9]*(\\.[0-9]*)?|-0\\.[0-9]+|[1-9]{1,1}[0-9]*(\\.[0-9]*)?|0\\.[0-9]{0,}|0{1,1}|-{1,1}|-0{1,1}|-0\\.{1,1}";
  213. if ([textField isEqual:self.percentageTextField])
  214. match = @"[1-9]{1,1}[0-9]*(\\.[0-9]*)?|0(\\.[0-9]*)?";
  215. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",match];
  216. return [predicate evaluateWithObject:newText];
  217. }
  218. @end