YXMyDeviceViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. //
  2. // YXMyDeviceViewController.m
  3. // Temperature
  4. //
  5. // Created by TC on 2025/2/22.
  6. //
  7. #import "YXMyDeviceViewController.h"
  8. #import "YXMyDeviceTableViewCell.h"
  9. #import "YXMyDeviceModel.h"
  10. #import "YXDeviceInfoViewController.h"
  11. #import <YBPopupMenu/YBPopupMenu.h>
  12. #import "YXMyDeviceModel.h"
  13. #import "RDSRootControl.h"
  14. @interface YXMyDeviceViewController ()<UITableViewDataSource,UITableViewDelegate,YBPopupMenuDelegate>
  15. @property (weak, nonatomic) UITableView *tableView;
  16. @property (copy, nonatomic) NSArray *devicesArray;
  17. @property (weak, nonatomic) UIButton *menuBtn;
  18. @property (weak, nonatomic) UILabel *numLabel;
  19. @property (copy, nonatomic) NSString *homeID;
  20. @property (strong, nonatomic) UILabel *emptyLabel;
  21. @end
  22. @implementation YXMyDeviceViewController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. // 隐藏导航栏
  26. self.fd_prefersNavigationBarHidden = YES;
  27. self.navigationItem.title = @"设备耗材";
  28. self.view.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
  29. [self createTableView];
  30. }
  31. -(void)viewWillAppear:(BOOL)animated
  32. {
  33. [super viewWillAppear:animated];
  34. [self p_getHomeList];
  35. }
  36. - (void)p_getHomeList
  37. {
  38. [RDSHomeModel rds_getHomeListIsAdmin:YES finished:^(NSError * _Nullable error) {
  39. [self getDeviceConsumables:TheDataManager.current_home_id];
  40. self.homeID = TheDataManager.current_home_id;
  41. }];
  42. }
  43. -(void)getDeviceConsumables:(NSString *)homeId
  44. {
  45. RDS_WEAKSELF(weakSelf)
  46. [RDSDemoApiHelper rds_getDeviceConsumablesWithHomeId:homeId Success:^(id responseObject) {
  47. if ([responseObject[@"code"] intValue] == 9999) {
  48. [RDSRootControl shareControl].isLoginSuccess = NO;
  49. TheDataManager.token = @"";
  50. }
  51. if ([responseObject[@"code"] intValue] == 0) {
  52. NSDictionary *dataDic = responseObject[@"data"];
  53. NSDictionary *devices = dataDic[@"devices"];
  54. NSMutableArray *tempArray = [YXMyDeviceModel mj_objectArrayWithKeyValuesArray:devices];
  55. for (YXMyDeviceModel *model in tempArray) {
  56. model.consumables = [YXConsumablesModel mj_objectArrayWithKeyValuesArray:model.consumables];
  57. }
  58. weakSelf.devicesArray = tempArray;
  59. if (weakSelf.devicesArray.count == 0) {
  60. weakSelf.emptyLabel.hidden = NO;
  61. }else{
  62. weakSelf.emptyLabel.hidden = YES;
  63. }
  64. NSNumber *enough = dataDic[@"enough"];
  65. NSString *desc = dataDic[@"desc"];
  66. if ([enough integerValue] == 1) {
  67. self.numLabel.textColor = [UIColor colorWithHexString:@"111111"];
  68. }else{
  69. self.numLabel.textColor = [UIColor redColor];
  70. }
  71. self.numLabel.text = desc;
  72. [weakSelf.tableView reloadData];
  73. } else{
  74. [RDSHudShower showErrorWithStatus:responseObject[@"message"]];
  75. }
  76. } failure:^(NSError *error) {
  77. }];
  78. }
  79. -(void)resetDeviceConsumablesWithId:(NSString *)deviceId
  80. {
  81. RDS_WEAKSELF(weakSelf)
  82. [RDSDemoApiHelper rds_resetDeviceConsumablesWithId:deviceId success:^(id responseObject) {
  83. if ([responseObject[@"code"] intValue] == 9999) {
  84. [RDSRootControl shareControl].isLoginSuccess = NO;
  85. TheDataManager.token = @"";
  86. }
  87. if ([responseObject[@"code"] intValue] == 0) {
  88. [RDSHudShower showSuccessWithStatus:@"重置成功"];
  89. [weakSelf getDeviceConsumables:weakSelf.homeID];
  90. } else{
  91. [RDSHudShower showErrorWithStatus:responseObject[@"message"]];
  92. }
  93. } failure:^(NSError *error) {
  94. }];
  95. }
  96. -(UILabel *)emptyLabel
  97. {
  98. if (_emptyLabel == nil) {
  99. _emptyLabel = [[UILabel alloc]init];
  100. _emptyLabel.text = @"暂无数据";
  101. _emptyLabel.font = [UIFont systemFontOfSize:20];
  102. _emptyLabel.textColor = [UIColor colorWithHexString:@"333333"];
  103. _emptyLabel.textAlignment = NSTextAlignmentCenter;
  104. _emptyLabel.hidden = YES;
  105. [self.view addSubview:_emptyLabel];
  106. [_emptyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  107. make.width.equalTo(@200);
  108. make.centerX.equalTo(@0);
  109. make.height.equalTo(@50);
  110. make.centerY.equalTo(@0);
  111. }];
  112. }
  113. return _emptyLabel;
  114. }
  115. -(void)createTableView
  116. {
  117. UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10) style:UITableViewStyleGrouped];
  118. tableView.delegate = self;
  119. tableView.dataSource = self;
  120. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  121. [tableView registerNib:[UINib nibWithNibName:@"YXMyDeviceTableViewCell" bundle:nil] forCellReuseIdentifier:@"myDeviceCellID"];
  122. tableView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
  123. tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  124. tableView.insetsContentViewsToSafeArea = false;
  125. [self.view addSubview:tableView];
  126. [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  127. make.top.equalTo(@0);
  128. make.right.equalTo(@0);
  129. make.left.equalTo(@0);
  130. make.bottom.equalTo(@0);
  131. }];
  132. self.tableView = tableView;
  133. [self createTableHeaderView];
  134. }
  135. -(void)createTableHeaderView
  136. {
  137. UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 190)];
  138. headerView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
  139. self.tableView.tableHeaderView = headerView;
  140. UIImageView *bgImgView = [[UIImageView alloc]init];
  141. bgImgView.image = [UIImage imageNamed:@"my_top_bg"];
  142. [headerView addSubview:bgImgView];
  143. [bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  144. make.top.bottom.left.right.equalTo(@0);
  145. }];
  146. UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  147. [backBtn setImage:[UIImage imageNamed:@"home_left_arrow"] forState:UIControlStateNormal];
  148. [backBtn addTarget:self action:@selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  149. [headerView addSubview:backBtn];
  150. [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  151. make.top.equalTo(@60);
  152. make.left.equalTo(@15);
  153. make.width.equalTo(@20);
  154. make.height.equalTo(@20);
  155. }];
  156. UILabel *titleLabel = [[UILabel alloc]init];
  157. titleLabel.textColor = [UIColor colorWithHexString:@"#1E1E1E"];
  158. titleLabel.font = [UIFont boldSystemFontOfSize:18];
  159. titleLabel.textAlignment = NSTextAlignmentCenter;
  160. titleLabel.text = @"设备耗材";
  161. [headerView addSubview:titleLabel];
  162. [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  163. make.centerY.equalTo(backBtn.mas_centerY);
  164. make.centerX.equalTo(@0);
  165. make.width.equalTo(@150);
  166. make.height.equalTo(@30);
  167. }];
  168. UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  169. [menuBtn setTitle:TheDataManager.current_home_name forState:UIControlStateNormal];
  170. menuBtn.titleLabel.font = [UIFont boldSystemFontOfSize:18];
  171. [menuBtn setTitleColor:[UIColor colorWithHexString:@"#1E1E1E"] forState:UIControlStateNormal];
  172. [menuBtn addTarget:self action:@selector(menuBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  173. [headerView addSubview:menuBtn];
  174. [menuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  175. make.left.equalTo(@15);
  176. make.top.equalTo(backBtn.mas_bottom).offset(30);
  177. // make.width.equalTo(@150);
  178. make.height.equalTo(@30);
  179. }];
  180. self.menuBtn = menuBtn;
  181. UIImageView *imgView = [[UIImageView alloc]init];
  182. imgView.image = [UIImage imageNamed:@"home_top_arrow"];
  183. [headerView addSubview:imgView];
  184. [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
  185. make.centerY.equalTo(menuBtn.mas_centerY);
  186. make.left.equalTo(menuBtn.mas_right).offset(2);
  187. make.width.equalTo(@25);
  188. make.height.equalTo(@18);
  189. }];
  190. UILabel *numLabel = [[UILabel alloc]init];
  191. numLabel.textColor = [UIColor colorWithHexString:@"#999999"];
  192. numLabel.font = [UIFont systemFontOfSize:14];
  193. [headerView addSubview:numLabel];
  194. [numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  195. make.top.equalTo(menuBtn.mas_bottom).offset(10);
  196. make.left.equalTo(@15);
  197. make.right.equalTo(@-15);
  198. make.height.equalTo(@30);
  199. }];
  200. self.numLabel = numLabel;
  201. }
  202. #pragma mark - 右上角按钮
  203. -(void)backBtnAction:(UIButton *)button {
  204. [self.navigationController popViewControllerAnimated:YES];
  205. }
  206. - (void)menuBtnAction:(UIButton *)button {
  207. NSMutableArray *titleArray = [[NSMutableArray alloc]initWithCapacity:5];
  208. for (int i = 0; i < TheDataManager.homes.count; i++) {
  209. RDSHomeModel *model = TheDataManager.homes[i];
  210. [titleArray addObject:model.name];
  211. }
  212. [YBPopupMenu showRelyOnView:button titles:titleArray icons:nil menuWidth:130 otherSettings:^(YBPopupMenu *popupMenu) {
  213. popupMenu.delegate = self;
  214. popupMenu.animationManager.duration = 0.25;
  215. popupMenu.textColor = [UIColor colorWithHexString:@"#1F1F1F"];
  216. }];
  217. }
  218. #pragma mark - YPPopupMenuDelegate
  219. - (void)ybPopupMenu:(YBPopupMenu *)ybPopupMenu didSelectedAtIndex:(NSInteger)index
  220. {
  221. RDSHomeModel *model = TheDataManager.homes[index];
  222. [self getDeviceConsumables:model.record_id];
  223. self.homeID = model.record_id;
  224. [self.menuBtn setTitle:model.name forState:UIControlStateNormal];
  225. }
  226. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  227. {
  228. return self.devicesArray.count;
  229. }
  230. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  231. {
  232. YXMyDeviceModel *deviceModel = self.devicesArray[section];
  233. return deviceModel.consumables.count;
  234. }
  235. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  236. {
  237. YXMyDeviceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myDeviceCellID" forIndexPath:indexPath];
  238. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  239. YXMyDeviceModel *deviceModel = self.devicesArray[indexPath.section];
  240. YXConsumablesModel *model = deviceModel.consumables[indexPath.row];
  241. if (indexPath.row == deviceModel.consumables.count - 1) {
  242. cell.lineView.hidden = YES;
  243. }else{
  244. cell.lineView.hidden = NO;
  245. }
  246. if ([model.user_rate floatValue] >= 0) {
  247. [cell.circleBar setProgress:[model.user_rate floatValue] animated:YES];
  248. }
  249. cell.titleLabel.text = model.consumable_name;
  250. cell.detailLabel.text = [NSString stringWithFormat:@"已使用%@天",model.used_time];
  251. RDS_WEAKSELF(weakSelf)
  252. cell.resetClick = ^(UITableViewCell * cell) {
  253. [weakSelf resetDeviceConsumablesWithId:model.record_id];
  254. };
  255. return cell;
  256. }
  257. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  258. {
  259. }
  260. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  261. {
  262. UIView *headerBgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 80)];
  263. headerBgView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
  264. UIView *headerView = [[UIView alloc]init];
  265. headerView.backgroundColor = [UIColor whiteColor];
  266. [headerBgView addSubview:headerView];
  267. [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
  268. make.top.equalTo(@0);
  269. make.left.equalTo(@15);
  270. make.right.equalTo(@-15);
  271. make.bottom.equalTo(@0);
  272. }];
  273. UIImageView *imgView = [[UIImageView alloc]init];
  274. imgView.image = [UIImage imageNamed:@"home_device_1"];
  275. [headerView addSubview:imgView];
  276. [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
  277. make.centerY.equalTo(@0);
  278. make.left.equalTo(@15);
  279. make.width.equalTo(@40);
  280. make.height.equalTo(@40);
  281. }];
  282. UILabel *titleLabel = [[UILabel alloc]init];
  283. titleLabel.textColor = [UIColor colorWithHexString:@"#333333"];
  284. titleLabel.font = [UIFont boldSystemFontOfSize:16];
  285. [headerView addSubview:titleLabel];
  286. [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  287. make.top.equalTo(imgView.mas_top);
  288. make.left.equalTo(imgView.mas_right).offset(15);
  289. make.right.equalTo(@-15);
  290. make.height.equalTo(@20);
  291. }];
  292. UILabel *textLabel = [[UILabel alloc]init];
  293. textLabel.textColor = [UIColor colorWithHexString:@"#999999"];
  294. textLabel.font = [UIFont systemFontOfSize:13];
  295. [headerView addSubview:textLabel];
  296. [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  297. make.top.equalTo(titleLabel.mas_bottom);
  298. make.left.equalTo(imgView.mas_right).offset(15);
  299. make.right.equalTo(@-15);
  300. make.height.equalTo(@20);
  301. }];
  302. UIView *lineView = [[UIView alloc]init];
  303. lineView.backgroundColor = [UIColor colorWithHexString:@"#E6E6E6"];
  304. [headerBgView addSubview:lineView];
  305. [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
  306. make.left.equalTo(@30);
  307. make.right.equalTo(@-30);
  308. make.height.equalTo(@1);
  309. make.bottom.equalTo(@-1);
  310. }];
  311. YXMyDeviceModel *deviceModel = self.devicesArray[section];
  312. if (deviceModel.consumables.count == 0) {
  313. lineView.hidden = YES;
  314. }else{
  315. lineView.hidden = NO;
  316. }
  317. titleLabel.text = deviceModel.device_name;
  318. textLabel.text = deviceModel.device_type_name;
  319. return headerBgView;
  320. }
  321. -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  322. {
  323. UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 10)];
  324. footerView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
  325. return footerView;
  326. }
  327. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  328. {
  329. return 80;
  330. }
  331. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  332. {
  333. return 10;
  334. }
  335. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  336. {
  337. return 85;
  338. }
  339. /*
  340. #pragma mark - Navigation
  341. // In a storyboard-based application, you will often want to do a little preparation before navigation
  342. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  343. // Get the new view controller using [segue destinationViewController].
  344. // Pass the selected object to the new view controller.
  345. }
  346. */
  347. @end