SchoolController.php 5.4 KB

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