MachineList.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace app\api\controller;
  3. use think\Request;
  4. use app\common\controller\Api;
  5. /**
  6. * 设备维修保养记录
  7. */
  8. class MachineList extends Api
  9. {
  10. protected $noNeedLogin = ['*'];
  11. protected $noNeedRight = ['*'];
  12. //设备维修保养记录左侧菜单
  13. public function getTab()
  14. {
  15. if ($this->request->isGet() === false){
  16. $this->error('请求错误');
  17. }
  18. $year = date('Y',time());
  19. $lastYear = date('Y', strtotime('-1 year'));
  20. //年度维修统计
  21. $yearResult1 = $this->getYearSite($year);
  22. $yearResult2 = $this->getYearSite($lastYear);
  23. $data['1、年度维修统计'] = [
  24. $year => $yearResult1,
  25. $lastYear => $yearResult2
  26. ];
  27. //月度维修记录
  28. $lastMouth = date('Y-m', strtotime('-1 year'));
  29. $mouthReault = $this->getMouthSite($lastMouth);
  30. $data['2、月度维修记录'] = $mouthReault;
  31. //年度保养记录
  32. $YearMaintenanceSist1 = $this->getYearMaintenanceSist($year);
  33. $YearMaintenanceSist2 = $this->getYearMaintenanceSist($lastYear);
  34. if (!empty($YearMaintenanceSist1)){
  35. $data['3、年度保养统计'][$year] = $YearMaintenanceSist1;
  36. }
  37. if (!empty($YearMaintenanceSist2)){
  38. $data['3、年度保养统计'][$lastYear] = $YearMaintenanceSist2;
  39. }
  40. //月度保养记录
  41. $mouthMaintenanceSist = $this->getMouthMaintenanceSist($lastMouth);
  42. $data['4、月度保养记录'] = $mouthMaintenanceSist;
  43. $this->success('成功',$data);
  44. }
  45. private function getYearSite($year)
  46. {
  47. $sist = db('设备_维修记录')
  48. ->where('报障时间','like',$year.'%')
  49. ->group('使用部门')
  50. ->column('rtrim(使用部门)');
  51. $data = [];
  52. if (!empty($sist)){
  53. foreach ($sist as $item){
  54. $count = db('设备_维修记录')
  55. ->where('使用部门',$item)
  56. ->where('报障时间','like',$year.'%')
  57. ->count();
  58. if ($count !== 0){
  59. $data[] = $item.'('.$count.'台次)';
  60. }
  61. }
  62. }
  63. return $data;
  64. }
  65. private function getMouthSite($mouth)
  66. {
  67. $mouth = db('设备_维修记录')
  68. ->where('报障时间','>',$mouth.'-01 00:00:00')
  69. ->where('报障时间','<',date('Y-m-d H:i:s',time()))
  70. ->group('date')
  71. ->order('date desc')
  72. ->column('DATE_FORMAT(报障时间, "%Y-%m") AS date');
  73. $data = [];
  74. foreach ($mouth as $item){
  75. $sist = db('设备_维修记录')
  76. ->where('报障时间','like',$item.'%')
  77. ->group('使用部门')
  78. ->column('rtrim(使用部门)');
  79. foreach ($sist as $value){
  80. $count = db('设备_维修记录')
  81. ->where('使用部门',$value)
  82. ->where('报障时间','like',$item.'%')
  83. ->count();
  84. $data[$item][] = $value.'('.$count.'台次)';
  85. }
  86. }
  87. return $data;
  88. }
  89. private function getYearMaintenanceSist($year)
  90. {
  91. $sist = db('设备_保养记录')
  92. ->where('保养开始时间','like',$year.'%')
  93. ->group('使用部门')
  94. ->column('rtrim(使用部门)');
  95. $data = [];
  96. if (!empty($sist)){
  97. foreach ($sist as $item){
  98. $count = db('设备_保养记录')
  99. ->where('使用部门',$item)
  100. ->where('保养开始时间','like',$year.'%')
  101. ->count();
  102. if ($count !== 0){
  103. $data[] = $item.'('.$count.'台次)';
  104. }
  105. }
  106. }
  107. return $data;
  108. }
  109. private function getMouthMaintenanceSist($mouth)
  110. {
  111. $mouth = db('设备_保养记录')
  112. ->where('保养开始时间','>',$mouth.'-01 00:00:00')
  113. ->where('保养开始时间','<',date('Y-m-d H:i:s',time()))
  114. ->group('date')
  115. ->order('date desc')
  116. ->column('DATE_FORMAT(保养开始时间, "%Y-%m") AS date');
  117. $data = [];
  118. foreach ($mouth as $item){
  119. $sist = db('设备_保养记录')
  120. ->where('保养开始时间','like',$item.'%')
  121. ->group('使用部门')
  122. ->column('rtrim(使用部门)');
  123. foreach ($sist as $value){
  124. $count = db('设备_保养记录')
  125. ->where('使用部门',$value)
  126. ->where('保养开始时间','like',$item.'%')
  127. ->count();
  128. $data[$item][] = $value.'('.$count.'台次)';
  129. }
  130. }
  131. return $data;
  132. }
  133. //年度维修记录上方列表
  134. public function MachineList()
  135. {
  136. if ($this->request->isGet() === false){
  137. $this->error('请求错误');
  138. }
  139. $param = $this->request->param();
  140. if (empty($param)){
  141. $this->error('参数错误');
  142. }
  143. }
  144. }