CustomerFromThreePlatForm.php 11 KB

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