LogLogic.php 42KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  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. $customerNum = Db::name('CrmCustomer')
  227. ->where($map)
  228. ->count();
  229. $businessNum = Db::name('CrmBusiness')
  230. ->where($map)
  231. ->count();
  232. $contractNum = Db::name('CrmContract')
  233. ->where($map)
  234. ->count();
  235. $receivablesMoneyNum = Db::name('CrmReceivables')
  236. ->where($map)
  237. ->sum('money');
  238. unset($map['owner_user_id']);
  239. $map['create_user_id'] = $user_id;
  240. $recordNum = db('crm_activity')
  241. ->where($map)
  242. ->where(['type' => 1, 'activity_type' => ['<', 7],'status'=>1])
  243. ->count();
  244. $data = [];
  245. $data['data']['customerNum'] = $customerNum;
  246. $data['data']['businessNum'] = $businessNum;
  247. $data['data']['contractNum'] = $contractNum;
  248. $data['data']['receivablesMoneyNum'] = $receivablesMoneyNum;
  249. $data['data']['recordNum'] = $recordNum;
  250. return $data;
  251. }
  252. /**
  253. * 今日新增客户
  254. * @param $param
  255. * @return array
  256. * @throws \think\db\exception\DataNotFoundException
  257. * @throws \think\db\exception\ModelNotFoundException
  258. * @throws \think\exception\DbException
  259. */
  260. public function Bulletin($param)
  261. {
  262. $user_id = $param['user_id'];
  263. $search = $param['search'];
  264. $start_time = $this->pastTime();
  265. $log_id = $param['log_id'] ?: 0;
  266. if (empty($log_id)) {
  267. $between_time = [$start_time['start_time'], $start_time['end_time']];
  268. $map1['customer.owner_user_id'] = $user_id;
  269. $map2['business.owner_user_id'] = $user_id;
  270. $map3['contract.owner_user_id'] = $user_id;
  271. $map4['receivables.owner_user_id'] = $user_id;
  272. } else {
  273. $item = Db::name('OaLog')->where('log_id', $log_id)->find();
  274. $between_time = [strtotime(date('Y-m-d 00:00:00', $item['create_time'])), strtotime(date('Y-m-d 23:59:59', $item['create_time']))];
  275. $map1['customer.owner_user_id'] = $item['create_user_id'];
  276. $map2['business.owner_user_id'] = $item['create_user_id'];
  277. $map3['contract.owner_user_id'] = $item['create_user_id'];
  278. $map4['receivables.owner_user_id'] = $item['create_user_id'];
  279. }
  280. if ($search) {
  281. $map['name'] = array('like', '%' . $search . '%');
  282. }
  283. $customerModel=new \app\crm\model\Customer();
  284. $customerMap =$customerModel->getWhereByCustomer();
  285. $type = $param['log_type'];
  286. switch ($type) {
  287. case '1':
  288. if ($search) $map['customer.name'] = array('like', '%' . $search . '%');
  289. $map['customer.create_time'] = array('between', $between_time);
  290. $activityData = Db::name('CrmCustomer')
  291. ->alias('customer')
  292. ->join('__ADMIN_USER__ user', 'user.id = customer.owner_user_id', 'LEFT')
  293. ->where($map)
  294. ->where($map1)
  295. ->where($customerMap)
  296. ->order('customer.customer_id desc')
  297. ->field('customer.customer_id,customer.name,customer.deal_status,customer.create_time,user.realname as owner_user_name,customer.last_time')
  298. ->select();
  299. $dataCount = Db::name('CrmCustomer')
  300. ->alias('customer')
  301. ->join('__ADMIN_USER__ user', 'user.id = customer.owner_user_id', 'LEFT')
  302. ->where($map)
  303. ->where($map1)
  304. ->where($customerMap)
  305. ->count();
  306. break;
  307. case '2':
  308. $map['business.name'] = array('like', '%' . $search . '%');
  309. $map['business.create_time'] = array('between', $between_time);
  310. $activityData = Db::name('CrmBusiness')
  311. ->alias('business')
  312. ->join('__CRM_BUSINESS_STATUS__ status', 'status.status_id=business.status_id')
  313. ->join('__ADMIN_USER__ user', 'user.id = business.owner_user_id', 'LEFT')
  314. ->where($map)
  315. ->where($map2)
  316. ->order('business.business_id desc')
  317. ->field('business.business_id,business.name,status.name as status_name,business.create_time,user.realname as owner_user_name,business.last_time')
  318. ->select();
  319. $dataCount = Db::name('CrmBusiness')
  320. ->alias('business')
  321. ->join('__CRM_BUSINESS_STATUS__ status', 'status.status_id=business.status_id')
  322. ->where($map)
  323. ->where($map2)
  324. ->count();
  325. break;
  326. case '3':
  327. $map['contract.name'] = array('like', '%' . $search . '%');
  328. $map['contract.create_time'] = array('between', $between_time);
  329. $activityData = Db::name('CrmContract')
  330. ->alias('contract')
  331. ->join('__ADMIN_USER__ u', 'u.id = contract.owner_user_id', 'LEFT')
  332. ->where($map)
  333. ->where($map3)
  334. ->order('contract.contract_id desc')
  335. ->field('contract.contract_id,contract.name,contract.create_time,contract.check_status,u.realname as order_user_name')
  336. ->select();
  337. $dataCount = Db::name('CrmContract')
  338. ->alias('contract')
  339. ->join('__ADMIN_USER__ u', 'u.id = contract.owner_user_id', 'LEFT')
  340. ->where($map)
  341. ->where($map3)
  342. ->count();
  343. break;
  344. case '4':
  345. $map['receivables.number'] = array('like', '%' . $search . '%');
  346. $map['receivables.create_time'] = array('between', $between_time);
  347. $activityData = Db::name('CrmReceivables')
  348. ->alias('receivables')
  349. ->join('__ADMIN_USER__ user', 'user.id = receivables.owner_user_id', 'LEFT')
  350. ->field('receivables.receivables_id,receivables.number,receivables.return_time,user.realname as owner_user_name')
  351. ->where($map)
  352. ->where($map4)
  353. ->order('receivables.receivables_id desc')
  354. ->select();
  355. $dataCount = Db::name('CrmReceivables')
  356. ->alias('receivables')
  357. ->join('__ADMIN_USER__ user', 'user.id = receivables.owner_user_id', 'LEFT')
  358. ->where($map)
  359. ->where($map4)
  360. ->count();
  361. break;
  362. }
  363. foreach ($activityData as $k => $v) {
  364. $activityData[$k]['create_time'] = $v['create_time'] ? date('Y-m-d H:i:s', $v['create_time']) : null;
  365. $activityData[$k]['last_time'] = $v['last_time'] ? date('Y-m-d H:i:s', $v['last_time']) : null;
  366. }
  367. $data = [];
  368. $data['list'] = $activityData;
  369. $data['dataCount'] = $dataCount ?: 0;
  370. return $data;
  371. }
  372. /**
  373. * 查看以往日志
  374. * @param $param
  375. * @return array
  376. */
  377. public function lastLog($param)
  378. {
  379. $user_id = $param('user_id');
  380. $activityData = db('oa_log')
  381. ->where(['send_user_ids' => ['like', '%' . $user_id . '%']])
  382. ->page($param['page'], $param['limit'])
  383. ->order('log_id desc')
  384. ->select();
  385. $dataCount = db('oa_log')->where(['send_user_ids' => ['like', '%' . $user_id . '%']])->count();
  386. $data = [];
  387. $data['page']['list'] = $activityData;
  388. $data['page']['dataCount'] = $dataCount ?: 0;
  389. if ($param['page'] != 1 && ($param['page'] * $param['limit']) >= $dataCount) {
  390. $data['page']['firstPage'] = false;
  391. $data['page']['lastPage'] = true;
  392. } else if ($param['page'] != 1 && (int)($param['page'] * $param['limit']) < $dataCount) {
  393. $data['page']['firstPage'] = false;
  394. $data['page']['lastPage'] = false;
  395. } else if ($param['page'] == 1) {
  396. $data['page']['firstPage'] = true;
  397. $data['page']['lastPage'] = false;
  398. }
  399. return $data;
  400. }
  401. /**
  402. * 跟进记录
  403. * @param $param
  404. * @return array
  405. * @throws \think\db\exception\DataNotFoundException
  406. * @throws \think\db\exception\ModelNotFoundException
  407. * @throws \think\exception\DbException
  408. */
  409. public function activity($param)
  410. {
  411. if ($param['search']) {
  412. $type['t.content'] = array('like', '%' . $param['search'] . '%');
  413. }
  414. if ($param['crmType'] == 0) {
  415. $type['t.activity_type'] = ['in', [1, 2, 3, 5, 6]];
  416. } else {
  417. $type['t.crmType'] = $param['activity_type'];
  418. }
  419. if ($param['type']) {
  420. $timeAry = getTimeByType($param['type']);
  421. $between_time = [$timeAry[0], $timeAry[1]];
  422. $type['t.create_time'] = array('between', $between_time);
  423. }
  424. if ($param['queryType'] == 0) {
  425. $type['t.type'] = ['in', [1, 4]];
  426. } else {
  427. $type['t.type'] = $param['queryType'];
  428. }
  429. if ($param['subUser'] == 'mycreate') {
  430. $type['t.create_user_id'] = $param['user_id'];
  431. //下属创建
  432. } elseif ($param['subUser'] == 'branchcreate') {
  433. $subList = getSubUserId(false, 0, $param['user_id']);
  434. $subStr = $subList ? implode(',', $subList) : '-1';
  435. $type['t.create_user_id'] = array('in', $subStr);
  436. } else {
  437. $userIds = getSubUserId(true, 1, $param['user_id']);
  438. $subStr = $userIds ? implode(',', $userIds) : '-1';
  439. $type['t.create_user_id'] = array('in', $subStr);
  440. }
  441. $list = db('crm_activity')
  442. ->alias('t')
  443. ->join('__ADMIN_USER__ user', 'user.id = t.create_user_id', 'LEFT')
  444. ->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')
  445. ->where($type)
  446. ->page($param['page'], $param['limit'])
  447. ->select();
  448. $dataCount = db('crm_activity')
  449. ->alias('t')
  450. ->join('__ADMIN_USER__ user', 'user.id = t.create_user_id', 'LEFT')
  451. ->field('t.content,t.next_time,category,t.activity_type,user.realname ,user.thumb_img ')
  452. ->where($type)
  453. ->count();
  454. foreach ($list as $k => $v) {
  455. // 业务名称(客户、线索、合同...)
  456. if ($v['type'] == 1 && $v['activity_type'] == 2) {
  457. $list[$k]['activity_type_name'] = Db::name('crm_customer')->where('customer_id', $v['activity_type_id'])->value('name');
  458. }
  459. }
  460. $data = [];
  461. $data['page']['list'] = $list;
  462. $data['page']['dataCount'] = $dataCount ?: 0;
  463. if ($param['page'] != 1 && ($param['page'] * $param['limit']) >= $dataCount) {
  464. $data['page']['firstPage'] = false;
  465. $data['page']['lastPage'] = true;
  466. } else if ($param['page'] != 1 && (int)($param['page'] * $param['limit']) < $dataCount) {
  467. $data['page']['firstPage'] = false;
  468. $data['page']['lastPage'] = false;
  469. } else if ($param['page'] == 1) {
  470. $data['page']['firstPage'] = true;
  471. $data['page']['lastPage'] = false;
  472. }
  473. return $data;
  474. }
  475. /**
  476. * 已完成日志
  477. * @param $param
  478. * @return array
  479. * @throws \think\db\exception\DataNotFoundException
  480. * @throws \think\db\exception\ModelNotFoundException
  481. * @throws \think\exception\DbException
  482. */
  483. public function completeLog($param)
  484. {
  485. $userModel = new \app\admin\model\User();
  486. $type = db('admin_oalog_rule')->where('type', 1)->find();
  487. $start_time = strtotime(date('Y-m-d', time()) . ' ' . $type['start_time'] . ':00');
  488. $end_time = strtotime(date('Y-m-d', time()) . ' ' . $type['end_time'] . ':00');
  489. $between_time = [$start_time, $end_time];
  490. $users = getSubUserId(false, 0, $param['user_id']);
  491. $between['create_time'] = array('between', $between_time);
  492. $list = db('oa_log')
  493. ->where($between)
  494. ->order('create_user_id', 'desc')
  495. ->column('create_user_id');
  496. if ($type['userIds'] == '') {
  497. $where['id'] = array('in', implode(',', array_intersect($users, $list)));
  498. } else {
  499. $users_diff = array_intersect($users, stringToArray($type['userIds']));//如果设置写日志人 显示要写下属id
  500. $where['id'] = array('in', implode(',', array_intersect($users_diff, $list)));
  501. }
  502. // if($type['userIds']==''){
  503. // $users=Db::name('AdminUser')->where('status',1)->column('id');
  504. // $where['log.create_user_id'] = array('in', implode(',',$users));
  505. // }else{
  506. // $where['log.create_user_id'] = array('in', implode(',', array_intersect(stringToArray($type['userIds']), $list)));
  507. // }
  508. $where['user.realname'] = array('like', '%' . $param['search'] . '%');
  509. $where['log.create_time'] = array('between', $between_time);
  510. $item = db('oa_log')
  511. ->alias('log')
  512. ->join('__ADMIN_USER__ user', 'user.id = log.create_user_id', 'LEFT')
  513. ->field('user.realname as user_name,log.content,log.create_time,log.log_id')
  514. ->where($where)
  515. ->order('log.log_id', 'desc')
  516. ->select();
  517. foreach ($item as $k => $v) {
  518. $item[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
  519. }
  520. $data = [];
  521. $data['list'] = $item;
  522. return $data;
  523. }
  524. /**
  525. * 未完成日志
  526. * @param $param
  527. * @return array
  528. * @throws \think\db\exception\DataNotFoundException
  529. * @throws \think\db\exception\ModelNotFoundException
  530. * @throws \think\exception\DbException
  531. */
  532. public function inCompleteLog($param)
  533. {
  534. //下属员工未完成
  535. $type = db('admin_oalog_rule')->where('type', 1)->find();
  536. $start_time = strtotime(date('Y-m-d', time()) . ' ' . $type['start_time'] . ':00');
  537. $end_time = strtotime(date('Y-m-d', time()) . ' ' . $type['end_time'] . ':00');
  538. $between_time = [$start_time, $end_time];
  539. $users = getSubUserId(false, 0, $param['user_id']);
  540. $between['create_time'] = array('between', $between_time);
  541. $list = db('oa_log')
  542. ->where($between)
  543. ->order('create_user_id', 'desc')
  544. ->column('create_user_id');
  545. if ($type['userIds'] == '') {
  546. $diff = array_intersect($users, $list);// 下属已写
  547. $where['id'] = array('in', implode(',', array_diff_assoc($users, $diff)));
  548. } else {
  549. $users_diff = array_intersect($users, stringToArray($type['userIds']));//如果设置写日志人 显示要写下属
  550. $diff = array_intersect($users_diff, $list);// 下属已写
  551. $where['id'] = array('in', implode(',', array_diff_assoc($users_diff, $diff)));
  552. }
  553. $where['realname'] = array('like', '%' . $param['search'] . '%');
  554. $item = db('admin_user')
  555. ->field('realname as user_name')
  556. ->where($where)
  557. ->order('id', 'desc')
  558. ->select();
  559. $data = [];
  560. foreach ($item as $k => $v) {
  561. $item[$k]['content'] = '';
  562. $item[$k]['create_time'] = '';
  563. }
  564. $data['list'] = $item;
  565. return $data;
  566. }
  567. /**
  568. * 日志导出
  569. * @param $param
  570. */
  571. public function excelExport($param)
  572. {
  573. $data = $this->getDataList($param);
  574. $excelModel = new \app\admin\model\Excel();
  575. $file_name = 'log';
  576. $title = '日志列表';
  577. $field_list = [
  578. '0' => ['name' => '日志类型', 'field' => 'category_name'],
  579. '1' => ['name' => '创建日期', 'field' => 'create_time'],
  580. '2' => ['name' => '创建人', 'field' => 'realname'],
  581. '3' => ['name' => '发送给', 'field' => 'send_user_name'],
  582. '4' => ['name' => '今日工作内容', 'field' => 'content'],
  583. '5' => ['name' => '明日工作内容', 'field' => 'tomorrow'],
  584. '6' => ['name' => '遇到问题', 'field' => 'question'],
  585. '7' => ['name' => '关联业务', 'field' => 'relation'],
  586. '8' => ['name' => '回复', 'field' => 'replyList'],
  587. ];
  588. return $excelModel->taskExportCsv($file_name, $field_list, $title, $data);
  589. }
  590. /**
  591. * 回复列表
  592. * @param $param
  593. */
  594. public function CommentList($param)
  595. {
  596. $commonModel = new CommentModel();
  597. $param['type_id'] = $param['log_id'];
  598. $param['type'] = 'oa_log';
  599. $replyList = $commonModel->read($param);
  600. $data = [];
  601. $data['list'] = $replyList;
  602. return $data;
  603. }
  604. /**
  605. * 销售简报跟进数量统计
  606. *
  607. * @param $param 参数
  608. */
  609. public function activityCount($param)
  610. {
  611. $user_id = $param['user_id'];
  612. $item = Db::name('OaLog')->where('log_id', $param['log_id'])->find();
  613. $start_time = $this->pastTime();
  614. if (empty($param['log_id'])) {
  615. $between_time = [$start_time['start_time'], $start_time['end_time']];
  616. $map['create_time'] = array('between', $between_time);
  617. $map['create_user_id'] =$user_id;
  618. } else {
  619. $start_time = strtotime(date("Y-m-d", $item['create_time']));
  620. $end_time = strtotime(date("Y-m-d H:i:s", $item['create_time']));
  621. $between_time = [$start_time, $end_time];
  622. $map['create_time'] = array('between', $between_time);
  623. $map['create_user_id'] = $item['create_user_id'];
  624. }
  625. $map['status']=1;
  626. $typesList = ['1', '2', '3', '5', '6'];
  627. foreach ($typesList as $k => $v) {
  628. $activityData = db('crm_activity')->where($map)->where(['type' => 1, 'activity_type' => $v])->count();
  629. if($v==1){
  630. $arr[$k]['types'] ='crm_leads';
  631. $arr[$k]['activity_type'] =1;
  632. }elseif ($v==2){
  633. $arr[$k]['types'] ='crm_customer';
  634. $arr[$k]['activity_type'] =2;
  635. }elseif ($v==3){
  636. $arr[$k]['types'] ='crm_contacts';
  637. $arr[$k]['activity_type'] =3;
  638. }elseif ($v==5){
  639. $arr[$k]['types'] ='crm_business';
  640. $arr[$k]['activity_type'] =5;
  641. }elseif ($v==6){
  642. $arr[$k]['types'] ='crm_contract';
  643. $arr[$k]['activity_type'] =6;
  644. }
  645. $arr[$k]['dataCount'] = $activityData;
  646. }
  647. $data = $arr;
  648. return $data;
  649. }
  650. /**
  651. *
  652. * @param $param
  653. * @return array
  654. * @throws \think\Exception
  655. * @throws \think\db\exception\DataNotFoundException
  656. * @throws \think\db\exception\ModelNotFoundException
  657. * @throws \think\exception\DbException
  658. */
  659. public function activityList($param)
  660. {
  661. $start_time = $this->pastTime();
  662. if (empty($param['log_id'])) {
  663. $between_time = [$start_time['start_time'], $start_time['end_time']];
  664. $where_activity['t.create_time'] = array('between', $between_time);
  665. $where_activity['t.create_user_id'] = $param['user_id'];
  666. } else {
  667. $item = Db::name('OaLog')->where('log_id', $param['log_id'])->find();
  668. $start_time = strtotime(date("Y-m-d", $item['create_time']));
  669. $end_time = strtotime(date("Y-m-d H:i:s", $item['create_time']));
  670. $between_time = [$start_time, $end_time];
  671. $where_activity['t.create_time'] = array('between', $between_time);
  672. $where_activity['t.create_user_id'] = $item['create_user_id'];
  673. }
  674. # 跟进记录类型
  675. $where_activity['t.activity_type'] = $param['activity_type'];
  676. $where_activity['t.status'] = 1;
  677. $where_activity['t.type'] = 1;
  678. $list = db('crm_activity')
  679. ->alias('t')
  680. ->join('__ADMIN_USER__ user', 'user.id = t.create_user_id', 'LEFT')
  681. ->where($where_activity)
  682. ->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')
  683. ->page($param['page'], $param['limit'])
  684. ->order('t.create_time desc')
  685. ->select();
  686. $dataCount = db('crm_activity')->alias('t')
  687. ->join('__ADMIN_USER__ user', 'user.id = t.create_user_id', 'LEFT')->where($where_activity)->count();
  688. foreach ($list as $k => $v) {
  689. // 业务名称(客户、线索、合同...)
  690. if ($param['activity_type'] == 1) {
  691. $activity_name = Db::name('crm_leads')->where('leads_id', $v['activity_type_id'])->find();
  692. $list[$k]['activity_type_name'] = $activity_name['name'];
  693. }
  694. if ($param['activity_type'] == 2) {
  695. $activity_name = Db::name('crm_customer')->where('customer_id', $v['activity_type_id'])->find();
  696. $list[$k]['activity_type_name'] = $activity_name['name'];
  697. $activity_business = Db::name('crm_business')->where('business_id', $v['activity_type_id'])->select();
  698. $activity_contacts = Db::name('crm_contacts')->where('contacts_id', $v['activity_type_id'])->select();
  699. $list[$k]['business_list'] = $activity_business ?: [];
  700. $list[$k]['contacts_list'] = $activity_contacts ?: [];
  701. }
  702. if ($param['activity_type'] == 3) {
  703. $activity_name = Db::name('crm_contacts')->where('contacts_id', $v['activity_type_id'])->find();
  704. $list[$k]['activity_type_name'] = $activity_name['name'];
  705. }
  706. if ($param['activity_type'] == 5) {
  707. $activity_name = Db::name('crm_business')->where('business_id', $v['activity_type_id'])->find();
  708. $list[$k]['activity_type_name'] = $activity_name['name'];
  709. }
  710. if ($param['activity_type'] == 6) {
  711. $activity_name = Db::name('crm_contract')->where('contract_id', $v['activity_type_id'])->find();
  712. $list[$k]['activity_type_name'] = $activity_name['name'];
  713. }
  714. $list[$k]['create_time'] = !empty($v['create_time']) ? date('Y-m-d H:i:s', $v['create_time']) : null;
  715. $list[$k]['update_time'] = !empty($v['update_time']) ? date('Y-m-d H:i:s', $v['update_time']) : null;
  716. $list[$k]['next_time'] = !empty($v['next_time']) ? date('Y-m-d H:i:s', $v['next_time']) : null;
  717. $fileModel = new \app\admin\model\File();
  718. $recordModel = new \app\admin\model\Record();
  719. $f_where = [];
  720. $f_where['module_id'] = $v['activity_id'];
  721. $relation_list = [];
  722. $f_where['module'] = 'crm_activity';
  723. $relation_list = $recordModel->getListByRelationId('activity', $v['activity_id']);
  724. $dataInfo = [];
  725. $newFileList = [];
  726. $newFileList = $fileModel->getDataList($f_where, 'all');
  727. if ($newFileList['list']) {
  728. foreach ($newFileList['list'] as $val) {
  729. if ($val['types'] == 'file') {
  730. $fileList[] = $val;
  731. } else {
  732. $imgList[] = $val;
  733. }
  734. }
  735. }
  736. $list[$k]['fileList'] = $fileList ?: [];
  737. $list[$k]['imgList'] = $imgList ?: [];
  738. $dataInfo['customerList'] = $relation_list['customer_list'] ?: [];
  739. $dataInfo['contactsList'] = $relation_list['contacts_list'] ?: [];
  740. $dataInfo['businessList'] = $relation_list['business_list'] ?: [];
  741. $dataInfo['contractList'] = $relation_list['contract_list'] ?: [];
  742. $list[$k]['dataInfo'] = $dataInfo ?: [];
  743. }
  744. $data = [];
  745. $data['list'] = $list;
  746. $data['dataCount'] = $dataCount ?: 0;
  747. if ($param['page'] != 1 && ($param['page'] * $param['limit']) >= $dataCount) {
  748. $data['firstPage'] = false;
  749. $data['lastPage'] = true;
  750. } else if ($param['page'] != 1 && (int)($param['page'] * $param['limit']) < $dataCount) {
  751. $data['firstPage'] = false;
  752. $data['lastPage'] = false;
  753. } else if ($param['page'] == 1) {
  754. $data['firstPage'] = true;
  755. $data['lastPage'] = false;
  756. }
  757. return $data ?: [];
  758. }
  759. /**
  760. * 日志列表销售简办数据
  761. * @param $param
  762. * @return array
  763. * @throws \think\Exception
  764. * @throws \think\db\exception\DataNotFoundException
  765. * @throws \think\db\exception\ModelNotFoundException
  766. * @throws \think\exception\DbException
  767. */
  768. public function queryLogBulletin($param)
  769. {
  770. $user_id = $param['user_id'];
  771. $search = $param['search'];
  772. $log_id = $param['log_id'];
  773. $item = Db::name('OaLog')->where('log_id', $log_id)->find();
  774. $between_time = [$item['start_time'], $item['end_time']];
  775. if ($search) {
  776. $map['name'] = array('like', '%' . $search . '%');
  777. }
  778. $type = $param['type'];
  779. switch ($type) {
  780. case '1':
  781. $map['name'] = array('like', '%' . $search . '%');
  782. $map['customer.create_time'] = array('between', $between_time);
  783. $activityData = Db::name('CrmCustomer')
  784. ->alias('customer')
  785. ->join('__ADMIN_USER__ user', 'user.id = customer.owner_user_id', 'LEFT')
  786. ->where($map)
  787. ->where('customer.create_user_id', $user_id)
  788. ->order('customer.customer_id desc')
  789. ->field('customer.name,customer.deal_status,customer.create_time,user.realname as owner_user_name,customer.last_time as activity_time')
  790. ->select();
  791. $dataCount = Db::name('CrmCustomer')
  792. ->alias('customer')
  793. ->join('__ADMIN_USER__ user', 'user.id = customer.owner_user_id', 'LEFT')
  794. ->where($map)
  795. ->where('customer.create_user_id', $user_id)
  796. ->count();
  797. break;
  798. case '2':
  799. $map['name'] = array('like', '%' . $search . '%');
  800. $map['business.create_time'] = array('between', $between_time);
  801. $activityData = Db::name('CrmBusiness')
  802. ->alias('business')
  803. ->join('__CRM_BUSINESS_STATUS__ status', 'status.status_id=business.status_id')
  804. ->where($map)
  805. ->where('business.create_user_id', $user_id)
  806. ->order('business.business_id desc')
  807. ->field('business.name,status.name,business.create_time,user.realname as owner_user_name,business.last_time as activity_time')
  808. ->select();
  809. $dataCount = Db::name('CrmBusiness')
  810. ->alias('business')
  811. ->join('__CRM_BUSINESS_STATUS__ status', 'status.status_id=business.status_id')
  812. ->where($map)
  813. ->where('business.create_user_id', $user_id)
  814. ->count();
  815. break;
  816. case '3':
  817. $map['name'] = array('like', '%' . $search . '%');
  818. $map['contract.create_time'] = array('between', $between_time);
  819. $activityData = Db::name('CrmContract')
  820. ->alias('contract')
  821. ->join('__ADMIN_USER__ u', 'u.id = contract.owner_user_id', 'LEFT')
  822. ->where($map)
  823. ->where('contract.create_user_id', $user_id)
  824. ->order('contract.contract_id desc')
  825. ->field('contract.name,contract.create_time,contract.check_status,u.realname as order_user_name')
  826. ->select();
  827. $dataCount = Db::name('CrmContract')
  828. ->alias('contract')
  829. ->join('__ADMIN_USER__ u', 'u.id = contract.owner_user_id', 'LEFT')
  830. ->where($map)
  831. ->where('contract.create_user_id', $user_id)
  832. ->count();
  833. break;
  834. case '4':
  835. $map['number'] = array('like', '%' . $search . '%');
  836. $map['receivables.create_time'] = array('between', $between_time);
  837. $activityData = Db::name('CrmReceivables')
  838. ->alias('receivables')
  839. ->join('__ADMIN_USER__ user', 'user.id = receivables.owner_user_id', 'LEFT')
  840. ->field('receivables.number,receivables.create_time,user.realname as owner_user_name')
  841. ->where($map)
  842. ->where('create_user_id', $user_id)
  843. ->order('receivables.receivables_id desc')
  844. ->column('receivables_id');
  845. $dataCount = Db::name('CrmReceivables')
  846. ->alias('receivables')
  847. ->join('__ADMIN_USER__ user', 'user.id = receivables.owner_user_id', 'LEFT')
  848. ->field('receivables.number,receivables.create_time,user.realname as owner_user_name')
  849. ->where($map)
  850. ->where('create_user_id', $user_id)
  851. ->count();
  852. break;
  853. }
  854. $data = [];
  855. $data['list'] = $activityData;
  856. $data['dataCount'] = $dataCount ?: 0;
  857. return $data;
  858. }
  859. /**
  860. * 日志详情
  861. * @param $param
  862. * @return array
  863. * @throws \think\db\exception\DataNotFoundException
  864. * @throws \think\db\exception\ModelNotFoundException
  865. * @throws \think\exception\DbException
  866. */
  867. public function queryLog($param)
  868. {
  869. $fileModel = new \app\admin\model\File();
  870. $recordModel = new \app\admin\model\Record();
  871. $commonModel = new \app\admin\model\Comment();
  872. $item = Db::name('OaLog')->where('log_id', $param['log_id'])->find();
  873. $fileList = [];
  874. $imgList = [];
  875. $where = [];
  876. $where['module'] = 'oa_log';
  877. $where['module_id'] = $item['log_id'];
  878. $newFileList = [];
  879. $newFileList = $fileModel->getDataList($where);
  880. foreach ($newFileList['list'] as $val) {
  881. if ($val['types'] == 'file') {
  882. $fileList[] = $val;
  883. } else {
  884. $imgList[] = $val;
  885. }
  886. }
  887. $param['type_id'] = $item['log_id'];
  888. $param['type'] = 'oa_log';
  889. $item['replyList'] = $commonModel->read($param);
  890. $item['fileList'] = $fileList ?: [];
  891. $item['imgList'] = $imgList ?: [];
  892. //相关业务
  893. $relationArr = $recordModel->getListByRelationId('log', $item['log_id']);
  894. $item['businessList'] = $relationArr['businessList'];
  895. $item['contactsList'] = $relationArr['contactsList'];
  896. $item['contractList'] = $relationArr['contractList'];
  897. $item['customerList'] = $relationArr['customerList'];
  898. $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
  899. if ($item['is_relation'] == 1) {
  900. $item['bulletin']['customerNum'] = $item['save_customer'];
  901. $item['bulletin']['businessNum'] = $item['save_business'];
  902. $item['bulletin']['contractNum'] = $item['save_contract'];
  903. $item['bulletin']['receivablesMoneyNum'] = $item['save_receivables'];
  904. $item['bulletin']['recordNum'] = $item['save_activity'];
  905. } else {
  906. $item['bulletin'] = 0;
  907. }
  908. $data = [];
  909. $data['list'] = $item;
  910. return $data;
  911. }
  912. }