FollowController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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',
  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. $userID = Yii::app()->user->_id;
  44. $this->type = $type;
  45. $this->tableArr = self::TYPE_TABLE_MAP[$this->type];
  46. $firstId = Helper::getPostInt('first_id');
  47. $secondId = Helper::getPostInt('second_id');
  48. $chatImgs = Helper::getArrParam($_POST, 'chat_imgs', Helper::PARAM_KEY_TYPE['array_string']);
  49. $detail = $_POST['detail']?? '';
  50. if (empty($firstId) || empty($secondId) || empty($detail) || empty($chatImgs)) {
  51. Helper::error('参数错误');
  52. }
  53. $trans = \Yii::app()->db->beginTransaction();
  54. try {
  55. DB::addData($this->tableArr['table'], [
  56. $this->tableArr['first_id'] => $firstId,
  57. $this->tableArr['second_id'] => $secondId,
  58. 'detail' => $detail,
  59. 'chat_imgs' => implode(',', $chatImgs),
  60. 'user_id' => $userID,
  61. ]);
  62. // 最后一次跟进时间更新
  63. $upInfo = ['last_user_id' => $userID, 'last_date' => date('Y-m-d H:i:s')];
  64. if ($this->type == 'school') {
  65. DB::updateById('school', $upInfo, $firstId);
  66. DB::updateById('school_contact', $upInfo, $secondId);
  67. } elseif ($this->type == 'canteen') {
  68. DB::updateById('canteen', $upInfo, $firstId);
  69. } elseif ($this->type == 'company') {
  70. DB::updateById('company', $upInfo, $firstId);
  71. DB::updateById('company_contact', $upInfo, $secondId);
  72. }
  73. $trans->commit();
  74. } catch (Exception $e) {
  75. $trans->rollback();
  76. Helper::error($e->getMessage());
  77. }
  78. Helper::ok();
  79. }
  80. public function actionSchoolAll()
  81. {
  82. $this->_all('school');
  83. }
  84. public function actionCanteenAll()
  85. {
  86. $this->_all('canteen');
  87. }
  88. public function actionCompanyAll()
  89. {
  90. $this->_all('company');
  91. }
  92. private function _all($type)
  93. {
  94. $this->type = $type;
  95. $this->tableArr = self::TYPE_TABLE_MAP[$this->type];
  96. $firstId = Helper::getPostInt('first_id');
  97. if ($firstId <= 0) {
  98. Helper::error('参数错误');
  99. }
  100. $filter = [
  101. $this->tableArr['first_id'] => $firstId,
  102. $this->tableArr['second_id'] => Helper::getPostInt('second_id') ? : null,
  103. ];
  104. $criteria = DbCriteria::simpleCompare($filter)->setOrder('id desc');
  105. $data = DB::getListWithCriteria($this->tableArr['table'], $criteria);
  106. $data['records'] = $this->formatFollowList($data['records']);
  107. Helper::ok($data['records']);
  108. }
  109. public function actionSchoolInfo()
  110. {
  111. $this->_info('school');
  112. }
  113. public function actionCanteenInfo()
  114. {
  115. $this->_info('canteen');
  116. }
  117. public function actionCompanyInfo()
  118. {
  119. $this->_info('company');
  120. }
  121. private function _info($type)
  122. {
  123. $this->type = $type;
  124. $this->tableArr = self::TYPE_TABLE_MAP[$this->type];
  125. $id = Helper::getPostInt('id');
  126. if (empty($id)) {
  127. Helper::error('参数错误');
  128. }
  129. $data = DB::getInfoById($this->tableArr['table'], $id);
  130. $data = $this->formatFollowList([$data])[0];
  131. Helper::ok($data);
  132. }
  133. public function actionSchoolList()
  134. {
  135. $this->_list('school');
  136. }
  137. public function actionCanteenList()
  138. {
  139. $this->_list('canteen');
  140. }
  141. public function actionCompanyList()
  142. {
  143. $this->_list('company');
  144. }
  145. private function _list($type)
  146. {
  147. $this->type = $type;
  148. $this->tableArr = self::TYPE_TABLE_MAP[$this->type];
  149. $schoolArr = Helper::getArrParam($_POST, 'school', Helper::PARAM_KEY_TYPE['array_int']);
  150. if ($schoolArr) {
  151. $filter = [
  152. $this->tableArr['first_id'] => $schoolArr[0]? : null,
  153. $this->tableArr['second_id'] => $schoolArr[1]? : null,
  154. ];
  155. } else {
  156. $filter = [
  157. $this->tableArr['first_id'] => Helper::getPostInt('first_id')? : null,
  158. $this->tableArr['second_id'] => Helper::getPostInt('second_id')? : null,
  159. ];
  160. }
  161. if ($phone = Helper::getPostString('phone')) {
  162. $rs = Helper::arrayColumn(DB::getListWithCriteria($this->tableArr['table2'], DbCriteria::simpleCompare(['phone' => $phone])->setSelect('id')), 'id');
  163. $filter[$this->tableArr['second_id']] = $rs?: [-1];
  164. }
  165. $criteria = DbCriteria::simpleCompareWithPage($filter)->setOrder('id desc');
  166. $data = DB::getListWithCriteria($this->tableArr['table'], $criteria);
  167. $data['records'] = $this->formatFollowList($data['records']);
  168. Helper::ok($data);
  169. }
  170. public function formatFollowList($list)
  171. {
  172. if (empty($list)) {
  173. return [];
  174. }
  175. // 跟进人员
  176. $userIds = array_unique(array_filter(array_column($list, 'user_id')));
  177. $users = [];
  178. if ($userIds) {
  179. $cri = DbCriteria::simpleCompare(['id' => $userIds])->setSelect('id,username,avatar');
  180. $users = Helper::arrayColumn(DB::getListWithCriteria('useradmin', $cri), null, 'id');
  181. }
  182. $field1 = $this->tableArr['first_id'];
  183. $field2 = $this->tableArr['second_id'];
  184. // 校园/公司
  185. $firstIds = array_unique(array_filter(array_column($list, $field1)));
  186. $firsts = [];
  187. if ($firstIds) {
  188. $cri = DbCriteria::simpleCompare(['id' => $firstIds])->setSelect('id,name');
  189. $firsts = Helper::arrayColumn(DB::getListWithCriteria($this->tableArr['table1'], $cri), 'name', 'id');
  190. }
  191. // 关系人
  192. $secondIds = array_unique(array_filter(array_column($list, $field2)));
  193. $seconds = [];
  194. if ($secondIds) {
  195. $cri = DbCriteria::simpleCompare(['id' => $secondIds])->setSelect('id,name,position,weixin,phone');
  196. if ($this->type == 'canteen') {
  197. $cri->setSelect('id,name,weixin,phone');
  198. }
  199. $seconds = Helper::arrayColumn(DB::getListWithCriteria($this->tableArr['table2'], $cri), null, 'id');
  200. }
  201. foreach ($list as &$item) {
  202. $uid = $item['user_id'];
  203. $f1 = $item[$field1];
  204. $f2 = $item[$field2];
  205. $item['chat_imgs'] = Helper::formatImgsFiled($item['chat_imgs']);
  206. $item['create_date'] = date('Y-m-d H:i', strtotime($item['create_date']));
  207. $item['user_name'] = $users[$uid]['username'] ?? '';
  208. $item['avatar'] = $users[$uid]['avatar'] ? Helper::getImageUrl($users[$uid]['avatar']) : '';
  209. $item['first_name'] = $firsts[$f1] ?? '';
  210. $item['second_name'] = $seconds[$f2]['name'] ?? '';
  211. $item['position'] = $seconds[$f2]['position'] ?? '';
  212. $item['weixin'] = $seconds[$f2]['weixin'] ?? '';
  213. $item['phone'] = $seconds[$f2]['phone'] ?? '';
  214. }
  215. return $list;
  216. }
  217. }