ScreenData.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Cache;
  4. use app\common\controller\Api;
  5. use think\facade\Db;
  6. class ScreenData extends Api
  7. {
  8. protected array $noNeedLogin = ['getdata'];
  9. protected array $noNeedPermission = ['index'];
  10. /**
  11. * 缓存中获取大屏数据
  12. * @return void
  13. */
  14. public function getdata(): void
  15. {
  16. $cacheKey = 'Screendata'; // 缓存键名
  17. // 检查缓存是否存在
  18. if (Cache::has($cacheKey)) {
  19. // 从缓存中获取数据
  20. $Screendata = Cache::get($cacheKey);
  21. } else {
  22. // 缓存不存在,执行查询操作
  23. $Screendata = $this->queryDataFromDatabase();
  24. // 将数据存入缓存,并设置过期时间
  25. Cache::set($cacheKey, $Screendata, 3600); // 缓存有效期为1小时
  26. }
  27. $this->success('ok', $Screendata);
  28. }
  29. /**
  30. * 查询大屏数据
  31. */
  32. private function queryDataFromDatabase()
  33. {
  34. //仪器总数
  35. $count = Db::name('asset')->count();
  36. //仪器总价值
  37. $value = Db::name('asset')->sum('unit_price');
  38. $value = round($value / 10000, 2);
  39. //已借出仪器数量
  40. $lent_number = Db::name('asset')->where('status', 1)->count();
  41. //维修中仪器数量
  42. $repair_number = Db::name('asset')->where('status', 2)->count();
  43. //仪器类型比例
  44. $Instrumentlist = ['水准仪', 'RTK', '全站仪', '无人机', '其他'];
  45. foreach ($Instrumentlist as $list) {
  46. $Instrument_type = Db::name('asset')->where('asset_name', 'like', '%' . $list . '%')->count();
  47. //$a仪器类型占比
  48. $a[$list] = $Instrument_type;
  49. if ($list == "其他") {
  50. $Instrument_type = Db::name('asset')->whereNotLike('asset_name', ['%水准仪%', '%RTK%', '%全站仪%', '%无人机%'])->count();
  51. $a[$list] = $Instrument_type;
  52. };
  53. };
  54. //用户信息
  55. $teacher = Db::name('admin')->count();
  56. $student = Db::name('student')->count();
  57. $usersum = $teacher + $student;
  58. $userinfo = array('教职工' => $teacher, '学生' => $student, '用户总数' => $usersum);
  59. //仪器借取数量
  60. foreach ($Instrumentlist as $list) {
  61. // $asset_groud_id=Db::name('asset_group')->where('belonging_category', 'like','%'.$list.'%')->column('id');
  62. if ($list !== "其他") {
  63. $Instrument_type = Db::name('borrow_number')->where('asset_name', 'like', '%' . $list . '%')->sum('num');
  64. $q[$list] = $Instrument_type;
  65. } else {
  66. // $asset_groud_id=Db::name('asset_group')->whereNotLike('belonging_category', ['%水准仪%','%RTK%','%全站仪%','%无人机%'])->column('id');
  67. $Instrument_type = Db::name('borrow_number')->whereNotLike('asset_name', ['%水准仪%', '%RTK%', '%全站仪%', '%无人机%'])->sum('num');
  68. //仪器借取数量
  69. $q[$list] = $Instrument_type;
  70. arsort($q);
  71. // halt($q);
  72. }
  73. };
  74. //每月仪器借取数量
  75. $year = date("Y");
  76. $Month = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
  77. foreach ($Month as $m) {
  78. $Monthq = Db::name('borrow_number')->whereMonth('create_time', $year . '-' . $m)->sum('num');
  79. $mq[$m] = $Monthq;
  80. }
  81. //实验室介绍
  82. $labinfo = Db::name('laboratory_introduction')->where('id', 1)->field('laboratorydisplay_diagram,laboratory_introduction')->select();
  83. //仪器展示
  84. $labdisplay = Db::name('instrument_display')->where('Instrument_status', 1)->field('Instrument_name,Instrument_picture,Instrument_value,Instrument_num,Instrument_introduction')->select();
  85. //借单列表
  86. $labborrow = Db::name('borrow')->alias('a')->leftjoin('admin b', 'a.user_id = b.id')->field('a.purpose,a.create_time,a.username,b.nickname,a.status')->order('a.create_time DESC')->limit(15)->select();
  87. $data = [
  88. // 数据数组
  89. 'count' => $count,
  90. 'value' => $value,
  91. 'lent_number' => $lent_number,
  92. 'repair_number' => $repair_number,
  93. 'a' => $a,
  94. 'userinfo' => $userinfo,
  95. 'q' => $q,
  96. 'mq' => $mq,
  97. 'labinfo' => $labinfo,
  98. 'labdisplay' => $labdisplay,
  99. 'labborrow' => $labborrow,
  100. ];
  101. return $data;
  102. }
  103. //大屏操作
  104. public function getCurrent(): void
  105. {
  106. $data = Db::name('operation')->where('switch', 1)->find();
  107. $this->success('', [
  108. 'data' => $data
  109. ]);
  110. }
  111. }