CustomerFromThreePlatForm.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Model\BasicType;
  4. use App\Model\Customer;
  5. use App\Model\CustomerInfo;
  6. use App\Model\Depart;
  7. use Illuminate\Console\Command;
  8. use Illuminate\Support\Facades\DB;
  9. class CustomerFromThreePlatForm extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'command:customer_from_three_plat_form';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'Command description';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return mixed
  36. */
  37. public function handle()
  38. {
  39. echo '任务--------start---------------';
  40. $basic = BasicType::where('del_time',0)
  41. ->where('type',BasicType::type_10)
  42. ->select('id','title')
  43. ->get()->toArray();
  44. \App\Model\CustomerFromThreePlatForm::where("del_time",0)
  45. ->where("customerName","<>","")
  46. ->orderBy('id')
  47. ->select('id','crt_time','city','actionType','acquireTime','clueType','product','wechat','clueId','transformType','clueSource','clueSourceId','customerName','platform','ipLocationCity','phone','isRepeatClue','flowType','fromUserId')
  48. ->chunkById(20, function ($data) use($basic){
  49. // 开启事务
  50. DB::transaction(function () use ($data,$basic) {
  51. $dataArray = $data->toArray();
  52. $nowStamp = time();
  53. list($main, $main_detail, $fail) = $this->processingData($dataArray, $nowStamp,$basic);
  54. $c_third_platform_id = array_column($main,'c_third_platform_id');
  55. if (! empty($main) ) {
  56. Customer::insert($main);
  57. $map = Customer::whereIn('c_third_platform_id',$c_third_platform_id)
  58. ->pluck('id','c_third_platform_id')
  59. ->toArray();
  60. $insert_detail = [];
  61. foreach ($main_detail as $c_third_platform_id => $value){
  62. if(isset($map[$c_third_platform_id])){
  63. foreach ($value as $v){
  64. $v['customer_id'] = $map[$c_third_platform_id];
  65. $insert_detail[] = $v;
  66. }
  67. }
  68. }
  69. if(! empty($insert_detail)) CustomerInfo::insert($insert_detail);
  70. echo '更新完成' . PHP_EOL;
  71. }
  72. if(! empty($fail)) {
  73. foreach ($fail as $update){
  74. $customer_id = $map[$c_third_platform_id] ?? 0;
  75. \App\Model\CustomerFromThreePlatForm::where('id', $update['id'])
  76. ->update(['del_time' => $update['result'] , 'customer_id' => $customer_id]);
  77. }
  78. }
  79. });
  80. });
  81. echo '任务结束--------end---------------';
  82. }
  83. private function processingData2($data,$time, $basic,$top_depart_id){
  84. $return = $return1 = $return2 = [];
  85. $search = $info_phone = $info_wechat = [];
  86. foreach ($data as $value){
  87. //同样的客户信息只保留最后一次的id
  88. if(! empty($value['phone'])) {
  89. $info_phone[$value['phone']] = $value['id'];
  90. $search[] = $value['phone'];
  91. }
  92. if(! empty($value['wechat'])) {
  93. $info_wechat[$value['wechat']] = $value['id'];
  94. $search[] = $value['wechat'];
  95. }
  96. }
  97. $contact = CustomerInfo::from('customer_info as a')
  98. ->join('customer as b','b.id','a.customer_id')
  99. ->where('a.del_time',0)
  100. ->where('b.del_time',0)
  101. ->where('b.top_depart_id',$top_depart_id)
  102. ->whereIn('a.contact_info', $search)
  103. ->pluck('a.contact_info')
  104. ->toArray();
  105. //result 1 联系方式数据库中已存在失败 2 联系方式重复传递失败 3
  106. foreach ($data as $value){
  107. if(! empty($value['phone'])){
  108. if(in_array($value['phone'], $contact)){
  109. $return2[] = [
  110. 'id' => $value['id'],
  111. 'result' => 1
  112. ];
  113. }else{
  114. $tmp_id = $info_phone[$value['phone']] ?? 0;
  115. if($tmp_id != $value['id']){
  116. $return2[] = [
  117. 'id' => $value['id'],
  118. 'result' => 2
  119. ];
  120. }
  121. }
  122. }
  123. if(! empty($value['wechat']) && in_array($value['wechat'], $contact)){
  124. $return2[] = [
  125. 'id' => $value['id'],
  126. 'result' => 1
  127. ];
  128. continue;
  129. }
  130. }
  131. }
  132. private function processingData($data, $time,$basic)
  133. {
  134. $return = []; // 成功数据
  135. $return_detail = []; // 成功数据
  136. $fail = []; // 失败数据(含 result 标记)
  137. $basic_map = array_column($basic,'title','id');
  138. $info_phone = [];
  139. $info_wechat = [];
  140. $search = [];
  141. // 第一步:同批次去重(保留最后一次 ID)
  142. foreach ($data as $value) {
  143. if (!empty($value['phone'])) {
  144. $info_phone[$value['phone']] = $value['id'];
  145. $search[] = $value['phone'];
  146. }
  147. if (!empty($value['wechat'])) {
  148. $info_wechat[$value['wechat']] = $value['id'];
  149. $search[] = $value['wechat'];
  150. }
  151. }
  152. // 第二步:数据库中已存在的联系方式
  153. $contact = CustomerInfo::from('customer_info as a')
  154. ->join('customer as b', 'b.id', 'a.customer_id')
  155. ->where('a.del_time', 0)
  156. ->where('b.del_time', 0)
  157. ->where('b.top_depart_id', 2)
  158. ->whereIn('a.contact_info', $search)
  159. ->pluck('a.contact_info')
  160. ->toArray();
  161. $contact = array_flip($contact); // 方便 in_array 变成 isset 检查
  162. // 第三步:逐条判断
  163. foreach ($data as $value) {
  164. $id = $value['id'];
  165. $isFail = false;
  166. // 检查手机号
  167. if (!empty($value['phone'])) {
  168. if (isset($contact[$value['phone']])) {
  169. // 数据库已存在
  170. $fail[] = ['id' => $id, 'result' => 1];
  171. $isFail = true;
  172. } elseif (($info_phone[$value['phone']] ?? 0) !== $id) {
  173. // 同批重复
  174. $fail[] = ['id' => $id, 'result' => 2];
  175. $isFail = true;
  176. }
  177. }
  178. // 检查微信号(如果之前手机号已 fail,这里也可以继续检查,因为两者可能不同)
  179. if (!$isFail && !empty($value['wechat'])) {
  180. if (isset($contact[$value['wechat']])) {
  181. $fail[] = ['id' => $id, 'result' => 1];
  182. $isFail = true;
  183. } elseif (($info_wechat[$value['wechat']] ?? 0) !== $id) {
  184. $fail[] = ['id' => $id, 'result' => 2];
  185. $isFail = true;
  186. }
  187. }
  188. // 成功数据
  189. if (! $isFail) {
  190. $fail[] = ['id' => $id, 'result' => 3];
  191. $car_type = 0;
  192. if(! empty($value['product'])){
  193. $t = explode('/', $value['product']);
  194. foreach ($t as $t_v){
  195. if($car_type) continue;
  196. if(isset($basic_map[$t_v])) $car_type = $basic_map[$t_v];
  197. }
  198. }
  199. $customer_from = 0;
  200. if($value['platform'] == 1){
  201. $customer_from = 17190;//抖音DY
  202. }elseif($value['platform'] == 2){
  203. $customer_from = 17202;//快手KS
  204. }elseif ($value['platform'] == 5){
  205. $customer_from = 17195;//公众号GZH
  206. }
  207. $enter_time = $time;
  208. if(! empty($value['acquireTime'])) $enter_time = bcdiv($value['acquireTime'],1000);
  209. $return[] = [
  210. 'c_third_platform_id' => $id,
  211. 'title' => $value['customerName'],
  212. 'model_type' => Customer::Model_type_one,
  213. 'progress_stage' => 9315, //A后台私信
  214. 'car_type' => $car_type,
  215. 'customer_from' => $customer_from,
  216. 'address2' => ! empty($value['city']) ? $value['city'] : $value['ipLocationCity'],
  217. 'depart_id' => 2,
  218. 'top_depart_id' => 2,
  219. 'crt_id' => 1,
  220. 'crt_time' => $time,
  221. 'upd_time' => $time,
  222. 'enter_time' => $enter_time,
  223. ];
  224. if(! empty($value['phone'])){
  225. $return_detail[$id][] = [
  226. 'customer_id' => 0,
  227. 'contact_type' => 133,
  228. 'contact_info' => $value['phone'],
  229. 'crt_time' => $time,
  230. 'type' => CustomerInfo::type_one
  231. ];
  232. }
  233. if(! empty($value['wechat'])){
  234. $return_detail[$id][] = [
  235. 'customer_id' => 0,
  236. 'contact_type' => 135,
  237. 'contact_info' => $value['wechat'],
  238. 'crt_time' => $time,
  239. 'type' => CustomerInfo::type_one
  240. ];
  241. }
  242. }
  243. }
  244. return [$return, $return_detail, $fail];
  245. }
  246. }