ScanModelListCell.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // ScanModelListCell.m
  3. // HMLG Scan Order
  4. //
  5. // Created by Rui Zhang on 3/17/22.
  6. // Copyright © 2022 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "ScanModelListCell.h"
  9. #import "RAConvertor.h"
  10. #import "RAUtils.h"
  11. #import "RASingleton.h"
  12. #import "AppDelegate.h"
  13. #import "RADataProvider.h"
  14. static const CGFloat kTableRowHeight = 24.0;
  15. static const CGFloat kCellHPadding = 20.0;
  16. static const CGFloat kMinColumnWidth = 125;
  17. @implementation ScanModelListCell
  18. - (void)awakeFromNib {
  19. [super awakeFromNib];
  20. self.selectionStyle = UITableViewCellSelectionStyleNone;
  21. self.priceScrollView.showsVerticalScrollIndicator = NO;
  22. self.priceScrollView.showsHorizontalScrollIndicator = YES;
  23. self.priceScrollView.alwaysBounceVertical = NO;
  24. self.priceScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  25. }
  26. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  27. [super setSelected:selected animated:animated];
  28. }
  29. // MARK: - setModelJson
  30. - (void)setModelJson:(NSMutableDictionary *)modelJson {
  31. _modelJson = modelJson;
  32. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  33. NSDictionary *price_group = [RADataProvider get_price_group:appDelegate.price_group];
  34. // ── 基础字段 ────────────────────────────────────────────────────────────
  35. NSString *port = _modelJson[@"port"]; if (port.length == 0) port = @"N/A";
  36. NSString *origin = _modelJson[@"origin"]; if (origin.length == 0) origin = @"N/A";
  37. NSString *available = _modelJson[@"available"]; if (available.length == 0) available = @"N/A";
  38. NSString *unit_cuft = _modelJson[@"unit_cuft"];
  39. if (unit_cuft.length == 0) {
  40. unit_cuft = @"N/A";
  41. } else {
  42. unit_cuft = [NSString stringWithFormat:@"%.2f", [_modelJson[@"unit_cuft"] doubleValue]];
  43. }
  44. // ── 静态 label ──────────────────────────────────────────────────────────
  45. _labelModel.text = _modelJson[@"model"];
  46. _labelDescription.text = _modelJson[@"description"];
  47. _labelDimension.text = _modelJson[@"dimension"];
  48. // ── 数量 / stepper ──────────────────────────────────────────────────────
  49. _editQTY.text = [_modelJson[@"count"] stringValue];
  50. int c = [_modelJson[@"count"] intValue];
  51. int s = [_modelJson[@"stockUom"] intValue];
  52. [self init_Stepper:s max:9999 min:0 value:c];
  53. // ── 价格 flag 处理(与 ScanListCell 保持一致)───────────────────────────
  54. NSString *price1 = _modelJson[price_group[@"price_1"][@"name"]];
  55. NSString *price2 = _modelJson[price_group[@"price_2"][@"name"]];
  56. NSString *price3 = _modelJson[price_group[@"price_3"][@"name"]];
  57. if (price1 && ![price1 isEqualToString:@"N/A"])
  58. price1 = [NSString stringWithFormat:@"%.2f", [price1 doubleValue]];
  59. if (price2 && ![price2 isEqualToString:@"N/A"]) {
  60. price2 = [NSString stringWithFormat:@"%.2f", [price2 doubleValue]];
  61. _modelJson[@"special_price"] = @YES;
  62. }
  63. if (price3 && ![price3 isEqualToString:@"N/A"])
  64. price3 = [NSString stringWithFormat:@"%.2f", [price3 doubleValue]];
  65. if ((price1 && ![price1 isEqualToString:@"N/A"] && [price1 isEqualToString:price3]) ||
  66. (price2 && ![price2 isEqualToString:@"N/A"] && [price2 isEqualToString:price3]))
  67. _modelJson[@"net_price"] = @NO;
  68. // ── 构建动态表格列 ───────────────────────────────────────────────────────
  69. NSMutableArray<NSDictionary *> *columns = [NSMutableArray array];
  70. [columns addObject:@{ @"header": @"CuFT", @"value": unit_cuft }];
  71. [columns addObject:@{ @"header": @"Origin", @"value": origin }];
  72. [columns addObject:@{ @"header": @"Port", @"value": port }];
  73. [columns addObject:@{ @"header": @"Available", @"value": available }];
  74. int priceCount = [price_group[@"price_count"] intValue];
  75. for (int i = 0; i < priceCount; i++) {
  76. NSString *key = [NSString stringWithFormat:@"price_%d", i];
  77. NSDictionary *pg = price_group[key];
  78. if (!pg || !pg[@"display"]) continue;
  79. NSString *rawValue = _modelJson[pg[@"name"]];
  80. NSString *displayValue = @"";
  81. if (rawValue && ![rawValue isEqualToString:@"N/A"]) {
  82. double dval = [rawValue doubleValue];
  83. if (dval > 0)
  84. displayValue = [RAConvertor currencyNumber:(float)dval];
  85. }
  86. // price_2 固定为 Show Special
  87. if (i == 2 && ![_modelJson[@"special_price"] boolValue])
  88. displayValue = @"";
  89. [columns addObject:@{ @"header": pg[@"display"], @"value": displayValue }];
  90. }
  91. [self buildPriceTableWithColumns:columns];
  92. }
  93. // MARK: - Stepper
  94. - (void)init_Stepper:(int)step max:(int)max min:(int)min value:(int)value {
  95. if (self.steper != nil) {
  96. if (step <= 0) step = 1;
  97. self.steper.minimumValue = min;
  98. self.steper.stepValue = step;
  99. self.steper.value = value;
  100. }
  101. }
  102. // MARK: - 动态表格构建
  103. - (void)buildPriceTableWithColumns:(NSArray<NSDictionary *> *)columns {
  104. for (UIView *v in self.priceScrollView.subviews)
  105. [v removeFromSuperview];
  106. if (columns.count == 0) {
  107. self.priceScrollView.contentSize = CGSizeZero;
  108. return;
  109. }
  110. UIFont *font = [UIFont systemFontOfSize:15.0];
  111. UIColor *borderColor = UIColor.labelColor;
  112. CGFloat totalWidth = 0;
  113. NSMutableArray<NSNumber *> *colWidths = [NSMutableArray arrayWithCapacity:columns.count];
  114. for (NSDictionary *col in columns) {
  115. NSString *header = col[@"header"] ?: @"";
  116. NSString *value = col[@"value"] ?: @"";
  117. NSDictionary *attrs = @{ NSFontAttributeName: font };
  118. CGFloat hw = [header sizeWithAttributes:attrs].width + kCellHPadding * 2;
  119. CGFloat vw = [value sizeWithAttributes:attrs].width + kCellHPadding * 2;
  120. CGFloat w = MAX(MAX(hw, vw), kMinColumnWidth);
  121. [colWidths addObject:@(w)];
  122. }
  123. BOOL isLast = NO;
  124. CGFloat x = 0;
  125. for (NSUInteger i = 0; i < columns.count; i++) {
  126. CGFloat w = [colWidths[i] floatValue];
  127. NSDictionary *col = columns[i];
  128. isLast = (i == columns.count - 1);
  129. UILabel *hLabel = [self makeCellLabel:col[@"header"]
  130. font:font
  131. borderColor:borderColor
  132. frame:CGRectMake(x, 0, w, kTableRowHeight)
  133. isLast:isLast];
  134. hLabel.backgroundColor = [UIColor secondarySystemBackgroundColor];
  135. [self.priceScrollView addSubview:hLabel];
  136. UILabel *vLabel = [self makeCellLabel:col[@"value"]
  137. font:font
  138. borderColor:borderColor
  139. frame:CGRectMake(x, kTableRowHeight, w, kTableRowHeight)
  140. isLast:isLast];
  141. [self.priceScrollView addSubview:vLabel];
  142. x += w;
  143. totalWidth = x;
  144. }
  145. self.priceScrollView.contentSize = CGSizeMake(totalWidth, kTableRowHeight * 2);
  146. }
  147. // MARK: - 辅助
  148. - (UILabel *)makeCellLabel:(NSString *)text
  149. font:(UIFont *)font
  150. borderColor:(UIColor *)borderColor
  151. frame:(CGRect)frame
  152. isLast:(BOOL)isLast {
  153. UILabel *label = [[UILabel alloc] initWithFrame:frame];
  154. label.text = text ?: @"";
  155. label.font = font;
  156. label.textAlignment = NSTextAlignmentCenter;
  157. label.clipsToBounds = YES;
  158. CGFloat w = frame.size.width;
  159. CGFloat h = frame.size.height;
  160. CGFloat lw = 1.0;
  161. CAShapeLayer *border = [CAShapeLayer layer];
  162. UIBezierPath *path = [UIBezierPath bezierPath];
  163. [path moveToPoint:CGPointMake(0, 0)];
  164. [path addLineToPoint:CGPointMake(w, 0)];
  165. [path moveToPoint:CGPointMake(0, h)];
  166. [path addLineToPoint:CGPointMake(w, h)];
  167. [path moveToPoint:CGPointMake(0, 0)];
  168. [path addLineToPoint:CGPointMake(0, h)];
  169. if (isLast) {
  170. [path moveToPoint:CGPointMake(w, 0)];
  171. [path addLineToPoint:CGPointMake(w, h)];
  172. }
  173. border.path = path.CGPath;
  174. border.strokeColor = borderColor.CGColor;
  175. border.lineWidth = lw;
  176. border.fillColor = [UIColor clearColor].CGColor;
  177. [label.layer addSublayer:border];
  178. return label;
  179. }
  180. @end