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. $contact = array_flip($contact); // 方便 in_array 变成 isset 检查
  147. // 第三步:逐条判断
  148. foreach ($data as $value) {
  149. $id = $value['id'];
  150. $isFail = false;
  151. // 检查手机号
  152. if (!empty($value['phone'])) {
  153. if (isset($contact[$value['phone']])) {
  154. // 数据库已存在
  155. $fail[] = ['id' => $id, 'result' => 1];
  156. $isFail = true;
  157. } elseif (($info_phone[$value['phone']] ?? 0) !== $id) {
  158. // 同批重复
  159. $fail[] = ['id' => $id, 'result' => 2];
  160. $isFail = true;
  161. }
  162. }
  163. // 检查微信号(如果之前手机号已 fail,这里也可以继续检查,因为两者可能不同)
  164. if (!$isFail && !empty($value['wechat'])) {
  165. if (isset($contact[$value['wechat']])) {
  166. $fail[] = ['id' => $id, 'result' => 1];
  167. $isFail = true;
  168. } elseif (($info_wechat[$value['wechat']] ?? 0) !== $id) {
  169. $fail[] = ['id' => $id, 'result' => 2];
  170. $isFail = true;
  171. }
  172. }
  173. // 成功数据
  174. if (! $isFail) {
  175. $fail[] = ['id' => $id, 'result' => 3];
  176. $car_type = 0;
  177. if(! empty($value['product'])){
  178. $t = explode('/', $value['product']);
  179. foreach ($t as $t_v){
  180. if($car_type) continue;
  181. if(isset($basic_map[$t_v])) $car_type = $basic_map[$t_v];
  182. }
  183. }
  184. $customer_from = 0;
  185. if($value['platform'] == 1){
  186. $customer_from = 17190;//抖音DY
  187. }elseif($value['platform'] == 2){
  188. $customer_from = 17202;//快手KS
  189. }elseif ($value['platform'] == 5){
  190. $customer_from = 17195;//公众号GZH
  191. }
  192. $enter_time = $time;
  193. if(! empty($value['acquireTime'])) $enter_time = bcdiv($value['acquireTime'],1000);
  194. $return[] = [
  195. 'c_third_platform_id' => $id,
  196. 'title' => $value['customerName'],
  197. 'model_type' => Customer::Model_type_one,
  198. 'progress_stage' => 9315, //A后台私信
  199. 'car_type' => $car_type,
  200. 'customer_from' => $customer_from,
  201. 'address2' => ! empty($value['city']) ? $value['city'] : $value['ipLocationCity'],
  202. 'depart_id' => 2,
  203. 'top_depart_id' => 2,
  204. 'crt_id' => 1,
  205. 'crt_time' => $time,
  206. 'upd_time' => $time,
  207. 'enter_time' => $enter_time,
  208. ];
  209. if(! empty($value['phone'])){
  210. $return_detail[$id][] = [
  211. 'customer_id' => 0,
  212. 'contact_type' => 133,
  213. 'contact_info' => $value['phone'],
  214. 'crt_time' => $time,
  215. 'type' => CustomerInfo::type_one
  216. ];
  217. }
  218. if(! empty($value['wechat'])){
  219. $return_detail[$id][] = [
  220. 'customer_id' => 0,
  221. 'contact_type' => 135,
  222. 'contact_info' => $value['wechat'],
  223. 'crt_time' => $time,
  224. 'type' => CustomerInfo::type_one
  225. ];
  226. }
  227. $setting = $dhf_map[$value['clueSourceId']] ?? [];
  228. if(! empty($setting['notify_id']) && ! empty($setting['top_depart_id'])){
  229. $return_detail2[$id][] = [
  230. 'customer_id' => 0,
  231. 'data_id' => $setting['notify_id'],
  232. 'crt_time' => $time,
  233. 'type' => CustomerInfo::type_two
  234. ];
  235. $return_detail3[$id][] = [
  236. 'customer_id' => 0,
  237. 'fp' => $setting['top_depart_id'],
  238. 'fz' => $setting['notify_id'],
  239. 'crt_time' => $time,
  240. ];
  241. }
  242. }
  243. }
  244. return [$return, $return_detail, $fail, $return_detail2, $return_detail3];
  245. }
  246. }