AccidentCasesFocusController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace frontend\modules\api\controllers;
  3. use common\components\AjaxException;
  4. use common\models\AccidentCasesFocus;
  5. use common\services\CaseService;
  6. use frontend\modules\api\components\BaseAdminController;
  7. use yii\db\Exception;
  8. use yii\web\Response;
  9. class AccidentCasesFocusController extends BaseAdminController
  10. {
  11. /**
  12. * 关注列表
  13. * @return Response
  14. */
  15. public function actionList()
  16. {
  17. $list = [];
  18. $AccidentCasesFocus = AccidentCasesFocus::findOne(["uid" => $this->uid]);
  19. if ($AccidentCasesFocus) {
  20. $accident_cases_id_list = json_decode($AccidentCasesFocus->accident_cases_id_list);
  21. $AccidentCasesList = CaseService::getCaseShowQuery()->andWhere(["id" => $accident_cases_id_list])->all();
  22. if (count($accident_cases_id_list) != count($AccidentCasesList)) {
  23. $accident_cases_id_list = array_column($AccidentCasesList, "id");
  24. $AccidentCasesFocus->accident_cases_id_list = json_encode($accident_cases_id_list);
  25. $AccidentCasesFocus->save();
  26. }
  27. $list = CaseService::dealWithFocusList($AccidentCasesList);
  28. }
  29. $data["data"] = $list;
  30. return $this->asJson($data);
  31. }
  32. /**
  33. * 添加关注
  34. * @param $id
  35. * @return Response
  36. * @throws AjaxException
  37. * @throws Exception
  38. */
  39. public function actionAdd($id)
  40. {
  41. CaseService::getCaseById($id);
  42. $maxCount = 10;
  43. $AccidentCasesFocus = AccidentCasesFocus::findOne(["uid" => $this->uid]);
  44. if (!$AccidentCasesFocus) {
  45. $AccidentCasesFocus = new AccidentCasesFocus();
  46. $AccidentCasesFocus->uid = $this->userInfo->id;
  47. $AccidentCasesFocus->accident_cases_id_list = json_encode([$id]);
  48. } else {
  49. $accident_cases_id_list = json_decode($AccidentCasesFocus->accident_cases_id_list, true);
  50. array_unshift($accident_cases_id_list, intval($id));
  51. $accident_cases_id_list = array_values(array_unique($accident_cases_id_list));
  52. if (count($accident_cases_id_list) > $maxCount) {
  53. throw new AjaxException("关注列表最大数量为" . $maxCount . "条!");
  54. }
  55. $AccidentCasesFocus->accident_cases_id_list = json_encode($accident_cases_id_list);
  56. }
  57. if (!$AccidentCasesFocus->save()) {
  58. throw new Exception($AccidentCasesFocus->getErrorSummary(true)[0]);
  59. }
  60. return $this->asJson([]);
  61. }
  62. /**
  63. * 取消关注
  64. * @param $id
  65. * @return Response
  66. * @throws Exception
  67. */
  68. public function actionCancel($id)
  69. {
  70. $AccidentCasesFocus = AccidentCasesFocus::findOne(["uid" => $this->uid]);
  71. if ($AccidentCasesFocus) {
  72. $accident_cases_id_list = json_decode($AccidentCasesFocus->accident_cases_id_list);
  73. $newList = [];
  74. foreach ($accident_cases_id_list as $accident_cases_id) {
  75. if ($accident_cases_id != $id) {
  76. $newList[] = $accident_cases_id;
  77. }
  78. }
  79. $AccidentCasesFocus->accident_cases_id_list = json_encode($newList);
  80. if (!$AccidentCasesFocus->save()) {
  81. throw new Exception($AccidentCasesFocus->getErrorSummary(true)[0]);
  82. }
  83. }
  84. return $this->asJson([]);
  85. }
  86. }