1
0

CrmWorkbench.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace app\api\controller;
  3. use app\model\Customer;
  4. use app\model\CustomerClue;
  5. use app\model\CustomerVisitLog;
  6. use think\facade\Db;
  7. class CrmWorkbench extends Base
  8. {
  9. /**
  10. * 数据统计
  11. */
  12. public function datacount()
  13. {
  14. // 日期
  15. $date = request()->param('date');
  16. $condition = [];
  17. if ($dataCondition = $this->getDate($date, 'addtime')) {
  18. $condition[] = $dataCondition;
  19. }
  20. //设计师、业务员
  21. $field = $this->request->token['org_type'] == 2 ? 'designer_id|employee_id' : 'employee_id';
  22. //查询出属于我的客户
  23. $customer_id = Customer::where($field, $this->request->token['employee_id'])->column('id');
  24. // 确认到店数量统计
  25. $visit = CustomerVisitLog::where(array_merge([CustomerVisitLog::changeState(['state', '=', '确认到店'])], [['customer_id', 'in', $customer_id]], $condition))->count();
  26. // 交定数量统计
  27. $ding = CustomerVisitLog::where(array_merge([CustomerVisitLog::changeState(['state', '=', '交定'])], [['customer_id', 'in', $customer_id]], $condition))->count();
  28. // 签单数量统计
  29. $sign = CustomerVisitLog::where(array_merge([CustomerVisitLog::changeState(['state', '=', '签单'])], [['customer_id', 'in', $customer_id]], $condition))->count();
  30. //客户线索数量统计
  31. $clueCount = CustomerClue::where('employee_id',$this->request->token['employee_id'])->where($condition)->count();
  32. return json(['code' => 0, 'data' => ['visit' => $visit, 'ding' => $ding, 'sign' => $sign, 'clueCount' => $clueCount]]);
  33. }
  34. /**
  35. * 获取查询的时间范围
  36. */
  37. private function getDate($dateType, $column)
  38. {
  39. switch ($dateType) {
  40. case 'today':
  41. return [$column, 'between', [date('Y-m-d 00:00:00'), date('Y-m-d 00:00:00', strtotime('tomorrow'))]];
  42. case 'yesterday':
  43. return [$column, 'between', [date('Y-m-d 00:00:00', strtotime('yesterday')), date('Y-m-d 00:00:00')]];
  44. case 'serven':
  45. return [$column, 'between', [date('Y-m-d 00:00:00', strtotime('6 days ago')), date('Y-m-d 00:00:00', strtotime('tomorrow'))]];
  46. case 'month':
  47. return [$column, 'between', [date('Y-m-d 00:00:00', strtotime('previous month')), date('Y-m-d 00:00:00', strtotime('tomorrow'))]];
  48. }
  49. return null;
  50. }
  51. }