| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- class SchoolRelationController extends Controller
- {
- public string $table = 'school_contact';
- public function actionInfo()
- {
- $id = Helper::getPostInt('id');
- if ($id <= 0) {
- Helper::error('参数错误');
- }
- $data = DB::getInfoById($this->table, $id);
- if (!$data) {
- Helper::error('数据不存在');
- }
- Helper::ok($data);
- }
- public function actionList()
- {
- $filter = [
- 'r.is_del' => 0,
- 'r.phone' => Helper::getPostString('phone'),
- 'r.school_id' => Helper::getPostInt('school_id') ?: null,
- ];
- if ($name = Helper::getPostString('name')) {
- $filter['r.name'] = '%'.$name;
- }
- $cri = DbCriteria::simpleCompareWithPage($filter)
- ->setAlias('r')
- ->setSelect('r.*, s.name as school_name')
- ->setJoin('LEFT JOIN wx_school s ON s.id=r.school_id');
- $data = DB::getListWithCriteria($this->table, $cri);
- if (!empty($data['records'])) {
- $data['records'] = array_map(function ($item) {
- return $item;
- }, $data['records']);
- }
- Helper::ok($data);
- }
- /**
- * 下拉列表获取
- * @return void
- */
- public function actionGetSelectList()
- {
- $cri = DbCriteria::simpleCompare([])->setSelect('id, name');
- $schools = Helper::arrayColumn(DB::getListWithCriteria(SchoolController::$table, $cri), null, 'id');
- if (empty($schools)) {
- Helper::ok([]);
- }
- $relations = DB::getListWithCriteria($this->table, $cri);
- foreach ($relations['records'] as $relation) {
- $sid = $relation['school_id'];
- if (!isset($schools[$sid])) {
- continue;
- }
- if (!isset($schools[$sid]['children'])) {
- $schools[$sid]['children'] = [];
- }
- $schools[$sid]['children'][] = [
- 'id' => $relation['id'],
- 'name' => $relation['name'],
- ];
- }
- Helper::ok($schools);
- }
- public function actionDelete()
- {
- $id = Helper::getPostInt('id');
- if ($id < 1) {
- Helper::error('参数错误');
- }
- Db::updateById($this->table, ['is_del' => 1], $id);
- Helper::ok();
- }
- public function actionAdd()
- {
- $this->_save();
- }
- public function actionEdit()
- {
- $id = Helper::getPostInt('id');
- if (!$id) {
- Helper::error('参数错误');
- }
- $this->_save($id);
- }
- private function _save($id = 0)
- {
- $data = [
- 'name' => Helper::getPostString('name'),
- 'school_id' => Helper::getPostInt('school_id'),
- 'phone' => Helper::getPostString('phone'),
- 'weixin' => Helper::getPostString('weixin'),
- 'position' => Helper::getPostString('position'),
- 'memo' => Helper::getPostString('memo'),
- ];
- $notNullField = ["name", "school_id", "phone", "weixin", "position"];
- $allowEmptyField = [];
- // 空字段检测
- if (!Helper::checkEmptyKey($data, $notNullField, $allowEmptyField)) {
- Helper::error('参数错误');
- }
- if ($id) {
- DB::updateById($this->table, $data, $id);
- } else {
- DB::addData($this->table, $data);
- }
- Helper::ok();
- }
- public function actionUpdateAttr()
- {
- $id = Helper::getPostInt('id');
- $attr = Helper::getPostString('attr');
- $value = Helper::getPostString('value');
- if ($id <= 0 || !$attr) {
- Helper::error('参数错误');
- }
- if (!in_array($attr, [])) {
- Helper::error('参数错误2');
- }
- if (DB::updateById($this->table, [$attr => $value], $id) === false) {
- Helper::error('更新失败');
- }
- Helper::ok();
- }
- }
|