SchoolController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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);
  49. $data = DB::getListWithCriteria(self::$table, $cri);
  50. if (!empty($data['records'])) {
  51. $data['records'] = array_map(function ($item) {
  52. return $item;
  53. }, $data['records']);
  54. }
  55. Helper::ok($data);
  56. }
  57. /**
  58. * 下拉列表获取
  59. * @return void
  60. */
  61. public function actionGetSelectList()
  62. {
  63. $cri = DbCriteria::simpleCompare([])->setSelect('id, name');
  64. $data = DB::getListWithCriteria(self::$table, $cri);
  65. Helper::ok($data['records']??[]);
  66. }
  67. public function actionDelete()
  68. {
  69. $id = Helper::getPostInt('id');
  70. if ($id < 1) {
  71. Helper::error('参数错误');
  72. }
  73. Db::updateById(self::$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. 'address' => Helper::getPostString('address'),
  93. 'person_num' => Helper::getPostString('person_num'),
  94. 'bind_user_id' => Helper::getPostInt('bind_user_id'),
  95. 'is_eleme_in_school' => Helper::getPostInt('is_eleme_in_school'),
  96. 'is_eleme_out_school' => Helper::getPostInt('is_eleme_out_school'),
  97. 'is_meituan_in_school' => Helper::getPostInt('is_meituan_in_school'),
  98. 'is_meituan_out_school' => Helper::getPostInt('is_meituan_out_school'),
  99. 'can_go_upstairs' => Helper::getPostInt('can_go_upstairs'),
  100. 'is_cooperate' => Helper::getPostInt('is_cooperate'),
  101. 'can_ride' => Helper::getPostInt('can_ride'),
  102. 'dormitory_distribution' => Helper::getPostString('dormitory_distribution'),
  103. 'qucan_station_distribution' => Helper::getPostString('qucan_station_distribution'),
  104. 'out_business_description' => Helper::getPostString('out_business_description'),
  105. 'memo' => Helper::getPostString('memo'),
  106. ];
  107. $notNullField = ["name","address","person_num","bind_user_id","is_eleme_in_school","is_eleme_out_school"
  108. ,"is_meituan_in_school","is_meituan_out_school","can_go_upstairs","is_cooperate","can_ride"];
  109. $allowEmptyField = ["bind_user_id","is_eleme_in_school","is_eleme_out_school","is_meituan_in_school"
  110. ,"is_meituan_out_school","can_go_upstairs","is_cooperate","can_ride"];
  111. // 空字段检测
  112. if (!Helper::checkEmptyKey($data, $notNullField, $allowEmptyField)) {
  113. Helper::error('参数错误');
  114. }
  115. // 处理地区
  116. $district = Helper::getArrParam($_POST, 'distinct', Helper::PARAM_KEY_TYPE['array_string']);
  117. $district = array_filter($district);
  118. if (count($district) != 3) {
  119. Helper::error('地区参数错误');
  120. }
  121. $data['province'] = $district[0];
  122. $data['city'] = $district[1];
  123. $data['area'] = $district[2];
  124. $name = $data['name'];
  125. // 检测名称重复
  126. $cri = DbCriteria::simpleCompare(['name' => $name])->setSelect('id');
  127. if ($id > 0) {
  128. $cri->addCondition('id!=' . $id);
  129. }
  130. if ($fid = DB::getScalerWithCriteria(self::$table, $cri)) {
  131. Helper::error('学校名称已存在 ' . $fid);
  132. }
  133. if ($id) {
  134. DB::updateById(self::$table, $data, $id);
  135. } else {
  136. DB::addData(self::$table, $data);
  137. }
  138. Helper::ok();
  139. }
  140. public function actionUpdateAttr()
  141. {
  142. $id = Helper::getPostInt('id');
  143. $attr = Helper::getPostString('attr');
  144. $value = Helper::getPostString('value');
  145. if ($id <= 0 || !$attr) {
  146. Helper::error('参数错误');
  147. }
  148. if (!in_array($attr, ['is_eleme_in_school', 'person_num'])) {
  149. Helper::error('参数错误2');
  150. }
  151. if ($attr == 'is_eleme_in_school' && !in_array($value, [1, 0])) {
  152. Helper::error('参数错误3');
  153. }
  154. if (DB::updateById(self::$table, [$attr => $value], $id) === false) {
  155. Helper::error('更新失败');
  156. }
  157. Helper::ok();
  158. }
  159. }