Plan.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace app\admin\model\equipment;
  3. use think\Db;
  4. use think\Exception;
  5. use think\Model;
  6. use traits\model\SoftDelete;
  7. class Plan extends Model
  8. {
  9. use SoftDelete;
  10. // 表名
  11. protected $name = 'equipment_plan';
  12. // 自动写入时间戳字段
  13. protected $autoWriteTimestamp = 'int';
  14. // 定义时间戳字段名
  15. protected $createTime = 'createtime';
  16. protected $updateTime = 'updatetime';
  17. protected $deleteTime = 'deletetime';
  18. // 追加属性
  19. protected $append = [
  20. 'first_duetime_text',
  21. 'last_duetime_text',
  22. 'status_text'
  23. ];
  24. public function getStatusList()
  25. {
  26. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  27. }
  28. public function getFirstDuetimeTextAttr($value, $data)
  29. {
  30. $value = $value ? $value : (isset($data['first_duetime']) ? $data['first_duetime'] : '');
  31. return is_numeric($value) ? date("Y年m月d日", $value) : $value;
  32. }
  33. public function getLastDuetimeTextAttr($value, $data)
  34. {
  35. $value = $value ? $value : (isset($data['last_duetime']) ? $data['last_duetime'] : '');
  36. return is_numeric($value) ? date("Y年m月d日", $value) : $value;
  37. }
  38. public function getStatusTextAttr($value, $data)
  39. {
  40. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  41. $list = $this->getStatusList();
  42. return isset($list[$value]) ? $list[$value] : '';
  43. }
  44. protected function setFirstDuetimeAttr($value)
  45. {
  46. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  47. }
  48. protected function setLastDuetimeAttr($value)
  49. {
  50. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  51. }
  52. public function planArchives()
  53. {
  54. return $this->hasMany('PlanArchive', 'plan_id')->with('archive')->field('plan_id, archive_id');
  55. }
  56. public function planFields()
  57. {
  58. return $this->hasMany('PlanField', 'plan_id');
  59. }
  60. public function planUsers()
  61. {
  62. return $this->hasMany('PlanUser', 'plan_id')->with('user')->field('plan_id, user_id');
  63. }
  64. public function addPlan($type, $data)
  65. {
  66. Db::startTrans();
  67. try {
  68. $equipmentModel = new Equipment();
  69. $archives = $data['archive_range'];
  70. $fields = json_decode($data['plan_fields'], true);
  71. $users = $data['plan_user'];
  72. $firstDuetime = str_replace(['年', '月', '日'], ['-', '-', ''], $data['first_duetime']);
  73. $lastDuetime = str_replace(['年', '月', '日'], ['-', '-', ''], $data['last_duetime']);
  74. $firstDuetime = strtotime($firstDuetime . ' 23:59:59');
  75. $lastDuetime = strtotime($lastDuetime . ' 23:59:59');
  76. if (empty($fields)) {
  77. throw new Exception(__('Parameter %s can not be empty', __('PlanField')));
  78. }
  79. if ($lastDuetime < $firstDuetime) {
  80. throw new Exception(__('The plan end date should not be earlier than the first implementation date'));
  81. }
  82. // 排查是否区间内有重叠设备
  83. $planArchiveModel = new PlanArchive();
  84. $planTaskModel = new PlanTask();
  85. $planIds = $planTaskModel->whereTime('duetime', 'between', [$firstDuetime, $lastDuetime])->column('plan_id');
  86. $planIds = array_unique($planIds);
  87. $planIds = $this->where('type', $type)->whereIn('id', $planIds)->column('id');
  88. $issetArchiveIds = $planArchiveModel->whereIn('plan_id', $planIds)->whereIn('archive_id', $archives)->column('archive_id');
  89. if (count($issetArchiveIds) > 0) {
  90. $archiveModel = new Archive();
  91. $archiveList = $archiveModel->whereIn('id', $issetArchiveIds)->column('concat("[", model, "] ", name)');
  92. $archiveList = implode('、', $archiveList);
  93. throw new Exception(__('%s Already exists in other plans', $archiveList));
  94. }
  95. // 执行添加
  96. $codings = $equipmentModel->createCoding(1, 'P');
  97. $plan = [
  98. 'coding' => $codings[0],
  99. 'name' => $data['name'],
  100. 'periodicity' => $data['periodicity'],
  101. 'first_duetime' => $firstDuetime,
  102. 'last_duetime' => $lastDuetime,
  103. 'type' => $type
  104. ];
  105. $result = $this->isUpdate(false)->data($plan)->save();
  106. if (!$result) {
  107. throw new Exception($this->getError());
  108. }
  109. $planId = $this->id;
  110. // 计划关联设备档案
  111. $planArchiveModel = new PlanArchive();
  112. $archiveResult = $planArchiveModel->addArchives($planId, $archives);
  113. if ($archiveResult != true) {
  114. throw new Exception($archiveResult);
  115. }
  116. // 计划关联巡检项
  117. $planFieldModel = new PlanField();
  118. $fieldResult = $planFieldModel->addFields($planId, $fields);
  119. if ($fieldResult != true) {
  120. throw new Exception($fieldResult);
  121. }
  122. // 计划关联巡检人员
  123. $planUserModel = new PlanUser();
  124. $userResult = $planUserModel->addUsers($planId, $users);
  125. if ($userResult != true) {
  126. throw new Exception($userResult);
  127. }
  128. // 计划关联周期任务
  129. $taskResult = $planTaskModel->addTasks($planId, $archives, $data['periodicity'], $firstDuetime, $lastDuetime);
  130. if ($taskResult != true) {
  131. throw new Exception($taskResult);
  132. }
  133. Db::commit();
  134. return true;
  135. } catch (\Exception $e) {
  136. Db::rollback();
  137. return $e->getMessage();
  138. }
  139. }
  140. public function stopPlan($ids)
  141. {
  142. Db::startTrans();
  143. try {
  144. $result = $this->destroy($ids);
  145. if (!$result) {
  146. throw new Exception($this->getError());
  147. }
  148. // 计划关联周期任务
  149. $planTaskModel = new PlanTask();
  150. $taskIds = $planTaskModel->whereIn('plan_id', $ids)->where(['status' => 'pending'])->column('id');
  151. if (count($taskIds) > 0) {
  152. $taskResult = $planTaskModel->destroy($taskIds);
  153. if ($taskResult != true) {
  154. throw new Exception($planTaskModel->getError());
  155. }
  156. }
  157. Db::commit();
  158. return true;
  159. } catch (\Exception $e) {
  160. Db::rollback();
  161. return $e->getMessage();
  162. }
  163. }
  164. }