Jiankangdata.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 健康检测数据
  7. */
  8. class Jiankangdata extends Api
  9. {
  10. protected $noNeedLogin = ['*'];
  11. protected $noNeedRight = ['*'];
  12. public function submit(){
  13. //今天的数据都改为非最后一条
  14. $map = [
  15. 'user_id' => $this->auth->id,
  16. 'createdate' => date('Y-m-d'),
  17. ];
  18. Db::name('jiankangdata')->where($map)->update(['is_last'=>0]);
  19. //新写入
  20. $data = [
  21. 'user_id' => $this->auth->id,
  22. 'xueya_low' => $this->request->post('xueya_low'),
  23. 'xueya_high' => $this->request->post('xueya_high'),
  24. 'xuetang' => $this->request->post('xuetang'),
  25. 'createtime' => time(),
  26. 'updatetime' => time(),
  27. 'createdate' => date('Y-m-d'),
  28. 'is_last' => 1, //默认就是最后一条
  29. ];
  30. Db::name('jiankangdata')->insert($data);
  31. $this->success('提交成功');
  32. }
  33. public function lists(){
  34. $list = Db::name('jiankangdata')->where('user_id',$this->auth->id)->autopage()->order('id desc')->select();
  35. $this->success(1, $list);
  36. }
  37. public function data(){
  38. $type = $this->request->post('type','week','trim');
  39. $page = $this->request->post('page',1,'intval');
  40. if($page < 1){ $page = 1;}
  41. if($type == 'week'){
  42. $start_time = strtotime('monday this week') - (7 * 86400 * ($page - 1));
  43. $ended_time = strtotime('sunday this week') - (7 * 86400 * ($page - 1));
  44. $start_date = date('Y-m-d', $start_time);
  45. $ended_date = date('Y-m-d', $ended_time);
  46. $days = 7;
  47. for($i=0;$i<$days;$i++){
  48. $date_array[] = date('m.d', 86400*$i + $start_time);
  49. }
  50. }
  51. if($type == 'month'){
  52. $start_time = strtotime('first day of -'.($page - 1).' month');
  53. $start_date = date('Y-m-01',$start_time);
  54. $ended_date = date('Y-m-t', $start_time);
  55. $days = date('t',$start_time);
  56. for($i=0;$i<$days;$i++){
  57. $date_array[] = date('m.d', 86400*$i + strtotime($start_date));
  58. }
  59. }
  60. // dump($start_date);
  61. // dump($ended_date);
  62. // dump($date_array);
  63. $list = Db::name('jiankangdata')
  64. ->where('user_id',$this->auth->id)
  65. ->whereBetween('createdate',[$start_date,$ended_date])
  66. ->where('is_last',1) //只拿之后一条
  67. ->order('id','asc')->select();
  68. $xueya_low = [];
  69. $xueya_high = [];
  70. $xuetang = [];
  71. foreach($date_array as $date){
  72. $xueya_low[$date] = 0;
  73. $xueya_high[$date] = 0;
  74. $xuetang[$date] = 0;
  75. foreach($list as $k=>$v){
  76. if(date('m.d',strtotime($v['createdate'])) == $date){
  77. $xueya_low[$date] = $v['xueya_low'];
  78. $xueya_high[$date] = $v['xueya_high'];
  79. $xuetang[$date] = $v['xuetang'];
  80. }
  81. }
  82. }
  83. // dump($xueya_low);
  84. // dump($xueya_high);
  85. // dump($xuetang);
  86. $rs = [
  87. 'start_date' => date('Y.m.d',strtotime($start_date)),
  88. 'end_date' => date('Y.m.d',strtotime($ended_date)),
  89. 'date_array' => $date_array,
  90. 'xueya_low' => array_values($xueya_low),
  91. 'xueya_high' => array_values($xueya_high),
  92. 'xuetang' => array_values($xuetang),
  93. ];
  94. $this->success(1,$rs);
  95. }
  96. }