Keine Beschreibung

OrderRateByDayHistory.php 3.9KB

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