CustomerRemain.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. $wx_map = WxEmployeeOfficial::whereIn('employee_id', $employee_id)
  56. ->pluck('openid','employee_id')
  57. ->toArray();
  58. $send_data = $insert = [];
  59. foreach ($data as $value){
  60. if(! empty($value['fp'])) {
  61. $insert[] = [
  62. 'data_id' => $value['customer_id'], //客户id
  63. 'data_type' => SeeRange::type_one,
  64. 'param_id' => $value['fp'], //门店id
  65. 'type' => SeeRange::data_three,
  66. 'crt_time' => $time,
  67. ];
  68. }
  69. $open_id_fz = $wx_map[$value['fz']] ?? "";
  70. $fp_man_t = $depart_map[$value['fp']] ?? 0;
  71. $open_id_fp = $wx_map[$fp_man_t] ?? "";
  72. //负责人 和 门店负责人都为空跳过
  73. if(empty($open_id_fz) && empty($open_id_fp)) continue;
  74. //组织发送
  75. $this->makeData($send_data, $open_id_fz, $value);
  76. $this->makeData($send_data, $open_id_fp, $value);
  77. }
  78. if(! empty($send_data)){
  79. echo "发送中\n";
  80. $send_data = array_values($send_data);
  81. (new OaService())->sendWxOaCheckMessage($send_data, 1);
  82. }
  83. $customer_id = array_column($data,'customer_id');
  84. //清空分配门店
  85. SeeRange::where('del_time',0)
  86. ->whereIn('data_id', $customer_id)
  87. ->where('data_type',SeeRange::type_one)
  88. ->where('type',SeeRange::data_three)
  89. ->update(['del_time' => $time]);
  90. //分配门店
  91. if(! empty($insert)) SeeRange::insert($insert);
  92. //更新单据
  93. CrModel::whereIn('id',array_column($data,'id'))
  94. ->update(['del_time' => $time]);
  95. Customer::whereIn('id', $customer_id)->update(['fp_time' => $time]);
  96. }else{
  97. echo "暂无\n";
  98. }
  99. echo '发送提醒--------end---------------';
  100. }
  101. private function makeData(&$send_data, $open_id, $value){
  102. if(! empty($open_id)) {
  103. if(! isset($send_data[$open_id])){
  104. $send_data[$open_id] = [
  105. 'employee_id' => 12343215,
  106. 'type' => 2,
  107. 'state' => 0,
  108. 'menu_id' => "16|list",
  109. 'openid' => $open_id,
  110. 'order_number' => $value['customer_id'],
  111. 'tmp_data' => [
  112. time(),
  113. "客资分配提醒",
  114. "已分配",
  115. "T9品牌中心",
  116. date('Y-m-d H:i:s'),
  117. ],
  118. ];
  119. }else{
  120. $tmp = explode(',', $send_data[$open_id]['order_number']);
  121. if(! in_array($value['customer_id'], $tmp)) $send_data[$open_id]['order_number'] .= ',' . $value['customer_id'];
  122. }
  123. }
  124. }
  125. }