diff --git a/api/controllers/wxapp/licheb/Statistics.php b/api/controllers/wxapp/licheb/Statistics.php index 482e8b55..f2cb7df6 100644 --- a/api/controllers/wxapp/licheb/Statistics.php +++ b/api/controllers/wxapp/licheb/Statistics.php @@ -15,6 +15,8 @@ class Statistics extends Wxapp{ $this->load->model("biz/biz_model"); $this->load->model('receiver/receiver_customers_model','customers_model'); $this->load->model('receiver/order/receiver_orders_model','orders_model'); + $this->load->model('receiver/order/receiver_orders_v2_model', 'mdOrders'); + $this->load->library('receiver/stats_entity'); } protected function get(){ @@ -620,4 +622,139 @@ class Statistics extends Wxapp{ } return $season_data; } + + + protected function get_stats() + { + $s_time = $this->input_param('s_time'); + $e_time = $this->input_param('e_time'); + $city_id = intval($this->input_param('city_id')); + !$s_time && $s_time = date('Y-m-d'); + !$e_time && $e_time = date('Y-m-d'); + $c_time = ['s_time' => strtotime($s_time), 'e_time' => strtotime(date('Y-m-d 23:59:59', strtotime($e_time)))]; + $o_time = ['s_time' => $s_time . ' 00:00:00', 'e_time' => $e_time . ' 23:59:59']; + //客户 + $where = [ + 'status>=' => 0, + 'c_time>=' => $c_time['s_time'], + 'c_time<=' => $c_time['e_time'] + ]; + $city_id && $where['city_id'] = $city_id; + $customers = $this->customers_model->count($where); + //进店 + $where = [ + 'a.type' => 4, + 'a.log' => '客户到店', + 'a.c_time>=' => $c_time['s_time'], + 'a.c_time<=' => $c_time['e_time'], + 'b.status>=' => 0, + ]; + $city_id && $where['b.city_id'] = $city_id; + $intos = $this->customers_model->db->select('a.id') + ->from('lc_receiver_customer_oplogs as a') + ->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left') + ->where($where) + ->count_all_results(); + //成交 + $where = [ + 'status>=' => 0, + 'order_time>=' => $o_time['s_time'], + 'order_time<=' => $o_time['e_time'] + ]; + $city_id && $where["customer_id in (select id from lc_receiver_customers where status>=0 and city_id={$city_id})"] = null; + $orders = $this->mdOrders->count($where); + $where = [ + 'a.status>=' => 0, + 'a.order_time>=' => $o_time['s_time'], + 'a.order_time<=' => $o_time['e_time'], + 'a.status<>' => 2, + 'b.pid_status' => 3, + 'b.status in (1,2)' => null + ]; + $city_id && $where["customer_id in (select id from lc_receiver_customers where status>=0 and city_id={$city_id})"] = null; + $bill_orders = $this->customers_model->db->select('a.id,count(b.status) as total') + ->from('lc_receiver_orders_v2 as a') + ->join('lc_receiver_order_status as b', "b.o_id=a.id", 'left') + ->where($where)->group_by('b.o_id')->having('total=1') + ->count_all_results(); + $data['tabs'] = [ + ['title' => '全部客户', 'value_1' => $customers, 'url' => '/pages/dataAnalysis/detail/index?type=0'], + ['title' => '到店客户', 'value_1' => $intos, 'url' => '/pages/dataAnalysis/detail/index?type=1'], + ['title' => '订单数', 'value_1' => $orders, 'url' => '/pages/dataAnalysis/detail/index?type=2'], + ['title' => '开票数', 'value_1' => $bill_orders, 'url' => '/pages/dataAnalysis/detail/index?type=3'], + ]; + $orders_per = number_format_com($orders / $customers * 100, 1, ''); + $intos_per = number_format_com($intos / $customers * 100, 1, ''); + $data['funnel'] = [ + 'title' => '线索转化漏斗', + 'expected_data' => [ + ['name' => "成交数({$orders_per}%)", 'value' => 33.3], + ['name' => "到店数({$intos_per}%)", 'value' => 66.7], + ['name' => '客户数100%', 'value' => 100] + ], + 'actual_data' => [ + ['name' => '成交数', 'value' => $orders], + ['name' => '到店数', 'value' => $intos], + ['name' => '客户量', 'value' => $customers] + ] + ]; + $data['title'] = '数据看板'; + return $data; + } + + protected function get_stats_days() + { + $days = intval($this->input_param('days')); + $city_id = intval($this->input_param('city_id')); + !$days && $days = 7; + $customers = $orders = $xAxis = []; + for ($i = ($days - 1); $i >= 0; $i--) { + $s_time = date('Y-m-d', strtotime("-{$i} day")); + $c_time = ['s_time' => strtotime($s_time), 'e_time' => strtotime(date('Y-m-d 23:59:59', strtotime($s_time)))]; + $o_time = ['s_time' => $s_time . ' 00:00:00', 'e_time' => $s_time . ' 23:59:59']; + $xAxis[] = $days > 7 ? date('d', strtotime($s_time)) : date('n-d', strtotime($s_time)); + $where = [ + 'city_id' => $city_id, + 'status>=' => 0, + 'c_time>=' => $c_time['s_time'], + 'c_time<=' => $c_time['e_time'] + ]; + $customers[] = $this->customers_model->count($where); + $where = [ + 'status>=' => 0, + 'order_time>=' => $o_time['s_time'], + 'order_time<=' => $o_time['e_time'], + "customer_id in (select id from lc_receiver_customers where status>=0 and city_id={$city_id})" => null + ]; + $orders[] = $this->mdOrders->count($where); + } + $title = $days == 7 ? "近一周" : "近{$days}日"; + $data = [ + 'title' => "{$title}走势图", 'legend_data' => ["客户数", "订单数"], + 'xAxis' => $xAxis, + 'series' => [ + ['name' => "客户数", 'type' => 'line', 'smooth' => true, 'data' => $customers], + ['name' => "订单数", 'type' => 'line', 'smooth' => true, 'data' => $orders] + ] + ]; + return $data; + } + + protected function get_stats_customer() + { + $s_time = $this->input_param('s_time'); + $e_time = $this->input_param('e_time'); + $city_id = intval($this->input_param('city_id')); + $type = intval($this->input_param('type')); + switch ($type){ + case 1: //到店客户 + return $this->stats_entity->customers($s_time,$e_time,$city_id,$type); + case 2: //订单数 + return $this->stats_entity->orders($s_time,$e_time,$city_id);; + case 3: //开票数 + return $this->stats_entity->orders($s_time,$e_time,$city_id,$type); + default: //全部客户 + return $this->stats_entity->customers($s_time,$e_time,$city_id); + } + } } diff --git a/common/libraries/receiver/Stats_entity.php b/common/libraries/receiver/Stats_entity.php new file mode 100644 index 00000000..b8ff0c17 --- /dev/null +++ b/common/libraries/receiver/Stats_entity.php @@ -0,0 +1,331 @@ +ci = & get_instance(); + $this->ci->load->model('receiver/receiver_customers_model','customers_model'); + $this->ci->load->model('receiver/order/receiver_orders_v2_model', 'mdOrders'); + $this->ci->load->model('biz/biz_model'); + $this->ci->load->model('auto/auto_brand_model'); + } + + /** + * @param $s_time + * @param $e_time + * @param $city_id + * @param $type 0全部 1到店 + * @return array + */ + public function customers($s_time,$e_time,$city_id,$type=0){ + !$s_time && $s_time = date('Y-m-d'); + !$e_time && $e_time = date('Y-m-d'); + $c_time = [ + 's_time' => strtotime($s_time), + 'e_time' => strtotime(date('Y-m-d 23:59:59', strtotime($e_time))) + ]; + $pie1_level = $pie2_level = $pie1_offline = $bizs = $biz_customer = []; + $sdata_level = $this->ci->customers_model->get_sdata('level'); + foreach ($sdata_level as $v) { + if($type){ + $where = [ + 'a.type' => 4, + 'a.log' => '客户到店', + 'a.c_time>=' => $c_time['s_time'], + 'a.c_time<=' => $c_time['e_time'], + 'b.city_id' => $city_id, + 'b.status>=' => 0, + 'b.level' => $v + ]; + $value = $this->ci->customers_model->db->select('a.id') + ->from('lc_receiver_customer_oplogs as a') + ->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left') + ->where($where) + ->count_all_results(); + }else{ + $where = [ + 'status>=' => 0, + 'level' => $v, + 'c_time>=' => $c_time['s_time'], + 'c_time<=' => $c_time['e_time'], + 'city_id' => $city_id + ]; + $value = $this->ci->customers_model->count($where); + } + $value && $pie1_level[] = ['name' => $v, 'value' => $value]; + } + $offlineSources = $this->ci->customers_model->offlineSources(); + foreach ($offlineSources as $k => $v) { + if($type){ + $where = [ + 'a.type' => 4, + 'a.log' => '客户到店', + 'a.c_time>=' => $c_time['s_time'], + 'a.c_time<=' => $c_time['e_time'], + 'b.status>=' => 0, + 'b.of_id' => $k + ]; + $value = $this->ci->customers_model->db->select('a.id') + ->from('lc_receiver_customer_oplogs as a') + ->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left') + ->where($where) + ->count_all_results(); + }else{ + $where = [ + 'status>=' => 0, + 'of_id' => $k, + 'c_time>=' => $c_time['s_time'], + 'c_time<=' => $c_time['e_time'], + 'city_id' => $city_id + ]; + $value = $this->ci->customers_model->count($where); + } + + $value && $pie1_offline[] = ['name' => $v['name'], 'value' => $value]; + } + $biz_type = $this->ci->biz_model->type_ary(); + foreach ($biz_type as $k => $v) { + if($type){ + $where = [ + 'b.status>=' => 0, + 'b.city_id' => $city_id, + 'c.c_time>=' => $c_time['s_time'], + 'c.c_time<=' => $c_time['e_time'], + 'a.type' => $k, + 'c.type' => 4, + 'c.log' => '客户到店', + ]; + $value = $this->ci->customers_model->db->select('b.id') + ->from('lc_biz as a') + ->join('lc_receiver_customers as b', "a.id=b.biz_id", 'left') + ->join('lc_receiver_customer_oplogs as c', "c.customer_id=b.id", 'left') + ->where($where) + ->count_all_results(); + }else{ + $where = [ + 'b.status>=' => 0, + 'b.c_time>=' => $c_time['s_time'], + 'b.c_time<=' => $c_time['e_time'], + 'b.city_id' => $city_id, + 'a.type' => $k + ]; + $value = $this->ci->customers_model->db->select('b.id') + ->from('lc_biz as a') + ->join('lc_receiver_customers as b', "a.id=b.biz_id", 'left') + ->where($where) + ->count_all_results(); + } + $pie2_level[] = ['name' => $v, 'value' => $value]; + } + $biz_rows = $this->ci->biz_model->select(['status' => 1,'city_id' => $city_id], 'id desc', 0, 0, 'id,biz_name'); + foreach ($biz_rows as $k => $v) { + $bizs[] = $v['biz_name']; + if($type){ + $where = [ + 'b.status>=' => 0, + 'b.city_id' => $city_id, + 'c.c_time>=' => $c_time['s_time'], + 'c.c_time<=' => $c_time['e_time'], + 'b.biz_id' => $k, + 'c.type' => 4, + 'c.log' => '客户到店', + ]; + $biz_customer[] = $this->ci->customers_model->db->select('b.id') + ->from('lc_receiver_customers as b') + ->join('lc_receiver_customer_oplogs as c', "c.customer_id=b.id", 'left') + ->where($where) + ->count_all_results(); + }else{ + $where = [ + 'status>=' => 0, + 'biz_id' => $v['id'], + 'c_time>=' => $c_time['s_time'], + 'c_time<=' => $c_time['e_time'], + 'city_id' => $city_id + ]; + $biz_customer[] = $this->ci->customers_model->count($where); + } + } + $data['pie1'] = ['title' => '客户来源', 'series_data_1' => $pie1_level, 'series_data_2' => $pie1_offline]; + $data['pie2'] = ['title' => '门店类型', 'series_data_1' => $pie2_level]; + $data['bar'] = ['title' => '门店列表', 'xAxis_data' => $bizs, 'series_data' => $biz_customer]; + $data['title'] = $type ? '到店客户' : '全部客户'; + return $data; + } + + /** + * @param $s_time + * @param $e_time + * @param $city_id + * @param $type 0全部 1开票 + * @return array + */ + public function orders($s_time,$e_time,$city_id,$type=0){ + !$s_time && $s_time = date('Y-m-d'); + !$e_time && $e_time = date('Y-m-d'); + $o_time = ['s_time' => $s_time . ' 00:00:00', 'e_time' => $e_time . ' 23:59:59']; + $pie1_level = $pie1_offline = $pie2_level = $pie23level = $bizs = $biz_customer = []; + $sdata_level = $this->ci->customers_model->get_sdata('level'); + foreach ($sdata_level as $v) { + if($type){ + $where = [ + 'a.status>=' => 0, + 'a.order_time>=' => $o_time['s_time'], + 'a.order_time<=' => $o_time['e_time'], + 'a.status<>' => 2, + 'b.pid_status' => 3, + 'b.status in (1,2)' => null, + "a.customer_id in (select id from lc_receiver_customers where status>=0 and city_id={$city_id} and level='{$v}')" => null + ]; + $value = $this->ci->db->select('a.id,count(b.status) as total') + ->from('lc_receiver_orders_v2 as a') + ->join('lc_receiver_order_status as b', "b.o_id=a.id", 'left') + ->where($where)->group_by('b.o_id')->having('total=1') + ->count_all_results(); + }else{ + $where = [ + 'status>=' => 0, + 'order_time>=' => $o_time['s_time'], + 'order_time<=' => $o_time['e_time'], + "customer_id in (select id from lc_receiver_customers where status>=0 and city_id={$city_id} and level='{$v}')" => null + ]; + $value = $this->ci->mdOrders->count($where); + } + $value && $pie1_level[] = ['name' => $v, 'value' => $value]; + } + $offlineSources = $this->ci->customers_model->offlineSources(); + foreach ($offlineSources as $k => $v) { + if($type){ + $where = [ + 'a.status>=' => 0, + 'a.order_time>=' => $o_time['s_time'], + 'a.order_time<=' => $o_time['e_time'], + 'a.status<>' => 2, + 'b.pid_status' => 3, + 'b.status in (1,2)' => null, + "a.customer_id in (select id from lc_receiver_customers where status>=0 and city_id={$city_id} and of_id=$k)" => null + ]; + $value = $this->ci->db->select('a.id,count(b.status) as total') + ->from('lc_receiver_orders_v2 as a') + ->join('lc_receiver_order_status as b', "b.o_id=a.id", 'left') + ->where($where)->group_by('b.o_id')->having('total=1') + ->count_all_results(); + }else{ + $where = [ + 'status>=' => 0, + 'order_time>=' => $o_time['s_time'], + 'order_time<=' => $o_time['e_time'], + "customer_id in (select id from lc_receiver_customers where status>=0 and city_id={$city_id} and of_id=$k)" => null + ]; + $value = $this->ci->mdOrders->count($where); + } + $value && $pie1_offline[] = ['name' => $v['name'], 'value' => $value]; + } + $biz_type = $this->ci->biz_model->type_ary(); + foreach ($biz_type as $k => $v) { + if($type){ + $where = [ + 'a.status>=' => 0, + 'a.order_time>=' => $o_time['s_time'], + 'a.order_time<=' => $o_time['e_time'], + 'a.status<>' => 2, + 'b.pid_status' => 3, + 'b.status in (1,2)' => null, + "a.customer_id in ( + select id from lc_receiver_customers where status>=0 and city_id={$city_id} and of_id=$k and biz_id in + (select id from lc_biz where type={$k}) + )" => null + ]; + $value = $this->ci->db->select('a.id,count(b.status) as total') + ->from('lc_receiver_orders_v2 as a') + ->join('lc_receiver_order_status as b', "b.o_id=a.id", 'left') + ->where($where)->group_by('b.o_id')->having('total=1') + ->count_all_results(); + }else{ + $where = [ + 'status>=' => 0, + 'order_time>=' => $o_time['s_time'], + 'order_time<=' => $o_time['e_time'], + "customer_id in ( + select id from lc_receiver_customers where status>=0 and city_id={$city_id} and of_id=$k and biz_id in + (select id from lc_biz where type={$k}) + )" => null + ]; + $value = $this->ci->mdOrders->count($where); + } + $pie2_level[] = ['name' => $v, 'value' => $value]; + } + if($type){ + $sql = "select * from (SELECT a.id,a.brand_id, count(b.status) as t + FROM lc_receiver_orders_v2 as a + LEFT JOIN lc_receiver_order_status as b ON b.o_id=a.id + WHERE a.status >= 0 + AND a.status <> 2 + AND a.brand_id > 0 + AND a.order_time>= '{$o_time['s_time']}' + AND a.order_time<= '{$o_time['e_time']}' + AND a.customer_id in (select id from lc_receiver_customers where status>=0 and city_id={$city_id}) + AND b.pid_status = 3 + AND b.status in (1,2) + GROUP BY b.o_id + HAVING t = 1) u group by brand_id"; + $brand_lists = $this->ci->db->query($sql)->result_array(); + }else{ + $where = [ + 'status>=' => 0, + 'brand_id>' => 0, + 'order_time>=' => $o_time['s_time'], + 'order_time<=' => $o_time['e_time'], + "customer_id in (select id from lc_receiver_customers where status>=0 and city_id={$city_id})" => null + ]; + $query = $this->ci->db->select('id,brand_id,count(id) as t')->from('lc_receiver_orders_v2') + ->where($where)->group_by('brand_id')->get(); + $brand_lists = $query->result_array(); + } + if($brand_lists){ + $brand_rows = []; + $brand_ids = implode(',',array_column($brand_lists,'brand_id')); + $brand_ids && $brand_rows = $this->ci->auto_brand_model->map('id','name',"id in ($brand_ids)",'','','','id,name'); + foreach ($brand_lists as $val) { + $brand_rows[$val['brand_id']] && $pie23level[] = ['name' => $brand_rows[$val['brand_id']], 'value' => $val['t']]; + } + } + $biz_rows = $this->ci->biz_model->select(['status' => 1,'city_id' => $city_id], 'id desc', 0, 0, 'id,biz_name'); + foreach ($biz_rows as $k => $v) { + $bizs[] = $v['biz_name']; + if($type){ + $where = [ + 'a.status>=' => 0, + 'a.order_time>=' => $o_time['s_time'], + 'a.order_time<=' => $o_time['e_time'], + 'a.status<>' => 2, + 'b.pid_status' => 3, + 'b.status in (1,2)' => null, + 'a.biz_id' => $v['id'], + "a.customer_id in (select id from lc_receiver_customers where status>=0 and city_id={$city_id})" => null + ]; + $biz_customer[] = $this->ci->db->select('a.id,count(b.status) as total') + ->from('lc_receiver_orders_v2 as a') + ->join('lc_receiver_order_status as b', "b.o_id=a.id", 'left') + ->where($where)->group_by('b.o_id')->having('total=1') + ->count_all_results(); + }else{ + $where = [ + 'status>=' => 0, + 'biz_id' => $v['id'], + 'order_time>=' => $o_time['s_time'], + 'order_time<=' => $o_time['e_time'], + "customer_id in (select id from lc_receiver_customers where status>=0 and city_id={$city_id})" => null + ]; + $biz_customer[] = $this->ci->mdOrders->count($where); + } + } + $data['pie1'] = ['title' => '客户来源', 'series_data_1' => $pie1_level, 'series_data_2' => $pie1_offline]; + $data['pie2'] = ['title' => '门店类型', 'series_data_1' => $pie2_level]; + $data['pie3'] = ['title' => '品牌分布', 'series_data_1' => $pie23level]; + $data['bar'] = ['title' => '门店列表', 'xAxis_data' => $bizs, 'series_data' => $biz_customer]; + $data['title'] = $type ? '开票数' : '订单数'; + return $data; + } +}