企微短剧业务系统

CustomerTagFlush.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\RedisModel;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\DB;
  6. class CustomerTagFlush extends Command
  7. {
  8. protected $signature = 'CustomerTagFlush';
  9. protected $description = '客户标签洗入客户详情表';
  10. private $startId = 0;
  11. private $pageSize = 1000;
  12. private $redisKey = 'Playlet::CustomerTagFlush-';
  13. public function handle()
  14. {
  15. \DB::connection()->disableQueryLog();
  16. $this->info(date('m-d H:i:s') . ' 开始整理');
  17. for($i=0; $i<50; $i++) {
  18. $start = RedisModel::get($this->redisKey.$i);
  19. if(empty($start)) {
  20. $this->startId = 0;
  21. RedisModel::set($this->redisKey.$i, 0);
  22. RedisModel::expire($this->redisKey.$i, 86400);
  23. } else {
  24. $this->startId = $start;
  25. }
  26. try{
  27. $this->flush($i);
  28. sleep(1);
  29. } catch (\Exception $exception) {
  30. $this->info('错误信息:'.$exception->getLine().', '.$exception->getMessage());
  31. }
  32. }
  33. $this->info(date('m-d H:i:s') . ' 整理结束');
  34. }
  35. private function flush($label)
  36. {
  37. $tagTable = 'dj_customer_tag_'.$label;
  38. $relationTable = 'dj_customer_relation_'.$label;
  39. $customerTable = 'dj_customer_'.$label;
  40. $customerDetail = 'dj_customer_details_'.$label;
  41. $this->info('当前处理的分表标识为:'. $label);
  42. do{
  43. # 获取外部联系人关系信息
  44. $customerRelationList = DB::table($relationTable)->select(['id', 'corpid', 'user_id', 'customer_id',
  45. 'external_userid', 'remark', 'description', 'createtime', 'remark_corp_name', 'remark_mobiles',
  46. 'add_way', 'wechat_channels', 'oper_userid', 'source', 'state', 'enable', 'loss_time', 'blacklist_status'])
  47. ->where('id', '>', $this->startId)->orderBy('id', 'asc')->limit($this->pageSize)->get();
  48. $count = $customerRelationList->count();
  49. $this->info('本次起始的ID:'.$this->startId);
  50. $this->info('本次共处理的消息条数'.$count);
  51. $customerIds = $customerRelationList->pluck('customer_id');
  52. # 获取外部联系人列表
  53. $customerList = DB::table($customerTable)->select(['id', 'corpid', 'external_userid', 'name', 'pay_num', 'gender'])
  54. ->whereIn('id', $customerIds)->get();
  55. # 获取对应外部联系人标签信息
  56. $customerTagList = DB::table($tagTable)->select(['tag_id', 'corpid', 'user_id', 'customer_id', 'external_userid'])
  57. ->where('enable', 1)->whereIn('customer_id', $customerIds)->get();
  58. $updateData = [];
  59. foreach($customerRelationList as $k => $customer) {
  60. $customerInfo = $customerList->where('id', $customer->customer_id)->first();
  61. $tagList = $customerTagList->where('customer_id', $customer->customer_id)->pluck('tag_id')->toArray();
  62. $newTagList = [];
  63. foreach($tagList as $tagId){
  64. $newTagList[] = md5($tagId);
  65. }
  66. $item = json_decode(json_encode($customer), 1);
  67. $item['name'] = $customerInfo->name;
  68. $item['gender'] = $customerInfo->gender;
  69. $item['is_paid'] = $customerInfo->pay_num ? 1 : 0;
  70. $item['tag_list'] = empty($newTagList) ? '' : implode(',', array_unique($newTagList));
  71. $item['con_user_cus'] = $customer->user_id.$customer->external_userid;
  72. // $updateData[$k] = $item;
  73. DB::table($customerDetail)
  74. ->updateOrInsert([
  75. 'corpid' => $item['corpid'],
  76. 'user_id' => $item['user_id'],
  77. 'external_userid' => $item['external_userid'],
  78. ], $item);
  79. $this->startId = $customerRelationList->max('id');
  80. RedisModel::set($this->redisKey.$label, $this->startId);
  81. }
  82. // DB::table($customerDetail)
  83. // ->insert($updateData);
  84. } while ($this->pageSize == $count);
  85. }
  86. }