SetNotVisitCustomers.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace app\command;
  3. use app\event\Msg;
  4. use app\model\Customer;
  5. use app\model\CustomerNotVisit;
  6. use app\model\CustomerVisitLog;
  7. use app\model\Employee;
  8. use app\model\Org;
  9. use think\console\Command;
  10. use think\console\input\Argument;
  11. use think\console\Input;
  12. use think\console\input\Option;
  13. use think\console\Output;
  14. //勋章管理
  15. class SetNotVisitCustomers extends Command
  16. {
  17. protected function configure()
  18. {
  19. $this->setName('set_not_visit_customers')
  20. ->addOption('date', 'd', Option::VALUE_OPTIONAL, "要统计的日期")
  21. ->setDescription('未回访客户提醒');
  22. }
  23. protected function execute(Input $input, Output $output)
  24. {
  25. if ($input->hasOption('date')) {
  26. $date = date('Y-m-d', strtotime($input->getOption('date')));
  27. } else {
  28. $date = date('Y-m-d');
  29. }
  30. $exit = CustomerNotVisit::where(['appoint_date' => $date])->find();
  31. if ($exit) return $output->writeln($date . '已记录完毕!无需重复计入。');
  32. // 查询预约到当前时间要回访的客户
  33. $appointVisit = CustomerVisitLog::where(['next_contact_date' => $date])
  34. ->group('customer_id')
  35. ->column('count(id)', 'customer_id');
  36. if (empty($appointVisit)) return $output->writeln($date . '已记录完毕!0条数据被录入');
  37. // 查询实际回访次数
  38. $actualVisit = CustomerVisitLog::where([['addtime', 'like', $date . '%']])
  39. ->group('customer_id')
  40. ->column('count(id)', 'customer_id');
  41. // 预约为回访客户提取
  42. $notVisitCustomerIds = [];
  43. foreach ($appointVisit as $customerId => $num) {
  44. if (!isset($actualVisit[$customerId]) || $actualVisit[$customerId] < $num) $notVisitCustomerIds[] = $customerId;
  45. }
  46. if (empty($notVisitCustomerIds)) return $output->writeln($date . '已记录完毕!0条数据被录入');
  47. // 获取客户数据
  48. $customers = Customer::where([['id', 'in', $notVisitCustomerIds]])
  49. ->field('id as customer_id,employee_id,designer_id,org_id,"' . $date . '" as appoint_date', 'state')
  50. ->select();
  51. // 数据整理,用于消息发送
  52. $org = [];
  53. foreach ($customers as $customer) {
  54. isset($org[$customer['org_id']]) ? $org[$customer['org_id']]++ : $org[$customer['org_id']] = 1;
  55. }
  56. // 获取部门信息 将数量向上统计
  57. $org1 = [];
  58. $orgList = Org::where([['id', 'in', array_keys($org)]])->column('path', 'id');
  59. foreach ($orgList as $orgid => $path) {
  60. $lids = explode('-', trim($path, '-'));
  61. foreach ($lids as $i) {
  62. isset($org1[$i]) ? $org1[$i] += $org[$orgid] : $org1[$i] = $org[$orgid];
  63. }
  64. }
  65. // 保存数据
  66. (new CustomerNotVisit())->saveAll($customers->toArray());
  67. // 发送消息
  68. foreach ($org1 as $id => $num) {
  69. $leaders = Employee::where([['org_id', '=', $id], ['is_manager', '=', 1], ['uid', '<>', 0], ['state', '=', '在职']])->column('id');
  70. foreach ($leaders as $lid) {
  71. event(new Msg($lid, '今日有' . $num . '个客户未及时跟进,点击查看未跟进客户名单', 'notVisit'));
  72. }
  73. }
  74. $output->writeln($date . '已记录完毕!' . count($customers) . '条数据录入');
  75. }
  76. }