Message.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\Event;
  4. use addons\qingdongams\model\Message as MessageModel;
  5. use addons\qingdongams\model\Notice;
  6. use addons\qingdongams\model\ExamineRecord;
  7. use addons\qingdongams\model\Record;
  8. use addons\qingdongams\model\ReceivablesPlan;
  9. /**
  10. * @desc 操作文档:https://doc.fastadmin.net/qingdongams
  11. * @desc 软件介绍:https://www.fastadmin.net/store/qingdongams.html
  12. * @desc 售后微信:qingdong_crm
  13. */
  14. /**
  15. * 通知接口
  16. */
  17. class Message extends StaffApi {
  18. protected $noNeedLogin = [];
  19. protected $noNeedRight = [];
  20. //获取工作报告通知列表
  21. public function getWorkreportMessage() {
  22. $limit = input("limit/d", 10);
  23. $where = ['to_staff_id' => $this->auth->id, 'relation_type' => MessageModel::WORKREPORT_TYPE];
  24. $records = MessageModel::where($where)
  25. ->with(['fromStaff','examine','workreport'])
  26. ->order('status asc,id desc')
  27. ->field('id,from_staff_id,relation_id,status,content,send_time')->paginate($limit);
  28. $this->success('请求成功', $records);
  29. }
  30. //待办日程
  31. public function getStartEvent(){
  32. //待办日程
  33. $eventWhere = [
  34. 'status' => ['in', [0, 1]],
  35. 'start_time' => ['lt', date('Y-m-d', strtotime('+1 day'))],
  36. // 'end_time' => ['gt', date('Y-m-d H:i:s')],
  37. 'staff_id' => $this->auth->id,
  38. ];
  39. $event = Event::where($eventWhere)->with([
  40. 'staff',
  41. 'customer',
  42. ])->order('id desc')->select();
  43. $event=collection($event)->toArray();
  44. foreach ($event as $k => $v) {
  45. $v['level'] = Event::getLevel($v['level']);
  46. $v['status'] = Event::getStatus($v['status']);
  47. $event[$k] = $v;
  48. }
  49. $this->success('请求成功', $event);
  50. }
  51. //逾期日程
  52. public function getOverEvent(){
  53. //逾期日程
  54. $overEventsWhere=[
  55. 'start_time' => ['lt', date('Y-m-d', strtotime('+1 day'))],
  56. 'end_time' => ['lt', date('Y-m-d H:i:s')],
  57. 'status' => ['in', [0, 1]],
  58. 'staff_id'=>$this->auth->id,
  59. ];
  60. $event = Event::where($overEventsWhere)->with([
  61. 'staff',
  62. 'customer'
  63. ])->order('id desc')->select();
  64. $event=collection($event)->toArray();
  65. foreach ($event as $k => $v) {
  66. $v['level'] = Event::getLevel($v['level']);
  67. $v['status'] = Event::getStatus($v['status']);
  68. $event[$k] = $v;
  69. }
  70. $this->success('请求成功', $event);
  71. }
  72. //工作动态
  73. public function getWorkSignin(){
  74. $limit = input("limit/d", 10);
  75. $where = ['to_staff_id' => $this->auth->id, 'relation_type' => MessageModel::RECORD_TYPE];
  76. $records = MessageModel::where($where)
  77. ->with(['fromStaff','examine'])
  78. ->order('status asc,id desc')->paginate($limit);
  79. $this->success('请求成功', $records);
  80. }
  81. //发送传阅消息
  82. public function sendMessage(){
  83. $relation_type=input('relation_type');
  84. $relation_id=input('relation_id');
  85. $staff_id=input('staff_id');
  86. $staff_ids=explode(',',$staff_id);
  87. $model=new MessageModel();
  88. switch ($relation_type) {
  89. case $model::CONSUME_TYPE://费用
  90. $rowModel = new \addons\qingdongams\model\Consume();
  91. break;
  92. case $model::CONTRACT_TYPE://合同
  93. $rowModel = new \addons\qingdongams\model\Contract();
  94. break;
  95. case $model::RECEIVABLES_TYPE://回款
  96. $rowModel = new \addons\qingdongams\model\Receivables();
  97. break;
  98. case $model::ACHIEVEMENT_TYPE://业绩目标
  99. $rowModel = new \addons\qingdongams\model\AchievementRecords();
  100. break;
  101. case $model::EVENT_TYPE://日程
  102. $rowModel = new \addons\qingdongams\model\Event();
  103. break;
  104. case $model::WORKORDER_TYPE://工单
  105. $rowModel = new \addons\qingdongams\model\Workorder();
  106. break;
  107. case $model::QUOTE_TYPE://报价单
  108. $rowModel = new \addons\qingdongams\model\Quote();
  109. break;
  110. case $model::CUSTOMER_TYPE://客户
  111. $rowModel = new \addons\qingdongams\model\Customer();
  112. break;
  113. case $model::APPROVAL_TYPE://审批
  114. $rowModel = new \addons\qingdongams\model\Approval();
  115. break;
  116. case $model::PARTS_TYPE://配件
  117. $rowModel = new \addons\qingdongams\model\Parts();
  118. break;
  119. case $model::PARTS_STOCK_RELOAD_TYPE://配件
  120. $rowModel = new \addons\qingdongams\model\PartsStockReload();
  121. break;
  122. default:
  123. $this->error('参数错误');
  124. }
  125. $row=$rowModel->get($relation_id);
  126. if(empty($row)){
  127. $this->error('信息不存在');
  128. }
  129. switch ($relation_type) {
  130. case $model::CONSUME_TYPE://费用
  131. $content=$this->auth->name."传阅费用《{$row['number']}》,邀请您审阅";
  132. break;
  133. case $model::CONTRACT_TYPE://合同
  134. $content=$this->auth->name."传阅合同《{$row['num']}》,邀请您审阅";
  135. break;
  136. case $model::RECEIVABLES_TYPE://回款
  137. $content=$this->auth->name."传阅回款《{$row['number']}》,邀请您审阅";
  138. break;
  139. case $model::ACHIEVEMENT_TYPE://业绩目标
  140. $content=$this->auth->name."传阅目标《{$row['year']}》,邀请您审阅";
  141. break;
  142. case $model::EVENT_TYPE://日程
  143. $content=$this->auth->name."传阅{$row['event_type']}《{$row['title']}》,邀请您审阅";
  144. break;
  145. case $model::WORKORDER_TYPE://工单
  146. $content=$this->auth->name."传阅{$row['workorder_type']}《{$row['title']}》,邀请您审阅";
  147. break;
  148. case $model::PROOF_TYPE://打样
  149. $content=$this->auth->name."传阅打样《{$row['process_number']}》,邀请您审阅";
  150. break;
  151. case $model::QUOTE_TYPE://报价单
  152. $content=$this->auth->name."传阅报价单《{$row['number']}》,邀请您审阅";
  153. break;
  154. case $model::CUSTOMER_TYPE://客户
  155. $content=$this->auth->name."传阅客户《{$row['name']}》,邀请您审阅";
  156. break;
  157. case $model::APPROVAL_TYPE://审批
  158. $content=$this->auth->name."传阅审批,邀请您审阅";
  159. break;
  160. case $model::PARTS_TYPE://配件
  161. $content=$this->auth->name."传阅配件信息,邀请您审阅";
  162. break;
  163. case $model::PARTS_STOCK_RELOAD_TYPE://配件
  164. $content=$this->auth->name."传阅配件信息,邀请您审阅";
  165. break;
  166. default:
  167. $this->error('参数错误');
  168. }
  169. foreach ($staff_ids as $id){
  170. MessageModel::addMessage($relation_type,$relation_id,$id, $this->auth->id, $content);
  171. }
  172. $this->success('请求成功');
  173. }
  174. /**
  175. * 获取通知列表
  176. */
  177. public function getList()
  178. {
  179. $limit = input("limit/d", 10);
  180. $where = ['to_staff_id' => $this->auth->id ,'relation_type'=>['neq',MessageModel::EXAMINE_TYPE]];
  181. $records = MessageModel::where($where)->with(['fromStaff', 'examine'])->order('status asc,id desc')->paginate($limit);
  182. $this->success('请求成功', $records);
  183. }
  184. /**
  185. * 获取审批列表
  186. */
  187. public function getExamineRecord()
  188. {
  189. $limit = input("limit/d", 10);
  190. //合同审批,回款审批、业绩目标审批、办公审批 费用
  191. $relation_type = input('type');
  192. $status = input('status',0);
  193. if($status == 1){
  194. $status = array('in','2,3');
  195. }
  196. $ids = ExamineRecord::where(['relation_type' => $relation_type,
  197. 'status' => $status,
  198. 'check_staff_id' => $this->auth->id])->column('id');
  199. $where = ['to_staff_id' => $this->auth->id, 'relation_id' => ['in', $ids], 'relation_type' => MessageModel::EXAMINE_TYPE];
  200. $records = MessageModel::where($where)->with(['fromStaff', 'examine'])->order('status asc,id desc')->paginate($limit);
  201. $this->success('请求成功', $records);
  202. }
  203. /**
  204. * 获取审批通知详情
  205. */
  206. public function getExamineInfo()
  207. {
  208. $data = [
  209. ExamineRecord::CONTRACT_TYPE => ['count' => 0, 'msg' => '暂无消息'],
  210. ExamineRecord::CONSUME_TYPE => ['count' => 0, 'msg' => '暂无消息'],
  211. ExamineRecord::RECEIVABLES_TYPE => ['count' => 0, 'msg' => '暂无消息'],
  212. ExamineRecord::APPROVAL_TYPE => ['count' => 0, 'msg' => '暂无消息'],
  213. ExamineRecord::ACHIEVEMENT_TYPE => ['count' => 0, 'msg' => '暂无消息'],
  214. ExamineRecord::CARD_TYPE => ['count' => 0, 'msg' => '暂无消息'],
  215. ExamineRecord::LEAVE_TYPE => ['count' => 0, 'msg' => '暂无消息'],
  216. ExamineRecord::QUOTE_TYPE => ['count' => 0, 'msg' => '暂无消息'],
  217. ExamineRecord::PARTS_TYPE => ['count' => 0, 'msg' => '暂无消息'],
  218. ExamineRecord::WORKORDER_TYPE => ['count' => 0, 'msg' => '暂无消息'],
  219. ExamineRecord::INVOICE_TYPE => ['count' => 0, 'msg' => '暂无消息'],
  220. ];
  221. foreach ($data as $type=>$v) {
  222. $ids = ExamineRecord::where(['status' => 0, 'check_staff_id' => $this->auth->id,
  223. 'relation_type' => $type
  224. ])->column('id');
  225. $count = MessageModel::where(['to_staff_id' => $this->auth->id,'relation_id' => ['in', $ids],
  226. 'relation_type' => MessageModel::EXAMINE_TYPE])
  227. ->count();
  228. if ($count) {
  229. $msg = MessageModel::where(['to_staff_id' => $this->auth->id, 'relation_id' => ['in', $ids],
  230. 'relation_type' => MessageModel::EXAMINE_TYPE])
  231. ->order('id desc')
  232. ->value('content');
  233. $data[$type] = ['count' => $count, 'msg' => $msg];
  234. }
  235. }
  236. $this->success('请求成功', $data);
  237. }
  238. /**
  239. * 获取通知详情
  240. */
  241. public function getInfo()
  242. {
  243. $leadsCount = MessageModel::where(['to_staff_id' => $this->auth->id,
  244. 'relation_type' => ['neq', MessageModel::EXAMINE_TYPE], 'status' => 0])->count();
  245. if ($leadsCount > 0) {//其他提醒消息
  246. $leadsDetail = MessageModel::where([
  247. 'relation_type' => ['neq', MessageModel::EXAMINE_TYPE],
  248. 'to_staff_id' => $this->auth->id,
  249. 'status' => 0
  250. ])->order('id desc')->find();
  251. }
  252. $noticeWhere['read_staff_ids'] = ['not like', "%,{$this->auth->id},%"];
  253. $noticeCount = Notice::where($noticeWhere)->count();
  254. if ($noticeCount > 0) {
  255. $noticeDetail = Notice::where(['read_staff_ids' => ['not like', "%,{$this->auth->id},%"]])->order('id desc')->find();
  256. }
  257. $eventOne = Event::where([
  258. 'type' => 1,
  259. 'status' => ['in', [0, 1]],
  260. 'start_time' => ['lt', date('Y-m-d', strtotime('+1 day'))],
  261. 'staff_id|owner_staff_id' => $this->auth->id
  262. ])->count();
  263. //合同审批
  264. $contract = ExamineRecord::where([
  265. 'relation_type' => ExamineRecord::CONTRACT_TYPE,
  266. 'status' => 0,
  267. 'check_staff_id' => $this->auth->id
  268. ])->count();
  269. //回款审批
  270. $receivables = ExamineRecord::where([
  271. 'relation_type' => ExamineRecord::RECEIVABLES_TYPE,
  272. 'status' => 0,
  273. 'check_staff_id' => $this->auth->id
  274. ])->count();
  275. //费用审批
  276. $consume = ExamineRecord::where([
  277. 'relation_type' => ExamineRecord::CONSUME_TYPE,
  278. 'status' => 0,
  279. 'check_staff_id' => $this->auth->id
  280. ])->count();
  281. //业绩目标审批
  282. $achievement = ExamineRecord::where([
  283. 'relation_type' => ExamineRecord::ACHIEVEMENT_TYPE,
  284. 'status' => 0,
  285. 'check_staff_id' => $this->auth->id
  286. ])->count();
  287. //办公审批
  288. $approval = ExamineRecord::where([
  289. 'relation_type' => ExamineRecord::APPROVAL_TYPE,
  290. 'status' => 0,
  291. 'check_staff_id' => $this->auth->id
  292. ])->count();
  293. //补卡审批
  294. $card = ExamineRecord::where([
  295. 'relation_type' => ExamineRecord::CARD_TYPE,
  296. 'status' => 0,
  297. 'check_staff_id' => $this->auth->id
  298. ])->count();
  299. //请假审批
  300. $leave = ExamineRecord::where([
  301. 'relation_type' => ExamineRecord::LEAVE_TYPE,
  302. 'status' => 0,
  303. 'check_staff_id' => $this->auth->id
  304. ])->count();
  305. //待发票审批
  306. $invoice = ExamineRecord::where([
  307. 'relation_type' => ExamineRecord::INVOICE_TYPE,
  308. 'status' => 0,
  309. 'check_staff_id' => $this->auth->id
  310. ])->count();
  311. $receivablesPlan = ReceivablesPlan::where([
  312. 'remind_date' => ['elt', date('Y-m-d')],
  313. 'status' => 0,
  314. 'owner_staff_id' => $this->auth->id
  315. ])->count();
  316. $eventsCount = Event::where([
  317. 'start_time' => ['lt', date('Y-m-d', strtotime('+1 day'))],
  318. 'end_time' => ['lt', date('Y-m-d', strtotime('+1 day'))],
  319. 'status' => ['in', [0, 1]],
  320. 'staff_id' => $this->auth->id,
  321. ])->count();
  322. $where['create_staff_id'] = $this->auth->id;
  323. $where['next_time'] = array(array('egt',date('Y-m-d 00:00:00')),array('lt',date('Y-m-d 23:59:59')));
  324. $where['follow_type'] = ['neq', '其它'];
  325. $where['status'] = 0;
  326. // 待跟进客户
  327. $where1['relation_type'] = 1;
  328. $customerlist = Record::where($where)->where($where1)->column('id');
  329. $customerlist1 = 0;
  330. if($customerlist){
  331. $whereExit['id'] = array('in',$customerlist);
  332. $whereExit['next_time'] = array('gt',date('Y-m-d 23:59:59'));
  333. $customerlist1 = Record::where($whereExit)->count();
  334. }
  335. $customer = count($customerlist) - $customerlist1;
  336. //待跟进合同
  337. $where2['relation_type'] = 3;
  338. $contractlist = Record::where($where)->where($where2)->column('id');
  339. $contractlist1 = 0;
  340. if($contractlist){
  341. $whereExitC['id'] = array('in',$contractlist);
  342. $whereExitC['next_time'] = array('gt',date('Y-m-d 23:59:59'));
  343. $contractlist1 = Record::where($whereExitC)->count();
  344. }
  345. $contracts = count($contractlist)-$contractlist1;
  346. //待跟进线索
  347. $where3['relation_type'] = 4;
  348. $leadlist = Record::where($where)->where($where3)->column('id');
  349. $leadlist1 = 0;
  350. if($leadlist){
  351. $whereExitL['id'] = array('in',$leadlist);
  352. $whereExitL['next_time'] = array('gt',date('Y-m-d 23:59:59'));
  353. $leadlist1 = Record::where($whereExitL)->count();
  354. }
  355. $lead = count($leadlist)-$leadlist1;
  356. //待跟进联系人
  357. $where4['relation_type'] = 2;
  358. $contactslist = Record::where($where)->where($where4)->column('id');
  359. $contactslist1 = 0;
  360. if($contactslist1){
  361. $whereExitCs['id'] = array('in',$contactslist);
  362. $whereExitCs['next_time'] = array('gt',date('Y-m-d 23:59:59'));
  363. $contactslist1 = Record::where($whereExitCs)->count();
  364. }
  365. $contacts = count($contactslist)-$contactslist1;
  366. //待跟进商机
  367. $where5['relation_type'] = 5;
  368. $businesslist = Record::where($where)->where($where5)->column('id');
  369. $businesslist1 = 0;
  370. if($businesslist1){
  371. $whereExitB['id'] = array('in',$businesslist);
  372. $whereExitB['next_time'] = array('gt',date('Y-m-d 23:59:59'));
  373. $businesslist1 = Record::where($whereExitB)->count();
  374. }
  375. $business = count($businesslist)-$businesslist1;
  376. //工作报告
  377. $workreportWhere=[
  378. 'to_staff_id' => $this->auth->id,
  379. 'status' => 0,
  380. 'relation_type' => MessageModel::WORKREPORT_TYPE
  381. ];
  382. $workreportCount = MessageModel::where($workreportWhere)->count();
  383. if ($workreportCount > 0) {//提醒消息
  384. $workreportDetail = MessageModel::where($workreportWhere)->order('id desc')->find();
  385. }
  386. //提醒消息
  387. $data = [
  388. 'leads' => ['count' => $leadsCount, 'msg' => $leadsDetail['content'] ?? '暂无消息'],//提醒消息
  389. 'workreport' => ['count' => $workreportCount, 'msg' => $workreportDetail['content'] ?? '暂无消息'],//工作报告
  390. 'examine' => ['count' => $contract + $receivables+$consume+$achievement+$approval+$card+$leave+$invoice,
  391. 'msg' => '审批消息提醒'],//提醒消息
  392. 'notice' => ['count' => $noticeCount, 'msg' => $noticeDetail['title'] ?? '暂无消息'],//系统公告
  393. 'agent' => [
  394. 'count' => $eventOne + $receivablesPlan+ $customer + $contracts + $lead + $contacts + $business,
  395. 'msg' => '您有新的待办事项需要处理!'
  396. ],//待办事项
  397. 'task' => [
  398. 'count' => $eventsCount,
  399. 'msg' => '即将超时的任务提醒!'
  400. ],//超时任务
  401. 'total' => $leadsCount + $noticeCount + $eventOne + $contract + $receivables + $receivablesPlan + $eventsCount+ $customer + $contracts + $lead + $contacts + $business+$consume+$achievement+$approval+$card+$leave+$invoice,
  402. 'userinfo' => $this->auth->getUserinfo()
  403. ];
  404. $this->success('请求成功', $data);
  405. }
  406. }