FollowController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. class FollowController extends Controller
  3. {
  4. const TYPE_TABLE_MAP = [
  5. 'school' => [
  6. 'table' => 'school_follow',
  7. 'first_id' => 'school_id',
  8. 'second_id' => 'contact_id',
  9. 'table1' => 'school',
  10. 'table2' => 'school_contact',
  11. ],
  12. 'canteen' => [
  13. 'table' => 'canteen_follow',
  14. 'first_id' => 'school_id',
  15. 'second_id' => 'canteen_id',
  16. 'table1' => 'school',
  17. 'table2' => 'canteen_id',
  18. ],
  19. 'company' => [
  20. 'table' => 'company_follow',
  21. 'first_id' => 'company_id',
  22. 'second_id' => 'contact_id',
  23. 'table1' => 'company',
  24. 'table2' => 'company_contact',
  25. ]
  26. ];
  27. public array $tableArr = [];
  28. public string $type = '';
  29. public function beforeAction($action): bool
  30. {
  31. if (!parent::beforeAction($action)) {
  32. return false;
  33. }
  34. $this->type = Helper::getPostString('type');
  35. if (!$this->type || !isset(self::TYPE_TABLE_MAP[$this->type])) {
  36. Helper::error('类型错误');
  37. }
  38. $this->tableArr = self::TYPE_TABLE_MAP[$this->type];
  39. return true;
  40. }
  41. public function actionAdd()
  42. {
  43. $firstId = Helper::getPostInt('first_id');
  44. $secondId = Helper::getPostInt('second_id');
  45. $chatImgs = Helper::getArrParam($_POST, 'chat_imgs', Helper::PARAM_KEY_TYPE['array_string']);
  46. $detail = Helper::getPostString('detail');
  47. if (empty($firstId) || empty($secondId) || empty($detail) || empty($chatImgs)) {
  48. Helper::error('参数错误');
  49. }
  50. DB::addData($this->tableArr['table'], [
  51. $this->tableArr['first_id'] => $firstId,
  52. $this->tableArr['second_id'] => $secondId,
  53. 'detail' => $detail,
  54. 'chat_imgs' => implode(',', $chatImgs),
  55. 'user_id' => Yii::app()->user->_id,
  56. ]);
  57. Helper::ok();
  58. }
  59. public function actionAll()
  60. {
  61. $firstId = Helper::getPostInt('first_id');
  62. if ($firstId <= 0) {
  63. Helper::error('参数错误');
  64. }
  65. $filter = [
  66. $this->tableArr['first_id'] => $firstId,
  67. $this->tableArr['second_id'] => Helper::getPostInt('second_id') ? : null,
  68. ];
  69. $criteria = DbCriteria::simpleCompare($filter)->setOrder('id desc');
  70. $data = DB::getListWithCriteria($this->tableArr['table'], $criteria);
  71. $data['records'] = $this->formatFollowList($data['records']);
  72. Helper::ok($data['records']);
  73. }
  74. public function actionInfo()
  75. {
  76. $id = Helper::getPostInt('id');
  77. if (empty($id)) {
  78. Helper::error('参数错误');
  79. }
  80. $data = DB::getInfoById($this->tableArr['table'], $id);
  81. $data = $this->formatFollowList([$data])[0];
  82. Helper::ok($data);
  83. }
  84. public function actionList()
  85. {
  86. $filter = [
  87. $this->tableArr['first_id'] => Helper::getPostInt('second_id'),
  88. $this->tableArr['second_id'] => Helper::getPostInt('second_id'),
  89. ];
  90. $criteria = DbCriteria::simpleCompareWithPage($filter)->setOrder('id desc');
  91. $data = DB::getListWithCriteria($this->tableArr['table'], $criteria);
  92. $data['records'] = $this->formatFollowList($data['records']);
  93. Helper::ok($data);
  94. }
  95. public function formatFollowList($list)
  96. {
  97. if (empty($list)) {
  98. return [];
  99. }
  100. // 跟进人员
  101. $userIds = array_unique(array_filter(array_column($list, 'user_id')));
  102. $users = [];
  103. if ($userIds) {
  104. $cri = DbCriteria::simpleCompare(['id' => $userIds])->setSelect('id,username,avatar');
  105. $users = Helper::arrayColumn(DB::getListWithCriteria('useradmin', $cri), null, 'id');
  106. }
  107. $field1 = $this->tableArr['first_id'];
  108. $field2 = $this->tableArr['second_id'];
  109. // 校园/公司
  110. $firstIds = array_unique(array_filter(array_column($list, $field1)));
  111. $firsts = [];
  112. if ($firstIds) {
  113. $cri = DbCriteria::simpleCompare(['id' => $firstIds])->setSelect('id,name');
  114. $firsts = Helper::arrayColumn(DB::getListWithCriteria($this->tableArr['table1'], $cri), 'name', 'id');
  115. }
  116. // 关系人
  117. $secondIds = array_unique(array_filter(array_column($list, $field2)));
  118. $seconds = [];
  119. if ($secondIds) {
  120. $cri = DbCriteria::simpleCompare(['id' => $secondIds])->setSelect('id,name,position,weixin,phone');
  121. if ($this->type == 'canteen') {
  122. $cri = DbCriteria::simpleCompare(['t.id' => $secondIds])
  123. ->setAlias('t')
  124. ->setSelect('t.id,c.name,c.position,c.weixin,c.phone')
  125. ->setJoin('left join wx_canteen_contact c on c.canteen_id = t.id');
  126. }
  127. $seconds = Helper::arrayColumn(DB::getListWithCriteria($this->tableArr['table2'], $cri), null, 'id');
  128. }
  129. foreach ($list as &$item) {
  130. $uid = $item['user_id'];
  131. $f1 = $item[$field1];
  132. $f2 = $item[$field2];
  133. $item['chat_imgs'] = Helper::formatImgFiled($item['chat_imgs']);
  134. $item['create_date'] = date('Y-m-d H:i', strtotime($item['create_date']));
  135. $item['user_name'] = $users[$uid]['username'] ?? '';
  136. $item['avatar'] = $users[$uid]['avatar'] ? Helper::getImageUrl($users[$uid]['avatar']) : '';
  137. $item['first_name'] = $firsts[$f1] ?? '';
  138. $item['second_name'] = $seconds[$f2]['name'] ?? '';
  139. $item['position'] = $seconds[$f2]['position'] ?? '';
  140. $item['weixin'] = $seconds[$f2]['weixin'] ?? '';
  141. $item['phone'] = $seconds[$f2]['phone'] ?? '';
  142. }
  143. return $list;
  144. }
  145. }