| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?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_id',
- ],
- '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 beforeAction($action): bool
- {
- if (!parent::beforeAction($action)) {
- return false;
- }
- $this->type = Helper::getPostString('type');
- if (!$this->type || !isset(self::TYPE_TABLE_MAP[$this->type])) {
- Helper::error('类型错误');
- }
- $this->tableArr = self::TYPE_TABLE_MAP[$this->type];
- return true;
- }
- public function actionAdd()
- {
- $firstId = Helper::getPostInt('first_id');
- $secondId = Helper::getPostInt('second_id');
- $chatImgs = Helper::getArrParam($_POST, 'chat_imgs', Helper::PARAM_KEY_TYPE['array_string']);
- $detail = Helper::getPostString('detail');
- if (empty($firstId) || empty($secondId) || empty($detail) || empty($chatImgs)) {
- Helper::error('参数错误');
- }
- DB::addData($this->tableArr['table'], [
- $this->tableArr['first_id'] => $firstId,
- $this->tableArr['second_id'] => $secondId,
- 'detail' => $detail,
- 'chat_imgs' => implode(',', $chatImgs),
- 'user_id' => Yii::app()->user->_id,
- ]);
- Helper::ok();
- }
- public function actionAll()
- {
- $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 actionInfo()
- {
- $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 actionList()
- {
- $filter = [
- $this->tableArr['first_id'] => Helper::getPostInt('second_id'),
- $this->tableArr['second_id'] => Helper::getPostInt('second_id'),
- ];
- $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 = DbCriteria::simpleCompare(['t.id' => $secondIds])
- ->setAlias('t')
- ->setSelect('t.id,c.name,c.position,c.weixin,c.phone')
- ->setJoin('left join wx_canteen_contact c on c.canteen_id = t.id');
- }
- $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::formatImgFiled($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;
- }
- }
|