Ei kuvausta

TeamOrderRateByDay.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use DB;
  5. class TeamOrderRateByDay extends Command {
  6. protected $signature = 'TeamOrderRateByDay';
  7. /**
  8. * The console command description.
  9. *
  10. * @var string
  11. */
  12. protected $description = '每日团队综合成单率日志脚本';
  13. public function handle()
  14. {
  15. $this->TeamOrderRateByDay();
  16. }
  17. public function TeamOrderRateByDay(){
  18. $team_ids = [1,3,5];
  19. foreach($team_ids as $team_id){
  20. #团队成员:
  21. $saler_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  22. #昨天日期:
  23. $date = date('Y-m-d', strtotime('-1 day'));
  24. //第一步: 统计昨日数据
  25. #昨日加粉 and 昨日成单
  26. $phones = DB::table('customers')->where('fanTime', $date)->lists('phone');
  27. $order_count = DB::table('order')->where('team_id', $team_id)->whereIn('receiverMobile', $phones)->where('createTime','>=',$date)->where('createTime','<=',$date.' 23:59:59')->where('is_del', 0)->count();
  28. #昨日加粉
  29. $fan_add = DB::table('cust_day_detail')->whereIn('admin_id', $saler_ids)->where('is_del', 0)->where('dtime', $date)->sum('fan_add');
  30. #计算昨日成单率
  31. $rate = $fan_add>0 ? round($order_count / $fan_add, 4) * 100 : 0;
  32. //存入数据表
  33. $result = $this->updateData($date, $rate, $date, $team_id);
  34. echo "\nDate:".$date." fan:".$fan_add." order_count:".$order_count." rate:".$rate." 处理结果:".$result;
  35. //第二布:统计往期数据
  36. #查日期列表
  37. $dates = DB::table('team_cust_day_remain')->select('idate')->where('team_id', $team_id)->where('idate','<', $date)->groupBy('idate')->orderBy('idate', 'desc')->get();
  38. if(!empty($dates)){
  39. $dates = json_decode(json_encode($dates), true);
  40. foreach($dates as $k=>$v){
  41. #当前日期加粉 and 昨日成单
  42. $phones = DB::table('customers')->where('fanTime', $v['idate'])->lists('phone');
  43. $order_count = DB::table('order')->where('team_id', $team_id)->whereIn('receiverMobile', $phones)->where('createTime','>=',$date)->where('createTime','<=',$date.' 23:59:59')->where('is_del', 0)->count();
  44. #当前日期加粉
  45. $fan_add = DB::table('cust_day_detail')->whereIn('admin_id', $saler_ids)->where('is_del', 0)->where('dtime', $v['idate'])->sum('fan_add');
  46. #计算当前日期加粉在昨日成单率
  47. $rate = $fan_add>0 ? round($order_count / $fan_add, 4) * 100 : 0;
  48. //存入数据表
  49. $result = $this->updateData($v['idate'], $rate, $date, $team_id);
  50. echo "\nDate:".$v['idate']." fan:".$fan_add." order_count:".$order_count." rate:".$rate." 处理结果:".$result;
  51. }
  52. }
  53. }
  54. }
  55. /**
  56. * @param idate:统计日期 rate:成单率 date:昨日
  57. *
  58. */
  59. public function updateData($idate, $rate, $date, $team_id){
  60. #当前月
  61. $month = date('Y-m', strtotime($date));
  62. #当月1号
  63. $month_first = date('Y-m-01', strtotime($date));
  64. $data = array();
  65. #如果是昨天的数据(或昨天是当月一号)直接插入
  66. if($idate == $date || $date == $month_first){
  67. $data['team_id'] = $team_id;
  68. $data['idate'] = $idate;
  69. $data['month_time'] = $month;
  70. $data['order_rate_data'] = json_encode([0=>['date'=>$date, 'rate'=>$rate]]);
  71. $res = DB::table('team_cust_day_remain')->insert($data);
  72. }else{
  73. //往期成单
  74. $odata = DB::table('team_cust_day_remain')->where('team_id', $team_id)->where('idate', $idate)->where('month_time', $month)->first();
  75. $order_rate_data = json_decode($odata->order_rate_data, true);
  76. $order_rate_data[] = ['date'=>$date, 'rate'=>$rate];
  77. $data['order_rate_data'] = json_encode($order_rate_data);
  78. $res = DB::table('team_cust_day_remain')->where('team_id', $team_id)->where('idate', $idate)->where('month_time', $month)->update($data);
  79. }
  80. return $res;
  81. }
  82. }