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. if(! empty($fail)) {
  71. foreach ($fail as $update){
  72. $customer_id = $map[$c_third_platform_id] ?? 0;
  73. \App\Model\CustomerFromThreePlatForm::where('id', $update['id'])
  74. ->update(['del_time' => $update['result'] , 'customer_id' => $customer_id]);
  75. }
  76. }
  77. echo '更新完成' . PHP_EOL;
  78. } else {
  79. echo '暂无数据写入' . PHP_EOL;
  80. }
  81. });
  82. });
  83. echo '任务结束--------end---------------';
  84. }
  85. private function processingData2($data,$time, $basic,$top_depart_id){
  86. $return = $return1 = $return2 = [];
  87. $search = $info_phone = $info_wechat = [];
  88. foreach ($data as $value){
  89. //同样的客户信息只保留最后一次的id
  90. if(! empty($value['phone'])) {
  91. $info_phone[$value['phone']] = $value['id'];
  92. $search[] = $value['phone'];
  93. }
  94. if(! empty($value['wechat'])) {
  95. $info_wechat[$value['wechat']] = $value['id'];
  96. $search[] = $value['wechat'];
  97. }
  98. }
  99. $contact = CustomerInfo::from('customer_info as a')
  100. ->join('customer as b','b.id','a.customer_id')
  101. ->where('a.del_time',0)
  102. ->where('b.del_time',0)
  103. ->where('b.top_depart_id',$top_depart_id)
  104. ->whereIn('a.contact_info', $search)
  105. ->pluck('a.contact_info')
  106. ->toArray();
  107. //result 1 联系方式数据库中已存在失败 2 联系方式重复传递失败 3
  108. foreach ($data as $value){
  109. if(! empty($value['phone'])){
  110. if(in_array($value['phone'], $contact)){
  111. $return2[] = [
  112. 'id' => $value['id'],
  113. 'result' => 1
  114. ];
  115. }else{
  116. $tmp_id = $info_phone[$value['phone']] ?? 0;
  117. if($tmp_id != $value['id']){
  118. $return2[] = [
  119. 'id' => $value['id'],
  120. 'result' => 2
  121. ];
  122. }
  123. }
  124. }
  125. if(! empty($value['wechat']) && in_array($value['wechat'], $contact)){
  126. $return2[] = [
  127. 'id' => $value['id'],
  128. 'result' => 1
  129. ];
  130. continue;
  131. }
  132. }
  133. }
  134. private function processingData($data, $time,$basic)
  135. {
  136. $return = []; // 成功数据
  137. $return_detail = []; // 成功数据
  138. $fail = []; // 失败数据(含 result 标记)
  139. $basic_map = array_column($basic,'title','id');
  140. $info_phone = [];
  141. $info_wechat = [];
  142. $search = [];
  143. // 第一步:同批次去重(保留最后一次 ID)
  144. foreach ($data as $value) {
  145. if (!empty($value['phone'])) {
  146. $info_phone[$value['phone']] = $value['id'];
  147. $search[] = $value['phone'];
  148. }
  149. if (!empty($value['wechat'])) {
  150. $info_wechat[$value['wechat']] = $value['id'];
  151. $search[] = $value['wechat'];
  152. }
  153. }
  154. // 第二步:数据库中已存在的联系方式
  155. $contact = CustomerInfo::from('customer_info as a')
  156. ->join('customer as b', 'b.id', 'a.customer_id')
  157. ->where('a.del_time', 0)
  158. ->where('b.del_time', 0)
  159. ->where('b.top_depart_id', 2)
  160. ->whereIn('a.contact_info', $search)
  161. ->pluck('a.contact_info')
  162. ->toArray();
  163. $contact = array_flip($contact); // 方便 in_array 变成 isset 检查
  164. // 第三步:逐条判断
  165. foreach ($data as $value) {
  166. $id = $value['id'];
  167. $isFail = false;
  168. // 检查手机号
  169. if (!empty($value['phone'])) {
  170. if (isset($contact[$value['phone']])) {
  171. // 数据库已存在
  172. $fail[] = ['id' => $id, 'result' => 1];
  173. $isFail = true;
  174. } elseif (($info_phone[$value['phone']] ?? 0) !== $id) {
  175. // 同批重复
  176. $fail[] = ['id' => $id, 'result' => 2];
  177. $isFail = true;
  178. }
  179. }
  180. // 检查微信号(如果之前手机号已 fail,这里也可以继续检查,因为两者可能不同)
  181. if (!$isFail && !empty($value['wechat'])) {
  182. if (isset($contact[$value['wechat']])) {
  183. $fail[] = ['id' => $id, 'result' => 1];
  184. $isFail = true;
  185. } elseif (($info_wechat[$value['wechat']] ?? 0) !== $id) {
  186. $fail[] = ['id' => $id, 'result' => 2];
  187. $isFail = true;
  188. }
  189. }
  190. // 成功数据
  191. if (! $isFail) {
  192. $fail[] = ['id' => $id, 'result' => 3];
  193. $car_type = 0;
  194. if(! empty($value['product'])){
  195. $t = explode('/', $value['product']);
  196. foreach ($t as $t_v){
  197. if($car_type) continue;
  198. if(isset($basic_map[$t_v])) $car_type = $basic_map[$t_v];
  199. }
  200. }
  201. $customer_from = 0;
  202. if($value['platform'] == 1){
  203. $customer_from = 17190;//抖音DY
  204. }elseif($value['platform'] == 2){
  205. $customer_from = 17202;//快手KS
  206. }elseif ($value['platform'] == 5){
  207. $customer_from = 17195;//公众号GZH
  208. }
  209. $enter_time = $time;
  210. if(! empty($value['acquireTime'])) $enter_time = bcdiv($value['acquireTime'],1000);
  211. $return[] = [
  212. 'c_third_platform_id' => $id,
  213. 'title' => $value['customerName'],
  214. 'model_type' => Customer::Model_type_one,
  215. 'progress_stage' => 9315, //A后台私信
  216. 'car_type' => $car_type,
  217. 'customer_from' => $customer_from,
  218. 'address2' => ! empty($value['city']) ? $value['city'] : $value['ipLocationCity'],
  219. 'depart_id' => 2,
  220. 'top_depart_id' => 2,
  221. 'crt_id' => 1,
  222. 'crt_time' => $time,
  223. 'upd_time' => $time,
  224. 'enter_time' => $enter_time,
  225. ];
  226. if(! empty($value['phone'])){
  227. $return_detail[$id][] = [
  228. 'customer_id' => 0,
  229. 'contact_type' => 133,
  230. 'contact_info' => $value['phone'],
  231. 'crt_time' => $time,
  232. 'type' => CustomerInfo::type_one
  233. ];
  234. }
  235. if(! empty($value['wechat'])){
  236. $return_detail[$id][] = [
  237. 'customer_id' => 0,
  238. 'contact_type' => 135,
  239. 'contact_info' => $value['wechat'],
  240. 'crt_time' => $time,
  241. 'type' => CustomerInfo::type_one
  242. ];
  243. }
  244. }
  245. }
  246. return [$return, $return_detail, $fail];
  247. }
  248. }