BiCustomerLogic.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * 员工客户分析逻辑类
  4. *
  5. * @author qifan
  6. * @date 2020-12-24
  7. */
  8. namespace app\bi\logic;
  9. use app\bi\traits\SortTrait;
  10. use think\Db;
  11. class BiCustomerLogic
  12. {
  13. use SortTrait;
  14. /**
  15. * 员工客户满意度分析
  16. *
  17. * @param $param
  18. * @return array
  19. * @throws \think\db\exception\DataNotFoundException
  20. * @throws \think\db\exception\ModelNotFoundException
  21. * @throws \think\exception\DbException
  22. */
  23. public function getCustomerSatisfaction($param)
  24. {
  25. $result = [];
  26. $userModel = new \app\admin\model\User();
  27. $adminModel = new \app\admin\model\Admin();
  28. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); # 权限范围内userIds
  29. $whereData = $adminModel->getWhere($param, '', $perUserIds); //统计条件
  30. $userIds = $whereData['userIds'];
  31. if (empty($userIds)) {
  32. # 普通员工没有查看权限,返回固定数据(根据员工筛选)
  33. $result[] = [
  34. 'realname' => db('admin_user')->where('id', $param['user_id'])->value('realname'),
  35. 'visitContractNum' => 0,
  36. '很满意' => 0,
  37. '满意' => 0,
  38. '一般' => 0,
  39. '不满意' => 0,
  40. '很不满意' => 0,
  41. ];
  42. return $result;
  43. }
  44. # 员工信息
  45. $userList = db('admin_user')->field(['id', 'realname'])->whereIn('id', $userIds)->select();
  46. foreach ($userList AS $key => $value) {
  47. $result[$value['id']] = [
  48. 'realname' => $value['realname'],
  49. 'visitContractNum' => 0,
  50. '很满意' => 0,
  51. '满意' => 0,
  52. '一般' => 0,
  53. '不满意' => 0,
  54. '很不满意' => 0,
  55. ];
  56. }
  57. $visitField = ['owner_user_id', 'satisfaction', 'count(`satisfaction`) AS satisfactionCount', 'count(`contract_id`) AS contractCount'];
  58. $where['owner_user_id'] = ['in', $userIds];
  59. $where['create_time'] = ['between', [$param['start_time'], $param['end_time']]];
  60. $where['deleted_state'] = 0;
  61. $visitList = db('crm_visit')->field($visitField)->where($where)->group('owner_user_id, satisfaction')->select();
  62. foreach ($visitList AS $key => $value) {
  63. if (!empty($value['satisfaction']) && trim($value['satisfaction']) == '很满意') {
  64. $result[$value['owner_user_id']]['很满意'] += $value['satisfactionCount'];
  65. }
  66. if (!empty($value['satisfaction']) && trim($value['satisfaction']) == '满意') {
  67. $result[$value['owner_user_id']]['满意'] += $value['satisfactionCount'];
  68. }
  69. if (!empty($value['satisfaction']) && trim($value['satisfaction']) == '一般') {
  70. $result[$value['owner_user_id']]['一般'] += $value['satisfactionCount'];
  71. }
  72. if (!empty($value['satisfaction']) && trim($value['satisfaction']) == '不满意') {
  73. $result[$value['owner_user_id']]['不满意'] += $value['satisfactionCount'];
  74. }
  75. if (!empty($value['satisfaction']) && trim($value['satisfaction']) == '很不满意') {
  76. $result[$value['owner_user_id']]['很不满意'] += $value['satisfactionCount'];
  77. }
  78. $result[$value['owner_user_id']]['visitContractNum'] += $value['contractCount'];
  79. }
  80. $result = $this->sortCommon($result, 'visitContractNum', 'desc');
  81. return array_values($result);
  82. }
  83. /**
  84. * 产品满意度分析
  85. *
  86. * @param $param
  87. * @return array
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\ModelNotFoundException
  90. * @throws \think\exception\DbException
  91. */
  92. public function getProductSatisfaction($param)
  93. {
  94. $productData = [];
  95. $userModel = new \app\admin\model\User();
  96. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); # 权限范围内userIds
  97. $userIds = !empty($param['user_id']) ? array_intersect([$param['user_id']], $perUserIds) : $perUserIds; # 数组交集
  98. # 产品列表(上架中)
  99. $productList = db('crm_product')->field(['product_id', 'name'])->where( 'delete_user_id',0)->select();
  100. foreach ($productList AS $key => $value) {
  101. $productData[$value['product_id']] = [
  102. 'productName' => $value['name'],
  103. 'visitNum' => 0,
  104. '很满意' => 0,
  105. '满意' => 0,
  106. '一般' => 0,
  107. '不满意' => 0,
  108. '很不满意' => 0,
  109. ];
  110. }
  111. # 普通员工没有查询权限,返回固定数据(根据员工筛选)
  112. if (empty($userIds)) array_values($productData);
  113. # 回访条件
  114. $where['visit.owner_user_id'] = ['in', $userIds];
  115. $where['visit.create_time'] = ['between', [$param['start_time'], $param['end_time']]];
  116. $where['visit.deleted_state'] = 0;
  117. # 回访数据
  118. $visitList = db('crm_visit')->alias('visit')->field(['visit.contract_id', 'visit.satisfaction'])
  119. ->join('__CRM_CONTRACT__ contract', 'contract.contract_id = visit.contract_id')
  120. ->where($where)->select();
  121. # 整理数据
  122. foreach ($visitList AS $key => $value) {
  123. if (empty($value['satisfaction'])) continue;
  124. $productIds = db('crm_contract_product')->where('contract_id', $value['contract_id'])->column('product_id');
  125. foreach ($productIds AS $k => $v) {
  126. if ($productData[$v]) {
  127. if (trim($value['satisfaction']) == '很满意') $productData[$v]['很满意']++;
  128. if (trim($value['satisfaction']) == '满意') $productData[$v]['满意']++;
  129. if (trim($value['satisfaction']) == '一般') $productData[$v]['一般']++;
  130. if (trim($value['satisfaction']) == '不满意') $productData[$v]['不满意']++;
  131. if (trim($value['satisfaction']) == '很不满意') $productData[$v]['很不满意']++;
  132. $productData[$v]['visitNum']++;
  133. }
  134. }
  135. }
  136. $productData = $this->sortCommon($productData, 'visitNum', 'desc');
  137. return array_values($productData);
  138. }
  139. }