| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- class SchoolController extends Controller
- {
- public static string $table = 'school';
- public function actionInfo()
- {
- $id = Helper::getPostInt('id');
- if ($id <= 0) {
- Helper::error('参数错误');
- }
- if (!$this->checkSchoolId($id)) {
- Helper::error('您没有权限操作此数据');
- }
- $data = DB::getInfoById(self::$table, $id);
- if (!$data) {
- Helper::error('数据不存在');
- }
- $data['distinct'] = [
- $data['province'],
- $data['city'],
- $data['area'],
- ];
- // 关系人
- $relations = DB::getListWithCriteria(
- 'school_contact',
- DbCriteria::simpleCompare(['school_id' => $id])->setSelect('id, name, phone, position, weixin'),
- );
- $data['relations'] = $relations['records'];
- // 关联食堂
- $canteens = DB::getListWithCriteria(
- 'canteen',
- DbCriteria::simpleCompare(['school_id' => $id])->setSelect('id, name, username, weixin, phone'),
- );
- $data['canteens'] = $canteens['records'];
- Helper::ok($data);
- }
- public function actionList()
- {
- $filter = [
- 't.is_del' => 0,
- 't.id' => $this->getSchoolFilter()
- ];
- $address = Helper::getArrParam($_POST, 'address', Helper::PARAM_KEY_TYPE['array_string']);
- $filter['province'] = $address[0]?? null;
- $filter['city'] = $address[1]?? null;
- $filter['area'] = $address[2]?? null;
- if ($name = Helper::getPostString('name')) {
- $filter['name'] = '%' . $name;
- }
- $is_cooperate = Helper::getPostInt('is_cooperate');
- if ($is_cooperate != -1) {
- $filter['is_cooperate'] = $is_cooperate;
- }
- // 被删除的关系要排除
- $delContactIds = Helper::arrayColumn(DB::getListWithCriteria('wx_school_contact', DbCriteria::simpleCompare(['is_del' => 1])->setSelect('id')), 'id');
- $followWhere = '';
- if ($delContactIds) {
- $followWhere = ' AND sf.contact_id NOT IN (' . implode(',', $delContactIds) . ')';
- }
- $cri = DbCriteria::simpleCompareWithPage($filter)
- ->setAlias('t')
- ->setSelect('t.*, group_concat(sf.id) AS follow_ids')
- ->setJoin('LEFT JOIN wx_school_follow AS sf ON sf.school_id = t.id ' . $followWhere)
- ->setGroup('t.id')
- ->setOrder('t.id desc');
- $data = DB::getListWithCriteria(self::$table, $cri);
- if (!empty($data['records'])) {
- $users = Helper::arrayColumn(
- DB::getListWithCriteria('useradmin', DbCriteria::simpleCompare([])->setSelect('id, username, avatar')),
- null,
- 'id'
- );
- $data['records'] = FollowSrv::formatWithFollowList($data['records'], 'wx_school_follow', $users);
- $data['records'] = array_map(function ($item) use ($users) {
- $item['bind_user_name'] = $users[$item['bind_user_id']]['username'] ?? '-';
- return $item;
- }, $data['records']);
- }
- Helper::ok($data);
- }
- public function actionDelete()
- {
- $id = Helper::getPostInt('id');
- if ($id < 1) {
- Helper::error('参数错误');
- }
- if (!$this->checkSchoolId($id)) {
- Helper::error('您没有权限操作此数据');
- }
- Db::updateById(self::$table, ['is_del' => 1], $id);
- Helper::ok();
- }
- public function actionAdd()
- {
- $this->_save();
- }
- public function actionEdit()
- {
- $id = Helper::getPostInt('id');
- if (!$id) {
- Helper::error('参数错误');
- }
- if (!$this->checkSchoolId($id)) {
- Helper::error('您没有权限操作此数据');
- }
- $this->_save($id);
- }
- private function _save($id = 0)
- {
- $data = [
- 'name' => Helper::getPostString('name'),
- 'address' => Helper::getPostString('address'),
- 'person_num' => Helper::getPostString('person_num'),
- 'bind_user_id' => Helper::getPostInt('bind_user_id'),
- 'is_eleme_in_school' => Helper::getPostInt('is_eleme_in_school'),
- 'is_eleme_out_school' => Helper::getPostInt('is_eleme_out_school'),
- 'is_meituan_in_school' => Helper::getPostInt('is_meituan_in_school'),
- 'is_meituan_out_school' => Helper::getPostInt('is_meituan_out_school'),
- 'can_go_upstairs' => Helper::getPostInt('can_go_upstairs'),
- 'is_cooperate' => Helper::getPostInt('is_cooperate'),
- 'can_ride' => Helper::getPostInt('can_ride'),
- 'dormitory_distribution' => Helper::getPostString('dormitory_distribution'),
- 'qucan_station_distribution' => Helper::getPostString('qucan_station_distribution'),
- 'out_business_description' => Helper::getPostString('out_business_description'),
- 'memo' => Helper::getPostString('memo'),
- ];
- $notNullField = ["name","address","person_num","bind_user_id","is_eleme_in_school","is_eleme_out_school"
- ,"is_meituan_in_school","is_meituan_out_school","can_go_upstairs","is_cooperate","can_ride"];
- $allowEmptyField = ["bind_user_id","is_eleme_in_school","is_eleme_out_school","is_meituan_in_school"
- ,"is_meituan_out_school","can_go_upstairs","is_cooperate","can_ride"];
- // 空字段检测
- if (!Helper::checkEmptyKey($data, $notNullField, $allowEmptyField)) {
- Helper::error('参数错误');
- }
- // 处理地区
- $district = Helper::getArrParam($_POST, 'distinct', Helper::PARAM_KEY_TYPE['array_string']);
- $district = array_filter($district);
- if (count($district) != 3) {
- Helper::error('地区参数错误');
- }
- $data['province'] = $district[0];
- $data['city'] = $district[1];
- $data['area'] = $district[2];
- $name = $data['name'];
- // 检测名称重复
- $cri = DbCriteria::simpleCompare(['name' => $name])->setSelect('id');
- if ($id > 0) {
- $cri->addCondition('id!=' . $id);
- }
- $isEdit = $id > 0;
- if ($fid = DB::getScalerWithCriteria(self::$table, $cri)) {
- Helper::error('学校名称已存在 ' . $fid);
- }
- $this->dobuleCheck();
- $trans = \Yii::app()->db->beginTransaction();
- try {
- if ($id) {
- DB::updateById(self::$table, $data, $id);
- } else {
- $id = DB::addData(self::$table, $data);
- // 给用户操作权限
- $user = DB::getInfoById('useradmin', $this->getUserId());
- if (!str_contains($user['school_ids'], '-1')) {
- DB::updateById(
- 'useradmin',
- ['school_ids' => trim($user['school_ids'].','.$id, ',')],
- $this->getUserId()
- );
- }
- }
- $trans->commit();
- } catch (\Exception $e) {
- $trans->rollback();
- Helper::error($e->getMessage());
- }
- if (!$isEdit && $data['city']) {
- $this->clearAuthByCity($data['city']);
- }
- Helper::ok();
- }
- public function actionUpdateAttr()
- {
- $id = Helper::getPostInt('id');
- $attr = Helper::getPostString('attr');
- $value = Helper::getPostString('value');
- if ($id <= 0 || !$attr) {
- Helper::error('参数错误');
- }
- if (!$this->checkSchoolId($id)) {
- Helper::error('您没有权限操作此数据');
- }
- if (!in_array($attr, ['is_eleme_in_school', 'person_num'])) {
- Helper::error('参数错误2');
- }
- if ($attr == 'is_eleme_in_school' && !in_array($value, [1, 0])) {
- Helper::error('参数错误3');
- }
- if (DB::updateById(self::$table, [$attr => $value], $id) === false) {
- Helper::error('更新失败');
- }
- Helper::ok();
- }
- }
|