| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- <?php
- class FollowController extends Controller
- {
- const TYPE_TABLE_MAP = [
- 'school' => [
- '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 function actionSchoolAdd()
- {
- $this->_add('school');
- }
- public function actionCanteenAdd()
- {
- $this->_add('canteen');
- }
- public function actionCompanyAdd()
- {
- $this->_add('company');
- }
- private function _add($type)
- {
- $userID = Yii::app()->user->_id;
- $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) || empty($detail) || empty($chatImgs)) {
- Helper::error('参数错误');
- }
- $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('canteen', $upInfo, $firstId);
- } 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 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('参数错误');
- }
- $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 ($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');
- $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;
- }
- }
|