SchoolRelationController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. class SchoolRelationController extends Controller
  3. {
  4. public string $table = 'school_contact';
  5. public function actionInfo()
  6. {
  7. $id = Helper::getPostInt('id');
  8. if ($id <= 0) {
  9. Helper::error('参数错误');
  10. }
  11. $data = DB::getInfoById($this->table, $id);
  12. if (!$data) {
  13. Helper::error('数据不存在');
  14. }
  15. if (!$this->checkSchoolId($data['school_id'])) {
  16. Helper::error('您没有权限操作此数据');
  17. }
  18. Helper::ok($data);
  19. }
  20. public function actionList()
  21. {
  22. $filter = [
  23. 'r.is_del' => 0,
  24. 'r.phone' => Helper::getPostString('phone'),
  25. ];
  26. $schoolId = Helper::getPostString('school_id');
  27. if ($schoolId) {
  28. if (!$this->checkSchoolId($schoolId)) {
  29. $filter['r.school_id'] = -1;
  30. } else {
  31. $filter['r.school_id'] = $schoolId;
  32. }
  33. } else {
  34. $filter['r.school_id'] = $this->getSchoolFilter();
  35. }
  36. if ($name = Helper::getPostString('name')) {
  37. $filter['r.name'] = '%'.$name;
  38. }
  39. $cri = DbCriteria::simpleCompareWithPage($filter)
  40. ->setAlias('r')
  41. ->setSelect('r.*, s.name as school_name')
  42. ->setJoin('LEFT JOIN wx_school s ON s.id=r.school_id')
  43. ->setOrder('r.id desc');
  44. $data = DB::getListWithCriteria($this->table, $cri);
  45. if (!empty($data['records'])) {
  46. $users = Helper::arrayColumn(
  47. DB::getListWithCriteria('useradmin', DbCriteria::simpleCompare([])->setSelect('id, username')),
  48. 'username',
  49. 'id'
  50. );
  51. $data['records'] = array_map(function ($item) use ($users) {
  52. $item['last_user_name'] = $users[$item['last_user_id']] ?? '-';
  53. return $item;
  54. }, $data['records']);
  55. }
  56. Helper::ok($data);
  57. }
  58. public function actionDelete()
  59. {
  60. $id = Helper::getPostInt('id');
  61. if ($id < 1) {
  62. Helper::error('参数错误');
  63. }
  64. $data = DB::getInfoById($this->table, $id);
  65. if (!$data || !$this->checkSchoolId($data['school_id'])) {
  66. Helper::error('您没有权限操作此数据');
  67. }
  68. Db::updateById($this->table, ['is_del' => 1], $id);
  69. Helper::ok();
  70. }
  71. public function actionAdd()
  72. {
  73. $this->_save();
  74. }
  75. public function actionEdit()
  76. {
  77. $id = Helper::getPostInt('id');
  78. if (!$id) {
  79. Helper::error('参数错误');
  80. }
  81. $data = DB::getInfoById($this->table, $id);
  82. if (!$data || !$this->checkSchoolId($data['school_id'])) {
  83. Helper::error('您没有权限操作此数据');
  84. }
  85. $this->_save($id);
  86. }
  87. private function _save($id = 0)
  88. {
  89. $data = [
  90. 'name' => Helper::getPostString('name'),
  91. 'school_id' => Helper::getPostInt('school_id'),
  92. 'phone' => Helper::getPostString('phone'),
  93. 'weixin' => Helper::getPostString('weixin'),
  94. 'position' => Helper::getPostString('position'),
  95. 'memo' => Helper::getPostString('memo'),
  96. ];
  97. $notNullField = ["name", "school_id", "phone", "weixin", "position"];
  98. $allowEmptyField = [];
  99. // 空字段检测
  100. if (!Helper::checkEmptyKey($data, $notNullField, $allowEmptyField)) {
  101. Helper::error('参数错误');
  102. }
  103. if ($id) {
  104. DB::updateById($this->table, $data, $id);
  105. } else {
  106. DB::addData($this->table, $data);
  107. }
  108. Helper::ok();
  109. }
  110. public function actionUpdateAttr()
  111. {
  112. $id = Helper::getPostInt('id');
  113. $attr = Helper::getPostString('attr');
  114. $value = Helper::getPostString('value');
  115. if ($id <= 0 || !$attr) {
  116. Helper::error('参数错误');
  117. }
  118. $data = DB::getInfoById($this->table, $id);
  119. if (!$data || !$this->checkSchoolId($data['school_id'])) {
  120. Helper::error('您没有权限操作此数据');
  121. }
  122. if (!in_array($attr, [])) {
  123. Helper::error('参数错误2');
  124. }
  125. if (DB::updateById($this->table, [$attr => $value], $id) === false) {
  126. Helper::error('更新失败');
  127. }
  128. Helper::ok();
  129. }
  130. }