FollowController.php 8.2 KB

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