CustomerRemain.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Model\Customer;
  4. use App\Model\Depart;
  5. use App\Model\SeeRange;
  6. use App\Model\WxEmployeeOfficial;
  7. use App\Model\CustomerRemain as CrModel;
  8. use App\Service\OaService;
  9. use Illuminate\Console\Command;
  10. class CustomerRemain extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'command:customer_remain';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Command description';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return mixed
  37. */
  38. public function handle()
  39. {
  40. echo '发送提醒--------start---------------';
  41. $time = time();
  42. $data = CrModel::where("del_time",0)
  43. ->select('id','customer_id','fp','fz')
  44. ->get()->toArray();
  45. if(! empty($data)){
  46. // $fp_id = array_unique(array_column($data,'fp'));
  47. // $depart = Depart::where("del_time",0)
  48. // ->whereIn("id", $fp_id)
  49. // ->select('id','notify_id')
  50. // ->get()->toArray();
  51. // $depart_map = array_column($depart,'notify_id','id');
  52. // $fp_man_id = array_filter(array_unique(array_column($depart,'notify_id')));
  53. $fz_id = array_unique(array_column($data,'fz'));
  54. // $employee_id = array_unique(array_merge($fp_man_id, $fz_id));
  55. $employee_id = array_unique($fz_id);
  56. $wx_map = WxEmployeeOfficial::whereIn('employee_id', $employee_id)
  57. ->pluck('openid','employee_id')
  58. ->toArray();
  59. $send_data = $insert = [];
  60. $customer_id = [];
  61. foreach ($data as $value){
  62. if(! empty($value['fp'])) {
  63. $insert[] = [
  64. 'data_id' => $value['customer_id'], //客户id
  65. 'data_type' => SeeRange::type_one,
  66. 'param_id' => $value['fp'], //门店id
  67. 'type' => SeeRange::data_three,
  68. 'crt_time' => $time,
  69. ];
  70. $customer_id[] = $value['customer_id'];
  71. }
  72. $open_id_fz = $wx_map[$value['fz']] ?? "";
  73. // $fp_man_t = $depart_map[$value['fp']] ?? 0;
  74. // $open_id_fp = $wx_map[$fp_man_t] ?? "";
  75. //负责人 和 门店负责人都为空跳过
  76. // if(empty($open_id_fz) && empty($open_id_fp)) continue;
  77. if(empty($open_id_fz)) continue;
  78. //组织发送
  79. $this->makeData($send_data, $open_id_fz, $value);
  80. // $this->makeData($send_data, $open_id_fp, $value);
  81. }
  82. if(! empty($send_data)){
  83. echo "发送中\n";
  84. $send_data = array_values($send_data);
  85. (new OaService())->sendWxOaCheckMessage($send_data, 1);
  86. }
  87. $c_id = array_column($data,'customer_id');
  88. //清空分配门店
  89. SeeRange::where('del_time',0)
  90. ->whereIn('data_id', $c_id)
  91. ->where('data_type',SeeRange::type_one)
  92. ->where('type',SeeRange::data_three)
  93. ->update(['del_time' => $time]);
  94. //分配门店
  95. if(! empty($insert)) SeeRange::insert($insert);
  96. //更新单据
  97. CrModel::whereIn('id',array_column($data,'id'))
  98. ->update(['del_time' => $time]);
  99. if(! empty($customer_id)) Customer::whereIn('id', $customer_id)->update(['fp_time' => $time]);
  100. }else{
  101. echo "暂无\n";
  102. }
  103. echo '发送提醒--------end---------------';
  104. }
  105. private function makeData(&$send_data, $open_id, $value){
  106. if(! empty($open_id)) {
  107. if(! isset($send_data[$open_id])){
  108. $send_data[$open_id] = [
  109. 'employee_id' => 12343215,
  110. 'type' => 2,
  111. 'state' => 0,
  112. 'menu_id' => "16|list",
  113. 'openid' => $open_id,
  114. 'order_number' => $value['customer_id'],
  115. 'tmp_data' => [
  116. time(),
  117. "客资分配提醒",
  118. "已分配",
  119. "T9品牌中心",
  120. date('Y-m-d H:i:s'),
  121. ],
  122. ];
  123. }else{
  124. $tmp = explode(',', $send_data[$open_id]['order_number']);
  125. if(! in_array($value['customer_id'], $tmp)) $send_data[$open_id]['order_number'] .= ',' . $value['customer_id'];
  126. }
  127. }
  128. }
  129. }