Açıklama Yok

TeamOrderRateByDayHistory.php 4.5KB

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