SchoolRelationController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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(['is_del' => 0])->setSelect('id, name');
  46. $schools = Helper::arrayColumn(DB::getListWithCriteria('school', $cri), null, 'id');
  47. if (empty($schools)) {
  48. Helper::ok();
  49. }
  50. $cri1 = DbCriteria::simpleCompare(['is_del' => 0])->setSelect('id, name, school_id');
  51. $relations = DB::getListWithCriteria($this->table, $cri1);
  52. foreach ($relations['records'] as $relation) {
  53. $sid = $relation['school_id'];
  54. if (!isset($schools[$sid])) {
  55. continue;
  56. }
  57. if (!isset($schools[$sid]['children'])) {
  58. $schools[$sid]['children'] = [];
  59. }
  60. $schools[$sid]['children'][] = [
  61. 'id' => $relation['id'],
  62. 'name' => $relation['name'],
  63. ];
  64. }
  65. Helper::ok(array_values($schools));
  66. }
  67. public function actionDelete()
  68. {
  69. $id = Helper::getPostInt('id');
  70. if ($id < 1) {
  71. Helper::error('参数错误');
  72. }
  73. Db::updateById($this->table, ['is_del' => 1], $id);
  74. Helper::ok();
  75. }
  76. public function actionAdd()
  77. {
  78. $this->_save();
  79. }
  80. public function actionEdit()
  81. {
  82. $id = Helper::getPostInt('id');
  83. if (!$id) {
  84. Helper::error('参数错误');
  85. }
  86. $this->_save($id);
  87. }
  88. private function _save($id = 0)
  89. {
  90. $data = [
  91. 'name' => Helper::getPostString('name'),
  92. 'school_id' => Helper::getPostInt('school_id'),
  93. 'phone' => Helper::getPostString('phone'),
  94. 'weixin' => Helper::getPostString('weixin'),
  95. 'position' => Helper::getPostString('position'),
  96. 'memo' => Helper::getPostString('memo'),
  97. ];
  98. $notNullField = ["name", "school_id", "phone", "weixin", "position"];
  99. $allowEmptyField = [];
  100. // 空字段检测
  101. if (!Helper::checkEmptyKey($data, $notNullField, $allowEmptyField)) {
  102. Helper::error('参数错误');
  103. }
  104. if ($id) {
  105. DB::updateById($this->table, $data, $id);
  106. } else {
  107. DB::addData($this->table, $data);
  108. }
  109. Helper::ok();
  110. }
  111. public function actionUpdateAttr()
  112. {
  113. $id = Helper::getPostInt('id');
  114. $attr = Helper::getPostString('attr');
  115. $value = Helper::getPostString('value');
  116. if ($id <= 0 || !$attr) {
  117. Helper::error('参数错误');
  118. }
  119. if (!in_array($attr, [])) {
  120. Helper::error('参数错误2');
  121. }
  122. if (DB::updateById($this->table, [$attr => $value], $id) === false) {
  123. Helper::error('更新失败');
  124. }
  125. Helper::ok();
  126. }
  127. }