| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?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('数据不存在');
- }
- if (!$this->checkSchoolId($data['school_id'])) {
- Helper::error('您没有权限操作此数据');
- }
- Helper::ok($data);
- }
- public function actionList()
- {
- $filter = [
- 'r.is_del' => 0,
- 'r.phone' => Helper::getPostString('phone'),
- ];
- $schoolId = Helper::getPostString('school_id');
- if ($schoolId) {
- if (!$this->checkSchoolId($schoolId)) {
- $filter['r.school_id'] = -1;
- } else {
- $filter['r.school_id'] = $schoolId;
- }
- } else {
- $filter['r.school_id'] = $this->getSchoolFilter();
- }
- if ($name = Helper::getPostString('name')) {
- $filter['r.name'] = '%'.$name;
- }
- if ($last_user_id = Helper::getPostString('last_user_id')) {
- $filter['r.last_user_id'] = $last_user_id;
- }
- $cri = DbCriteria::simpleCompareWithPage($filter)
- ->setAlias('r')
- ->setSelect('r.*, s.name as school_name, group_concat(sf.id) AS follow_ids')
- ->setJoin('LEFT JOIN wx_school s ON s.id=r.school_id')
- ->addJoin('LEFT JOIN wx_school_follow AS sf ON sf.contact_id = r.id')
- ->setGroup('r.id')
- ->setOrder('r.id desc');
- if ($date = Helper::getPostDate('date')) {
- $cri->addBetweenCondition('r.create_date', $date, $date . ' 23:59:59');
- }
- $last_date = $_POST['last_date'];
- if (is_array($last_date) && count($last_date) == 2 && $last_date[0] < $last_date[1]) {
- $cri->addcondition("r.last_date > '{$last_date[0]}' and r.last_date < '{$last_date[1]}'");
- }
- $data = DB::getListWithCriteria($this->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['last_user_name'] = $users[$item['last_user_id']]['username'] ?? '-';
- return $item;
- }, $data['records']);
- }
- Helper::ok($data);
- }
- public function actionDelete()
- {
- $id = Helper::getPostInt('id');
- if ($id < 1) {
- Helper::error('参数错误');
- }
- $data = DB::getInfoById($this->table, $id);
- if (!$data || !$this->checkSchoolId($data['school_id'])) {
- 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('参数错误');
- }
- $data = DB::getInfoById($this->table, $id);
- if (!$data || !$this->checkSchoolId($data['school_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 = ['weixin'];
- // 空字段检测
- if (!Helper::checkEmptyKey($data, $notNullField, $allowEmptyField)) {
- Helper::error('参数错误');
- }
- $this->dobuleCheck();
- 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('参数错误');
- }
- $data = DB::getInfoById($this->table, $id);
- if (!$data || !$this->checkSchoolId($data['school_id'])) {
- Helper::error('您没有权限操作此数据');
- }
- if (!in_array($attr, [])) {
- Helper::error('参数错误2');
- }
- if (DB::updateById($this->table, [$attr => $value], $id) === false) {
- Helper::error('更新失败');
- }
- Helper::ok();
- }
- }
|