SchoolRelationController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. Helper::ok($data);
  16. }
  17. public function actionList()
  18. {
  19. $filter = [
  20. 'r.is_del' => 0,
  21. 'r.phone' => Helper::getPostString('phone'),
  22. 'r.school_id' => Helper::getPostInt('school_id') ?: null,
  23. ];
  24. if ($name = Helper::getPostString('name')) {
  25. $filter['r.name'] = '%'.$name;
  26. }
  27. $cri = DbCriteria::simpleCompareWithPage($filter)
  28. ->setAlias('r')
  29. ->setSelect('r.*, s.name as school_name')
  30. ->setJoin('LEFT JOIN wx_school s ON s.id=r.school_id')
  31. ->setOrder('r.id desc');
  32. $data = DB::getListWithCriteria($this->table, $cri);
  33. if (!empty($data['records'])) {
  34. $users = Helper::arrayColumn(
  35. DB::getListWithCriteria('useradmin', DbCriteria::simpleCompare([])->setSelect('id, username')),
  36. 'username',
  37. 'id'
  38. );
  39. $data['records'] = array_map(function ($item) use ($users) {
  40. $item['last_user_name'] = $users[$item['last_user_id']] ?? '-';
  41. return $item;
  42. }, $data['records']);
  43. }
  44. Helper::ok($data);
  45. }
  46. /**
  47. * 下拉列表获取
  48. * @return void
  49. */
  50. public function actionGetSelectList()
  51. {
  52. $cri = DbCriteria::simpleCompare(['is_del' => 0])->setSelect('id, name');
  53. $schools = Helper::arrayColumn(DB::getListWithCriteria('school', $cri), null, 'id');
  54. if (empty($schools)) {
  55. Helper::ok();
  56. }
  57. $cri1 = DbCriteria::simpleCompare(['is_del' => 0])->setSelect('id, name, school_id');
  58. $relations = DB::getListWithCriteria($this->table, $cri1);
  59. foreach ($relations['records'] as $relation) {
  60. $sid = $relation['school_id'];
  61. if (!isset($schools[$sid])) {
  62. continue;
  63. }
  64. if (!isset($schools[$sid]['children'])) {
  65. $schools[$sid]['children'] = [];
  66. }
  67. $schools[$sid]['children'][] = [
  68. 'id' => $relation['id'],
  69. 'name' => $relation['name'],
  70. ];
  71. }
  72. Helper::ok(array_values($schools));
  73. }
  74. public function actionDelete()
  75. {
  76. $id = Helper::getPostInt('id');
  77. if ($id < 1) {
  78. Helper::error('参数错误');
  79. }
  80. Db::updateById($this->table, ['is_del' => 1], $id);
  81. Helper::ok();
  82. }
  83. public function actionAdd()
  84. {
  85. $this->_save();
  86. }
  87. public function actionEdit()
  88. {
  89. $id = Helper::getPostInt('id');
  90. if (!$id) {
  91. Helper::error('参数错误');
  92. }
  93. $this->_save($id);
  94. }
  95. private function _save($id = 0)
  96. {
  97. $data = [
  98. 'name' => Helper::getPostString('name'),
  99. 'school_id' => Helper::getPostInt('school_id'),
  100. 'phone' => Helper::getPostString('phone'),
  101. 'weixin' => Helper::getPostString('weixin'),
  102. 'position' => Helper::getPostString('position'),
  103. 'memo' => Helper::getPostString('memo'),
  104. ];
  105. $notNullField = ["name", "school_id", "phone", "weixin", "position"];
  106. $allowEmptyField = [];
  107. // 空字段检测
  108. if (!Helper::checkEmptyKey($data, $notNullField, $allowEmptyField)) {
  109. Helper::error('参数错误');
  110. }
  111. if ($id) {
  112. DB::updateById($this->table, $data, $id);
  113. } else {
  114. DB::addData($this->table, $data);
  115. }
  116. Helper::ok();
  117. }
  118. public function actionUpdateAttr()
  119. {
  120. $id = Helper::getPostInt('id');
  121. $attr = Helper::getPostString('attr');
  122. $value = Helper::getPostString('value');
  123. if ($id <= 0 || !$attr) {
  124. Helper::error('参数错误');
  125. }
  126. if (!in_array($attr, [])) {
  127. Helper::error('参数错误2');
  128. }
  129. if (DB::updateById($this->table, [$attr => $value], $id) === false) {
  130. Helper::error('更新失败');
  131. }
  132. Helper::ok();
  133. }
  134. }