SchoolRelationController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. $data = DB::getListWithCriteria($this->table, $cri);
  32. if (!empty($data['records'])) {
  33. $data['records'] = array_map(function ($item) {
  34. return $item;
  35. }, $data['records']);
  36. }
  37. Helper::ok($data);
  38. }
  39. /**
  40. * 下拉列表获取
  41. * @return void
  42. */
  43. public function actionGetSelectList()
  44. {
  45. $cri = DbCriteria::simpleCompare([])->setSelect('id, name');
  46. $schools = Helper::arrayColumn(DB::getListWithCriteria(SchoolController::$table, $cri), null, 'id');
  47. if (empty($schools)) {
  48. Helper::ok([]);
  49. }
  50. $relations = DB::getListWithCriteria($this->table, $cri);
  51. foreach ($relations['records'] as $relation) {
  52. $sid = $relation['school_id'];
  53. if (!isset($schools[$sid])) {
  54. continue;
  55. }
  56. if (!isset($schools[$sid]['children'])) {
  57. $schools[$sid]['children'] = [];
  58. }
  59. $schools[$sid]['children'][] = [
  60. 'id' => $relation['id'],
  61. 'name' => $relation['name'],
  62. ];
  63. }
  64. Helper::ok($schools);
  65. }
  66. public function actionDelete()
  67. {
  68. $id = Helper::getPostInt('id');
  69. if ($id < 1) {
  70. Helper::error('参数错误');
  71. }
  72. Db::updateById($this->table, ['is_del' => 1], $id);
  73. Helper::ok();
  74. }
  75. public function actionAdd()
  76. {
  77. $this->_save();
  78. }
  79. public function actionEdit()
  80. {
  81. $id = Helper::getPostInt('id');
  82. if (!$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. if (!in_array($attr, [])) {
  119. Helper::error('参数错误2');
  120. }
  121. if (DB::updateById($this->table, [$attr => $value], $id) === false) {
  122. Helper::error('更新失败');
  123. }
  124. Helper::ok();
  125. }
  126. }