CustomerFromThreePlatForm.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Model\BasicType;
  4. use App\Model\Customer;
  5. use App\Model\CustomerAccessFlat;
  6. use App\Model\CustomerInfo;
  7. use App\Model\CustomerRemain;
  8. use App\Model\DepartWithDHF;
  9. use Illuminate\Console\Command;
  10. use Illuminate\Support\Facades\DB;
  11. class CustomerFromThreePlatForm extends Command
  12. {
  13. /**
  14. * The name and signature of the console command.
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'command:customer_from_three_plat_form';
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = 'Command description';
  25. /**
  26. * Create a new command instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. }
  34. /**
  35. * Execute the console command.
  36. *
  37. * @return mixed
  38. */
  39. public function handle()
  40. {
  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)) {
  89. CustomerInfo::insert($insert_detail_2);
  90. // 新增
  91. $insertArray = CustomerInfo::whereIn('customer_id', array_values($map))
  92. ->where('crt_time', $nowStamp)
  93. ->whereIn('type', [CustomerInfo::type_two])
  94. ->select('id as source_id',DB::raw(CustomerAccessFlat::source_type_one .' as source_type'), 'customer_id','data_id as target_id',DB::raw(CustomerAccessFlat::type_one .' as type'),DB::raw('2 as top_depart_id'))
  95. ->get()
  96. ->toArray();
  97. if(! empty($insertArray)) CustomerAccessFlat::insert($insertArray);
  98. }
  99. }
  100. if(! empty($main_detail3)){
  101. $insert_detail_3 = [];
  102. foreach ($main_detail3 as $id => $value){
  103. if(isset($map[$id])){
  104. foreach ($value as $v){
  105. $v['customer_id'] = $map[$id];
  106. $insert_detail_3[] = $v;
  107. }
  108. }
  109. }
  110. if(! empty($insert_detail_3)) CustomerRemain::insert($insert_detail_3);
  111. }
  112. echo '更新完成' . PHP_EOL;
  113. }
  114. if(! empty($fail)) {
  115. foreach ($fail as $update){
  116. $customer_id = $map[$update['id']] ?? 0;
  117. \App\Model\CustomerFromThreePlatForm::where('id', $update['id'])
  118. ->update(['del_time' => $update['result'] , 'customer_id' => $customer_id]);
  119. }
  120. }
  121. });
  122. });
  123. }
  124. private function processingData($data, $time,$basic,$dhf_map)
  125. {
  126. $return = []; // 客户主表
  127. $return_detail = []; // 客户联系方式
  128. $return_detail2 = []; // 客户负责人
  129. $return_detail3 = []; // 客户负责人提醒
  130. $fail = []; // 失败数据(含 result 标记)
  131. $basic_map = array_column($basic,'title','id');
  132. $info_phone = [];
  133. $info_wechat = [];
  134. $search = [];
  135. // 第一步:同批次去重(保留最后一次 ID)
  136. foreach ($data as $value) {
  137. if (!empty($value['phone'])) {
  138. $info_phone[$value['phone']] = $value['id'];
  139. $search[] = $value['phone'];
  140. }
  141. if (!empty($value['wechat'])) {
  142. $info_wechat[$value['wechat']] = $value['id'];
  143. $search[] = $value['wechat'];
  144. }
  145. }
  146. // 第二步:数据库中已存在的联系方式
  147. // $contact = CustomerInfo::from('customer_info as a')
  148. // ->join('customer as b', 'b.id', 'a.customer_id')
  149. // ->where('a.del_time', 0)
  150. // ->where('b.del_time', 0)
  151. // ->where('b.top_depart_id', 2)
  152. // ->whereIn('a.contact_info', $search)
  153. // ->pluck('a.contact_info')
  154. // ->toArray();
  155. //
  156. // $contact = array_flip($contact); // 方便 in_array 变成 isset 检查
  157. // 第三步:逐条判断
  158. foreach ($data as $value) {
  159. $id = $value['id'];
  160. $isFail = false;
  161. // 检查手机号
  162. if (!empty($value['phone'])) {
  163. if (isset($contact[$value['phone']])) {
  164. // 数据库已存在
  165. $fail[] = ['id' => $id, 'result' => 1];
  166. $isFail = true;
  167. } elseif (($info_phone[$value['phone']] ?? 0) !== $id) {
  168. // 同批重复
  169. $fail[] = ['id' => $id, 'result' => 2];
  170. $isFail = true;
  171. }
  172. }
  173. // 检查微信号(如果之前手机号已 fail,这里也可以继续检查,因为两者可能不同)
  174. if (!$isFail && !empty($value['wechat'])) {
  175. if (isset($contact[$value['wechat']])) {
  176. $fail[] = ['id' => $id, 'result' => 1];
  177. $isFail = true;
  178. } elseif (($info_wechat[$value['wechat']] ?? 0) !== $id) {
  179. $fail[] = ['id' => $id, 'result' => 2];
  180. $isFail = true;
  181. }
  182. }
  183. // 成功数据
  184. if (! $isFail) {
  185. $fail[] = ['id' => $id, 'result' => 3];
  186. $car_type = 0;
  187. if(! empty($value['product'])){
  188. $t = explode('/', $value['product']);
  189. foreach ($t as $t_v){
  190. if($car_type) continue;
  191. if(isset($basic_map[$t_v])) $car_type = $basic_map[$t_v];
  192. }
  193. }
  194. $customer_from = 0;
  195. if($value['platform'] == 1){
  196. $customer_from = 17190;//抖音DY
  197. }elseif($value['platform'] == 2){
  198. $customer_from = 17202;//快手KS
  199. }elseif ($value['platform'] == 5){
  200. $customer_from = 17195;//公众号GZH
  201. }
  202. $enter_time = $time;
  203. if(! empty($value['acquireTime'])) $enter_time = bcdiv($value['acquireTime'],1000);
  204. $return[] = [
  205. 'c_third_platform_id' => $id,
  206. 'title' => $value['customerName'],
  207. 'model_type' => Customer::Model_type_one,
  208. 'progress_stage' => 9315, //A后台私信
  209. 'car_type' => $car_type,
  210. 'customer_from' => $customer_from,
  211. 'address2' => ! empty($value['city']) ? $value['city'] : $value['ipLocationCity'],
  212. 'depart_id' => 2,
  213. 'top_depart_id' => 2,
  214. 'crt_id' => 1,
  215. 'crt_time' => $time,
  216. 'upd_time' => $time,
  217. 'enter_time' => $enter_time,
  218. ];
  219. if(! empty($value['phone'])){
  220. $return_detail[$id][] = [
  221. 'customer_id' => 0,
  222. 'contact_type' => 133,
  223. 'contact_info' => $value['phone'],
  224. 'crt_time' => $time,
  225. 'type' => CustomerInfo::type_one
  226. ];
  227. }
  228. if(! empty($value['wechat'])){
  229. $return_detail[$id][] = [
  230. 'customer_id' => 0,
  231. 'contact_type' => 135,
  232. 'contact_info' => $value['wechat'],
  233. 'crt_time' => $time,
  234. 'type' => CustomerInfo::type_one
  235. ];
  236. }
  237. $setting = $dhf_map[$value['clueSourceId']] ?? [];
  238. if(! empty($setting['notify_id']) && ! empty($setting['top_depart_id'])){
  239. $return_detail2[$id][] = [
  240. 'customer_id' => 0,
  241. 'data_id' => $setting['notify_id'],
  242. 'crt_time' => $time,
  243. 'type' => CustomerInfo::type_two
  244. ];
  245. $return_detail3[$id][] = [
  246. 'customer_id' => 0,
  247. 'fp' => $setting['top_depart_id'],
  248. 'fz' => $setting['notify_id'],
  249. 'crt_time' => $time,
  250. ];
  251. }
  252. }
  253. }
  254. return [$return, $return_detail, $fail, $return_detail2, $return_detail3];
  255. }
  256. }