SchoolController.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 = [
  40. 't.is_del' => 0,
  41. 't.id' => $this->getSchoolFilter()
  42. ];
  43. $address = Helper::getArrParam($_POST, 'address', Helper::PARAM_KEY_TYPE['array_string']);
  44. $filter['province'] = $address[0]?? null;
  45. $filter['city'] = $address[1]?? null;
  46. $filter['area'] = $address[2]?? null;
  47. if ($name = Helper::getPostString('name')) {
  48. $filter['t.name'] = '%' . $name;
  49. }
  50. foreach (['is_cooperate', 'is_eleme_in_school', 'is_eleme_out_school', 'is_meituan_in_school', 'is_meituan_out_school', 'can_go_upstairs', 'can_ride'] as $item) {
  51. $v = Helper::getPostInt($item, -1);
  52. if ($v != -1) {
  53. $filter['t.' . $item] = $v;
  54. }
  55. }
  56. $bind_user_id = Helper::getPostInt('bind_user_id');
  57. if ($bind_user_id > 0) {
  58. $filter['t.bind_user_id'] = $bind_user_id;
  59. }
  60. // 被删除的关系要排除
  61. $delContactIds = Helper::arrayColumn(DB::getListWithCriteria('wx_school_contact', DbCriteria::simpleCompare(['is_del' => 1])->setSelect('id')), 'id');
  62. $followWhere = '';
  63. if ($delContactIds) {
  64. $followWhere = ' AND sf.contact_id NOT IN (' . implode(',', $delContactIds) . ')';
  65. }
  66. $cri = DbCriteria::simpleCompareWithPage($filter)
  67. ->setAlias('t')
  68. ->setSelect('t.*, group_concat(sf.id) AS follow_ids')
  69. ->setJoin('LEFT JOIN wx_school_follow AS sf ON sf.school_id = t.id ' . $followWhere)
  70. ->setGroup('t.id')
  71. ->setOrder('t.id desc');
  72. $data = DB::getListWithCriteria(self::$table, $cri);
  73. if (!empty($data['records'])) {
  74. $users = Helper::arrayColumn(
  75. DB::getListWithCriteria('useradmin', DbCriteria::simpleCompare([])->setSelect('id, username, avatar')),
  76. null,
  77. 'id'
  78. );
  79. $data['records'] = FollowSrv::formatWithFollowList($data['records'], 'wx_school_follow', $users);
  80. $data['records'] = array_map(function ($item) use ($users) {
  81. $item['bind_user_name'] = $users[$item['bind_user_id']]['username'] ?? '-';
  82. return $item;
  83. }, $data['records']);
  84. }
  85. Helper::ok($data);
  86. }
  87. public function actionDelete()
  88. {
  89. $id = Helper::getPostInt('id');
  90. if ($id < 1) {
  91. Helper::error('参数错误');
  92. }
  93. if (!$this->checkSchoolId($id)) {
  94. Helper::error('您没有权限操作此数据');
  95. }
  96. Db::updateById(self::$table, ['is_del' => 1], $id);
  97. Helper::ok();
  98. }
  99. public function actionAdd()
  100. {
  101. $this->_save();
  102. }
  103. public function actionEdit()
  104. {
  105. $id = Helper::getPostInt('id');
  106. if (!$id) {
  107. Helper::error('参数错误');
  108. }
  109. if (!$this->checkSchoolId($id)) {
  110. Helper::error('您没有权限操作此数据');
  111. }
  112. $this->_save($id);
  113. }
  114. private function _save($id = 0)
  115. {
  116. $data = [
  117. 'name' => Helper::getPostString('name'),
  118. 'address' => Helper::getPostString('address'),
  119. 'person_num' => Helper::getPostString('person_num'),
  120. 'bind_user_id' => Helper::getPostInt('bind_user_id'),
  121. 'is_eleme_in_school' => Helper::getPostInt('is_eleme_in_school'),
  122. 'is_eleme_out_school' => Helper::getPostInt('is_eleme_out_school'),
  123. 'is_meituan_in_school' => Helper::getPostInt('is_meituan_in_school'),
  124. 'is_meituan_out_school' => Helper::getPostInt('is_meituan_out_school'),
  125. 'can_go_upstairs' => Helper::getPostInt('can_go_upstairs'),
  126. 'is_cooperate' => Helper::getPostInt('is_cooperate'),
  127. 'can_ride' => Helper::getPostInt('can_ride'),
  128. 'dormitory_distribution' => Helper::getPostString('dormitory_distribution'),
  129. 'qucan_station_distribution' => Helper::getPostString('qucan_station_distribution'),
  130. 'out_business_description' => Helper::getPostString('out_business_description'),
  131. 'memo' => Helper::getPostString('memo'),
  132. ];
  133. $notNullField = ["name","address","person_num","bind_user_id","is_eleme_in_school","is_eleme_out_school"
  134. ,"is_meituan_in_school","is_meituan_out_school","can_go_upstairs","is_cooperate","can_ride"];
  135. $allowEmptyField = ["bind_user_id","is_eleme_in_school","is_eleme_out_school","is_meituan_in_school"
  136. ,"is_meituan_out_school","can_go_upstairs","is_cooperate","can_ride"];
  137. // 空字段检测
  138. if (!Helper::checkEmptyKey($data, $notNullField, $allowEmptyField)) {
  139. Helper::error('参数错误');
  140. }
  141. // 处理地区
  142. $district = Helper::getArrParam($_POST, 'distinct', Helper::PARAM_KEY_TYPE['array_string']);
  143. $district = array_filter($district);
  144. if (count($district) != 3) {
  145. Helper::error('地区参数错误');
  146. }
  147. $data['province'] = $district[0];
  148. $data['city'] = $district[1];
  149. $data['area'] = $district[2];
  150. $name = $data['name'];
  151. // 检测名称重复
  152. $cri = DbCriteria::simpleCompare(['name' => $name])->setSelect('id');
  153. if ($id > 0) {
  154. $cri->addCondition('id!=' . $id);
  155. }
  156. $isEdit = $id > 0;
  157. if ($fid = DB::getScalerWithCriteria(self::$table, $cri)) {
  158. Helper::error('学校名称已存在 ' . $fid);
  159. }
  160. $this->dobuleCheck();
  161. $trans = \Yii::app()->db->beginTransaction();
  162. try {
  163. if ($id) {
  164. DB::updateById(self::$table, $data, $id);
  165. } else {
  166. $id = DB::addData(self::$table, $data);
  167. // 给用户操作权限
  168. $user = DB::getInfoById('useradmin', $this->getUserId());
  169. if (!str_contains($user['school_ids'], '-1')) {
  170. DB::updateById(
  171. 'useradmin',
  172. ['school_ids' => trim($user['school_ids'].','.$id, ',')],
  173. $this->getUserId()
  174. );
  175. }
  176. }
  177. $trans->commit();
  178. } catch (\Exception $e) {
  179. $trans->rollback();
  180. Helper::error($e->getMessage());
  181. }
  182. $this->clearAuth();
  183. $this->clearAuthByCity($data['city']);
  184. Helper::ok();
  185. }
  186. public function actionUpdateAttr()
  187. {
  188. $id = Helper::getPostInt('id');
  189. $attr = Helper::getPostString('attr');
  190. $value = Helper::getPostString('value');
  191. if ($id <= 0 || !$attr) {
  192. Helper::error('参数错误');
  193. }
  194. if (!$this->checkSchoolId($id)) {
  195. Helper::error('您没有权限操作此数据');
  196. }
  197. if (!in_array($attr, ['is_eleme_in_school', 'person_num'])) {
  198. Helper::error('参数错误2');
  199. }
  200. if ($attr == 'is_eleme_in_school' && !in_array($value, [1, 0])) {
  201. Helper::error('参数错误3');
  202. }
  203. if (DB::updateById(self::$table, [$attr => $value], $id) === false) {
  204. Helper::error('更新失败');
  205. }
  206. Helper::ok();
  207. }
  208. }