No Description

TemplateController.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Apiistrator
  5. * Date: 2018-02-22
  6. * Time: 10:32
  7. */
  8. namespace App\Http\Controllers\Api;
  9. use App\Http\Controllers\Controller;
  10. use Illuminate\Http\Request;
  11. use App\Log;
  12. use App\Templates;
  13. use App\TemplatesSource;
  14. use App\TemplatesSalers;
  15. use App\TemplatesSourceSalers;
  16. use App\Admin;
  17. use App\TemplatesLog;
  18. use App\CustTotal;
  19. use App\CustDetail;
  20. use App\Order;
  21. use Illuminate\Support\Facades\Hash;
  22. use Illuminate\Support\Facades\DB;
  23. class TemplateController extends Controller {
  24. /**
  25. * 模板匹配销售qrcode
  26. */
  27. public function salerQrcode(Request $request){
  28. $t_id = (int)$request->input('t_id');
  29. if(!$t_id) $t_id = 1;
  30. //查询未离职销售
  31. $_open_ids = Admin::where('is_use', 1)->where('is_qrcode_use', 1)->lists('id');
  32. //获取背景图
  33. $back_img = TemplatesSource::where('id', $t_id)->pluck('back_img');
  34. $new_context = TemplatesSource::where('id', $t_id)->pluck('new_context');
  35. $new_data = array();
  36. $title = '';
  37. if(!empty($new_context)){
  38. $new_context = json_decode($new_context, true);
  39. $title = $new_context['title'];
  40. $new_data[0]['text'] = $new_context['text1'];
  41. $new_data[0]['img'] = [$new_context['img1']];
  42. $new_data[1]['text'] = $new_context['text2'];
  43. $new_data[1]['img'] = [$new_context['img2_1'], $new_context['img2_2']];
  44. $new_data[2]['text'] = $new_context['text3'];
  45. $new_data[2]['img'] = [$new_context['img3']];
  46. }
  47. $result = TemplatesSourceSalers::where('s_id', $t_id)->where('is_del', 0)->where('weight','>',0)->whereIn('admin_id', $_open_ids)->orderBy('weight', 'asc')->lists('weight', 'admin_id');
  48. if(empty($result)){
  49. return self::returnValue([],1001);
  50. }
  51. $saler_id = null;
  52. $rand = 0;
  53. //获取最大随机值
  54. $max = array_sum($result) * 10;
  55. //随机一个值
  56. $rand_re = mt_rand(1, $max);
  57. foreach($result as $k=>$weight){
  58. $rand += $weight * 10;
  59. if($rand_re <= $rand){
  60. $saler_id = $k;
  61. break;
  62. }
  63. }
  64. $saler_ids = array_keys($result);
  65. $ip = $this->getRealIp();
  66. if($saler_id){
  67. //记录行为
  68. $log = array();
  69. $log['t_id'] = $t_id;
  70. $log['t_url'] = TemplatesSource::where('id', $t_id)->pluck('url');
  71. $log['admin_id'] = $saler_id;
  72. $log['ip'] = $ip;
  73. TemplatesLog::insert($log);
  74. }
  75. $qrcode = Admin::where('id', $saler_id)->pluck('qrcode');
  76. $qrcodes = Admin::whereIn('id', $saler_ids)->lists('qrcode');
  77. return self::returnValue(['qrcode'=>$qrcode, 'ip'=>$ip, 'saler_id'=>$saler_id, 'qrcodes'=>$qrcodes, 'back_img'=>$back_img, 'title'=>$title, 'new_context'=>$new_data]);
  78. }
  79. /**
  80. * 获取真实ip
  81. */
  82. public function getRealIp()
  83. {
  84. $ip=false;
  85. if(!empty($_SERVER["HTTP_CLIENT_IP"])){
  86. $ip = $_SERVER["HTTP_CLIENT_IP"];
  87. }
  88. if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  89. $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
  90. if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
  91. for ($i = 0; $i < count($ips); $i++) {
  92. if (!preg_match ("/^(10│172.16│192.168)./", $ips[$i])) {
  93. $ip = $ips[$i];
  94. break;
  95. }
  96. }
  97. }
  98. return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
  99. }
  100. /**
  101. * 长按用户存日志
  102. */
  103. public function addLongLog(Request $request){
  104. $saler_id = $request->input('saler_id');
  105. $t_id = $request->input('t_id');
  106. $ip = $this->getRealIp();
  107. if($saler_id && $t_id){
  108. //记录行为
  109. $log = array();
  110. $log['t_id'] = $t_id;
  111. $log['t_url'] = TemplatesSource::where('id', $t_id)->pluck('url');
  112. $log['admin_id'] = $saler_id;
  113. $log['ip'] = $ip;
  114. $log['type'] = 2; //长按
  115. TemplatesLog::insert($log);
  116. }
  117. return self::returnValue([]);
  118. }
  119. /**
  120. * 每日累计汇总数据
  121. */
  122. public function dayGrandTotal(Request $request){
  123. $days = (int)$request->input('days');
  124. if(!$days){
  125. $days = 7;
  126. }
  127. $stime = date('Y-m-d', strtotime('-'.$days.' day'));
  128. $data = array();
  129. $_start = '2019-09-04';
  130. //今日数据实时计算
  131. $idate = date('Y-m-d');
  132. $data['idate'] = $idate;
  133. //总投入
  134. $data['throw_cost'] = CustTotal::where('is_del',0)->where('dtime','>=',$_start)->sum('total_cost');
  135. //订单信息
  136. $order = Order::select(DB::raw('sum(cost) as goods_cost, sum(freight_cost) as freight_cost, sum(aftersale_fee) as aftersale_cost, count(1) as order_count, sum(receivedAmount) as order_amount, count(distinct(receiverMobile)) as cust_count, sum(refund_price) as refund_fee'))->where('createTime','>=',$_start)->where('is_del',0)->first();
  137. $data['goods_cost'] = $order->goods_cost;
  138. $data['freight_cost'] = $order->freight_cost;
  139. $data['aftersale_cost'] = $order->aftersale_cost;
  140. $data['refund_fee'] = $order->refund_fee;
  141. $data['order_count'] = $order->order_count;
  142. $data['order_amount'] = $order->order_amount;
  143. $data['cust_count'] = $order->cust_count;
  144. //加粉
  145. $data['fan_count'] = CustDetail::where('is_del',0)->where('dtime','>=',$_start)->sum('fan_add');
  146. //毛利 = 总销售额-总商品成本-总运费-总售后
  147. $data['profit'] = $order->order_amount - $order->goods_cost - $order->freight_cost - $order->aftersale_cost - $data['throw_cost'] + $order->refund_fee;
  148. $data['roi'] = $data['throw_cost']>0? round($order->order_amount / $data['throw_cost'], 4) : '';
  149. //总新粉单数
  150. $new_order = Order::select(DB::raw('count(1) as order_count'))->leftJoin('customers as cu','cu.phone', '=', 'order.receiverMobile')->whereRaw('left(order.createTime, 10) = cu.fanTime')->where('order.is_del', 0)->where('order.createTime','>=',$_start)->first();
  151. $data['new_order_count'] = $new_order->order_count;
  152. //总老粉单数
  153. $data['old_order_count'] = $data['order_count'] - $data['new_order_count'];
  154. //总复购订单
  155. $data['fugou_order_count'] = $data['order_count'] - $data['cust_count'];
  156. //总复购率
  157. $data['fugou_rate'] = $data['cust_count']>0 ? round($data['fugou_order_count'] / $data['cust_count'], 4) : '';
  158. //查询历史数据
  159. $result = DB::table('day_grand_total')->where('idate', '>', $stime)->orderBy('idate', 'desc')->get();
  160. $result = json_decode(json_encode($result), true);
  161. foreach($result as $k=>&$v){
  162. $v['roi'] = $v['throw_cost']>0 ? round($v['order_amount'] / $v['throw_cost'], 4) : '';
  163. $v['fugou_rate'] = $v['cust_count']>0 ? round($v['fugou_order_count'] / $v['cust_count'], 4) : '';
  164. }
  165. $result = array_merge([$data], $result);
  166. exit(json_encode($result));
  167. }
  168. }