CustomerRemain.php 6.3 KB

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