FollowController.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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' => 'wx_school',
  10. 'table2' => 'wx_school_contact',
  11. ],
  12. 'canteen' => [
  13. 'table' => 'canteen_follow',
  14. 'first_id' => 'school_id',
  15. 'second_id' => 'canteen_id',
  16. 'table1' => 'wx_school',
  17. 'table2' => 'wx_canteen',
  18. ],
  19. 'company' => [
  20. 'table' => 'company_follow',
  21. 'first_id' => 'company_id',
  22. 'second_id' => 'contact_id',
  23. 'table1' => 'wx_company',
  24. 'table2' => 'wx_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. 't1.' . $this->tableArr['first_id'] => $firstId,
  126. 't1.' . $this->tableArr['second_id'] => Helper::getPostInt('second_id') ? : null,
  127. ];
  128. $criteria = DbCriteria::simpleCompare($filter)->setOrder('id desc');
  129. $criteria->setAlias('t1')->addCondition('t2.id > 0');
  130. if ($this->type == 'canteen') {
  131. $criteria->setSelect('t1.*,t2.name as second_name,t2.weixin,t2.phone');
  132. } else {
  133. $criteria->setSelect('t1.*,t2.name as second_name,t2.weixin,t2.phone,t2.position');
  134. }
  135. $criteria->join = "left join {$this->tableArr['table2']} t2 on t2.id = t1.{$this->tableArr['second_id']} and t2.is_del = 0";
  136. $data = DB::getListWithCriteria($this->tableArr['table'], $criteria);
  137. $data['records'] = $this->formatFollowList($data['records']);
  138. Helper::ok($data['records']);
  139. }
  140. public function actionSchoolInfo()
  141. {
  142. $this->_info('school');
  143. }
  144. public function actionCanteenInfo()
  145. {
  146. $this->_info('canteen');
  147. }
  148. public function actionCompanyInfo()
  149. {
  150. $this->_info('company');
  151. }
  152. private function _info($type)
  153. {
  154. $this->type = $type;
  155. $this->tableArr = self::TYPE_TABLE_MAP[$this->type];
  156. $id = Helper::getPostInt('id');
  157. if (empty($id)) {
  158. Helper::error('参数错误');
  159. }
  160. $data = DB::getInfoById($this->tableArr['table'], $id);
  161. $data = $this->formatFollowList([$data])[0];
  162. Helper::ok($data);
  163. }
  164. public function actionSchoolList()
  165. {
  166. $this->_list('school');
  167. }
  168. public function actionCanteenList()
  169. {
  170. $this->_list('canteen');
  171. }
  172. public function actionCompanyList()
  173. {
  174. $this->_list('company');
  175. }
  176. private function _list($type)
  177. {
  178. $this->type = $type;
  179. $this->tableArr = self::TYPE_TABLE_MAP[$this->type];
  180. $schoolArr = Helper::getArrParam($_POST, 'school', Helper::PARAM_KEY_TYPE['array_int']);
  181. if ($schoolArr) {
  182. $filter = [
  183. 't1.' . $this->tableArr['first_id'] => $schoolArr[0]? : null,
  184. 't1.' . $this->tableArr['second_id'] => $schoolArr[1]? : null,
  185. ];
  186. } else {
  187. $filter = [
  188. 't1.' . $this->tableArr['first_id'] => Helper::getPostInt('first_id')? : null,
  189. 't1.' . $this->tableArr['second_id'] => Helper::getPostInt('second_id')? : null,
  190. ];
  191. }
  192. if (empty($filter['t1.' .$this->tableArr['first_id']])) {
  193. $filter['t1.' . $this->tableArr['first_id']] = $this->type == 'company' ? $this->getCompanyFilter() : $this->getSchoolFilter();
  194. } else {
  195. $this->checkAuth($filter['t1.' .$this->tableArr['first_id']]);
  196. }
  197. if ($phone = Helper::getPostString('phone')) {
  198. $rs = Helper::arrayColumn(DB::getListWithCriteria($this->tableArr['table2'], DbCriteria::simpleCompare(['phone' => $phone])->setSelect('id')), 'id');
  199. $filter['t1.' . $this->tableArr['second_id']] = $rs?: [-1];
  200. }
  201. $criteria = DbCriteria::simpleCompareWithPage($filter)->setOrder('id desc');
  202. if ($date = Helper::getPostDate('date')) {
  203. $criteria->addBetweenCondition('t1.create_date', $date, $date . ' 23:59:59');
  204. }
  205. $criteria->setAlias('t1')->addCondition('t2.id > 0');
  206. if ($this->type == 'canteen') {
  207. $criteria->setSelect('t1.*,t2.name as second_name,t2.weixin,t2.phone');
  208. } else {
  209. $criteria->setSelect('t1.*,t2.name as second_name,t2.weixin,t2.phone,t2.position');
  210. }
  211. $criteria->join = "left join {$this->tableArr['table2']} t2 on t2.id = t1.{$this->tableArr['second_id']} and t2.is_del = 0";
  212. $data = DB::getListWithCriteria($this->tableArr['table'], $criteria);
  213. $data['records'] = $this->formatFollowList($data['records']);
  214. Helper::ok($data);
  215. }
  216. public function formatFollowList($list)
  217. {
  218. if (empty($list)) {
  219. return [];
  220. }
  221. // 跟进人员
  222. $userIds = array_unique(array_filter(array_column($list, 'user_id')));
  223. $users = [];
  224. if ($userIds) {
  225. $cri = DbCriteria::simpleCompare(['id' => $userIds])->setSelect('id,username,avatar');
  226. $users = Helper::arrayColumn(DB::getListWithCriteria('useradmin', $cri), null, 'id');
  227. }
  228. $field1 = $this->tableArr['first_id'];
  229. $field2 = $this->tableArr['second_id'];
  230. // 校园/公司
  231. $firstIds = array_unique(array_filter(array_column($list, $field1)));
  232. $firsts = [];
  233. if ($firstIds) {
  234. $cri = DbCriteria::simpleCompare(['id' => $firstIds])->setSelect('id,name');
  235. $firsts = Helper::arrayColumn(DB::getListWithCriteria($this->tableArr['table1'], $cri), 'name', 'id');
  236. }
  237. foreach ($list as &$item) {
  238. $uid = $item['user_id'];
  239. $f1 = $item[$field1];
  240. $f2 = $item[$field2];
  241. $item['chat_imgs'] = Helper::formatImgsFiled($item['chat_imgs']);
  242. $item['create_date'] = date('Y-m-d H:i', strtotime($item['create_date']));
  243. $item['user_name'] = $users[$uid]['username'] ?? '';
  244. $item['avatar'] = $users[$uid]['avatar'] ? Helper::getImageUrl($users[$uid]['avatar']) : '';
  245. $item['first_name'] = $firsts[$f1] ?? '';
  246. $item['position'] = $item['position'] ?? '';
  247. }
  248. return $list;
  249. }
  250. }