FollowController.php 9.0 KB

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