statisticsVC.m 33 KB

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