Files
liche/api/controllers/wxapp/liche/Statistics.php
T
2022-07-24 12:26:08 +08:00

262 lines
10 KiB
PHP

<?php
defined('WXAPP_APP') OR exit('No direct script access allowed');
/**
* Created by Vim
* User: lcc
* Date: 2021/07/29
* Time: 14:08
*/
require_once APPPATH.'controllers/wxapp/Wxapp.php';
class Statistics extends Wxapp{
function __construct($inputs, $app_key){
parent::__construct($inputs, $app_key);
$this->load->model('receiver/receiver_clue_statistics_model','statistics_model');
$this->load->model('receiver/receiver_clues_model','clues_model');
$this->load->model('receiver/order/receiver_orders_model','orders_model');
$this->load->model('receiver/order/receiver_orders_v2_model');
$this->load->model('auto/auto_brand_model');
$this->load->model('auto/auto_series_model');
$this->load->model('auto/auto_attr_model');
$this->load->model('app/deal_log_model');
$this->load->library('entity/deal_entity',['app_id'=>$this->app_id]);
}
protected function get(){
}
//累计数据
protected function get_cal(){
$uid = $this->session['uid'];
$brower = $this->statistics_model->sum('browse_num',['app_id'=>$this->app_id,'uid'=>$uid]);
$clues = $this->clues_model->count(['app_id'=>$this->app_id,'recommend_id'=>$uid]);
$where = [
'lc_receiver_clues.recommend_id' => $uid,
'lc_receiver_orders_v2.status' => 1
];
$done = $this->clues_model->count_order($where);
$sql = "select count(distinct c.o_id) as total from lc_receiver_clues as a left join lc_receiver_orders_v2 as b on a.id=b.clue_id left join lc_app_liche_orders as c on c.o_id=b.id where c.status=1 and a.recommend_id={$uid}";
$re=$this->db->query($sql)->result_array();
$data = [
['name'=>'浏览','value'=>intval($brower['browse_num']),'url' => ''],
['name'=>'报名','value'=>$clues,'url' => '/pages/distribute/statistics/customer'],
['name'=>'下定','value'=>$re[0]['total'],'url' => '/pages/distribute/statistics/order?type=0&title=下定'],
['name'=>'成交','value'=>$done,'url' => '/pages/distribute/statistics/order?type=1&title=成交']
];
return $data;
}
//根据车型获取统计
protected function get_car(){
$uid = $this->session['uid'];
$page = $this->input_param('page');
$size = $this->input_param('size');
!$page && $page = 1;
!$size && $size = 20;
$fileds = "id,name";
$where = [];
$count = $this->auto_brand_model->count($where);
$rows = $this->auto_brand_model->select($where,'',$page,$size,$fileds);
$list = [];
if($rows){
foreach($rows as $key => $val){
$enroll = $this->clues_model->count(['app_id'=>$this->app_id,'recommend_id'=>$uid,'brand_id'=>$val['id']]);
$where = [
'lc_receiver_clues.recommend_id' => $uid,
'lc_receiver_orders_v2.brand_id' => $val['id'],
'lc_receiver_orders_v2.status' => 1
];
$done = $this->clues_model->count_order($where);
$sql = "select count(distinct c.o_id) as total from lc_receiver_clues as a left join lc_receiver_orders_v2 as b on a.id=b.clue_id left join lc_app_liche_orders as c on c.o_id=b.id where c.status=1 and a.recommend_id={$uid} and a.brand_id={$val['id']}";
$re=$this->db->query($sql)->result_array();
$ispay = $re[0]['total'];
$where = [
'lc_app_deal_log.status' => 1,
'lc_app_deal_log.app_uid' => $uid,
'lc_receiver_orders_v2.brand_id' => $val['id'],
];
$done_commision = $this->deal_entity->sum_done($where);
$list[] = [
'title' => $val['name'],
'enroll' => $enroll,
'ispay' => $ispay,
'done' => $done,
'commision' => number_format($done_commision,2)
];
}
}
$data = [
'list' => $list,
'total' => $count
];
return $data;
}
//表格统计数据
protected function get_chart(){
$uid = $this->session['uid'];
$type = $this->input_param('type');
$before = 7;
$type && $before = 30;
$show_date = $date = [];
for ($i=1; $i<=$before; $i++){
$show_date[] = date('n.d' ,strtotime( '+' . $i-$before .' days', time()));
$date[] = date('Y-m-d' ,strtotime( '+' . $i-$before .' days', time()));
}
//根据时间统计
$enroll_data = $l_data = [];
foreach($date as $kye=>$val){
$enroll = $this->statistics_model->get(['app_id'=>$this->app_id,'uid'=>$uid,'day'=>$val]);
$l_data[] = $enroll ? $enroll['browse_num'] : 0;
$s_time = strtotime($val.' 00:00:00');
$e_time = strtotime($val.' 23:59:59');
$enroll_data[] = $this->clues_model->count(['app_id'=>$this->app_id,'recommend_id'=>$uid,'c_time>'=>$s_time,'c_time<='=>$e_time]);
}
$data = [
'legend' => [
'data' => ['浏览','报名'],
'bottom' => 20,
'left' => 'center',
'z' => 100
],
'grid' => ['containLabel' => true],
'tooltip' => ['show' => true,'trigger' => 'axis'],
'xAxis' => [
'type' => 'category',
'boundaryGap' => false,
'data' => $show_date,
'show' => true
],
'yAxis' => [
'x' => 'center',
'type' => 'value',
'splitLine' => [
'lineStyle' => ['type'=>'dashed']
],
'show' => true
],
'series' => [
[
'name'=>'浏览',
'type'=>'line',
'smooth'=>true,
'data' => $l_data,
],
[
'name'=>'报名',
'type'=>'line',
'smooth'=>true,
'data' => $enroll_data,
]
]
];
return $data;
}
//增加浏览
protected function post(){
$cf_uid = $this->input_param('cf_uid');
if(!$cf_uid){
throw new Exception('参数错误', API_CODE_INVILD_PARAM);
}
$where = [
'app_id' => $this->app_id,
'uid' => $cf_uid,
'day' => date('Y-m-d')
];
$row = $this->statistics_model->get($where);
if($row){
$res = $this->statistics_model->update(['browse_num = browse_num+1 ' => null],['id'=>$row['id']]);
}else{
$add_data = [
'app_id' => $this->app_id,
'uid' => $cf_uid,
'browse_num' => 1,
'day' => date('Y-m-d'),
'c_time' => time()
];
$res = $this->statistics_model->add($add_data);
}
if($res){
throw new Exception('保存成功', API_CODE_SUCCESS);
}else{
throw new Exception('保存失败', API_CODE_FAIL);
}
}
protected function get_orders(){
$type = $this->input_param('type');
$page = $this->input_param('page');
$size = $this->input_param('size');
!$page && $page = 1;
!$size && $size = 20;
$t1 = 'lc_receiver_clues';
$t2 = 'lc_receiver_orders_v2';
$where = [
"{$t1}.recommend_id" => $this->session['uid'],
"{$t2}.status" => 1
];
if($type){
$where["{$t2}.id in (select o_id from lc_receiver_order_status where pid_status=5 and status=1)"] = null;
}else{
$where["{$t2}.id in (select o_id from lc_receiver_order_status where pid_status=0 and status=1)"] = null;
}
$count = $this->clues_model->count_order($where);
$lists = [];
if($count){
$fileds = "{$t2}.id,{$t2}.name,{$t2}.mobile,{$t2}.brand_id,{$t2}.s_id,{$t2}.v_id,{$t2}.cor_id,{$t2}.incor_id,{$t2}.payway,{$t2}.status,{$t2}.c_time,{$t2}.money_json";
$rows = $this->clues_model->select_order($where,"{$t2}.id desc",$page,$size,$fileds);
//品牌车型
$brand_arr = array_unique(array_column($rows,'brand_id'));
$brands = $this->auto_brand_model->get_map_by_ids($brand_arr,'id,name');
//车系车型
$series_arr = array_unique(array_column($rows,'s_id'));
$series = $this->auto_series_model->get_map_by_ids($series_arr,'id,name');
//获取车辆属性
$version_arr = array_unique(array_column($rows,'v_id'));
$color_arr = array_unique(array_column($rows,'cor_id'));
$attrs_arr = array_merge($version_arr,$color_arr);
$attrs = $this->auto_attr_model->get_map_by_ids($attrs_arr,'id,title');
$status_arr = $this->receiver_orders_v2_model->get_status();
foreach($rows as $key=>$val){
$money_json = json_decode($val['money_json'],true);
$brand_name = isset($brands[$val['brand_id']]) ? $brands[$val['brand_id']][0]['name'] : '';
$serie_name = isset($series[$val['s_id']]) ? $series[$val['s_id']][0]['name'] : '';
$color = isset($attrs[$val['cor_id']]) ? $attrs[$val['cor_id']][0]['title'] : '';
$version = isset($attrs[$val['v_id']]) ? $attrs[$val['v_id']][0]['title'] : '';
$other_data = [
'品牌车型' => "{$brand_name}{$serie_name}-{$color}-{$version}",
'付款方式' => $val['payway']?'全款':'分期',
'代办车牌' => '需要',
'定金金额' => $money_json['price_book'],
'订单日期' => date('Y-m-d',$val['c_time']),
];
$lists[] = [
'id' => $val['id'],
'name' => $val['name'],
'mobile' => mobile_asterisk($val['mobile']),
'status_name' => $status_arr[$val['status']],
'other_data' => $other_data,
];
}
}
$data = [
'list' => $lists,
'total' => $count
];
return $data;
}
}