statisticsVC.m 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. //
  2. // statisticsVC.m
  3. // LN_School
  4. //
  5. // Created by apple on 2017/4/5.
  6. // Copyright © 2017年 Danson. All rights reserved.
  7. //
  8. #import "statisticsVC.h"
  9. #import "RangePickerViewController.h"
  10. #import "AFView.h"
  11. #import "OtherStatistcsVC.h"
  12. @interface statisticsVC ()
  13. {
  14. UILabel *calendarLabel;
  15. UIButton *calendarbtn;
  16. UIView *calendarView;
  17. UIButton *lastDatebtn;
  18. UILabel *numberLabel;
  19. UILabel *compareNumLabel;
  20. UIView *afView;
  21. NSString *todayString;
  22. NSString *yesterdayString;
  23. NSString *weekNow;
  24. NSString *weekLast;
  25. NSString *monthNow;
  26. NSString *monthLast;
  27. NSString *startDateString;
  28. NSString *endDateString;
  29. // 1代表报名统计 2代表退学
  30. NSInteger styleType;
  31. // 1代表是日 2代表周 3代表月 4代表年
  32. NSInteger dateType;
  33. }
  34. @end
  35. @implementation statisticsVC
  36. - (void)viewDidLoad {
  37. [super viewDidLoad];
  38. self.navigationItem.title = @"智能统计";//某某驾校
  39. self.view.backgroundColor = KBackGroundColor;
  40. self.navigationController.navigationBar.translucent = NO;
  41. UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
  42. btn.frame = CGRectMake(0, 0, 43, 44);
  43. btn.titleLabel.font = [UIFont systemFontOfSize:17];
  44. [btn setTitle:@"更多" forState:UIControlStateNormal];
  45. [btn setTitleColor:defGreen forState:UIControlStateNormal];
  46. [btn addTarget:self action:@selector(otherStatistcsAction) forControlEvents:UIControlEventTouchUpInside];
  47. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
  48. [self myInit];
  49. }
  50. -(void)viewDidAppear:(BOOL)animated {
  51. [super viewDidAppear:animated];
  52. self.tabBarController.tabBar.hidden = NO;
  53. }
  54. - (void)myInit {
  55. //顶部日历条
  56. CGFloat x, y, w, h, bd;
  57. bd = 10;
  58. x = 0;
  59. y = 0;
  60. w = kSize.width;
  61. h = 50;
  62. UIView *view = [[UIView alloc] KSetxywh];
  63. view.backgroundColor = defGreen;
  64. [self.view addSubview:view];
  65. x = y = bd;
  66. w = h = 30;
  67. UIImageView *imgView = [[UIImageView alloc] KSetxywh];
  68. [imgView setModeAspectFill];
  69. imgView.image = [UIImage imageNamed:@"arrow_left"];
  70. [view addSubview:imgView];
  71. x = kSize.width - w - bd;
  72. imgView = [[UIImageView alloc] KSetxywh];
  73. [imgView setModeAspectFill];
  74. imgView.image = [UIImage imageNamed:@"arrow_right"];
  75. [view addSubview:imgView];
  76. x = y = 0;
  77. w = 70;
  78. h = 50;
  79. UIButton *btn = [[UIButton alloc] KSetxywh];
  80. [btn target:self Tag:1];
  81. [view addSubview:btn];
  82. x = kSize.width - w;
  83. btn = [[UIButton alloc] KSetxywh];
  84. [btn target:self Tag:2];
  85. [view addSubview:btn];
  86. x = w;
  87. w = kSize.width - 2*x;
  88. btn = [[UIButton alloc] KSetxywh];
  89. [btn target:self Tag:3];
  90. [view addSubview:btn];
  91. calendarbtn = btn;
  92. x = 0;
  93. w = kSize.width;
  94. UILabel *label = [[UILabel alloc] KSetxywh];
  95. [label setText:@"请选择日期 ▽" Font:Font16 TextColor:[UIColor whiteColor] Alignment:NSTextAlignmentCenter];
  96. [view addSubview:label];
  97. calendarLabel = label;
  98. //报名/退学
  99. x = 0;
  100. y = 50;
  101. w = kSize.width/2.0;
  102. h = 60;
  103. btn = [[UIButton alloc] KSetxywh];
  104. [btn setTitle:@"报名统计" textColor:KTitleColor font:Font21 fotState:UIControlStateNormal];
  105. [btn setTitleColor:defGreen forState:UIControlStateSelected];
  106. [btn target:self Tag:4];
  107. [self.view addSubview:btn];
  108. btn.selected = YES;
  109. [btn addViewWithRect:CGRectMake(w - 1, y + 5, 1, h - 10)];
  110. x += w;
  111. btn = [[UIButton alloc] KSetxywh];
  112. [btn setTitle:@"退学统计" textColor:KTitleColor font:Font21 fotState:UIControlStateNormal];
  113. [btn setTitleColor:defGreen forState:UIControlStateSelected];
  114. [btn target:self Tag:5];
  115. [self.view addSubview:btn];
  116. [btn addViewWithRect:CGRectMake(bd, y + h - 1, 2*w - 2*bd, 1)];
  117. x = bd;
  118. y += h;
  119. w = kSize.width - 2*bd;
  120. label = [[UILabel alloc] KSetxywh];
  121. [label setText:@"受理人数" Font:Font18 TextColor:KTitleColor Alignment:NSTextAlignmentLeft];
  122. [self.view addSubview:label];
  123. label = [[UILabel alloc] KSetxywh];
  124. [label setText:@"暂无数据" Font:Font18 TextColor:KContentTextColor Alignment:NSTextAlignmentCenter];
  125. [self.view addSubview:label];
  126. numberLabel = label;
  127. label = [[UILabel alloc] KSetxywh];
  128. [label setText:@"比前一日\n--" Font:Font14 TextColor:KContentTextColor Alignment:NSTextAlignmentRight];
  129. label.numberOfLines = 2;
  130. [self.view addSubview:label];
  131. [label addViewWithRect:CGRectMake(10, y + h - 1, w, 1)];
  132. compareNumLabel = label;
  133. //折线图
  134. y += h + 4*bd;
  135. x = 0;
  136. w = kSize.width;
  137. h = w/2.0 + 3*bd;
  138. afView = [[UIView alloc] KSetxywh];
  139. afView.backgroundColor = defGreen;
  140. [self.view addSubview:afView];
  141. // AFView *afv = [[AFView alloc] initWithFrame:CGRectMake(0, 20, afView.width, afView.height - 30)];
  142. // NSArray* xTitles = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7"];
  143. // [afv setX_labels:xTitles];
  144. // [afv setMin_X:1];
  145. // [afv setMax_X:xTitles.count];
  146. // [afv setMin_Y:0];
  147. // [afv setMax_Y:100];
  148. // [afv setY_labels:@[@"0",@"20",@"40",@"60",@"80",@"100"]];
  149. // [afView addSubview:afv];
  150. //隐藏的日期选择
  151. x = 0;
  152. y = 50;
  153. w = kSize.width;
  154. h = kSize.height - y;
  155. view = [[UIView alloc] KSetxywh];
  156. view.backgroundColor = [UIColor colorWithWhite:.7 alpha:1.0];
  157. [self.view addSubview:view];
  158. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap)];
  159. [view addGestureRecognizer:tap];
  160. calendarView = view;
  161. y = 0;
  162. h = 170;
  163. view = [[UIView alloc] KSetxywh];
  164. view.backgroundColor = KBackGroundColor;
  165. [calendarView addSubview:view];
  166. x = bd;
  167. w -= 2*x;
  168. h = 40;
  169. label = [[UILabel alloc] KSetxywh];
  170. [label setText:@"日期" Font:Font17 TextColor:KTitleColor];
  171. [view addSubview:label];
  172. y += h;
  173. w = (kSize.width - 40)/3.0;
  174. h = 30;
  175. NSArray *titleArray = @[@"今日",@"昨日",@"本周",@"上周",@"本月",@"上月"];
  176. for (int i = 0; i < titleArray.count; i ++) {
  177. int row = i/3;
  178. int column = i%3;
  179. btn = [[UIButton alloc] initWithFrame:CGRectMake(x + column*(w + bd), y + row*(h + bd), w, h)];
  180. [btn target:self Tag:i-6];
  181. [btn setTitle:titleArray[i] textColor:KSubTitleColor font:Font16 fotState:UIControlStateNormal];
  182. [btn setBackgroundColor:kLineColor];
  183. [btn setTitleColor:defGreen forState:UIControlStateSelected];
  184. [btn borderCornorRadios:3];
  185. [view addSubview:btn];
  186. if (i == 0) {
  187. btn.selected = YES;
  188. btn.backgroundColor = [UIColor whiteColor];
  189. [btn boardWid:1 Color:defGreen];
  190. lastDatebtn = btn;
  191. }
  192. }
  193. y += 2*h + 2*bd;
  194. w = kSize.width - 2*x;
  195. btn = [[UIButton alloc] KSetxywh];
  196. [btn target:self Tag:-7];
  197. [btn setTitle:@"自定义日期范围" textColor:KSubTitleColor font:Font16 fotState:UIControlStateNormal];
  198. [btn setTitleColor:defGreen forState:UIControlStateSelected];
  199. [btn setBackgroundColor:kLineColor];
  200. [btn borderCornorRadios:3];
  201. [view addSubview:btn];
  202. calendarView.hidden = YES;
  203. dateType = 1;
  204. [self initDefaultDate]; //设置默认日期
  205. [self getPeopleNum];
  206. }
  207. - (void)otherStatistcsAction {
  208. OtherStatistcsVC *vc = [[OtherStatistcsVC alloc] init];
  209. [self navPushHideTabbarToVC:vc];
  210. }
  211. - (void)btnClick:(UIButton *)sender
  212. {
  213. // NSLog(@"%d",(int)sender.tag);
  214. if (sender.tag == -7) {
  215. //自定义日期
  216. calendarView.hidden = YES;
  217. RangePickerViewController *vc = [[RangePickerViewController alloc]init];
  218. __weak typeof(self) weakSelf = self;
  219. [vc setSelectCheckDateBlock:^(NSString *startDateStr, NSString *endDateStr, NSString *daysStr) {
  220. if ([daysStr integerValue] > 92) {
  221. showMsgByAlert(self, @"系统仅查询时间跨度三个月以内的数据");
  222. return;
  223. }
  224. startDateString = [startDateStr stringByReplacingOccurrencesOfString:@"-" withString:@"."];
  225. endDateString = [endDateStr stringByReplacingOccurrencesOfString:@"-" withString:@"."];
  226. //更改按钮状态 调接口
  227. UIButton *selectBtn;
  228. if ([[NSString stringWithFormat:@"%@-%@",startDateString,endDateString] isEqualToString:todayString]) {
  229. //相当于选择当天
  230. dateType = 1;
  231. selectBtn = (UIButton *)[calendarView viewWithTag:-6];
  232. }else if ([[NSString stringWithFormat:@"%@-%@",startDateString,endDateString] isEqualToString:yesterdayString]) {
  233. //相当于选择昨天
  234. dateType = 1;
  235. selectBtn = (UIButton *)[calendarView viewWithTag:-5];
  236. }else if ([[NSString stringWithFormat:@"%@-%@",startDateString,endDateString] isEqualToString:weekNow]) {
  237. //相当于选择本周
  238. dateType = 2;
  239. selectBtn = (UIButton *)[calendarView viewWithTag:-4];
  240. }else if ([[NSString stringWithFormat:@"%@-%@",startDateString,endDateString] isEqualToString:weekLast]) {
  241. //相当于选择上周
  242. dateType = 2;
  243. selectBtn = (UIButton *)[calendarView viewWithTag:-3];
  244. }else if ([[NSString stringWithFormat:@"%@-%@",startDateString,endDateString] isEqualToString:monthNow]) {
  245. //相当于选择本月
  246. dateType = 3;
  247. selectBtn = (UIButton *)[calendarView viewWithTag:-2];
  248. }else if ([[NSString stringWithFormat:@"%@-%@",startDateString,endDateString] isEqualToString:monthLast]) {
  249. //相当于选择上月
  250. dateType = 3;
  251. selectBtn = (UIButton *)[calendarView viewWithTag:-1];
  252. }else {
  253. //其他日期
  254. selectBtn = sender;
  255. dateType = 1;
  256. }
  257. lastDatebtn.selected = NO;
  258. [lastDatebtn boardWid:.1 Color:kLineColor];
  259. lastDatebtn.backgroundColor = kLineColor;
  260. selectBtn.selected = YES;
  261. selectBtn.backgroundColor = [UIColor whiteColor];
  262. [selectBtn boardWid:1 Color:defGreen];
  263. lastDatebtn = selectBtn;
  264. calendarView.hidden = YES;
  265. [weakSelf getPeopleNum];
  266. }];
  267. [self navPushHideTabbarToVC:vc];
  268. return;
  269. }
  270. if (sender.tag < 0 && sender.tag > -7) {
  271. if (lastDatebtn.tag != sender.tag) {
  272. if (sender.tag < -4) {
  273. dateType = 1;
  274. }else if (sender.tag < -2) {
  275. dateType = 2;
  276. }else {
  277. dateType = 3;
  278. }
  279. lastDatebtn.selected = NO;
  280. [lastDatebtn boardWid:.1 Color:kLineColor];
  281. lastDatebtn.backgroundColor = kLineColor;
  282. sender.selected = !sender.selected;
  283. sender.backgroundColor = [UIColor whiteColor];
  284. [sender boardWid:1 Color:defGreen];
  285. lastDatebtn = sender;
  286. calendarView.hidden = YES;
  287. //逻辑处理 要确定日期 然后调接口
  288. NSArray *dateArray = @[todayString,yesterdayString,weekNow,weekLast,monthNow,monthLast];
  289. NSArray *beginAndEndDate = [dateArray[lastDatebtn.tag + 6] componentsSeparatedByString:@"-"];
  290. startDateString = [beginAndEndDate firstObject];
  291. endDateString = [beginAndEndDate lastObject];
  292. [self getPeopleNum];
  293. }else{
  294. calendarView.hidden = YES;
  295. }
  296. return;
  297. }
  298. if (sender.tag == 1 || sender.tag == 2) {
  299. //更新日期
  300. [self getDateWithtype:sender.tag == 1 ? -1 : 1];
  301. //更改按钮状态 调接口
  302. UIButton *selectBtn;
  303. if ([[NSString stringWithFormat:@"%@-%@",startDateString,endDateString] isEqualToString:todayString]) {
  304. //相当于选择当天
  305. selectBtn = (UIButton *)[calendarView viewWithTag:-6];
  306. }else if ([[NSString stringWithFormat:@"%@-%@",startDateString,endDateString] isEqualToString:yesterdayString]) {
  307. //相当于选择昨天
  308. selectBtn = (UIButton *)[calendarView viewWithTag:-5];
  309. }else if ([[NSString stringWithFormat:@"%@-%@",startDateString,endDateString] isEqualToString:weekNow]) {
  310. //相当于选择本周
  311. selectBtn = (UIButton *)[calendarView viewWithTag:-4];
  312. }else if ([[NSString stringWithFormat:@"%@-%@",startDateString,endDateString] isEqualToString:weekLast]) {
  313. //相当于选择上周
  314. selectBtn = (UIButton *)[calendarView viewWithTag:-3];
  315. }else if ([[NSString stringWithFormat:@"%@-%@",startDateString,endDateString] isEqualToString:monthNow]) {
  316. //相当于选择本月
  317. selectBtn = (UIButton *)[calendarView viewWithTag:-2];
  318. }else if ([[NSString stringWithFormat:@"%@-%@",startDateString,endDateString] isEqualToString:monthLast]) {
  319. //相当于选择上月
  320. selectBtn = (UIButton *)[calendarView viewWithTag:-1];
  321. }else {
  322. //其他日期
  323. selectBtn = (UIButton *)[calendarView viewWithTag:-7];
  324. }
  325. lastDatebtn.selected = NO;
  326. [lastDatebtn boardWid:.1 Color:kLineColor];
  327. lastDatebtn.backgroundColor = kLineColor;
  328. selectBtn.selected = YES;
  329. selectBtn.backgroundColor = [UIColor whiteColor];
  330. [selectBtn boardWid:1 Color:defGreen];
  331. lastDatebtn = selectBtn;
  332. if (!calendarView.hidden) {
  333. calendarView.hidden = YES;
  334. }
  335. [self getPeopleNum];
  336. return;
  337. }
  338. if (sender.tag == 3) {
  339. //显示或收起
  340. calendarView.hidden = !calendarView.hidden;
  341. return;
  342. }
  343. if (sender.tag == 4) {//签到统计
  344. if (styleType != 1) {
  345. styleType = 1;
  346. UIButton *btn = (UIButton *)[self.view viewWithTag:5];
  347. btn.selected = NO;
  348. sender.selected = YES;
  349. [self getPeopleNum];
  350. }
  351. return;
  352. }
  353. if (sender.tag == 5) {//签到统计
  354. if (styleType != 2) {
  355. styleType = 2;
  356. UIButton *btn = (UIButton *)[self.view viewWithTag:4];
  357. btn.selected = NO;
  358. sender.selected = YES;
  359. [self getPeopleNum];
  360. }
  361. return;
  362. }
  363. }
  364. - (void)handleTap
  365. {
  366. calendarView.hidden = YES;
  367. }
  368. - (void)initDefaultDate {
  369. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  370. [dateFormatter setDateFormat:@"yyyy.MM.dd"];
  371. //今天
  372. NSString *dateStr = [dateFormatter stringFromDate:[NSDate date]];
  373. todayString = [NSString stringWithFormat:@"%@-%@",dateStr,dateStr];
  374. //设置默认值
  375. startDateString = endDateString = dateStr;
  376. calendarLabel.text = [NSString stringWithFormat:@"今日(%@) ▽",dateStr];
  377. //昨天
  378. dateStr = [self getDateWithdifferentDay:-1];
  379. yesterdayString =[NSString stringWithFormat:@"%@-%@",dateStr,dateStr];
  380. //本周
  381. [dateFormatter setDateFormat:@"e"];//1-7 这里把周日当作一周的开始
  382. int weekDay = [[dateFormatter stringFromDate:[NSDate date]] intValue] - 1;
  383. if (weekDay == 0) {
  384. weekDay = 7;
  385. }
  386. NSString *beginDate = [self getDateWithdifferentDay: 1-weekDay];
  387. NSString *endDate = [self getDateWithdifferentDay: 7-weekDay];
  388. weekNow = [NSString stringWithFormat:@"%@-%@",beginDate,endDate];
  389. //上周
  390. beginDate = [self getDateWithdifferentDay: -6-weekDay];
  391. endDate = [self getDateWithdifferentDay: -weekDay];
  392. weekLast = [NSString stringWithFormat:@"%@-%@",beginDate,endDate];
  393. //本月
  394. [dateFormatter setDateFormat:@"yyyy.MM"];
  395. dateStr = [dateFormatter stringFromDate:[NSDate date]];
  396. int moneyInt = [[dateStr substringFromIndex:5] intValue];
  397. int yearInt = [[dateStr substringToIndex:4] intValue];
  398. int moneyDay = 31;
  399. if (moneyInt == 2) {
  400. if (yearInt%4 == 0) {
  401. moneyDay = 28;
  402. }else{
  403. moneyDay = 29;
  404. }
  405. }else{
  406. if (moneyInt == 4 || moneyInt == 6 || moneyInt == 9 || moneyInt == 11) {
  407. moneyDay = 30;
  408. }
  409. }
  410. monthNow = [NSString stringWithFormat:@"%@.01-%@.%d",dateStr,dateStr,moneyDay];
  411. //上月
  412. if (moneyInt == 1) {
  413. yearInt -= 1;
  414. moneyInt = 12;
  415. }else{
  416. moneyInt -= 1;
  417. }
  418. moneyDay = 31;
  419. if (moneyInt == 2) {
  420. if (yearInt%4 == 0) {
  421. moneyDay = 28;
  422. }else{
  423. moneyDay = 29;
  424. }
  425. }else{
  426. if (moneyInt == 4 || moneyInt == 6 || moneyInt == 9 || moneyInt == 11) {
  427. moneyDay = 30;
  428. }
  429. }
  430. monthLast = [NSString stringWithFormat:@"%d.%02d.01-%d.%02d.%d",yearInt,moneyInt,yearInt,moneyInt,moneyDay];
  431. // NSLog(@"%@\n%@\n%@\n%@\n%@\n%@",todayString,yesterdayString,weekNow,weekLast,monthNow,monthLast);
  432. }
  433. - (NSString *)getDateWithdifferentDay:(NSInteger)day
  434. {
  435. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  436. [dateFormatter setDateFormat:@"yyyy.MM.dd"];
  437. return [self getDateWithDay:[dateFormatter stringFromDate:[NSDate date]] DifferentDay:day];
  438. }
  439. /**获取指定日期相差某天的日期 格式 yyyy.MM.dd
  440. @param now 指定日期
  441. @param day 相差日期 向后为正 向前为负
  442. @return 返回计算后的日期
  443. */
  444. - (NSString *)getDateWithDay:(NSString *)now DifferentDay:(NSInteger)day
  445. {
  446. if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
  447. NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
  448. NSDateComponents *comps = nil;
  449. comps = [calendar components:NSCalendarUnitDay fromDate:[NSDate date]];
  450. NSDateComponents *adcomps = [[NSDateComponents alloc] init];
  451. [adcomps setDay:day];
  452. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  453. [dateFormatter setDateFormat:@"yyyy.MM.dd"];
  454. NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:[dateFormatter dateFromString:now] options:0];
  455. return [dateFormatter stringFromDate:newdate];
  456. }else{
  457. NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
  458. NSDateComponents *comps = nil;
  459. comps = [calendar components:NSDayCalendarUnit fromDate:[NSDate date]];
  460. NSDateComponents *adcomps = [[NSDateComponents alloc] init];
  461. [adcomps setDay:day];
  462. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  463. [dateFormatter setDateFormat:@"yyyy.MM.dd"];
  464. NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:[dateFormatter dateFromString:now] options:0];
  465. return [dateFormatter stringFromDate:newdate];
  466. }
  467. }
  468. /**给定一串 日期与受理人数的数组 补全开始到结束中的所有天 没有的补0
  469. */
  470. - (NSArray *)getCompleteDataWithArray:(NSArray *)array
  471. {
  472. // if ([startDateString isEqualToString:endDateString]) {
  473. // NSMutableArray *aArray = [NSMutableArray array];
  474. // if (array.count > 0) {
  475. // [aArray addObject:[array firstObject]];
  476. // }else {
  477. // [aArray addObject:[NSDictionary dictionaryWithObject:@"0" forKey:startDateString]];
  478. // }
  479. //
  480. // return aArray;
  481. // }
  482. NSMutableDictionary *preDic = [NSMutableDictionary dictionaryWithCapacity:array.count];
  483. for (NSDictionary *dic in array) {
  484. [preDic setObject:[dic.allValues firstObject] forKey:[[dic.allKeys firstObject] stringByReplacingOccurrencesOfString:@"-" withString:@"."]];
  485. }
  486. NSMutableArray *aArray = [NSMutableArray array];
  487. NSString *dateString = startDateString;
  488. for (int i = 0; ![dateString isEqualToString:[self getDateWithDay:endDateString DifferentDay:1]]; i ++) {
  489. if ([preDic.allKeys containsObject:dateString]) {
  490. [aArray addObject:[NSDictionary dictionaryWithObject:preDic[dateString] forKey:dateString]];
  491. }else {
  492. [aArray addObject:[NSDictionary dictionaryWithObject:@"0" forKey:dateString]];
  493. }
  494. dateString = [self getDateWithDay:dateString DifferentDay:1];
  495. }
  496. return aArray;
  497. }
  498. /**获取给定时间前或后的日期
  499. @param type 1、向前 2、向后
  500. */
  501. - (void)getDateWithtype:(NSInteger)type
  502. {
  503. if (dateType == 2) {
  504. startDateString = [self getDateWithDay:startDateString DifferentDay:type*7];
  505. endDateString = [self getDateWithDay:endDateString DifferentDay:type*7];
  506. return;
  507. }
  508. if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
  509. NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
  510. NSDateComponents *comps = nil;
  511. comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|kCFCalendarUnitWeekday fromDate:[NSDate date]];
  512. NSDateComponents *adcomps = [[NSDateComponents alloc] init];
  513. switch (dateType) {
  514. case 1:
  515. [adcomps setDay:type];
  516. break;
  517. case 2:
  518. // [adcomps setWeekday:type];
  519. break;
  520. case 3:
  521. [adcomps setMonth:type];
  522. break;
  523. case 4:
  524. [adcomps setYear:type];
  525. break;
  526. default:
  527. break;
  528. }
  529. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  530. [dateFormatter setDateFormat:@"yyyy.MM.dd"];
  531. NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:[dateFormatter dateFromString:startDateString] options:0];
  532. startDateString = [dateFormatter stringFromDate:newdate];
  533. newdate = [calendar dateByAddingComponents:adcomps toDate:[dateFormatter dateFromString:endDateString] options:0];
  534. endDateString = [dateFormatter stringFromDate:newdate];
  535. if (dateType == 3) {
  536. int month = [[endDateString componentsSeparatedByString:@"."][1] intValue];
  537. if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
  538. if ([endDateString containsString:@".30"]) {
  539. endDateString = [endDateString stringByReplacingOccurrencesOfString:@".30" withString:@".31"];
  540. }
  541. if ([endDateString containsString:@".29"]) {
  542. endDateString = [endDateString stringByReplacingOccurrencesOfString:@".29" withString:@".31"];
  543. }
  544. if ([endDateString containsString:@".28"]) {
  545. endDateString = [endDateString stringByReplacingOccurrencesOfString:@".28" withString:@".31"];
  546. }
  547. }
  548. if (month == 4 || month == 6 || month == 9 || month == 11) {
  549. if ([endDateString containsString:@".31"]) {
  550. endDateString = [endDateString stringByReplacingOccurrencesOfString:@".31" withString:@".30"];
  551. }
  552. if ([endDateString containsString:@".29"]) {
  553. endDateString = [endDateString stringByReplacingOccurrencesOfString:@".29" withString:@".30"];
  554. }
  555. if ([endDateString containsString:@".28"]) {
  556. endDateString = [endDateString stringByReplacingOccurrencesOfString:@".28" withString:@".30"];
  557. }
  558. }
  559. }
  560. }else{
  561. NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
  562. NSDateComponents *comps = nil;
  563. comps = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSCalendarUnitWeekday fromDate:[NSDate date]];
  564. NSDateComponents *adcomps = [[NSDateComponents alloc] init];
  565.     switch (dateType) {
  566. case 1:
  567. [adcomps setDay:type];
  568. break;
  569. case 2:
  570. // [adcomps setWeekday:type];
  571. break;
  572. case 3:
  573. [adcomps setMonth:type];
  574. break;
  575. case 4:
  576. [adcomps setYear:type];
  577. break;
  578. default:
  579. break;
  580. }
  581. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  582. [dateFormatter setDateFormat:@"yyyy.MM.dd"];
  583. NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:[dateFormatter dateFromString:startDateString] options:0];
  584. startDateString = [dateFormatter stringFromDate:newdate];
  585. newdate = [calendar dateByAddingComponents:adcomps toDate:[dateFormatter dateFromString:endDateString] options:0];
  586. endDateString = [dateFormatter stringFromDate:newdate];
  587. if (dateType == 3) {
  588. int month = [[endDateString componentsSeparatedByString:@"."][1] intValue];
  589. if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
  590. if ([endDateString containsString:@".30"]) {
  591. endDateString = [endDateString stringByReplacingOccurrencesOfString:@".30" withString:@".31"];
  592. }
  593. if ([endDateString containsString:@".29"]) {
  594. endDateString = [endDateString stringByReplacingOccurrencesOfString:@".29" withString:@".31"];
  595. }
  596. if ([endDateString containsString:@".28"]) {
  597. endDateString = [endDateString stringByReplacingOccurrencesOfString:@".28" withString:@".31"];
  598. }
  599. }
  600. if (month == 4 || month == 6 || month == 9 || month == 11) {
  601. if ([endDateString containsString:@".31"]) {
  602. endDateString = [endDateString stringByReplacingOccurrencesOfString:@".31" withString:@".30"];
  603. }
  604. if ([endDateString containsString:@".29"]) {
  605. endDateString = [endDateString stringByReplacingOccurrencesOfString:@".29" withString:@".30"];
  606. }
  607. if ([endDateString containsString:@".28"]) {
  608. endDateString = [endDateString stringByReplacingOccurrencesOfString:@".28" withString:@".30"];
  609. }
  610. }
  611. }
  612. }
  613. }
  614. #pragma mark 数据请求
  615. - (void)getPeopleNum {
  616. if (![NetManager connectedToNetWork]) {
  617. showMsgUnconnect();
  618. return;
  619. }
  620. NSString *typeString = [NSString stringWithFormat:@"%d",(int)dateType];
  621. if (lastDatebtn.tag == -7 && ![startDateString isEqualToString:endDateString]) {
  622. typeString = @"4";
  623. }
  624. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  625. [dic setObject:defUser.userDict[@"school"] forKey:@"schoolId"];
  626. [dic setObject:typeString forKey:@"type"];
  627. [dic setObject:[startDateString stringByReplacingOccurrencesOfString:@"." withString:@"-"] forKey:@"startTime"];
  628. [dic setObject:[endDateString stringByReplacingOccurrencesOfString:@"." withString:@"-"] forKey:@"endTime"];
  629. NSString *method = @"getStudentSignStatistics";
  630. if (styleType == 2) {
  631. method = @"getStudentDropStatistics";
  632. }
  633. [MBProgressHUD showLoadToView:self.view];
  634. [NetManager requestAnythingWithURL:method dictionary:dic dataArray:nil completion:^(NSDictionary *root) {
  635. [MBProgressHUD hideHUDForView:self.view];
  636. if (!root) {
  637. ShowErrorMsg(@"请求失败!");
  638. return;
  639. }
  640. if ([root[@"code"] isEqualToString:@"1"]) {
  641. ShowErrorMsg(root[@"msg"]);
  642. return;
  643. }
  644. //请求成功
  645. //根据传过来的时间 改变calendarLabel
  646. NSString *calendarString = calendarString = [NSString stringWithFormat:@"%@-%@ ▽",startDateString,endDateString];
  647. if ([[NSString stringWithFormat:@"%@-%@",startDateString,endDateString] isEqualToString:todayString]) {
  648. //相当于选择当天
  649. calendarString = [NSString stringWithFormat:@"今日(%@) ▽",startDateString];
  650. }else if ([[NSString stringWithFormat:@"%@-%@",startDateString,endDateString] isEqualToString:yesterdayString]) {
  651. //相当于选择昨天
  652. calendarString = [NSString stringWithFormat:@"昨日(%@) ▽",startDateString];
  653. }else if ([[NSString stringWithFormat:@"%@-%@",startDateString,endDateString] isEqualToString:weekNow]) {
  654. //相当于选择本周
  655. calendarString = [NSString stringWithFormat:@"本周(%@-%@) ▽",startDateString,endDateString];
  656. }else if ([[NSString stringWithFormat:@"%@-%@",startDateString,endDateString] isEqualToString:weekLast]) {
  657. //相当于选择上周
  658. calendarString = [NSString stringWithFormat:@"上周(%@-%@) ▽",startDateString,endDateString];
  659. }else if ([[NSString stringWithFormat:@"%@-%@",startDateString,endDateString] isEqualToString:monthNow]) {
  660. //相当于选择本月
  661. calendarString = [NSString stringWithFormat:@"本月(%@-%@) ▽",startDateString,endDateString];
  662. }else if ([[NSString stringWithFormat:@"%@-%@",startDateString,endDateString] isEqualToString:monthLast]) {
  663. //相当于选择上月
  664. calendarString = [NSString stringWithFormat:@"上月(%@-%@) ▽",startDateString,endDateString];
  665. }else {
  666. //其他日期
  667. if (lastDatebtn.tag == -7) {
  668. if ([startDateString isEqualToString:endDateString]) {
  669. calendarString = [NSString stringWithFormat:@"%@ ▽",startDateString];
  670. }
  671. }
  672. }
  673. //根据type不同 分为报名和退学
  674. calendarLabel.text = calendarString;
  675. //根据返回值 设置人数和折线图
  676. NSDictionary *dict = root[@"body"];
  677. NSArray *realArray = dict[@"nums"];
  678. if (![startDateString isEqualToString:endDateString]) {
  679. if ([dict[@"nums"] count] > 0) {
  680. realArray = [self getCompleteDataWithArray:dict[@"nums"]];
  681. }
  682. }
  683. // NSLog(@"realArray----><>%@",realArray);
  684. NSString *numString = @"0";
  685. if ([dict.allKeys containsObject:@"CURRENTNUM"]) {
  686. numString = [NSString stringWithFormat:@"%@",dict[@"CURRENTNUM"]];
  687. }
  688. numberLabel.text = [NSString stringWithFormat:@"%@人",numString];
  689. int differentNumber = [dict[@"CURRENTNUM"] intValue] - [dict[@"LASTNUM"] intValue];
  690. NSString *differentString = @"--";
  691. if (differentNumber < 0) {
  692. differentString = [NSString stringWithFormat:@"- %d人",-differentNumber];
  693. }else {
  694. differentString = [NSString stringWithFormat:@"+ %d人",differentNumber];
  695. }
  696. //请求结束后
  697. NSInteger pointNum = 0;
  698. NSArray *xTitles = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"];
  699. compareNumLabel.hidden = NO;
  700. switch (dateType) {
  701. case 1:
  702. if ([startDateString isEqualToString:endDateString]) {
  703. pointNum = 1;
  704. xTitles = @[startDateString];
  705. compareNumLabel.text = [NSString stringWithFormat:@"比前一日\n%@",differentString];
  706. }else{
  707. pointNum = realArray.count > 10 ? 10 : realArray.count;//这个返回多少天 就=多少个吧
  708. NSMutableArray *aray = [NSMutableArray array];
  709. for (int i = 0; i < pointNum; i ++) {
  710. [aray addObject:[NSString stringWithFormat:@"%d",i + 1]];
  711. }
  712. xTitles = [NSArray arrayWithArray:aray];
  713. compareNumLabel.hidden = YES;
  714. }
  715. break;
  716. case 2:
  717. pointNum = 7;
  718. xTitles = @[@"周一",@"周二",@"周三",@"周四",@"周五",@"周六",@"周日"];
  719. compareNumLabel.text = [NSString stringWithFormat:@"比前一周\n%@",differentString];
  720. break;
  721. case 3:
  722. pointNum = 4;
  723. xTitles = @[@"第1周",@"第2周",@"第3周",@"第4周"];
  724. compareNumLabel.text = [NSString stringWithFormat:@"比前一月\n%@",differentString];
  725. break;
  726. case 4:
  727. pointNum = 12;
  728. xTitles = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12"];
  729. compareNumLabel.text = [NSString stringWithFormat:@"比前一年\n%@",differentString];
  730. break;
  731. default:
  732. break;
  733. }
  734. for (AFView *subV in afView.subviews) {
  735. [subV removeFromSuperview];
  736. }
  737. AFView *afv = [[AFView alloc] initWithFrame:CGRectMake(0, 20, afView.width, afView.height - 30)];
  738. //设置X轴
  739. [afv setX_labels:xTitles];
  740. [afv setMin_X:1];
  741. [afv setMax_X:xTitles.count];
  742. //设置Y轴
  743. [afv setMin_Y:0];
  744. [afv setMax_Y:100];
  745. [afv setY_labels:@[@"0",@"20",@"40",@"60",@"80",@"100"]];
  746. [afView addSubview:afv];
  747. NSMutableArray *points = [NSMutableArray arrayWithCapacity:pointNum];
  748. NSArray *counterfeitDate = @[@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0"];
  749. if ([dict[@"nums"] count] != 0) {
  750. if (dateType == 1 || dateType == 2) {
  751. NSMutableArray *aray = [NSMutableArray array];
  752. for (int i = 0; i < pointNum; i ++) {
  753. [aray addObject:[[realArray[i] allValues] firstObject]];
  754. }
  755. counterfeitDate = [NSArray arrayWithArray:aray];
  756. }else if (dateType == 3) {
  757. NSMutableArray *aray = [NSMutableArray array];
  758. int num = 0;
  759. for (int i = 0; i < realArray.count; i ++) {
  760. num += [[[realArray[i] allValues] firstObject] intValue];
  761. if (i%7 == 6) {
  762. if (aray.count < 4) {
  763. num = 0;
  764. [aray addObject:[NSString stringWithFormat:@"%d",num]];
  765. }else{
  766. if (i == realArray.count - 1) {
  767. [aray addObject:[NSString stringWithFormat:@"%d",num]];
  768. }
  769. }
  770. }
  771. }
  772. counterfeitDate = [NSArray arrayWithArray:aray];
  773. }else if (dateType == 4) {
  774. //年就不作处理了先
  775. }
  776. }
  777. for (int i = 0; i < pointNum; i++) {
  778. [points addObject:[NSValue valueWithCGPoint:CGPointMake(i + 1, [counterfeitDate[i] integerValue])]];
  779. }
  780. [afv setData:points];//折线图的数据。
  781. }];
  782. }
  783. - (void)didReceiveMemoryWarning {
  784. [super didReceiveMemoryWarning];
  785. // Dispose of any resources that can be recreated.
  786. }
  787. @end