SchoolController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. class SchoolController extends Controller
  3. {
  4. public static string $table = 'school';
  5. public function actionInfo()
  6. {
  7. $id = Helper::getPostInt('id');
  8. if ($id <= 0) {
  9. Helper::error('参数错误');
  10. }
  11. $data = DB::getInfoById(self::$table, $id);
  12. if (!$data) {
  13. Helper::error('数据不存在');
  14. }
  15. $data['distinct'] = [
  16. $data['province'],
  17. $data['city'],
  18. $data['area'],
  19. ];
  20. Helper::ok($data);
  21. }
  22. public function actionList()
  23. {
  24. $filter = ['is_del' => 0];
  25. $address = Helper::getArrParam($_POST, 'address', Helper::PARAM_KEY_TYPE['array_string']);
  26. $filter['province'] = $address[0]?? null;
  27. $filter['city'] = $address[1]?? null;
  28. $filter['area'] = $address[2]?? null;
  29. if ($name = Helper::getPostString('name')) {
  30. $filter['name'] = '%' . $name;
  31. }
  32. $is_cooperate = Helper::getPostInt('is_cooperate');
  33. if ($is_cooperate != -1) {
  34. $filter['is_cooperate'] = $is_cooperate;
  35. }
  36. $cri = DbCriteria::simpleCompareWithPage($filter);
  37. $data = DB::getListWithCriteria(self::$table, $cri);
  38. if (!empty($data['records'])) {
  39. $data['records'] = array_map(function ($item) {
  40. return $item;
  41. }, $data['records']);
  42. }
  43. Helper::ok($data);
  44. }
  45. /**
  46. * 下拉列表获取
  47. * @return void
  48. */
  49. public function actionGetSelectList()
  50. {
  51. $cri = DbCriteria::simpleCompare([])->setSelect('id, name');
  52. $data = DB::getListWithCriteria(self::$table, $cri);
  53. Helper::ok($data['records']??[]);
  54. }
  55. public function actionDelete()
  56. {
  57. $id = Helper::getPostInt('id');
  58. if ($id < 1) {
  59. Helper::error('参数错误');
  60. }
  61. Db::updateById(self::$table, ['is_del' => 1], $id);
  62. Helper::ok();
  63. }
  64. public function actionAdd()
  65. {
  66. $this->_save();
  67. }
  68. public function actionEdit()
  69. {
  70. $id = Helper::getPostInt('id');
  71. if (!$id) {
  72. Helper::error('参数错误');
  73. }
  74. $this->_save($id);
  75. }
  76. private function _save($id = 0)
  77. {
  78. $data = [
  79. 'name' => Helper::getPostString('name'),
  80. 'address' => Helper::getPostString('address'),
  81. 'person_num' => Helper::getPostString('person_num'),
  82. 'bind_user_id' => Helper::getPostInt('bind_user_id'),
  83. 'is_eleme_in_school' => Helper::getPostInt('is_eleme_in_school'),
  84. 'is_eleme_out_school' => Helper::getPostInt('is_eleme_out_school'),
  85. 'is_meituan_in_school' => Helper::getPostInt('is_meituan_in_school'),
  86. 'is_meituan_out_school' => Helper::getPostInt('is_meituan_out_school'),
  87. 'can_go_upstairs' => Helper::getPostInt('can_go_upstairs'),
  88. 'is_cooperate' => Helper::getPostInt('is_cooperate'),
  89. 'can_ride' => Helper::getPostInt('can_ride'),
  90. 'dormitory_distribution' => Helper::getPostString('dormitory_distribution'),
  91. 'qucan_station_distribution' => Helper::getPostString('qucan_station_distribution'),
  92. 'out_business_description' => Helper::getPostString('out_business_description'),
  93. 'memo' => Helper::getPostString('memo'),
  94. ];
  95. $notNullField = ["name","address","person_num","bind_user_id","is_eleme_in_school","is_eleme_out_school"
  96. ,"is_meituan_in_school","is_meituan_out_school","can_go_upstairs","is_cooperate","can_ride"];
  97. $allowEmptyField = ["bind_user_id","is_eleme_in_school","is_eleme_out_school","is_meituan_in_school"
  98. ,"is_meituan_out_school","can_go_upstairs","is_cooperate","can_ride"];
  99. // 空字段检测
  100. if (!Helper::checkEmptyKey($data, $notNullField, $allowEmptyField)) {
  101. Helper::error('参数错误');
  102. }
  103. // 处理地区
  104. $district = Helper::getArrParam($_POST, 'distinct', Helper::PARAM_KEY_TYPE['array_string']);
  105. $district = array_filter($district);
  106. if (count($district) != 3) {
  107. Helper::error('地区参数错误');
  108. }
  109. $data['province'] = $district[0];
  110. $data['city'] = $district[1];
  111. $data['area'] = $district[2];
  112. $name = $data['name'];
  113. // 检测名称重复
  114. $cri = DbCriteria::simpleCompare(['name' => $name])->setSelect('id');
  115. if ($id > 0) {
  116. $cri->addCondition('id!=' . $id);
  117. }
  118. if ($fid = DB::getScalerWithCriteria(self::$table, $cri)) {
  119. Helper::error('学校名称已存在 ' . $fid);
  120. }
  121. if ($id) {
  122. DB::updateById(self::$table, $data, $id);
  123. } else {
  124. DB::addData(self::$table, $data);
  125. }
  126. Helper::ok();
  127. }
  128. public function actionUpdateAttr()
  129. {
  130. $id = Helper::getPostInt('id');
  131. $attr = Helper::getPostString('attr');
  132. $value = Helper::getPostString('value');
  133. if ($id <= 0 || !$attr) {
  134. Helper::error('参数错误');
  135. }
  136. if (!in_array($attr, ['is_eleme_in_school', 'person_num'])) {
  137. Helper::error('参数错误2');
  138. }
  139. if ($attr == 'is_eleme_in_school' && !in_array($value, [1, 0])) {
  140. Helper::error('参数错误3');
  141. }
  142. if (DB::updateById(self::$table, [$attr => $value], $id) === false) {
  143. Helper::error('更新失败');
  144. }
  145. Helper::ok();
  146. }
  147. }