Record.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 客户跟进
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\bi\model;
  8. use think\Db;
  9. use app\admin\model\Common;
  10. use think\Request;
  11. class Record extends Common
  12. {
  13. /**
  14. * 为了数据库的整洁,同时又不影响Model和Controller的名称
  15. * 我们约定每个模块的数据表都加上相同的前缀,比如CRM模块用crm作为数据表前缀
  16. */
  17. protected $name = 'admin_record';
  18. /**
  19. * [跟进统计]
  20. * @author Michael_xu
  21. * @param
  22. * @return
  23. */
  24. function getDataList($request){
  25. $userModel = new \app\admin\model\User();
  26. //员工IDS
  27. $map_user_ids = [];
  28. if ($request['user_id']) {
  29. $map_user_ids = array($request['user_id']);
  30. } else {
  31. if ($request['structure_id']) {
  32. $map_user_ids = $userModel->getSubUserByStr($request['structure_id'], 2);
  33. }
  34. }
  35. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  36. $userIds = $map_user_ids ? array_intersect($map_user_ids, $perUserIds) : $perUserIds; //数组交集
  37. $where['id'] = array('in',$userIds);
  38. $where['type'] = 1;
  39. $userList = db('admin_user')->where($where)->field('id,username,realname')->select();
  40. foreach ($userList as $k=>$v) {
  41. $whereArr = [];
  42. $customer_num = 0; //跟进客户数
  43. $record_num = 0; //跟进次数
  44. $whereArr['create_user_id'] = $v['id'];
  45. $start_time = $request['start_time'];
  46. $end_time = $request['end_time'];
  47. if ($start_time && $end_time) {
  48. $create_time = array('between',array($start_time,$end_time));
  49. }
  50. $whereArr['create_time'] = $create_time;
  51. $userList[$k]['customer_num'] = $customer_num = $this->getCustomerNum($whereArr);
  52. $userList[$k]['record_num'] = $record_num = $this->getRecordNum($whereArr);
  53. }
  54. return $userList ? : [];
  55. }
  56. /**
  57. * 根据条件获取跟进客户数
  58. * @author zhi
  59. * @param
  60. * @return
  61. */
  62. function getCustomerNum($whereArr){
  63. $dataCount = db('admin_record')->where($whereArr)->group('types_id')->count();
  64. return $dataCount;
  65. }
  66. /**
  67. * [根据条件获取跟进次数]
  68. * @author zhi
  69. * @param
  70. * @return
  71. */
  72. function getRecordNum($whereArr){
  73. $dataCount = db('admin_record')->where($whereArr)->count();
  74. return $dataCount;
  75. }
  76. /**
  77. * 跟进次数排行
  78. * @author zhi
  79. * @param
  80. * @return
  81. */
  82. function getSortByCount($whereArr)
  83. {
  84. $count = db('admin_record')->group('create_user_id')->field('create_user_id,count(record_id) as count')->order('count desc')->where($whereArr)->select();
  85. return $count;
  86. }
  87. /**
  88. * 跟进客户排行
  89. * @author zhi
  90. * @param
  91. * @return
  92. */
  93. function getSortByCustomer($whereArr)
  94. {
  95. $list = db('admin_record')->group('create_user_id')->field('create_user_id')->where($whereArr)->select();
  96. foreach ($list as $key => $value) {
  97. $where = array();
  98. $where['create_user_id'] = array('eq',$value['create_user_id']);
  99. $list[$key]['count'] = count(db('admin_record')->group('types_id')->field('count(types_id) as count')->order('count desc')->where($where)->select());
  100. }
  101. return sort_select($list,'count');
  102. }
  103. }