FollowController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 actionSchoolAdd()
  30. {
  31. $this->_add('school');
  32. }
  33. public function actionCanteenAdd()
  34. {
  35. $this->_add('canteen');
  36. }
  37. public function actionCompanyAdd()
  38. {
  39. $this->_add('company');
  40. }
  41. private function _add($type)
  42. {
  43. $this->type = $type;
  44. $this->tableArr = self::TYPE_TABLE_MAP[$this->type];
  45. $firstId = Helper::getPostInt('first_id');
  46. $secondId = Helper::getPostInt('second_id');
  47. $chatImgs = Helper::getArrParam($_POST, 'chat_imgs', Helper::PARAM_KEY_TYPE['array_string']);
  48. $detail = Helper::getPostString('detail');
  49. if (empty($firstId) || empty($secondId) || empty($detail) || empty($chatImgs)) {
  50. Helper::error('参数错误');
  51. }
  52. DB::addData($this->tableArr['table'], [
  53. $this->tableArr['first_id'] => $firstId,
  54. $this->tableArr['second_id'] => $secondId,
  55. 'detail' => $detail,
  56. 'chat_imgs' => implode(',', $chatImgs),
  57. 'user_id' => Yii::app()->user->_id,
  58. ]);
  59. Helper::ok();
  60. }
  61. public function actionSchoolAll()
  62. {
  63. $this->_all('school');
  64. }
  65. public function actionCanteenAll()
  66. {
  67. $this->_all('canteen');
  68. }
  69. public function actionCompanyAll()
  70. {
  71. $this->_all('company');
  72. }
  73. private function _all($type)
  74. {
  75. $this->type = $type;
  76. $this->tableArr = self::TYPE_TABLE_MAP[$this->type];
  77. $firstId = Helper::getPostInt('first_id');
  78. if ($firstId <= 0) {
  79. Helper::error('参数错误');
  80. }
  81. $filter = [
  82. $this->tableArr['first_id'] => $firstId,
  83. $this->tableArr['second_id'] => Helper::getPostInt('second_id') ? : null,
  84. ];
  85. $criteria = DbCriteria::simpleCompare($filter)->setOrder('id desc');
  86. $data = DB::getListWithCriteria($this->tableArr['table'], $criteria);
  87. $data['records'] = $this->formatFollowList($data['records']);
  88. Helper::ok($data['records']);
  89. }
  90. public function actionSchoolInfo()
  91. {
  92. $this->_info('school');
  93. }
  94. public function actionCanteenInfo()
  95. {
  96. $this->_info('canteen');
  97. }
  98. public function actionCompanyInfo()
  99. {
  100. $this->_info('company');
  101. }
  102. private function _info($type)
  103. {
  104. $this->type = $type;
  105. $this->tableArr = self::TYPE_TABLE_MAP[$this->type];
  106. $id = Helper::getPostInt('id');
  107. if (empty($id)) {
  108. Helper::error('参数错误');
  109. }
  110. $data = DB::getInfoById($this->tableArr['table'], $id);
  111. $data = $this->formatFollowList([$data])[0];
  112. Helper::ok($data);
  113. }
  114. public function actionSchoolList()
  115. {
  116. $this->_list('school');
  117. }
  118. public function actionCanteenList()
  119. {
  120. $this->_list('canteen');
  121. }
  122. public function actionCompanyList()
  123. {
  124. $this->_list('company');
  125. }
  126. private function _list($type)
  127. {
  128. $this->type = $type;
  129. $this->tableArr = self::TYPE_TABLE_MAP[$this->type];
  130. $schoolArr = Helper::getArrParam($_POST, 'school', Helper::PARAM_KEY_TYPE['array_int']);
  131. $filter = [
  132. $this->tableArr['first_id'] => Helper::getPostInt('first_id')? : null,
  133. $this->tableArr['second_id'] => Helper::getPostInt('first_id')? : null,
  134. ];
  135. if ($phone = Helper::getPostString('phone')) {
  136. $rs = Helper::arrayColumn(DB::getListWithCriteria($this->tableArr['table2'], DbCriteria::simpleCompare(['phone' => $phone])->setSelect('id')), 'id');
  137. $filter[$this->tableArr['second_id']] = $rs?: [-1];
  138. }
  139. $criteria = DbCriteria::simpleCompareWithPage($filter)->setOrder('id desc');
  140. $data = DB::getListWithCriteria($this->tableArr['table'], $criteria);
  141. $data['records'] = $this->formatFollowList($data['records']);
  142. Helper::ok($data);
  143. }
  144. public function formatFollowList($list)
  145. {
  146. if (empty($list)) {
  147. return [];
  148. }
  149. // 跟进人员
  150. $userIds = array_unique(array_filter(array_column($list, 'user_id')));
  151. $users = [];
  152. if ($userIds) {
  153. $cri = DbCriteria::simpleCompare(['id' => $userIds])->setSelect('id,username,avatar');
  154. $users = Helper::arrayColumn(DB::getListWithCriteria('useradmin', $cri), null, 'id');
  155. }
  156. $field1 = $this->tableArr['first_id'];
  157. $field2 = $this->tableArr['second_id'];
  158. // 校园/公司
  159. $firstIds = array_unique(array_filter(array_column($list, $field1)));
  160. $firsts = [];
  161. if ($firstIds) {
  162. $cri = DbCriteria::simpleCompare(['id' => $firstIds])->setSelect('id,name');
  163. $firsts = Helper::arrayColumn(DB::getListWithCriteria($this->tableArr['table1'], $cri), 'name', 'id');
  164. }
  165. // 关系人
  166. $secondIds = array_unique(array_filter(array_column($list, $field2)));
  167. $seconds = [];
  168. if ($secondIds) {
  169. $cri = DbCriteria::simpleCompare(['id' => $secondIds])->setSelect('id,name,position,weixin,phone');
  170. if ($this->type == 'canteen') {
  171. $cri = DbCriteria::simpleCompare(['t.id' => $secondIds])
  172. ->setAlias('t')
  173. ->setSelect('t.id,c.name,c.position,c.weixin,c.phone')
  174. ->setJoin('left join wx_canteen_contact c on c.canteen_id = t.id');
  175. }
  176. $seconds = Helper::arrayColumn(DB::getListWithCriteria($this->tableArr['table2'], $cri), null, 'id');
  177. }
  178. foreach ($list as &$item) {
  179. $uid = $item['user_id'];
  180. $f1 = $item[$field1];
  181. $f2 = $item[$field2];
  182. $item['chat_imgs'] = Helper::formatImgFiled($item['chat_imgs']);
  183. $item['create_date'] = date('Y-m-d H:i', strtotime($item['create_date']));
  184. $item['user_name'] = $users[$uid]['username'] ?? '';
  185. $item['avatar'] = $users[$uid]['avatar'] ? Helper::getImageUrl($users[$uid]['avatar']) : '';
  186. $item['first_name'] = $firsts[$f1] ?? '';
  187. $item['second_name'] = $seconds[$f2]['name'] ?? '';
  188. $item['position'] = $seconds[$f2]['position'] ?? '';
  189. $item['weixin'] = $seconds[$f2]['weixin'] ?? '';
  190. $item['phone'] = $seconds[$f2]['phone'] ?? '';
  191. }
  192. return $list;
  193. }
  194. }