SchoolController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. // 关系人
  21. $relations = DB::getListWithCriteria(
  22. 'school_contact',
  23. DbCriteria::simpleCompare(['school_id' => $id])->setSelect('id, name, phone, position, weixin'),
  24. );
  25. $data['relations'] = $relations['records'];
  26. // 关联食堂
  27. $canteens = DB::getListWithCriteria(
  28. 'canteen',
  29. DbCriteria::simpleCompare(['school_id' => $id])->setSelect('id, name, username, weixin, phone'),
  30. );
  31. $data['canteens'] = $canteens['records'];
  32. Helper::ok($data);
  33. }
  34. public function actionList()
  35. {
  36. $filter = ['is_del' => 0];
  37. $address = Helper::getArrParam($_POST, 'address', Helper::PARAM_KEY_TYPE['array_string']);
  38. $filter['province'] = $address[0]?? null;
  39. $filter['city'] = $address[1]?? null;
  40. $filter['area'] = $address[2]?? null;
  41. if ($name = Helper::getPostString('name')) {
  42. $filter['name'] = '%' . $name;
  43. }
  44. $is_cooperate = Helper::getPostInt('is_cooperate');
  45. if ($is_cooperate != -1) {
  46. $filter['is_cooperate'] = $is_cooperate;
  47. }
  48. $cri = DbCriteria::simpleCompareWithPage($filter)->setOrder('id desc');
  49. $data = DB::getListWithCriteria(self::$table, $cri);
  50. if (!empty($data['records'])) {
  51. $users = Helper::arrayColumn(
  52. DB::getListWithCriteria('useradmin', DbCriteria::simpleCompare([])->setSelect('id, username')),
  53. 'username',
  54. 'id'
  55. );
  56. $data['records'] = array_map(function ($item) use ($users) {
  57. $item['bind_user_name'] = $users[$item['bind_user_id']] ?? '-';
  58. return $item;
  59. }, $data['records']);
  60. }
  61. Helper::ok($data);
  62. }
  63. /**
  64. * 下拉列表获取
  65. * @return void
  66. */
  67. public function actionGetSelectList()
  68. {
  69. $cri = DbCriteria::simpleCompare([])->setSelect('id, name');
  70. $data = DB::getListWithCriteria(self::$table, $cri);
  71. Helper::ok($data['records']??[]);
  72. }
  73. public function actionDelete()
  74. {
  75. $id = Helper::getPostInt('id');
  76. if ($id < 1) {
  77. Helper::error('参数错误');
  78. }
  79. Db::updateById(self::$table, ['is_del' => 1], $id);
  80. Helper::ok();
  81. }
  82. public function actionAdd()
  83. {
  84. $this->_save();
  85. }
  86. public function actionEdit()
  87. {
  88. $id = Helper::getPostInt('id');
  89. if (!$id) {
  90. Helper::error('参数错误');
  91. }
  92. $this->_save($id);
  93. }
  94. private function _save($id = 0)
  95. {
  96. $data = [
  97. 'name' => Helper::getPostString('name'),
  98. 'address' => Helper::getPostString('address'),
  99. 'person_num' => Helper::getPostString('person_num'),
  100. 'bind_user_id' => Helper::getPostInt('bind_user_id'),
  101. 'is_eleme_in_school' => Helper::getPostInt('is_eleme_in_school'),
  102. 'is_eleme_out_school' => Helper::getPostInt('is_eleme_out_school'),
  103. 'is_meituan_in_school' => Helper::getPostInt('is_meituan_in_school'),
  104. 'is_meituan_out_school' => Helper::getPostInt('is_meituan_out_school'),
  105. 'can_go_upstairs' => Helper::getPostInt('can_go_upstairs'),
  106. 'is_cooperate' => Helper::getPostInt('is_cooperate'),
  107. 'can_ride' => Helper::getPostInt('can_ride'),
  108. 'dormitory_distribution' => Helper::getPostString('dormitory_distribution'),
  109. 'qucan_station_distribution' => Helper::getPostString('qucan_station_distribution'),
  110. 'out_business_description' => Helper::getPostString('out_business_description'),
  111. 'memo' => Helper::getPostString('memo'),
  112. ];
  113. $notNullField = ["name","address","person_num","bind_user_id","is_eleme_in_school","is_eleme_out_school"
  114. ,"is_meituan_in_school","is_meituan_out_school","can_go_upstairs","is_cooperate","can_ride"];
  115. $allowEmptyField = ["bind_user_id","is_eleme_in_school","is_eleme_out_school","is_meituan_in_school"
  116. ,"is_meituan_out_school","can_go_upstairs","is_cooperate","can_ride"];
  117. // 空字段检测
  118. if (!Helper::checkEmptyKey($data, $notNullField, $allowEmptyField)) {
  119. Helper::error('参数错误');
  120. }
  121. // 处理地区
  122. $district = Helper::getArrParam($_POST, 'distinct', Helper::PARAM_KEY_TYPE['array_string']);
  123. $district = array_filter($district);
  124. if (count($district) != 3) {
  125. Helper::error('地区参数错误');
  126. }
  127. $data['province'] = $district[0];
  128. $data['city'] = $district[1];
  129. $data['area'] = $district[2];
  130. $name = $data['name'];
  131. // 检测名称重复
  132. $cri = DbCriteria::simpleCompare(['name' => $name])->setSelect('id');
  133. if ($id > 0) {
  134. $cri->addCondition('id!=' . $id);
  135. }
  136. if ($fid = DB::getScalerWithCriteria(self::$table, $cri)) {
  137. Helper::error('学校名称已存在 ' . $fid);
  138. }
  139. if ($id) {
  140. DB::updateById(self::$table, $data, $id);
  141. } else {
  142. DB::addData(self::$table, $data);
  143. }
  144. Helper::ok();
  145. }
  146. public function actionUpdateAttr()
  147. {
  148. $id = Helper::getPostInt('id');
  149. $attr = Helper::getPostString('attr');
  150. $value = Helper::getPostString('value');
  151. if ($id <= 0 || !$attr) {
  152. Helper::error('参数错误');
  153. }
  154. if (!in_array($attr, ['is_eleme_in_school', 'person_num'])) {
  155. Helper::error('参数错误2');
  156. }
  157. if ($attr == 'is_eleme_in_school' && !in_array($value, [1, 0])) {
  158. Helper::error('参数错误3');
  159. }
  160. if (DB::updateById(self::$table, [$attr => $value], $id) === false) {
  161. Helper::error('更新失败');
  162. }
  163. Helper::ok();
  164. }
  165. }