SchoolController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. if (!$this->checkSchoolId($id)) {
  12. Helper::error('您没有权限操作此数据');
  13. }
  14. $data = DB::getInfoById(self::$table, $id);
  15. if (!$data) {
  16. Helper::error('数据不存在');
  17. }
  18. $data['distinct'] = [
  19. $data['province'],
  20. $data['city'],
  21. $data['area'],
  22. ];
  23. // 关系人
  24. $relations = DB::getListWithCriteria(
  25. 'school_contact',
  26. DbCriteria::simpleCompare(['school_id' => $id])->setSelect('id, name, phone, position, weixin'),
  27. );
  28. $data['relations'] = $relations['records'];
  29. // 关联食堂
  30. $canteens = DB::getListWithCriteria(
  31. 'canteen',
  32. DbCriteria::simpleCompare(['school_id' => $id])->setSelect('id, name, username, weixin, phone'),
  33. );
  34. $data['canteens'] = $canteens['records'];
  35. Helper::ok($data);
  36. }
  37. public function actionList()
  38. {
  39. $filter['is_del'] = 0;
  40. $filter = [
  41. 'is_del' => 0,
  42. 'id' => $this->getSchoolFilter()
  43. ];
  44. $address = Helper::getArrParam($_POST, 'address', Helper::PARAM_KEY_TYPE['array_string']);
  45. $filter['province'] = $address[0]?? null;
  46. $filter['city'] = $address[1]?? null;
  47. $filter['area'] = $address[2]?? null;
  48. if ($name = Helper::getPostString('name')) {
  49. $filter['name'] = '%' . $name;
  50. }
  51. $is_cooperate = Helper::getPostInt('is_cooperate');
  52. if ($is_cooperate != -1) {
  53. $filter['is_cooperate'] = $is_cooperate;
  54. }
  55. $cri = DbCriteria::simpleCompareWithPage($filter)->setOrder('id desc');
  56. $data = DB::getListWithCriteria(self::$table, $cri);
  57. if (!empty($data['records'])) {
  58. $users = Helper::arrayColumn(
  59. DB::getListWithCriteria('useradmin', DbCriteria::simpleCompare([])->setSelect('id, username')),
  60. 'username',
  61. 'id'
  62. );
  63. $data['records'] = array_map(function ($item) use ($users) {
  64. $item['bind_user_name'] = $users[$item['bind_user_id']] ?? '-';
  65. return $item;
  66. }, $data['records']);
  67. }
  68. Helper::ok($data);
  69. }
  70. public function actionDelete()
  71. {
  72. $id = Helper::getPostInt('id');
  73. if ($id < 1) {
  74. Helper::error('参数错误');
  75. }
  76. if (!$this->checkSchoolId($id)) {
  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. if (!$this->checkSchoolId($id)) {
  93. Helper::error('您没有权限操作此数据');
  94. }
  95. $this->_save($id);
  96. }
  97. private function _save($id = 0)
  98. {
  99. $data = [
  100. 'name' => Helper::getPostString('name'),
  101. 'address' => Helper::getPostString('address'),
  102. 'person_num' => Helper::getPostString('person_num'),
  103. 'bind_user_id' => Helper::getPostInt('bind_user_id'),
  104. 'is_eleme_in_school' => Helper::getPostInt('is_eleme_in_school'),
  105. 'is_eleme_out_school' => Helper::getPostInt('is_eleme_out_school'),
  106. 'is_meituan_in_school' => Helper::getPostInt('is_meituan_in_school'),
  107. 'is_meituan_out_school' => Helper::getPostInt('is_meituan_out_school'),
  108. 'can_go_upstairs' => Helper::getPostInt('can_go_upstairs'),
  109. 'is_cooperate' => Helper::getPostInt('is_cooperate'),
  110. 'can_ride' => Helper::getPostInt('can_ride'),
  111. 'dormitory_distribution' => Helper::getPostString('dormitory_distribution'),
  112. 'qucan_station_distribution' => Helper::getPostString('qucan_station_distribution'),
  113. 'out_business_description' => Helper::getPostString('out_business_description'),
  114. 'memo' => Helper::getPostString('memo'),
  115. ];
  116. $notNullField = ["name","address","person_num","bind_user_id","is_eleme_in_school","is_eleme_out_school"
  117. ,"is_meituan_in_school","is_meituan_out_school","can_go_upstairs","is_cooperate","can_ride"];
  118. $allowEmptyField = ["bind_user_id","is_eleme_in_school","is_eleme_out_school","is_meituan_in_school"
  119. ,"is_meituan_out_school","can_go_upstairs","is_cooperate","can_ride"];
  120. // 空字段检测
  121. if (!Helper::checkEmptyKey($data, $notNullField, $allowEmptyField)) {
  122. Helper::error('参数错误');
  123. }
  124. // 处理地区
  125. $district = Helper::getArrParam($_POST, 'distinct', Helper::PARAM_KEY_TYPE['array_string']);
  126. $district = array_filter($district);
  127. if (count($district) != 3) {
  128. Helper::error('地区参数错误');
  129. }
  130. $data['province'] = $district[0];
  131. $data['city'] = $district[1];
  132. $data['area'] = $district[2];
  133. $name = $data['name'];
  134. // 检测名称重复
  135. $cri = DbCriteria::simpleCompare(['name' => $name])->setSelect('id');
  136. if ($id > 0) {
  137. $cri->addCondition('id!=' . $id);
  138. }
  139. $isEdit = $id > 0;
  140. if ($fid = DB::getScalerWithCriteria(self::$table, $cri)) {
  141. Helper::error('学校名称已存在 ' . $fid);
  142. }
  143. $this->dobuleCheck();
  144. $trans = \Yii::app()->db->beginTransaction();
  145. try {
  146. if ($id) {
  147. DB::updateById(self::$table, $data, $id);
  148. } else {
  149. $id = DB::addData(self::$table, $data);
  150. // 给用户操作权限
  151. $user = DB::getInfoById('useradmin', $this->getUserId());
  152. if (!str_contains($user['school_ids'], '-1')) {
  153. DB::updateById(
  154. 'useradmin',
  155. ['school_ids' => trim($user['school_ids'].','.$id, ',')],
  156. $this->getUserId()
  157. );
  158. }
  159. }
  160. $trans->commit();
  161. } catch (\Exception $e) {
  162. $trans->rollback();
  163. Helper::error($e->getMessage());
  164. }
  165. if (!$isEdit && $data['city']) {
  166. $this->clearAuthByCity($data['city']);
  167. }
  168. Helper::ok();
  169. }
  170. public function actionUpdateAttr()
  171. {
  172. $id = Helper::getPostInt('id');
  173. $attr = Helper::getPostString('attr');
  174. $value = Helper::getPostString('value');
  175. if ($id <= 0 || !$attr) {
  176. Helper::error('参数错误');
  177. }
  178. if (!$this->checkSchoolId($id)) {
  179. Helper::error('您没有权限操作此数据');
  180. }
  181. if (!in_array($attr, ['is_eleme_in_school', 'person_num'])) {
  182. Helper::error('参数错误2');
  183. }
  184. if ($attr == 'is_eleme_in_school' && !in_array($value, [1, 0])) {
  185. Helper::error('参数错误3');
  186. }
  187. if (DB::updateById(self::$table, [$attr => $value], $id) === false) {
  188. Helper::error('更新失败');
  189. }
  190. Helper::ok();
  191. }
  192. }