Plan.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace app\admin\controller\equipment;
  3. use app\admin\model\equipment\Equipment;
  4. use app\admin\model\equipment\PlanArchive;
  5. use app\admin\model\equipment\PlanTask;
  6. use app\common\controller\Backend;
  7. /**
  8. * 设备计划
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Plan extends Backend
  13. {
  14. /**
  15. * Plan模型对象
  16. * @var \app\admin\model\equipment\Plan
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\equipment\Plan;
  23. $this->view->assign("statusList", $this->model->getStatusList());
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 停用
  32. */
  33. public function stop($ids = "")
  34. {
  35. if (!$this->request->isPost()) {
  36. $this->error(__("Invalid parameters"));
  37. }
  38. $ids = $ids ? $ids : $this->request->post("ids");
  39. $ids = explode(',', $ids);
  40. $planIds = $this->model->whereIn('id', $ids)->select();
  41. $this->modelValidate = true;
  42. if (!$planIds) {
  43. $this->error(__('No Results were found'));
  44. }
  45. $result = $this->model->stopPlan($ids);
  46. if ($result === true) {
  47. $this->success();
  48. } else {
  49. $this->error($result);
  50. }
  51. }
  52. /**
  53. * 设备明细
  54. */
  55. public function list($ids = null)
  56. {
  57. //设置过滤方法
  58. $this->request->filter(['strip_tags', 'trim']);
  59. if ($this->request->isAjax()) {
  60. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  61. $planArchiveModel = new PlanArchive();
  62. $archiveIds = $planArchiveModel->where('plan_id', $ids)->column('archive_id');
  63. $equipmentModel = new Equipment();
  64. $list = $equipmentModel
  65. ->with(['archive' => ['load_supplier', 'load_responsible_user']])
  66. ->where($where)
  67. ->whereIn('archive_id', $archiveIds)
  68. ->order($sort, $order)
  69. ->paginate($limit);
  70. $result = array("total" => $list->total(), "rows" => $list->items());
  71. return json($result);
  72. }
  73. return $this->view->fetch();
  74. }
  75. /**
  76. * 清理无效任务
  77. */
  78. public function clearInvalidTask()
  79. {
  80. $planTaskIdsOne = [];
  81. $planTaskIdsTwo = [];
  82. $planTaskModel = new PlanTask();
  83. // 根据计划任务
  84. $planIds = $planTaskModel->where(['status' => 'pending'])->group('plan_id')->column('plan_id');
  85. $normalPlanIds = $this->model->whereIn('id', $planIds)->where(['status' => 'normal'])->column('id');
  86. $invalidPlanIds = array_diff($planIds, $normalPlanIds);
  87. if (!empty($invalidPlanIds)) {
  88. $planTaskIdsOne = $planTaskModel->whereIn('plan_id', $invalidPlanIds)->where(['status' => 'pending'])->column('id');
  89. }
  90. // 根据设备
  91. $equipmentIds = $planTaskModel->where(['status' => 'pending'])->group('equipment_id')->column('equipment_id');
  92. $equipmentModel = new Equipment();
  93. $normalEquipmentIds = $equipmentModel->whereIn('id', $equipmentIds)->column('id');
  94. $invalidEquipmentIds = array_diff($equipmentIds, $normalEquipmentIds);
  95. if (!empty($invalidEquipmentIds)) {
  96. $planTaskIdsTwo = $planTaskModel->whereIn('equipment_id', $invalidEquipmentIds)->where(['status' => 'pending'])->column('id');
  97. }
  98. $planTaskIds = array_unique(array_merge($planTaskIdsOne, $planTaskIdsTwo));
  99. if (count($planTaskIds) > 0) {
  100. $planTaskModel->destroy($planTaskIds);
  101. }
  102. $this->success();
  103. }
  104. }