暂无描述

GroupFanAmountByMonth.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php namespace App\Console\Commands;
  2. use Illuminate\Console\Command;
  3. use Illuminate\Support\Facades\DB;
  4. use App\Admin;
  5. use App\Distributors;
  6. class GroupFanAmountByMonth extends Command {
  7. /**
  8. * The console command name.
  9. *
  10. * @var string
  11. */
  12. protected $name = 'GroupFanAmountByMonth';
  13. protected $date;
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = '新粉月销售额梯形图';
  20. public function handle()
  21. {
  22. try{
  23. $this->date = date('Y-m-01',strtotime('-1 months'));
  24. $this->TeamFanAmountByMonth();
  25. } catch (\Exception $e) {
  26. echo 'line:'.$e->getLine().' message:'.$e->getMessage();
  27. }
  28. }
  29. public function TeamFanAmountByMonth() {
  30. $team_ids = Admin::getTeams();
  31. foreach ($team_ids as $team_id) {
  32. $date = $this->date;
  33. $date_first = $this->date;
  34. $date_end = $this->getthemonth($date);
  35. #团队成员:
  36. $saler_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  37. //第一步: 统计上月数据
  38. #上月加粉
  39. $phones = DB::table('customers')->whereIn('admin_id',$saler_ids)->where('fanTime', '>=', $date_first)->where('fanTime', '<=', $date_end)->lists('phone');
  40. $amount = DB::table('order')->whereIn('receiverMobile', $phones)->where('createTime','>=',$date)->where('createTime', '<=', $date_end. '23:59:59')->where('is_del', 0)->where('team_id', $team_id)->sum('receivedAmount');
  41. //存入数据表
  42. $result = $this->updateData($date, $amount, $date, $team_id);
  43. echo "\nDate:".$date." team_id:".$team_id." amount:".$amount." 处理结果:".$result;
  44. //第二布:统计往期数据
  45. #查日期列表
  46. $dates = DB::table('cust_month_remain')->select('idate')->where('team_id', $team_id)->where('idate','<', $date)->groupBy('idate')->orderBy('idate', 'desc')->get();
  47. if( !empty($dates) ) {
  48. $dates = json_decode(json_encode($dates),true);
  49. foreach ($dates as $k=>&$v) {
  50. $idate_first = date('Y-m-01',strtotime($v['idate']));
  51. $idate_end = $this->getthemonth($v['idate']);
  52. $phones = DB::table('customers')->whereIn('admin_id',$saler_ids)->where('fanTime', '>=', $idate_first)->where('fanTime', '<=', $idate_end)->lists('phone');
  53. $amount = DB::table('order')->whereIn('receiverMobile', $phones)->where('createTime','>=', $date_first)->where('createTime', '<', $date_end. '23:59:59')->where('is_del', 0)->where('team_id', $team_id)->sum('receivedAmount');
  54. //存入数据表
  55. $result = $this->updateData($v['idate'], $amount, $date, $team_id);
  56. echo "\nDate:".$v['idate']." team_id:".$team_id." amount:".$amount." 处理结果:".$result;
  57. }
  58. }
  59. }
  60. }
  61. public function updateData($idate, $amount, $date, $team_id){
  62. #当前年份
  63. $month = date('Y-01', strtotime($date));
  64. $data = array();
  65. #如果是上个月的数据直接插入
  66. if($idate == $date ){
  67. $data['team_id'] = $team_id;
  68. $data['idate'] = $idate;
  69. $data['month_time'] = $month;
  70. $data['amount_data'] = json_encode([0=>['date'=>$date, 'amount'=>$amount]]);
  71. $data['create_time'] = date('Y-m-d H:i:s',time());
  72. $res = DB::table('cust_month_remain')->insert($data);
  73. }else{
  74. if(12 < $this->getMonthNum( $idate, $date)){
  75. return 0;
  76. }
  77. //往期成单
  78. $odata = DB::table('cust_month_remain')->where('team_id', $team_id)->where('idate', $idate)->first();
  79. $amount_data = json_decode($odata->amount_data, true);
  80. $amount_data[] = ['date'=>$date, 'amount'=>$amount];
  81. $data['amount_data'] = json_encode($amount_data);
  82. $res = DB::table('cust_month_remain')->where('team_id', $team_id)->where('idate', $idate)->update($data);
  83. }
  84. return $res;
  85. }
  86. public function getthemonth($date)
  87. {
  88. $firstday = date('Y-m-01', strtotime($date));
  89. $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
  90. return $lastday;
  91. }
  92. public function getMonthNum($date1,$date2){
  93. $date1_stamp=strtotime($date1);
  94. $date2_stamp=strtotime($date2);
  95. list($date_1['y'],$date_1['m'])=explode("-",date('Y-m',$date1_stamp));
  96. list($date_2['y'],$date_2['m'])=explode("-",date('Y-m',$date2_stamp));
  97. return abs($date_1['y']-$date_2['y'])*12 +$date_2['m']-$date_1['m'];
  98. }
  99. }