Jiankangdata.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. $map = [
  14. 'user_id' => $this->auth->id,
  15. 'createdate' => date('Y-m-d'),
  16. ];
  17. $check = Db::name('jiankangdata')->where($map)->find();
  18. $data = [
  19. 'user_id' => $this->auth->id,
  20. 'xueya_low' => $this->request->post('xueya_low'),
  21. 'xueya_high' => $this->request->post('xueya_high'),
  22. 'xuetang' => $this->request->post('xuetang'),
  23. 'createtime' => time(),
  24. 'updatetime' => time(),
  25. 'createdate' => date('Y-m-d'),
  26. ];
  27. if($check){
  28. Db::name('jiankangdata')->where($map)->update($data);
  29. }else{
  30. Db::name('jiankangdata')->insert($data);
  31. }
  32. $this->success('提交成功');
  33. }
  34. public function data(){
  35. $type = $this->request->post('type','week','trim');
  36. if($type == 'week'){
  37. $start_date = date('Y-m-d', strtotime('monday this week'));
  38. $ended_date = date('Y-m-d', strtotime('sunday this week'));
  39. $days = 7;
  40. for($i=0;$i<$days;$i++){
  41. $date_array[] = date('m.d', 86400*$i + strtotime('monday this week'));
  42. }
  43. }
  44. if($type == 'month'){
  45. $start_date = date('Y-m-01');
  46. $ended_date = date('Y-m-t');
  47. $days = date('t');
  48. for($i=0;$i<$days;$i++){
  49. $date_array[] = date('m.d', 86400*$i + strtotime(date('Y-m-01')));
  50. }
  51. }
  52. // dump($start_date);
  53. // dump($ended_date);
  54. // dump($date_array);
  55. $list = Db::name('jiankangdata')->where('user_id',$this->auth->id)->whereBetween('createdate',[$start_date,$ended_date])->order('id','asc')->select();
  56. $xueya_low = [];
  57. $xueya_high = [];
  58. $xuetang = [];
  59. foreach($date_array as $date){
  60. $xueya_low[$date] = 0;
  61. $xueya_high[$date] = 0;
  62. $xuetang[$date] = 0;
  63. foreach($list as $k=>$v){
  64. if(date('m.d',strtotime($v['createdate'])) == $date){
  65. $xueya_low[$date] = $v['xueya_low'];
  66. $xueya_high[$date] = $v['xueya_high'];
  67. $xuetang[$date] = $v['xuetang'];
  68. }
  69. }
  70. }
  71. // dump($xueya_low);
  72. // dump($xueya_high);
  73. // dump($xuetang);
  74. $rs = [
  75. 'start_date' => date('Y.m.d',strtotime($start_date)),
  76. 'end_date' => date('Y.m.d',strtotime($ended_date)),
  77. 'date_array' => $date_array,
  78. 'xueya_low' => array_values($xueya_low),
  79. 'xueya_high' => array_values($xueya_high),
  80. 'xuetang' => array_values($xuetang),
  81. ];
  82. $this->success(1,$rs);
  83. }
  84. }