EventSourceDataService.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace common\services;
  3. use common\components\AjaxException;
  4. use common\models\EventSourceData;
  5. use Yii;
  6. use yii\db\ActiveQuery;
  7. use yii\db\ActiveRecord;
  8. class EventSourceDataService
  9. {
  10. public static $sourceDataListByAccidentIdCacheKey = "sourceDataListByAccidentId_";
  11. /**
  12. * @param $id
  13. * @return EventSourceData
  14. * @throws AjaxException
  15. */
  16. public static function getEventSourceDataById($id): EventSourceData
  17. {
  18. /** @var EventSourceData $EventOverview */
  19. $EventOverview = self::getQuery()->andWhere(["id" => $id])->one();
  20. if (!$EventOverview) {
  21. throw new AjaxException("该资料不存在!");
  22. }
  23. return $EventOverview;
  24. }
  25. /**
  26. * @return ActiveQuery
  27. */
  28. public static function getQuery(): ActiveQuery
  29. {
  30. return EventSourceData::find()->where(["delete_time" => 0]);
  31. }
  32. public static function getQueryByAccidentId($accidentId): ActiveQuery
  33. {
  34. return self::getQuery()->andWhere(["accident_id" => $accidentId]);
  35. }
  36. /**
  37. * @param $accidentId
  38. * @return ActiveRecord[]
  39. */
  40. public static function getSourceDataListByAccidentId($accidentId): array
  41. {
  42. $cache = Yii::$app->cache;
  43. return $cache->getOrSet(self::$sourceDataListByAccidentIdCacheKey . $accidentId, function () use ($accidentId) {
  44. return EventSourceDataService::getQueryByAccidentId($accidentId)->all();
  45. }, 1000);
  46. }
  47. public static function clearSourceDataListByAccidentIdCacheData($accidentId)
  48. {
  49. Yii::$app->cache->delete(self::$sourceDataListByAccidentIdCacheKey . $accidentId);
  50. }
  51. /**
  52. * 通过案例ID删除应急处置
  53. * @param $accident_id
  54. */
  55. public static function deleteByAccidentCasesId($accident_id): void
  56. {
  57. EventSourceData::updateAll(["delete_time" => time()], ["accident_id" => $accident_id]);
  58. //删除缓存
  59. self::clearSourceDataListByAccidentIdCacheData($accident_id);
  60. }
  61. }