[ 'table' => 'school_follow', 'first_id' => 'school_id', 'second_id' => 'contact_id', 'table1' => 'school', 'table2' => 'school_contact', ], 'canteen' => [ 'table' => 'canteen_follow', 'first_id' => 'school_id', 'second_id' => 'canteen_id', 'table1' => 'school', 'table2' => 'canteen', ], 'company' => [ 'table' => 'company_follow', 'first_id' => 'company_id', 'second_id' => 'contact_id', 'table1' => 'company', 'table2' => 'company_contact', ] ]; public array $tableArr = []; public string $type = ''; public ?array $firstFilter; public function actionSchoolAdd() { $this->_add('school'); } public function actionCanteenAdd() { $this->_add('canteen'); } public function actionCompanyAdd() { $this->_add('company'); } private function _add($type) { $userID = $this->getUserId(); $this->type = $type; $this->tableArr = self::TYPE_TABLE_MAP[$this->type]; $firstId = Helper::getPostInt('first_id'); $secondId = Helper::getPostInt('second_id'); $chatImgs = Helper::getArrParam($_POST, 'chat_imgs', Helper::PARAM_KEY_TYPE['array_string'], []); $detail = $_POST['detail']?? ''; if (empty($firstId) || empty($secondId)) { Helper::error('请选择跟进对象'); } if (empty($detail)) { Helper::error('请填写跟进内容'); } $this->dobuleCheck(); $this->checkAuth($firstId); $trans = \Yii::app()->db->beginTransaction(); try { DB::addData($this->tableArr['table'], [ $this->tableArr['first_id'] => $firstId, $this->tableArr['second_id'] => $secondId, 'detail' => $detail, 'chat_imgs' => implode(',', $chatImgs), 'user_id' => $userID, ]); // 最后一次跟进时间更新 $upInfo = ['last_user_id' => $userID, 'last_date' => date('Y-m-d H:i:s')]; if ($this->type == 'school') { DB::updateById('school', $upInfo, $firstId); DB::updateById('school_contact', $upInfo, $secondId); } elseif ($this->type == 'canteen') { DB::updateById('school', $upInfo, $firstId); DB::updateById('canteen', $upInfo, $secondId); } elseif ($this->type == 'company') { DB::updateById('company', $upInfo, $firstId); DB::updateById('company_contact', $upInfo, $secondId); } $trans->commit(); } catch (Exception $e) { $trans->rollback(); Helper::error($e->getMessage()); } Helper::ok(); } public function checkAuth($id):void { if ($this->type == 'school') { if (!$this->checkSchoolId($id)) { Helper::error('无该学校权限'); } } elseif ($this->type == 'canteen') { if (!$this->checkSchoolId($id)) { Helper::error('无该学校权限'); } } elseif ($this->type == 'company') { if (!$this->checkCompanyId($id)) { Helper::error('无该公司权限'); } } } public function actionSchoolAll() { $this->_all('school'); } public function actionCanteenAll() { $this->_all('canteen'); } public function actionCompanyAll() { $this->_all('company'); } private function _all($type) { $this->type = $type; $this->tableArr = self::TYPE_TABLE_MAP[$this->type]; $firstId = Helper::getPostInt('first_id'); if ($firstId <= 0) { Helper::error('参数错误'); } $this->checkAuth($firstId); $filter = [ $this->tableArr['first_id'] => $firstId, $this->tableArr['second_id'] => Helper::getPostInt('second_id') ? : null, ]; $criteria = DbCriteria::simpleCompare($filter)->setOrder('id desc'); $data = DB::getListWithCriteria($this->tableArr['table'], $criteria); $data['records'] = $this->formatFollowList($data['records']); Helper::ok($data['records']); } public function actionSchoolInfo() { $this->_info('school'); } public function actionCanteenInfo() { $this->_info('canteen'); } public function actionCompanyInfo() { $this->_info('company'); } private function _info($type) { $this->type = $type; $this->tableArr = self::TYPE_TABLE_MAP[$this->type]; $id = Helper::getPostInt('id'); if (empty($id)) { Helper::error('参数错误'); } $data = DB::getInfoById($this->tableArr['table'], $id); $data = $this->formatFollowList([$data])[0]; Helper::ok($data); } public function actionSchoolList() { $this->_list('school'); } public function actionCanteenList() { $this->_list('canteen'); } public function actionCompanyList() { $this->_list('company'); } private function _list($type) { $this->type = $type; $this->tableArr = self::TYPE_TABLE_MAP[$this->type]; $schoolArr = Helper::getArrParam($_POST, 'school', Helper::PARAM_KEY_TYPE['array_int']); if ($schoolArr) { $filter = [ $this->tableArr['first_id'] => $schoolArr[0]? : null, $this->tableArr['second_id'] => $schoolArr[1]? : null, ]; } else { $filter = [ $this->tableArr['first_id'] => Helper::getPostInt('first_id')? : null, $this->tableArr['second_id'] => Helper::getPostInt('second_id')? : null, ]; } if (empty($filter[$this->tableArr['first_id']])) { $filter[$this->tableArr['first_id']] = $this->type == 'company' ? $this->getCompanyFilter() : $this->getSchoolFilter(); } if ($phone = Helper::getPostString('phone')) { $rs = Helper::arrayColumn(DB::getListWithCriteria($this->tableArr['table2'], DbCriteria::simpleCompare(['phone' => $phone])->setSelect('id')), 'id'); $filter[$this->tableArr['second_id']] = $rs?: [-1]; } $criteria = DbCriteria::simpleCompareWithPage($filter)->setOrder('id desc'); if ($date = Helper::getPostDate('date')) { $criteria->addBetweenCondition('create_date', $date, $date . ' 23:59:59'); } $data = DB::getListWithCriteria($this->tableArr['table'], $criteria); $data['records'] = $this->formatFollowList($data['records']); Helper::ok($data); } public function formatFollowList($list) { if (empty($list)) { return []; } // 跟进人员 $userIds = array_unique(array_filter(array_column($list, 'user_id'))); $users = []; if ($userIds) { $cri = DbCriteria::simpleCompare(['id' => $userIds])->setSelect('id,username,avatar'); $users = Helper::arrayColumn(DB::getListWithCriteria('useradmin', $cri), null, 'id'); } $field1 = $this->tableArr['first_id']; $field2 = $this->tableArr['second_id']; // 校园/公司 $firstIds = array_unique(array_filter(array_column($list, $field1))); $firsts = []; if ($firstIds) { $cri = DbCriteria::simpleCompare(['id' => $firstIds])->setSelect('id,name'); $firsts = Helper::arrayColumn(DB::getListWithCriteria($this->tableArr['table1'], $cri), 'name', 'id'); } // 关系人 $secondIds = array_unique(array_filter(array_column($list, $field2))); $seconds = []; if ($secondIds) { $cri = DbCriteria::simpleCompare(['id' => $secondIds])->setSelect('id,name,position,weixin,phone'); if ($this->type == 'canteen') { $cri->setSelect('id,name,weixin,phone'); } $seconds = Helper::arrayColumn(DB::getListWithCriteria($this->tableArr['table2'], $cri), null, 'id'); } foreach ($list as &$item) { $uid = $item['user_id']; $f1 = $item[$field1]; $f2 = $item[$field2]; $item['chat_imgs'] = Helper::formatImgsFiled($item['chat_imgs']); $item['create_date'] = date('Y-m-d H:i', strtotime($item['create_date'])); $item['user_name'] = $users[$uid]['username'] ?? ''; $item['avatar'] = $users[$uid]['avatar'] ? Helper::getImageUrl($users[$uid]['avatar']) : ''; $item['first_name'] = $firsts[$f1] ?? ''; $item['second_name'] = $seconds[$f2]['name'] ?? ''; $item['position'] = $seconds[$f2]['position'] ?? ''; $item['weixin'] = $seconds[$f2]['weixin'] ?? ''; $item['phone'] = $seconds[$f2]['phone'] ?? ''; } return $list; } }