No Description

TemplateController.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 && $t_id !=17){
  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. $log['idate'] = date('Y-m-d');
  74. $log['mdate'] = date('Y-m');
  75. TemplatesLog::insert($log);
  76. }
  77. $qrcode = Admin::where('id', $saler_id)->pluck('qrcode');
  78. $qrcodes = Admin::whereIn('id', $saler_ids)->lists('qrcode');
  79. return self::returnValue(['qrcode'=>$qrcode, 'ip'=>$ip, 'saler_id'=>$saler_id, 'qrcodes'=>$qrcodes, 'back_img'=>$back_img, 'title'=>$title, 'new_context'=>$new_data]);
  80. }
  81. /**
  82. * 获取真实ip
  83. */
  84. public function getRealIp()
  85. {
  86. $ip=false;
  87. if(!empty($_SERVER["HTTP_CLIENT_IP"])){
  88. $ip = $_SERVER["HTTP_CLIENT_IP"];
  89. }
  90. if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  91. $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
  92. if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
  93. for ($i = 0; $i < count($ips); $i++) {
  94. if (!preg_match ("/^(10│172.16│192.168)./", $ips[$i])) {
  95. $ip = $ips[$i];
  96. break;
  97. }
  98. }
  99. }
  100. return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
  101. }
  102. /**
  103. * 长按用户存日志
  104. */
  105. public function addLongLog(Request $request){
  106. $saler_id = $request->input('saler_id');
  107. $t_id = $request->input('t_id');
  108. $ip = $this->getRealIp();
  109. if($saler_id && $t_id && $t_id !=17){
  110. //记录行为
  111. $log = array();
  112. $log['t_id'] = $t_id;
  113. $log['t_url'] = TemplatesSource::where('id', $t_id)->pluck('url');
  114. $log['admin_id'] = $saler_id;
  115. $log['ip'] = $ip;
  116. $log['type'] = 2; //长按
  117. $log['idate'] = date('Y-m-d');
  118. $log['mdate'] = date('Y-m');
  119. TemplatesLog::insert($log);
  120. }
  121. return self::returnValue([]);
  122. }
  123. /**
  124. * 每日累计汇总数据
  125. */
  126. public function dayGrandTotal(Request $request){
  127. $days = (int)$request->input('days');
  128. if(!$days){
  129. $days = 7;
  130. }
  131. $stime = date('Y-m-d', strtotime('-'.$days.' day'));
  132. $data = array();
  133. $_start = '2019-09-04';
  134. //今日数据实时计算
  135. $idate = date('Y-m-d');
  136. $data['idate'] = $idate;
  137. //总投入
  138. $data['throw_cost'] = CustTotal::where('is_del',0)->where('dtime','>=',$_start)->sum('total_cost');
  139. //订单信息
  140. $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();
  141. $data['goods_cost'] = $order->goods_cost;
  142. $data['freight_cost'] = $order->freight_cost;
  143. $data['aftersale_cost'] = $order->aftersale_cost;
  144. $data['refund_fee'] = $order->refund_fee;
  145. $data['order_count'] = $order->order_count;
  146. $data['order_amount'] = $order->order_amount;
  147. $data['cust_count'] = $order->cust_count;
  148. //加粉
  149. $data['fan_count'] = CustDetail::where('is_del',0)->where('dtime','>=',$_start)->sum('fan_add');
  150. //毛利 = 总销售额-总商品成本-总运费-总售后
  151. $data['profit'] = $order->order_amount - $order->goods_cost - $order->freight_cost - $order->aftersale_cost - $data['throw_cost'] + $order->refund_fee;
  152. $data['roi'] = $data['throw_cost']>0? round($order->order_amount / $data['throw_cost'], 4) : '';
  153. //总新粉单数
  154. $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();
  155. $data['new_order_count'] = $new_order->order_count;
  156. //总老粉单数
  157. $data['old_order_count'] = $data['order_count'] - $data['new_order_count'];
  158. //总复购订单
  159. $data['fugou_order_count'] = $data['order_count'] - $data['cust_count'];
  160. //总复购率
  161. $data['fugou_rate'] = $data['cust_count']>0 ? round($data['fugou_order_count'] / $data['cust_count'], 4) : '';
  162. //查询历史数据
  163. $result = DB::table('day_grand_total')->where('idate', '>', $stime)->orderBy('idate', 'desc')->get();
  164. $result = json_decode(json_encode($result), true);
  165. foreach($result as $k=>&$v){
  166. $v['roi'] = $v['throw_cost']>0 ? round($v['order_amount'] / $v['throw_cost'], 4) : '';
  167. $v['fugou_rate'] = $v['cust_count']>0 ? round($v['fugou_order_count'] / $v['cust_count'], 4) : '';
  168. }
  169. $result = array_merge([$data], $result);
  170. exit(json_encode($result));
  171. }
  172. }