BiCustomerLogic.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); # 权限范围内userIds
  28. $userIds = $perUserIds; # 数组交集
  29. if (empty($userIds)) return [];
  30. # 员工信息
  31. $userList = db('admin_user')->field(['id', 'realname'])->whereIn('id', $userIds)->select();
  32. foreach ($userList AS $key => $value) {
  33. $result[$value['id']] = [
  34. 'realname' => $value['realname'],
  35. 'visitContractNum' => 0,
  36. '很满意' => 0,
  37. '满意' => 0,
  38. '一般' => 0,
  39. '不满意' => 0,
  40. '很不满意' => 0,
  41. ];
  42. }
  43. $visitField = ['owner_user_id', 'satisfaction', 'count(`satisfaction`) AS satisfactionCount', 'count(`contract_id`) AS contractCount'];
  44. $where['owner_user_id'] = ['in', $userIds];
  45. $where['create_time'] = ['between', [$param['start_time'], $param['end_time']]];
  46. $where['deleted_state'] = 0;
  47. $visitList = db('crm_visit')->field($visitField)->where($where)->group('owner_user_id, satisfaction')->select();
  48. foreach ($visitList AS $key => $value) {
  49. if (!empty($value['satisfaction']) && trim($value['satisfaction']) == '很满意') {
  50. $result[$value['owner_user_id']]['很满意'] += $value['satisfactionCount'];
  51. }
  52. if (!empty($value['satisfaction']) && trim($value['satisfaction']) == '满意') {
  53. $result[$value['owner_user_id']]['满意'] += $value['satisfactionCount'];
  54. }
  55. if (!empty($value['satisfaction']) && trim($value['satisfaction']) == '一般') {
  56. $result[$value['owner_user_id']]['一般'] += $value['satisfactionCount'];
  57. }
  58. if (!empty($value['satisfaction']) && trim($value['satisfaction']) == '不满意') {
  59. $result[$value['owner_user_id']]['不满意'] += $value['satisfactionCount'];
  60. }
  61. if (!empty($value['satisfaction']) && trim($value['satisfaction']) == '很不满意') {
  62. $result[$value['owner_user_id']]['很不满意'] += $value['satisfactionCount'];
  63. }
  64. $result[$value['owner_user_id']]['visitContractNum'] += $value['contractCount'];
  65. }
  66. $result = $this->sortCommon($result, 'visitContractNum', 'desc');
  67. return array_values($result);
  68. }
  69. /**
  70. * 产品满意度分析
  71. *
  72. * @param $param
  73. * @return array
  74. * @throws \think\db\exception\DataNotFoundException
  75. * @throws \think\db\exception\ModelNotFoundException
  76. * @throws \think\exception\DbException
  77. */
  78. public function getProductSatisfaction($param)
  79. {
  80. $productData = [];
  81. $userModel = new \app\admin\model\User();
  82. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); # 权限范围内userIds
  83. $userIds = !empty($param['user_id']) ? array_intersect([$param['user_id']], $perUserIds) : $perUserIds; # 数组交集
  84. if (empty($userIds)) return [];
  85. # 产品列表(上架中)
  86. $productList = db('crm_product')->field(['product_id', 'name'])->where('status', '上架')->select();
  87. foreach ($productList AS $key => $value) {
  88. $productData[$value['product_id']] = [
  89. 'productName' => $value['name'],
  90. 'visitNum' => 0,
  91. '很满意' => 0,
  92. '满意' => 0,
  93. '一般' => 0,
  94. '不满意' => 0,
  95. '很不满意' => 0,
  96. ];
  97. }
  98. # 回访条件
  99. $where['visit.owner_user_id'] = ['in', $userIds];
  100. $where['visit.create_time'] = ['between', [$param['start_time'], $param['end_time']]];
  101. $where['visit.deleted_state'] = 0;
  102. # 回访数据
  103. $visitList = db('crm_visit')->alias('visit')->field(['visit.contract_id', 'visit.satisfaction'])
  104. ->join('__CRM_CONTRACT__ contract', 'contract.contract_id = visit.contract_id')
  105. ->where($where)->select();
  106. # 整理数据
  107. foreach ($visitList AS $key => $value) {
  108. if (empty($value['satisfaction'])) continue;
  109. $productIds = db('crm_contract_product')->where('contract_id', $value['contract_id'])->column('product_id');
  110. foreach ($productIds AS $k => $v) {
  111. if ($productData[$v]) {
  112. if (trim($value['satisfaction']) == '很满意') $productData[$v]['很满意']++;
  113. if (trim($value['satisfaction']) == '满意') $productData[$v]['满意']++;
  114. if (trim($value['satisfaction']) == '一般') $productData[$v]['一般']++;
  115. if (trim($value['satisfaction']) == '不满意') $productData[$v]['不满意']++;
  116. if (trim($value['satisfaction']) == '很不满意') $productData[$v]['很不满意']++;
  117. $productData[$v]['visitNum']++;
  118. }
  119. }
  120. }
  121. $productData = $this->sortCommon($productData, 'visitNum', 'desc');
  122. return array_values($productData);
  123. }
  124. }