SchoolController.php 8.1 KB

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