checkCompanyId($id)) { Helper::error('您没有权限操作此数据'); } $data = DB::getInfoById(self::$table, $id); if (!$data) { Helper::error('数据不存在'); } $cri = DbCriteria::simpleCompare(['id' => [$data['last_user_id'], $data['bind_user_id']]])->setSelect('id, username'); $users = Helper::arrayColumn(DB::getListWithCriteria('useradmin', $cri), 'username', 'id'); $data['last_user_name'] = $users[$data['last_user_id']] ?? ''; $data['bind_user_name'] = $users[$data['bind_user_id']] ?? ''; $data['distinct'] = [ $data['province'], $data['city'], // $data['area'], ]; // 关联食堂 $data['canteen_names'] = []; $data['canteens'] = []; $cri = DbCriteria::simpleCompare(['t.company_id' => $id]) ->setAlias('t') ->setSelect('t.school_id, t.canteen_id, s.name as school_name, c.name as canteen_name') ->setJoin('left join wx_school s on t.school_id = s.id') ->addJoin('left join wx_canteen c on t.canteen_id = c.id'); $canteens = DB::getListWithCriteria('company_canteen_relation', $cri); foreach ($canteens['records'] as $item) { $data['canteens'][] = [(int)$item['school_id'], (int)$item['canteen_id']]; $data['canteen_names'][] = "{$item['canteen_name']}({$item['school_name']})"; } // 关系人 $relations = DB::getListWithCriteria( 'company_contact', DbCriteria::simpleCompare(['company_id' => $id])->setSelect('id, name, phone, position, weixin'), ); $data['relations'] = $relations['records']; Helper::ok($data); } public function actionList() { $filter = [ 'is_del' => 0, 'phone' => Helper::getPostString('phone'), 'id' => $this->getcompanyFilter(), ]; if ($name = Helper::getPostString('name')) { $filter['name'] = '%' . $name; } $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 ($school_id = Helper::getPostInt('school_id')) { $cri = DbCriteria::simpleCompare(['school_id' => $school_id])->setSelect('company_id'); $ids = Helper::arrayColumn( DB::getListWithCriteria('company_canteen_relation', $cri), 'company_id' ); $filter['id'] = $ids?: -1; } // 被删除的关系要排除 $delContactIds = Helper::arrayColumn(DB::getListWithCriteria('wx_company_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_company_follow AS sf ON sf.company_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_company_follow', $users); $data['records'] = array_map(function ($item) use ($users) { $item['last_user_name'] = $users[$item['last_user_id']]['username'] ?? '-'; $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->checkCompanyId($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->checkCompanyId($id)) { Helper::error('您没有权限操作此数据'); } $this->_save($id); } private function _save($id = 0) { $data = [ 'name' => Helper::getPostString('name'), 'address' => Helper::getPostString('address'), 'memo' => Helper::getPostString('memo'), 'bind_user_id' => Helper::getPostInt('bind_user_id'), ]; // 空字段检测 if (!Helper::checkEmptyKey($data, ['name', 'address', 'memo'], ['memo'])) { Helper::error('参数错误'); } // 处理地区 $district = Helper::getArrParam($_POST, 'distinct', Helper::PARAM_KEY_TYPE['array_string']); $district = array_filter($district); if (count($district) != 2) { Helper::error('地区参数错误'); } $data['province'] = $district[0]; $data['city'] = $district[1]; $data['area'] = ''; // 关联食堂 $canteens = $_POST['canteens']?? []; if (!$canteens) { Helper::error('请选择关联的食堂'); } foreach ($canteens as $k => $canteen) { $canteens[$k] = array_filter(explode(',', $canteen)); if (count($canteens[$k]) != 2 || empty($canteens[$k][0]) || empty($canteens[$k][1])) { Helper::error('选择的食堂参数有误 ' . json_encode($canteens)); } } $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 ($isEdit) { DB::updateById(self::$table, $data, $id); DB::deleteByCondition('company_canteen_relation', ['company_id' => $id]); } else { $id = DB::addData(self::$table, $data); if (!$id) { throw new \Exception('添加失败'); } // 给用户操作权限 $user = DB::getInfoById('useradmin', $this->getUserId()); if (!str_contains($user['company_ids'], '-1')) { DB::updateById( 'useradmin', ['company_ids' => trim($user['company_ids'] . ',' . $id, ',')], $this->getUserId() ); } } $batchArr = []; foreach ($canteens as $canteen) { $batchArr[] = [ 'company_id' => $id, 'school_id' => $canteen[0], 'canteen_id' => $canteen[1], ]; } DB::safeBatchInsert('company_canteen_relation', $batchArr); $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->checkCompanyId($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(); } }