Files
amb_wechatapp/application/admin/controller/live/GoodsAction.php
T

173 lines
5.8 KiB
PHP

<?php
namespace app\admin\controller\live;
use app\common\controller\Backend;
/**
* 直播带货商品操作记录
*
* @icon fa fa-circle-o
*/
class GoodsAction extends Backend
{
/**
* GoodsAction模型对象
* @var \app\admin\model\live\GoodsAction
*/
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\live\GoodsAction;
$this->orderModel = new \app\admin\model\order\Order;
}
/**
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
*/
/**
* 查看
*/
public function index()
{
//当前是否为关联查询
$this->relationSearch = false;
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField')) {
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$list = $this->model
->with(['user'])
->where($where)
->setUniacid(false)
->where([
'goods_action.uniacid'=>UNIACID
])
->field([
'COUNT(*) as click_count', // 点击次数
'MAX(goods_action.createtime) as latest_click_time', // 最近点击时间
'SUM(CASE WHEN goods_action.order_no <> "" THEN 1 ELSE 0 END) as order_count', // 下单次数
'GROUP_CONCAT(order_no SEPARATOR ",") as all_order_nos'
])
->order($sort, $order)
->group('CONCAT(goods_action.user_id, "_",goods_type, "_", goods_id)')
->paginate($limit);
foreach ($list as &$row) {
$row->visible(['id','goods_id','goods_type','goods','click_count','all_order_nos','latest_click_time','order_count','pay_status']);
$row->visible(['user']);
$row->getRelation('user')->visible(['id','username','nickname']);
$row->goods = \app\common\model\goods\Handle::getGoodsQuery()->where([
'id'=>$row->goods_id,
'type'=>$row->goods_type
])->find();
$row->pay_status = false;
if($row->all_order_nos){
$isPayed = $this->orderModel
->where(['order_no'=>['in',explode(",",$row->all_order_nos)]])
->where([
'status'=>['in',\app\common\constant\order\Status::getSuccessOrderStatus()]
])->find();
if($isPayed){
$row->pay_status = true;
}
}
}
$result = array("total" => $list->total(), "rows" => $list->items());
return $this->success("获取成功",$result);
}
/**
* 带货数据统计
* @return mixed
*/
public function total(){
$liveId = input('live_id');
$data = [
'payed_user_amount'=>0,
'payed_price_amount'=>0,
'payed_order_amount'=>0,
'goods_click_amount'=>0,
'payed_price_user_average'=>0,
'online_user_amount'=>0,
'all_user_amount'=>0,
'study_time_total'=>0,
'pay_conversion_rate'=>0,
'study_time_average'=>0
];
$payedData = $this->model
->with(['order'])
->field([
'sum(real_price) as payed_price_amount',//成交金额
'count(*) as payed_order_amount',//成交金额
'COUNT(DISTINCT order.user_id) as payed_user_amount'//成交人数
])
->where([
'order.status'=>['in',\app\common\constant\order\Status::getSuccessOrderStatus()],
'goods_action.live_id'=>$liveId
])
->find();
if($payedData){
$data = array_merge($data,$payedData->toArray());
}
$studyModel = new \app\common\model\user\Study;
$data['goods_click_amount'] = $this->model->where(['uniacid'=>UNIACID,'live_id'=>$liveId])->group('user_id')->count();//商品点击人数
$data['payed_price_user_average'] = 0;
if($data['payed_price_amount'] && $data['payed_user_amount']){
$data['payed_price_user_average'] = $data['payed_price_amount'] / $data['payed_user_amount'];//平均客单价
}
$data['online_user_amount'] = (new \app\admin\model\live\User)->getOnlineUserCount($liveId);//在线人数
$data['all_user_amount'] = $studyModel->where([
'uniacid'=>UNIACID,
'course_id'=>$liveId
])->group('user_id')->count();//累计观看人数
$data['pay_conversion_rate'] = 0;//成交转化率
if($data['all_user_amount'] && $data['payed_user_amount']){
$data['pay_conversion_rate'] = floatval(sprintf("%.2f", $data['payed_user_amount'] / $data['all_user_amount'] * 100));
}
//全部观看时长
$data['study_time_total'] = $studyModel->where(['uniacid'=>UNIACID,'course_id'=>$liveId])->sum('total_time');
//平均观看时长
$data['study_time_average'] = 0;
if($data['study_time_total'] && $data['all_user_amount']){
$data['study_time_average'] = intval($data['study_time_total'] / $data['all_user_amount']);
$data['study_time_average'] = intval($data['study_time_average'] / 60);
}
return $this->success("获取成功",$data);
}
}