| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- <?php
- class FollowController extends Controller
- {
- const TYPE_TABLE_MAP = [
- 'school' => [
- 'table' => 'school_follow',
- 'first_id' => 'school_id',
- 'second_id' => 'contact_id',
- 'table1' => 'wx_school',
- 'table2' => 'wx_school_contact',
- ],
- 'canteen' => [
- 'table' => 'canteen_follow',
- 'first_id' => 'school_id',
- 'second_id' => 'canteen_id',
- 'table1' => 'wx_school',
- 'table2' => 'wx_canteen',
- ],
- 'company' => [
- 'table' => 'company_follow',
- 'first_id' => 'company_id',
- 'second_id' => 'contact_id',
- 'table1' => 'wx_company',
- 'table2' => 'wx_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 = [
- 't1.' . $this->tableArr['first_id'] => $firstId,
- 't1.' . $this->tableArr['second_id'] => Helper::getPostInt('second_id') ? : null,
- ];
- $criteria = DbCriteria::simpleCompare($filter)->setOrder('id desc');
- $criteria->setAlias('t1')->addCondition('t2.id > 0');
- if ($this->type == 'canteen') {
- $criteria->setSelect('t1.*,t2.name as second_name,t2.weixin,t2.phone');
- } else {
- $criteria->setSelect('t1.*,t2.name as second_name,t2.weixin,t2.phone,t2.position');
- }
- $criteria->join = "left join {$this->tableArr['table2']} t2 on t2.id = t1.{$this->tableArr['second_id']} and t2.is_del = 0";
- $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 = [
- 't1.' . $this->tableArr['first_id'] => $schoolArr[0]? : null,
- 't1.' . $this->tableArr['second_id'] => $schoolArr[1]? : null,
- ];
- } else {
- $filter = [
- 't1.' . $this->tableArr['first_id'] => Helper::getPostInt('first_id')? : null,
- 't1.' . $this->tableArr['second_id'] => Helper::getPostInt('second_id')? : null,
- ];
- }
- if (empty($filter['t1.' .$this->tableArr['first_id']])) {
- $filter['t1.' . $this->tableArr['first_id']] = $this->type == 'company' ? $this->getCompanyFilter() : $this->getSchoolFilter();
- } else {
- $this->checkAuth($filter['t1.' .$this->tableArr['first_id']]);
- }
- if ($phone = Helper::getPostString('phone')) {
- $rs = Helper::arrayColumn(DB::getListWithCriteria($this->tableArr['table2'], DbCriteria::simpleCompare(['phone' => $phone])->setSelect('id')), 'id');
- $filter['t1.' . $this->tableArr['second_id']] = $rs?: [-1];
- }
- $criteria = DbCriteria::simpleCompareWithPage($filter)->setOrder('id desc');
- if ($date = Helper::getPostDate('date')) {
- $criteria->addBetweenCondition('t1.create_date', $date, $date . ' 23:59:59');
- }
- $criteria->setAlias('t1')->addCondition('t2.id > 0');
- if ($this->type == 'canteen') {
- $criteria->setSelect('t1.*,t2.name as second_name,t2.weixin,t2.phone');
- } else {
- $criteria->setSelect('t1.*,t2.name as second_name,t2.weixin,t2.phone,t2.position');
- }
- $criteria->join = "left join {$this->tableArr['table2']} t2 on t2.id = t1.{$this->tableArr['second_id']} and t2.is_del = 0";
- $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');
- }
- 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['position'] = $item['position'] ?? '';
- }
- return $list;
- }
- }
|