Sin descripción

TeamOrderRateByDay.php 4.3KB

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