Nav apraksta

OrderRateByDay.php 3.8KB

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