DropPoolRemind.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace app\command;
  3. use app\event\Msg;
  4. use think\console\Command;
  5. use think\console\input\Argument;
  6. use think\console\Input;
  7. use think\console\Output;
  8. use app\model\Customer;
  9. use app\model\Employee;
  10. use app\model\Setting;
  11. class DropPoolRemind extends Command
  12. {
  13. protected function configure()
  14. {
  15. $this->setName('droppoolremind')->setDescription('customer drop pool remind');
  16. }
  17. protected function execute(Input $input, Output $output)
  18. {
  19. $setting_list = Setting::where('name', '=', 'pubpool')->select()->toArray();
  20. foreach ($setting_list as $setting) {
  21. $content = !empty($setting['content']) ? json_decode($setting['content'], true) : [];
  22. if (!empty($content)) {
  23. $empty = false; //判断是否是正常数据,因为之前可能有垃圾数据
  24. foreach ($content as $k => $v) {
  25. if (!isset($v['day']) || !isset($v['state'])) {
  26. $empty = true;
  27. }
  28. }
  29. if (!$empty) {
  30. $whereOr = [];
  31. foreach ($content as $k => $v) {
  32. if ($v['state'] == 1 && $v['day'] > 0) {
  33. $today_s = date('Y-m-d 23:59:59', strtotime('yesterday'));
  34. $today_e = date('Y-m-d 00:00:00', strtotime('tomorrow'));
  35. $whereOr[] = [
  36. ['state', 'in', Customer::changeState($k, 'chaos')],
  37. ['protected_to', 'between', [$today_s, $today_e]],
  38. ];
  39. }
  40. }
  41. if (!empty($whereOr)) {
  42. $root_id = $setting['root_id'];
  43. $org_ids = orgSubIds($root_id);
  44. $manager = Employee::where([['root_id', '=', $root_id], ['is_manager', '=', 1]])->column('id,org_id');
  45. $customers = Customer::where([['org_id', 'in', $org_ids]])->where(function ($query) use ($whereOr) {
  46. $query->whereOr($whereOr);
  47. })->field('count(id) as count_id,org_id')->group('org_id')->select()->toArray();
  48. $customers_arr = array_column($customers, 'count_id', 'org_id');
  49. foreach ($manager as $k => $v) {
  50. $count = 0;
  51. if (isset($customers_arr[$v['org_id']])) {
  52. $count = $customers_arr[$v['org_id']];
  53. }
  54. $msg = '今天待回收客户' . $count .'个';
  55. if ($count) {
  56. event(new Msg($v['id'], $msg, 'willDropCustomer', json_encode(date('Y-m-d'))));
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. $output->writeln("true");
  64. }
  65. }