FollowController.php 9.1 KB

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