SchoolRelationController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. if ($last_user_id = Helper::getPostString('last_user_id')) {
  40. $filter['r.last_user_id'] = $last_user_id;
  41. }
  42. $cri = DbCriteria::simpleCompareWithPage($filter)
  43. ->setAlias('r')
  44. ->setSelect('r.*, s.name as school_name, group_concat(sf.id) AS follow_ids')
  45. ->setJoin('LEFT JOIN wx_school s ON s.id=r.school_id')
  46. ->addJoin('LEFT JOIN wx_school_follow AS sf ON sf.contact_id = r.id')
  47. ->setGroup('r.id')
  48. ->setOrder('r.id desc');
  49. if ($date = Helper::getPostDate('date')) {
  50. $cri->addBetweenCondition('r.create_date', $date, $date . ' 23:59:59');
  51. }
  52. $last_date = $_POST['last_date'];
  53. if (is_array($last_date) && count($last_date) == 2 && $last_date[0] < $last_date[1]) {
  54. $cri->addcondition("r.last_date > '{$last_date[0]}' and r.last_date < '{$last_date[1]}'");
  55. }
  56. $data = DB::getListWithCriteria($this->table, $cri);
  57. if (!empty($data['records'])) {
  58. $users = Helper::arrayColumn(
  59. DB::getListWithCriteria('useradmin', DbCriteria::simpleCompare([])->setSelect('id, username,avatar')),
  60. null,
  61. 'id'
  62. );
  63. $data['records'] = FollowSrv::formatWithFollowList($data['records'], 'wx_school_follow', $users);
  64. $data['records'] = array_map(function ($item) use ($users) {
  65. $item['last_user_name'] = $users[$item['last_user_id']]['username'] ?? '-';
  66. return $item;
  67. }, $data['records']);
  68. }
  69. Helper::ok($data);
  70. }
  71. public function actionDelete()
  72. {
  73. $id = Helper::getPostInt('id');
  74. if ($id < 1) {
  75. Helper::error('参数错误');
  76. }
  77. $data = DB::getInfoById($this->table, $id);
  78. if (!$data || !$this->checkSchoolId($data['school_id'])) {
  79. Helper::error('您没有权限操作此数据');
  80. }
  81. Db::updateById($this->table, ['is_del' => 1], $id);
  82. Helper::ok();
  83. }
  84. public function actionAdd()
  85. {
  86. $this->_save();
  87. }
  88. public function actionEdit()
  89. {
  90. $id = Helper::getPostInt('id');
  91. if (!$id) {
  92. Helper::error('参数错误');
  93. }
  94. $data = DB::getInfoById($this->table, $id);
  95. if (!$data || !$this->checkSchoolId($data['school_id'])) {
  96. Helper::error('您没有权限操作此数据');
  97. }
  98. $this->_save($id);
  99. }
  100. private function _save($id = 0)
  101. {
  102. $data = [
  103. 'name' => Helper::getPostString('name'),
  104. 'school_id' => Helper::getPostInt('school_id'),
  105. 'phone' => Helper::getPostString('phone'),
  106. 'weixin' => Helper::getPostString('weixin'),
  107. 'position' => Helper::getPostString('position'),
  108. 'memo' => Helper::getPostString('memo'),
  109. ];
  110. $notNullField = ["name", "school_id", "phone", "weixin", "position"];
  111. $allowEmptyField = ['weixin'];
  112. // 空字段检测
  113. if (!Helper::checkEmptyKey($data, $notNullField, $allowEmptyField)) {
  114. Helper::error('参数错误');
  115. }
  116. $this->dobuleCheck();
  117. if ($id) {
  118. DB::updateById($this->table, $data, $id);
  119. } else {
  120. DB::addData($this->table, $data);
  121. }
  122. Helper::ok();
  123. }
  124. public function actionUpdateAttr()
  125. {
  126. $id = Helper::getPostInt('id');
  127. $attr = Helper::getPostString('attr');
  128. $value = Helper::getPostString('value');
  129. if ($id <= 0 || !$attr) {
  130. Helper::error('参数错误');
  131. }
  132. $data = DB::getInfoById($this->table, $id);
  133. if (!$data || !$this->checkSchoolId($data['school_id'])) {
  134. Helper::error('您没有权限操作此数据');
  135. }
  136. if (!in_array($attr, [])) {
  137. Helper::error('参数错误2');
  138. }
  139. if (DB::updateById($this->table, [$attr => $value], $id) === false) {
  140. Helper::error('更新失败');
  141. }
  142. Helper::ok();
  143. }
  144. }