LogLogic.php 42KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. <?php
  2. namespace app\oa\logic;
  3. use app\admin\model\Common;
  4. use app\oa\model\Log;
  5. use think\Db;
  6. use app\admin\model\Comment as CommentModel;
  7. class LogLogic extends Common
  8. {
  9. protected $monthName = [
  10. '1' => '一',
  11. '2' => '二',
  12. '3' => '三',
  13. '4' => '四',
  14. '5' => '五',
  15. '6' => '六',
  16. ];
  17. //时间
  18. public function pastTime()
  19. {
  20. $dataTime['start_time'] = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
  21. $dataTime['end_time'] = mktime(23, 59, 59, date('m'), date('d'), date('Y'));
  22. return $dataTime;
  23. }
  24. /**
  25. * [getDataList 日志list]
  26. * @param [string] $map [查询条件]
  27. * @param [number] $page [当前页数]
  28. * @param [number] $limit [每页数量]
  29. * @return [array] [description]
  30. * @author Michael_xu
  31. */
  32. public function getDataList($request)
  33. {
  34. $userModel = new \app\admin\model\User();
  35. $commonModel = new \app\admin\model\Comment();
  36. $recordModel = new \app\admin\model\Record();
  37. $user_id = $request['read_user_id'];
  38. $by = $request['by'] ?: '';
  39. $map = [];
  40. $search = $request['search'];
  41. if (isset($request['search']) && $request['search']) {
  42. //普通筛选
  43. $searchMap = function ($query) use ($search) {
  44. $query->where('log.content', array('like', '%' . $search . '%'))
  45. ->whereOr('log.tomorrow', array('like', '%' . $search . '%'))
  46. ->whereOr('log.question', array('like', '%' . $search . '%'));
  47. };
  48. }
  49. if ($request['category_id']) {
  50. $map['log.category_id'] = $request['category_id'];
  51. }
  52. if ($request['create_user_id']) {
  53. $map['log.create_user_id'] = $request['create_user_id'];
  54. }
  55. if ($request['type']) {
  56. $timeAry = ByDateTime($request['type']);
  57. $between_time = [$timeAry[0], $timeAry[1]];
  58. $map['log.create_time'] = ['between', $between_time];
  59. } else {
  60. //自定义时间
  61. $start_time = $request['start_time'] ? strtotime($request['start_time'].' 00:00:00') : strtotime(date('Y-m-01', time()));
  62. $end_time = $request['end_time'] ? strtotime($request['end_time'].' 23:59:59') : strtotime(date('Y-m-01', time()) . ' +1 month -1 day');
  63. $map['log.create_time'] = ['between', [$start_time, $end_time]];
  64. }
  65. $requestData = $this->requestData();
  66. //获取权限范围内的员工
  67. //获取权限范围内的员工
  68. $auth_user_ids = getSubUserId();
  69. $dataWhere['user_id'] = $user_id;
  70. $dataWhere['structure_id'] = $request['structure_id'];
  71. $dataWhere['auth_user_ids'] = $auth_user_ids;
  72. $logMap = '';
  73. if ($request['send_user_id'] != '') {
  74. $map['log.create_user_id'] = ['in', trim(arrayToString($request['send_user_id']), ',')];
  75. }
  76. switch ($by) {
  77. case 'me' :
  78. $map['log.create_user_id'] = $user_id;
  79. break;
  80. case 'other':
  81. $logMap = function ($query) use ($dataWhere) {
  82. $query->where('log.send_user_ids', array('like', '%,' . $dataWhere['user_id'] . ',%'))
  83. ->whereOr('log.send_structure_ids', array('like', '%,' . $dataWhere['structure_id'] . ',%'));
  84. };
  85. break;
  86. default :
  87. $logMap = function ($query) use ($dataWhere) {
  88. $query->where('log.create_user_id', array('in', implode(',', $dataWhere['auth_user_ids'])))
  89. ->whereOr('log.send_user_ids', array('like', '%,' . $dataWhere['user_id'] . ',%'))
  90. ->whereOr('log.send_structure_ids', array('like', '%,' . $dataWhere['structure_id'] . ',%'));
  91. };
  92. break;
  93. }
  94. $list = Db::name('OaLog')
  95. ->where($map)
  96. ->where($logMap)
  97. ->where($searchMap)
  98. ->alias('log')
  99. ->join('__ADMIN_USER__ user', 'user.id = log.create_user_id', 'LEFT')
  100. ->field('log.*,user.realname')
  101. ->order('log.update_time desc')
  102. ->select();
  103. foreach ($list as $k => $v) {
  104. $param['type_id'] = $v['log_id'];
  105. $param['type'] = 'oa_log';
  106. $list[$k]['replyList'] = $commonModel->read($param);
  107. $list[$k]['replyList'] = arrayToString(array_column($list[$k]['replyList'], 'content'));
  108. $list[$k]['replyList'] = str_replace(',', ' ', $list[$k]['replyList']);
  109. $list[$k]['send_user_name'] = arrayToString(array_column($userModel->getListByStr($v['send_user_ids']), 'realname'));
  110. $list[$k]['send_user_name'] = str_replace(",", " ", $list[$k]['send_user_name']);
  111. if ($v['category_id'] == 1) {
  112. $list[$k]['category_name'] = '日报';
  113. } elseif ($v['category_id'] == 2) {
  114. $list[$k]['category_name'] = '周报';
  115. } else {
  116. $list[$k]['category_name'] = '月报';
  117. }
  118. $list[$k]['create_time'] = date('Y-m-d', $v['create_time']);
  119. //相关业务
  120. $relationArr = $recordModel->getListByRelationId('log', $v['log_id']);
  121. $list[$k]['relation'] = arrayToString(array_column($relationArr['businessList'], 'name')) . ' ' .
  122. arrayToString(array_column($relationArr['contactsList'], 'name')) . ' ' .
  123. arrayToString(array_column($relationArr['contractList'], 'name')) . ' ' .
  124. arrayToString(array_column($relationArr['customerList'], 'name'));
  125. $list[$k]['relation'] = str_replace(',', ' ', $list[$k]['relation']);
  126. }
  127. return $list;
  128. }
  129. /**
  130. * 随机获取日志欢迎语
  131. * @return array|int|string
  132. * @throws \think\db\exception\DataNotFoundException
  133. * @throws \think\db\exception\ModelNotFoundException
  134. * @throws \think\exception\DbException
  135. */
  136. public function LogWelcomeSpeech()
  137. {
  138. $logWelcome = db('admin_oalog_rule')->where('type', 4)->find();
  139. $data = unserialize($logWelcome['mark']);
  140. $key = array_rand($data);
  141. return $data[$key];
  142. }
  143. /**
  144. * 日报完成情况
  145. * @param $param
  146. */
  147. public function completeStats($param)
  148. {
  149. # 是否开启了日、周、月报
  150. $logStatus = Db::name('admin_oalog_rule')->where('type', $param['type'])->value('status');
  151. //每日完成数量
  152. $users = getSubUserId(false, 0, $param['user_id']);
  153. $logCount = Db::name('admin_oalog_rule')->where(['userIds' => ['like', '%' . $param['user_id'] . '%'], 'type' => $param['type']])->find();
  154. if ($logCount['userIds'] == '') {
  155. $logCount = count($users) ?: 1;
  156. } else {
  157. $logCount = count(array_intersect(stringToArray($logCount['userIds']), $users)) ?: 0;
  158. }
  159. //日志统计时间
  160. $type = db('admin_oalog_rule')->where('type', $param['type'])->find();
  161. if ($param['type'] == 1) {
  162. $start_time = strtotime(date('Y-m-d', time()) . ' ' . $type['start_time'] . ':00');
  163. $end_time = strtotime(date('Y-m-d', time()) . ' ' . $type['end_time'] . ':00');
  164. $between_time = [$start_time, $end_time];
  165. $timeCount = array('between', $between_time);
  166. } elseif ($param['type'] == 2) {
  167. //本周
  168. $start_time = strtotime(date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - $type['start_time']) * 24 * 3600)));
  169. $end_time = strtotime(date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - $type['end_time']) * 24 * 3600)) . '23:59:59');
  170. $between_time = [$start_time, $end_time];
  171. $timeCount = array('between', $between_time);
  172. } elseif ($param['type'] == 3) {
  173. //本月
  174. $start_time = strtotime(date('Y-m-d', strtotime(date('Y-m', time()) . '-' . date($type['start_time'], time()) . ' 00:00:00')));
  175. $end_time = strtotime(date('Y-m-d', strtotime(date('Y-m', time()) . '-' . date($type['end_time'], time()) . ' 23:59:59')));
  176. $between_time = [$start_time, $end_time];
  177. $timeCount = array('between', $between_time);
  178. }
  179. $endCount = Db::name('OaLog')->where(['create_time' => $timeCount, 'create_user_id' => ['in', arrayToString($users)]])->count();
  180. $data = [];
  181. $data['logCount'] = $logCount ?: 0;
  182. $data['endCount'] = $endCount ?: 0;
  183. # 判断是否显示日志统计按钮(日报、周报、月报)
  184. $data['status'] = !empty($logStatus) ? 1 : 0;
  185. return $data;
  186. }
  187. /**
  188. * 月报完成情况
  189. * @param $param
  190. */
  191. public function logBulletin($param)
  192. {
  193. //本月完成日志篇数
  194. $timeAry = getTimeByType('month');
  195. $between_time = [$timeAry[0], $timeAry[1]];
  196. $mothCount = array('between', $between_time);
  197. //日志统计时间
  198. $type = db('admin_oalog_rule')->where('type', 1)->find();
  199. $start_time = strtotime(date('Y-m-d', time()) . ' ' . $type['start_time'] . ':00');
  200. $end_time = strtotime(date('Y-m-d', time()) . ' ' . $type['end_time'] . ':00');
  201. $between_time = [$start_time, $end_time];
  202. $timeCount = array('between', $between_time);
  203. $mothEndCount = Db::name('OaLog')->where(['create_time' => $mothCount, 'create_user_id' => $param['user_id']])->count();
  204. $start_log = Db::name('OaLog')->where(['create_time' => $timeCount, 'create_user_id' => $param['user_id']])->count();
  205. $data = [];
  206. $data['logCount'] = 0;
  207. $data['startLog'] = $start_log ?: 0;
  208. $data['mothEndCount'] = $mothEndCount;
  209. return $data;
  210. }
  211. /**
  212. * 每日销售简报
  213. * @param $param
  214. * @return array
  215. * @throws \think\db\exception\DataNotFoundException
  216. * @throws \think\db\exception\ModelNotFoundException
  217. * @throws \think\exception\DbException
  218. */
  219. public function oneBulletin($param)
  220. {
  221. $user_id = $param['user_id'];
  222. $start_time = $this->pastTime();
  223. $between_time = [$start_time['start_time'], $start_time['end_time']];
  224. $map['owner_user_id'] = $user_id;
  225. $map['create_time'] = array('between', $between_time);
  226. $map1['update_time'] = array('between', $between_time);
  227. $customerNum = Db::name('CrmCustomer')
  228. ->where($map1)
  229. ->count();
  230. $businessNum = Db::name('CrmBusiness')
  231. ->where($map)
  232. ->count();
  233. $contractNum = Db::name('CrmContract')
  234. ->where($map)
  235. ->count();
  236. $receivablesMoneyNum = Db::name('CrmReceivables')
  237. ->where($map)
  238. ->sum('money');
  239. unset($map['owner_user_id']);
  240. $map['create_user_id'] = $user_id;
  241. $recordNum = db('crm_activity')
  242. ->where($map)
  243. ->where(['type' => 1, 'activity_type' => ['<', 7],'status'=>1])
  244. ->count();
  245. $data = [];
  246. $data['data']['customerNum'] = $customerNum;
  247. $data['data']['businessNum'] = $businessNum;
  248. $data['data']['contractNum'] = $contractNum;
  249. $data['data']['receivablesMoneyNum'] = $receivablesMoneyNum;
  250. $data['data']['recordNum'] = $recordNum;
  251. return $data;
  252. }
  253. /**
  254. * 今日新增客户
  255. * @param $param
  256. * @return array
  257. * @throws \think\db\exception\DataNotFoundException
  258. * @throws \think\db\exception\ModelNotFoundException
  259. * @throws \think\exception\DbException
  260. */
  261. public function Bulletin($param)
  262. {
  263. $user_id = $param['user_id'];
  264. $search = $param['search'];
  265. $start_time = $this->pastTime();
  266. $log_id = $param['log_id'] ?: 0;
  267. if (empty($log_id)) {
  268. $between_time = [$start_time['start_time'], $start_time['end_time']];
  269. $map1['customer.owner_user_id'] = $user_id;
  270. $map2['business.owner_user_id'] = $user_id;
  271. $map3['contract.owner_user_id'] = $user_id;
  272. $map4['receivables.owner_user_id'] = $user_id;
  273. } else {
  274. $item = Db::name('OaLog')->where('log_id', $log_id)->find();
  275. $between_time = [strtotime(date('Y-m-d 00:00:00', $item['create_time'])), strtotime(date('Y-m-d 23:59:59', $item['create_time']))];
  276. $map1['customer.owner_user_id'] = $item['create_user_id'];
  277. $map2['business.owner_user_id'] = $item['create_user_id'];
  278. $map3['contract.owner_user_id'] = $item['create_user_id'];
  279. $map4['receivables.owner_user_id'] = $item['create_user_id'];
  280. }
  281. if ($search) {
  282. $map['name'] = array('like', '%' . $search . '%');
  283. }
  284. $customerModel=new \app\crm\model\Customer();
  285. $customerMap =$customerModel->getWhereByCustomer();
  286. $type = $param['log_type'];
  287. switch ($type) {
  288. case '1':
  289. if ($search) $map['customer.name'] = array('like', '%' . $search . '%');
  290. $map['customer.update_time'] = array('between', $between_time);
  291. $activityData = Db::name('CrmCustomer')
  292. ->alias('customer')
  293. ->join('__ADMIN_USER__ user', 'user.id = customer.owner_user_id', 'LEFT')
  294. ->where($map)
  295. ->where($map1)
  296. ->where($customerMap)
  297. ->order('customer.customer_id desc')
  298. ->field('customer.customer_id,customer.name,customer.deal_status,customer.create_time,user.realname as owner_user_name,customer.last_time')
  299. ->page($param['page'],$param['limit'])
  300. ->select();
  301. $dataCount = Db::name('CrmCustomer')
  302. ->alias('customer')
  303. ->join('__ADMIN_USER__ user', 'user.id = customer.owner_user_id', 'LEFT')
  304. ->where($map)
  305. ->where($map1)
  306. ->where($customerMap)
  307. ->count();
  308. break;
  309. case '2':
  310. $map['business.name'] = array('like', '%' . $search . '%');
  311. $map['business.create_time'] = array('between', $between_time);
  312. $activityData = Db::name('CrmBusiness')
  313. ->alias('business')
  314. ->join('__CRM_BUSINESS_STATUS__ status', 'status.status_id=business.status_id')
  315. ->join('__ADMIN_USER__ user', 'user.id = business.owner_user_id', 'LEFT')
  316. ->where($map)
  317. ->where($map2)
  318. ->order('business.business_id desc')
  319. ->page($param['page'],$param['limit'])
  320. ->field('business.business_id,business.name,status.name as status_name,business.create_time,user.realname as owner_user_name,business.last_time')
  321. ->select();
  322. $dataCount = Db::name('CrmBusiness')
  323. ->alias('business')
  324. ->join('__CRM_BUSINESS_STATUS__ status', 'status.status_id=business.status_id')
  325. ->where($map)
  326. ->where($map2)
  327. ->count();
  328. break;
  329. case '3':
  330. $map['contract.name'] = array('like', '%' . $search . '%');
  331. $map['contract.create_time'] = array('between', $between_time);
  332. $activityData = Db::name('CrmContract')
  333. ->alias('contract')
  334. ->join('__ADMIN_USER__ u', 'u.id = contract.owner_user_id', 'LEFT')
  335. ->where($map)
  336. ->where($map3)
  337. ->order('contract.contract_id desc')
  338. ->page($param['page'],$param['limit'])
  339. ->field('contract.contract_id,contract.name,contract.create_time,contract.check_status,u.realname as order_user_name')
  340. ->select();
  341. $dataCount = Db::name('CrmContract')
  342. ->alias('contract')
  343. ->join('__ADMIN_USER__ u', 'u.id = contract.owner_user_id', 'LEFT')
  344. ->where($map)
  345. ->where($map3)
  346. ->count();
  347. break;
  348. case '4':
  349. $map['receivables.number'] = array('like', '%' . $search . '%');
  350. $map['receivables.create_time'] = array('between', $between_time);
  351. $activityData = Db::name('CrmReceivables')
  352. ->alias('receivables')
  353. ->join('__ADMIN_USER__ user', 'user.id = receivables.owner_user_id', 'LEFT')
  354. ->field('receivables.receivables_id,receivables.number,receivables.return_time,user.realname as owner_user_name')
  355. ->where($map)
  356. ->where($map4)
  357. ->page($param['page'],$param['limit'])
  358. ->order('receivables.receivables_id desc')
  359. ->select();
  360. $dataCount = Db::name('CrmReceivables')
  361. ->alias('receivables')
  362. ->join('__ADMIN_USER__ user', 'user.id = receivables.owner_user_id', 'LEFT')
  363. ->where($map)
  364. ->where($map4)
  365. ->count();
  366. break;
  367. }
  368. foreach ($activityData as $k => $v) {
  369. $activityData[$k]['create_time'] = $v['create_time'] ? date('Y-m-d H:i:s', $v['create_time']) : null;
  370. $activityData[$k]['last_time'] = $v['last_time'] ? date('Y-m-d H:i:s', $v['last_time']) : null;
  371. }
  372. $data = [];
  373. $data['list'] = $activityData;
  374. $data['dataCount'] = $dataCount ?: 0;
  375. return $data;
  376. }
  377. /**
  378. * 查看以往日志
  379. * @param $param
  380. * @return array
  381. */
  382. public function lastLog($param)
  383. {
  384. $user_id = $param('user_id');
  385. $activityData = db('oa_log')
  386. ->where(['send_user_ids' => ['like', '%' . $user_id . '%']])
  387. ->page($param['page'], $param['limit'])
  388. ->order('log_id desc')
  389. ->select();
  390. $dataCount = db('oa_log')->where(['send_user_ids' => ['like', '%' . $user_id . '%']])->count();
  391. $data = [];
  392. $data['page']['list'] = $activityData;
  393. $data['page']['dataCount'] = $dataCount ?: 0;
  394. if ($param['page'] != 1 && ($param['page'] * $param['limit']) >= $dataCount) {
  395. $data['page']['firstPage'] = false;
  396. $data['page']['lastPage'] = true;
  397. } else if ($param['page'] != 1 && (int)($param['page'] * $param['limit']) < $dataCount) {
  398. $data['page']['firstPage'] = false;
  399. $data['page']['lastPage'] = false;
  400. } else if ($param['page'] == 1) {
  401. $data['page']['firstPage'] = true;
  402. $data['page']['lastPage'] = false;
  403. }
  404. return $data;
  405. }
  406. /**
  407. * 跟进记录
  408. * @param $param
  409. * @return array
  410. * @throws \think\db\exception\DataNotFoundException
  411. * @throws \think\db\exception\ModelNotFoundException
  412. * @throws \think\exception\DbException
  413. */
  414. public function activity($param)
  415. {
  416. if ($param['search']) {
  417. $type['t.content'] = array('like', '%' . $param['search'] . '%');
  418. }
  419. if ($param['crmType'] == 0) {
  420. $type['t.activity_type'] = ['in', [1, 2, 3, 5, 6]];
  421. } else {
  422. $type['t.crmType'] = $param['activity_type'];
  423. }
  424. if ($param['type']) {
  425. $timeAry = getTimeByType($param['type']);
  426. $between_time = [$timeAry[0], $timeAry[1]];
  427. $type['t.create_time'] = array('between', $between_time);
  428. }
  429. if ($param['queryType'] == 0) {
  430. $type['t.type'] = ['in', [1, 4]];
  431. } else {
  432. $type['t.type'] = $param['queryType'];
  433. }
  434. if ($param['subUser'] == 'mycreate') {
  435. $type['t.create_user_id'] = $param['user_id'];
  436. //下属创建
  437. } elseif ($param['subUser'] == 'branchcreate') {
  438. $subList = getSubUserId(false, 0, $param['user_id']);
  439. $subStr = $subList ? implode(',', $subList) : '-1';
  440. $type['t.create_user_id'] = array('in', $subStr);
  441. } else {
  442. $userIds = getSubUserId(true, 1, $param['user_id']);
  443. $subStr = $userIds ? implode(',', $userIds) : '-1';
  444. $type['t.create_user_id'] = array('in', $subStr);
  445. }
  446. $list = db('crm_activity')
  447. ->alias('t')
  448. ->join('__ADMIN_USER__ user', 'user.id = t.create_user_id', 'LEFT')
  449. ->field('t.content,t.next_time as update_time,category,t.activity_type,t.type,t.activity_id,t.activity_type_id,user.realname as create_user_name,user.thumb_img')
  450. ->where($type)
  451. ->page($param['page'], $param['limit'])
  452. ->select();
  453. $dataCount = db('crm_activity')
  454. ->alias('t')
  455. ->join('__ADMIN_USER__ user', 'user.id = t.create_user_id', 'LEFT')
  456. ->field('t.content,t.next_time,category,t.activity_type,user.realname ,user.thumb_img ')
  457. ->where($type)
  458. ->count();
  459. foreach ($list as $k => $v) {
  460. // 业务名称(客户、线索、合同...)
  461. if ($v['type'] == 1 && $v['activity_type'] == 2) {
  462. $list[$k]['activity_type_name'] = Db::name('crm_customer')->where('customer_id', $v['activity_type_id'])->value('name');
  463. }
  464. }
  465. $data = [];
  466. $data['page']['list'] = $list;
  467. $data['page']['dataCount'] = $dataCount ?: 0;
  468. if ($param['page'] != 1 && ($param['page'] * $param['limit']) >= $dataCount) {
  469. $data['page']['firstPage'] = false;
  470. $data['page']['lastPage'] = true;
  471. } else if ($param['page'] != 1 && (int)($param['page'] * $param['limit']) < $dataCount) {
  472. $data['page']['firstPage'] = false;
  473. $data['page']['lastPage'] = false;
  474. } else if ($param['page'] == 1) {
  475. $data['page']['firstPage'] = true;
  476. $data['page']['lastPage'] = false;
  477. }
  478. return $data;
  479. }
  480. /**
  481. * 已完成日志
  482. * @param $param
  483. * @return array
  484. * @throws \think\db\exception\DataNotFoundException
  485. * @throws \think\db\exception\ModelNotFoundException
  486. * @throws \think\exception\DbException
  487. */
  488. public function completeLog($param)
  489. {
  490. $userModel = new \app\admin\model\User();
  491. $type = db('admin_oalog_rule')->where('type', 1)->find();
  492. $start_time = strtotime(date('Y-m-d', time()) . ' ' . $type['start_time'] . ':00');
  493. $end_time = strtotime(date('Y-m-d', time()) . ' ' . $type['end_time'] . ':00');
  494. $between_time = [$start_time, $end_time];
  495. $users = getSubUserId(false, 0, $param['user_id']);
  496. $between['create_time'] = array('between', $between_time);
  497. $list = db('oa_log')
  498. ->where($between)
  499. ->order('create_user_id', 'desc')
  500. ->column('create_user_id');
  501. if ($type['userIds'] == '') {
  502. $where['id'] = array('in', implode(',', array_intersect($users, $list)));
  503. } else {
  504. $users_diff = array_intersect($users, stringToArray($type['userIds']));//如果设置写日志人 显示要写下属id
  505. $where['id'] = array('in', implode(',', array_intersect($users_diff, $list)));
  506. }
  507. // if($type['userIds']==''){
  508. // $users=Db::name('AdminUser')->where('status',1)->column('id');
  509. // $where['log.create_user_id'] = array('in', implode(',',$users));
  510. // }else{
  511. // $where['log.create_user_id'] = array('in', implode(',', array_intersect(stringToArray($type['userIds']), $list)));
  512. // }
  513. $where['user.realname'] = array('like', '%' . $param['search'] . '%');
  514. $where['log.create_time'] = array('between', $between_time);
  515. $item = db('oa_log')
  516. ->alias('log')
  517. ->join('__ADMIN_USER__ user', 'user.id = log.create_user_id', 'LEFT')
  518. ->field('user.realname as user_name,log.content,log.create_time,log.log_id')
  519. ->where($where)
  520. ->order('log.log_id', 'desc')
  521. ->select();
  522. foreach ($item as $k => $v) {
  523. $item[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
  524. }
  525. $data = [];
  526. $data['list'] = $item;
  527. return $data;
  528. }
  529. /**
  530. * 未完成日志
  531. * @param $param
  532. * @return array
  533. * @throws \think\db\exception\DataNotFoundException
  534. * @throws \think\db\exception\ModelNotFoundException
  535. * @throws \think\exception\DbException
  536. */
  537. public function inCompleteLog($param)
  538. {
  539. //下属员工未完成
  540. $type = db('admin_oalog_rule')->where('type', 1)->find();
  541. $start_time = strtotime(date('Y-m-d', time()) . ' ' . $type['start_time'] . ':00');
  542. $end_time = strtotime(date('Y-m-d', time()) . ' ' . $type['end_time'] . ':00');
  543. $between_time = [$start_time, $end_time];
  544. $users = getSubUserId(false, 0, $param['user_id']);
  545. $between['create_time'] = array('between', $between_time);
  546. $list = db('oa_log')
  547. ->where($between)
  548. ->order('create_user_id', 'desc')
  549. ->column('create_user_id');
  550. if ($type['userIds'] == '') {
  551. $diff = array_intersect($users, $list);// 下属已写
  552. $where['id'] = array('in', implode(',', array_diff_assoc($users, $diff)));
  553. } else {
  554. $users_diff = array_intersect($users, stringToArray($type['userIds']));//如果设置写日志人 显示要写下属
  555. $diff = array_intersect($users_diff, $list);// 下属已写
  556. $where['id'] = array('in', implode(',', array_diff_assoc($users_diff, $diff)));
  557. }
  558. $where['realname'] = array('like', '%' . $param['search'] . '%');
  559. $item = db('admin_user')
  560. ->field('realname as user_name')
  561. ->where($where)
  562. ->order('id', 'desc')
  563. ->select();
  564. $data = [];
  565. foreach ($item as $k => $v) {
  566. $item[$k]['content'] = '';
  567. $item[$k]['create_time'] = '';
  568. }
  569. $data['list'] = $item;
  570. return $data;
  571. }
  572. /**
  573. * 日志导出
  574. * @param $param
  575. */
  576. public function excelExport($param)
  577. {
  578. $data = $this->getDataList($param);
  579. $excelModel = new \app\admin\model\Excel();
  580. $file_name = 'log';
  581. $title = '日志列表';
  582. $field_list = [
  583. '0' => ['name' => '日志类型', 'field' => 'category_name'],
  584. '1' => ['name' => '创建日期', 'field' => 'create_time'],
  585. '2' => ['name' => '创建人', 'field' => 'realname'],
  586. '3' => ['name' => '发送给', 'field' => 'send_user_name'],
  587. '4' => ['name' => '今日工作内容', 'field' => 'content'],
  588. '5' => ['name' => '明日工作内容', 'field' => 'tomorrow'],
  589. '6' => ['name' => '遇到问题', 'field' => 'question'],
  590. '7' => ['name' => '关联业务', 'field' => 'relation'],
  591. '8' => ['name' => '回复', 'field' => 'replyList'],
  592. ];
  593. return $excelModel->taskExportCsv($file_name, $field_list, $title, $data);
  594. }
  595. /**
  596. * 回复列表
  597. * @param $param
  598. */
  599. public function CommentList($param)
  600. {
  601. $commonModel = new CommentModel();
  602. $param['type_id'] = $param['log_id'];
  603. $param['type'] = 'oa_log';
  604. $replyList = $commonModel->read($param);
  605. $data = [];
  606. $data['list'] = $replyList;
  607. return $data;
  608. }
  609. /**
  610. * 销售简报跟进数量统计
  611. *
  612. * @param $param 参数
  613. */
  614. public function activityCount($param)
  615. {
  616. $user_id = $param['user_id'];
  617. $item = Db::name('OaLog')->where('log_id', $param['log_id'])->find();
  618. $start_time = $this->pastTime();
  619. if (empty($param['log_id'])) {
  620. $between_time = [$start_time['start_time'], $start_time['end_time']];
  621. $map['create_time'] = array('between', $between_time);
  622. $map['create_user_id'] =$user_id;
  623. } else {
  624. $start_time = strtotime(date("Y-m-d", $item['create_time']));
  625. $end_time = strtotime(date("Y-m-d H:i:s", $item['create_time']));
  626. $between_time = [$start_time, $end_time];
  627. $map['create_time'] = array('between', $between_time);
  628. $map['create_user_id'] = $item['create_user_id'];
  629. }
  630. $map['status']=1;
  631. $typesList = ['1', '2', '3', '5', '6'];
  632. foreach ($typesList as $k => $v) {
  633. $activityData = db('crm_activity')->where($map)->where(['type' => 1, 'activity_type' => $v])->count();
  634. if($v==1){
  635. $arr[$k]['types'] ='crm_leads';
  636. $arr[$k]['activity_type'] =1;
  637. }elseif ($v==2){
  638. $arr[$k]['types'] ='crm_customer';
  639. $arr[$k]['activity_type'] =2;
  640. }elseif ($v==3){
  641. $arr[$k]['types'] ='crm_contacts';
  642. $arr[$k]['activity_type'] =3;
  643. }elseif ($v==5){
  644. $arr[$k]['types'] ='crm_business';
  645. $arr[$k]['activity_type'] =5;
  646. }elseif ($v==6){
  647. $arr[$k]['types'] ='crm_contract';
  648. $arr[$k]['activity_type'] =6;
  649. }
  650. $arr[$k]['dataCount'] = $activityData;
  651. }
  652. $data = $arr;
  653. return $data;
  654. }
  655. /**
  656. *
  657. * @param $param
  658. * @return array
  659. * @throws \think\Exception
  660. * @throws \think\db\exception\DataNotFoundException
  661. * @throws \think\db\exception\ModelNotFoundException
  662. * @throws \think\exception\DbException
  663. */
  664. public function activityList($param)
  665. {
  666. $start_time = $this->pastTime();
  667. if (empty($param['log_id'])) {
  668. $between_time = [$start_time['start_time'], $start_time['end_time']];
  669. $where_activity['t.create_time'] = array('between', $between_time);
  670. $where_activity['t.create_user_id'] = $param['user_id'];
  671. } else {
  672. $item = Db::name('OaLog')->where('log_id', $param['log_id'])->find();
  673. $start_time = strtotime(date("Y-m-d", $item['create_time']));
  674. $end_time = strtotime(date("Y-m-d H:i:s", $item['create_time']));
  675. $between_time = [$start_time, $end_time];
  676. $where_activity['t.create_time'] = array('between', $between_time);
  677. $where_activity['t.create_user_id'] = $item['create_user_id'];
  678. }
  679. # 跟进记录类型
  680. $where_activity['t.activity_type'] = $param['activity_type'];
  681. $where_activity['t.status'] = 1;
  682. $where_activity['t.type'] = 1;
  683. $list = db('crm_activity')
  684. ->alias('t')
  685. ->join('__ADMIN_USER__ user', 'user.id = t.create_user_id', 'LEFT')
  686. ->where($where_activity)
  687. ->field('t.content,t.next_time,t.update_time,t.category,t.activity_type,t.type,t.activity_id,t.activity_type_id,user.realname as create_user_name,user.thumb_img')
  688. ->page($param['page'], $param['limit'])
  689. ->order('t.create_time desc')
  690. ->select();
  691. $dataCount = db('crm_activity')->alias('t')
  692. ->join('__ADMIN_USER__ user', 'user.id = t.create_user_id', 'LEFT')->where($where_activity)->count();
  693. foreach ($list as $k => $v) {
  694. // 业务名称(客户、线索、合同...)
  695. if ($param['activity_type'] == 1) {
  696. $activity_name = Db::name('crm_leads')->where('leads_id', $v['activity_type_id'])->find();
  697. $list[$k]['activity_type_name'] = $activity_name['name'];
  698. }
  699. if ($param['activity_type'] == 2) {
  700. $activity_name = Db::name('crm_customer')->where('customer_id', $v['activity_type_id'])->find();
  701. $list[$k]['activity_type_name'] = $activity_name['name'];
  702. $activity_business = Db::name('crm_business')->where('business_id', $v['activity_type_id'])->select();
  703. $activity_contacts = Db::name('crm_contacts')->where('contacts_id', $v['activity_type_id'])->select();
  704. $list[$k]['business_list'] = $activity_business ?: [];
  705. $list[$k]['contacts_list'] = $activity_contacts ?: [];
  706. }
  707. if ($param['activity_type'] == 3) {
  708. $activity_name = Db::name('crm_contacts')->where('contacts_id', $v['activity_type_id'])->find();
  709. $list[$k]['activity_type_name'] = $activity_name['name'];
  710. }
  711. if ($param['activity_type'] == 5) {
  712. $activity_name = Db::name('crm_business')->where('business_id', $v['activity_type_id'])->find();
  713. $list[$k]['activity_type_name'] = $activity_name['name'];
  714. }
  715. if ($param['activity_type'] == 6) {
  716. $activity_name = Db::name('crm_contract')->where('contract_id', $v['activity_type_id'])->find();
  717. $list[$k]['activity_type_name'] = $activity_name['name'];
  718. }
  719. $list[$k]['create_time'] = !empty($v['create_time']) ? date('Y-m-d H:i:s', $v['create_time']) : null;
  720. $list[$k]['update_time'] = !empty($v['update_time']) ? date('Y-m-d H:i:s', $v['update_time']) : null;
  721. $list[$k]['next_time'] = !empty($v['next_time']) ? date('Y-m-d H:i:s', $v['next_time']) : null;
  722. $fileModel = new \app\admin\model\File();
  723. $recordModel = new \app\admin\model\Record();
  724. $f_where = [];
  725. $f_where['module_id'] = $v['activity_id'];
  726. $relation_list = [];
  727. $f_where['module'] = 'crm_activity';
  728. $relation_list = $recordModel->getListByRelationId('activity', $v['activity_id']);
  729. $dataInfo = [];
  730. $newFileList = [];
  731. $newFileList = $fileModel->getDataList($f_where, 'all');
  732. if ($newFileList['list']) {
  733. foreach ($newFileList['list'] as $val) {
  734. if ($val['types'] == 'file') {
  735. $fileList[] = $val;
  736. } else {
  737. $imgList[] = $val;
  738. }
  739. }
  740. }
  741. $list[$k]['fileList'] = $fileList ?: [];
  742. $list[$k]['imgList'] = $imgList ?: [];
  743. $dataInfo['customerList'] = $relation_list['customer_list'] ?: [];
  744. $dataInfo['contactsList'] = $relation_list['contacts_list'] ?: [];
  745. $dataInfo['businessList'] = $relation_list['business_list'] ?: [];
  746. $dataInfo['contractList'] = $relation_list['contract_list'] ?: [];
  747. $list[$k]['dataInfo'] = $dataInfo ?: [];
  748. }
  749. $data = [];
  750. $data['list'] = $list;
  751. $data['dataCount'] = $dataCount ?: 0;
  752. if ($param['page'] != 1 && ($param['page'] * $param['limit']) >= $dataCount) {
  753. $data['firstPage'] = false;
  754. $data['lastPage'] = true;
  755. } else if ($param['page'] != 1 && (int)($param['page'] * $param['limit']) < $dataCount) {
  756. $data['firstPage'] = false;
  757. $data['lastPage'] = false;
  758. } else if ($param['page'] == 1) {
  759. $data['firstPage'] = true;
  760. $data['lastPage'] = false;
  761. }
  762. return $data ?: [];
  763. }
  764. /**
  765. * 日志列表销售简办数据
  766. * @param $param
  767. * @return array
  768. * @throws \think\Exception
  769. * @throws \think\db\exception\DataNotFoundException
  770. * @throws \think\db\exception\ModelNotFoundException
  771. * @throws \think\exception\DbException
  772. */
  773. public function queryLogBulletin($param)
  774. {
  775. $user_id = $param['user_id'];
  776. $search = $param['search'];
  777. $log_id = $param['log_id'];
  778. $item = Db::name('OaLog')->where('log_id', $log_id)->find();
  779. $between_time = [$item['start_time'], $item['end_time']];
  780. if ($search) {
  781. $map['name'] = array('like', '%' . $search . '%');
  782. }
  783. $type = $param['type'];
  784. switch ($type) {
  785. case '1':
  786. $map['name'] = array('like', '%' . $search . '%');
  787. $map['customer.create_time'] = array('between', $between_time);
  788. $activityData = Db::name('CrmCustomer')
  789. ->alias('customer')
  790. ->join('__ADMIN_USER__ user', 'user.id = customer.owner_user_id', 'LEFT')
  791. ->where($map)
  792. ->where('customer.create_user_id', $user_id)
  793. ->order('customer.customer_id desc')
  794. ->field('customer.name,customer.deal_status,customer.create_time,user.realname as owner_user_name,customer.last_time as activity_time')
  795. ->select();
  796. $dataCount = Db::name('CrmCustomer')
  797. ->alias('customer')
  798. ->join('__ADMIN_USER__ user', 'user.id = customer.owner_user_id', 'LEFT')
  799. ->where($map)
  800. ->where('customer.create_user_id', $user_id)
  801. ->count();
  802. break;
  803. case '2':
  804. $map['name'] = array('like', '%' . $search . '%');
  805. $map['business.create_time'] = array('between', $between_time);
  806. $activityData = Db::name('CrmBusiness')
  807. ->alias('business')
  808. ->join('__CRM_BUSINESS_STATUS__ status', 'status.status_id=business.status_id')
  809. ->where($map)
  810. ->where('business.create_user_id', $user_id)
  811. ->order('business.business_id desc')
  812. ->field('business.name,status.name,business.create_time,user.realname as owner_user_name,business.last_time as activity_time')
  813. ->select();
  814. $dataCount = Db::name('CrmBusiness')
  815. ->alias('business')
  816. ->join('__CRM_BUSINESS_STATUS__ status', 'status.status_id=business.status_id')
  817. ->where($map)
  818. ->where('business.create_user_id', $user_id)
  819. ->count();
  820. break;
  821. case '3':
  822. $map['name'] = array('like', '%' . $search . '%');
  823. $map['contract.create_time'] = array('between', $between_time);
  824. $activityData = Db::name('CrmContract')
  825. ->alias('contract')
  826. ->join('__ADMIN_USER__ u', 'u.id = contract.owner_user_id', 'LEFT')
  827. ->where($map)
  828. ->where('contract.create_user_id', $user_id)
  829. ->order('contract.contract_id desc')
  830. ->field('contract.name,contract.create_time,contract.check_status,u.realname as order_user_name')
  831. ->select();
  832. $dataCount = Db::name('CrmContract')
  833. ->alias('contract')
  834. ->join('__ADMIN_USER__ u', 'u.id = contract.owner_user_id', 'LEFT')
  835. ->where($map)
  836. ->where('contract.create_user_id', $user_id)
  837. ->count();
  838. break;
  839. case '4':
  840. $map['number'] = array('like', '%' . $search . '%');
  841. $map['receivables.create_time'] = array('between', $between_time);
  842. $activityData = Db::name('CrmReceivables')
  843. ->alias('receivables')
  844. ->join('__ADMIN_USER__ user', 'user.id = receivables.owner_user_id', 'LEFT')
  845. ->field('receivables.number,receivables.create_time,user.realname as owner_user_name')
  846. ->where($map)
  847. ->where('create_user_id', $user_id)
  848. ->order('receivables.receivables_id desc')
  849. ->column('receivables_id');
  850. $dataCount = Db::name('CrmReceivables')
  851. ->alias('receivables')
  852. ->join('__ADMIN_USER__ user', 'user.id = receivables.owner_user_id', 'LEFT')
  853. ->field('receivables.number,receivables.create_time,user.realname as owner_user_name')
  854. ->where($map)
  855. ->where('create_user_id', $user_id)
  856. ->count();
  857. break;
  858. }
  859. $data = [];
  860. $data['list'] = $activityData;
  861. $data['dataCount'] = $dataCount ?: 0;
  862. return $data;
  863. }
  864. /**
  865. * 日志详情
  866. * @param $param
  867. * @return array
  868. * @throws \think\db\exception\DataNotFoundException
  869. * @throws \think\db\exception\ModelNotFoundException
  870. * @throws \think\exception\DbException
  871. */
  872. public function queryLog($param)
  873. {
  874. $fileModel = new \app\admin\model\File();
  875. $recordModel = new \app\admin\model\Record();
  876. $commonModel = new \app\admin\model\Comment();
  877. $item = Db::name('OaLog')->where('log_id', $param['log_id'])->find();
  878. $fileList = [];
  879. $imgList = [];
  880. $where = [];
  881. $where['module'] = 'oa_log';
  882. $where['module_id'] = $item['log_id'];
  883. $newFileList = [];
  884. $newFileList = $fileModel->getDataList($where);
  885. foreach ($newFileList['list'] as $val) {
  886. if ($val['types'] == 'file') {
  887. $fileList[] = $val;
  888. } else {
  889. $imgList[] = $val;
  890. }
  891. }
  892. $is_update = 0;
  893. $is_delete = 0;
  894. //创建人或负责人或管理员有撤销权限
  895. if ($item['create_user_id'] == $param['user_id']) {
  896. $is_update = 1;
  897. $is_delete = 1;
  898. }
  899. $param['type_id'] = $item['log_id'];
  900. $param['type'] = 'oa_log';
  901. $item['replyList'] = $commonModel->read($param);
  902. $item['fileList'] = $fileList ?: [];
  903. $item['imgList'] = $imgList ?: [];
  904. $permission['is_delete'] = $is_update;
  905. $permission['is_update'] = $is_delete;
  906. $item['permission'] = $permission;
  907. //相关业务
  908. $relationArr = $recordModel->getListByRelationId('log', $item['log_id']);
  909. $item['businessList'] = $relationArr['businessList'];
  910. $item['contactsList'] = $relationArr['contactsList'];
  911. $item['contractList'] = $relationArr['contractList'];
  912. $item['customerList'] = $relationArr['customerList'];
  913. $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
  914. if ($item['is_relation'] == 1) {
  915. $item['bulletin']['customerNum'] = $item['save_customer'];
  916. $item['bulletin']['businessNum'] = $item['save_business'];
  917. $item['bulletin']['contractNum'] = $item['save_contract'];
  918. $item['bulletin']['receivablesMoneyNum'] = $item['save_receivables'];
  919. $item['bulletin']['recordNum'] = $item['save_activity'];
  920. } else {
  921. $item['bulletin'] = 0;
  922. }
  923. $data = [];
  924. $data['list'] = $item;
  925. return $data;
  926. }
  927. }