RulesService.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace common\services;
  3. use common\components\AjaxException;
  4. use common\models\Rules;
  5. use common\util\RulesObj;
  6. use common\util\RulesWithContentObj;
  7. use yii\db\ActiveQuery;
  8. class RulesService
  9. {
  10. /**
  11. * @param $id
  12. * @return Rules
  13. * @throws AjaxException
  14. */
  15. public static function getRulesById($id): Rules
  16. {
  17. /** @var Rules $Rules */
  18. $Rules = self::getQuery()->andWhere(["id" => $id])->one();
  19. if (!$Rules) {
  20. throw new AjaxException("该规章不存在!");
  21. }
  22. return $Rules;
  23. }
  24. /**
  25. * @return ActiveQuery
  26. */
  27. public static function getQuery(): ActiveQuery
  28. {
  29. return Rules::find()->where(["delete_time" => 0]);
  30. }
  31. /**
  32. * @param $query
  33. * @return RulesObj[]
  34. */
  35. public static function getAllList($query): array
  36. {
  37. return self::dealWithList($query->select("id,rule_name,rule_content,rule_category,rule_status,create_time,end_time,update_time,version")->all());
  38. }
  39. /**
  40. * @param $query
  41. * @param $point_content
  42. * @return RulesWithContentObj
  43. */
  44. public static function getRuleWithContent($query, $point_content): RulesWithContentObj
  45. {
  46. $data = new RulesWithContentObj($query);
  47. $data->setRuleContent($point_content);
  48. return $data;
  49. }
  50. /**
  51. * @param $data
  52. * @return RulesObj[]
  53. */
  54. public static function dealWithList($data): array
  55. {
  56. $newData = [];
  57. foreach ($data as $key => $value) {
  58. /** @var Rules $value */
  59. $newData[] = new RulesObj($value);
  60. }
  61. return $newData;
  62. }
  63. }