model = new CouponUserModel; } public function index(){ //当前是否为关联查询 $this->relationSearch = false; //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); list($where, $sort, $order, $offset, $limit) = $this->buildparams(); // 设置不统计的订单状态 $excluded_statuses = [\app\common\constant\order\Status::STATUS_CLOSE, \app\common\constant\order\Status::STATUS_UNPAID, \app\common\constant\order\Status::STATUS_CANCEL, \app\common\constant\order\Status::STATUS_REFUND]; $list = \app\admin\model\order\Item::alias('item') ->join("order","item.order_no = order.order_no","left") ->field('item.goods_type,item.item_id,count(item.id) AS total_item, COUNT(DISTINCT item.user_id) AS total_people') ->group("CONCAT_WS('_', item.item_id,item.goods_type)") ->where($where) ->where('status', 'not in', $excluded_statuses) ->order($sort, $order)->paginate($limit); if($list){ foreach ($list as &$row){ if($row->goods_type == 'course'){ $type = ['article','column','video','audio','live']; }else{ $type = [$row->goods_type]; } $row->goods = \app\common\model\goods\Handle::getGoodsQuery()->where([ 'id'=>$row->item_id, 'type'=>['in',$type] ])->find(); } } $goodsTotal = \app\admin\model\order\Item::alias('item') ->join("order","item.order_no = order.order_no","left") ->field('COUNT(item.id) AS goods_total') ->where('status', 'not in', $excluded_statuses) ->where($where)->find(); $result = array("total" => $list->total(), "rows" => $list->items(),'attr'=>[ 'goods_type'=>(new \app\admin\model\order\Item())->getGoodsTypeList(), 'goods_total'=>$goodsTotal['goods_total'] ]); return $this->success("获取成功",$result); } /** * 数据统计 * @return mixed */ public function statistical(){ $coupon_id = input('coupon_id',''); $orderModel = new \app\admin\model\order\Order; // 设置不统计的订单状态 $excluded_statuses = [\app\common\constant\order\Status::STATUS_CLOSE, \app\common\constant\order\Status::STATUS_UNPAID, \app\common\constant\order\Status::STATUS_CANCEL, \app\common\constant\order\Status::STATUS_REFUND]; $result = $orderModel ->where('status', 'not in', $excluded_statuses) ->where('coupon_id', $coupon_id) ->field([ 'SUM(real_price) AS total_order_amount', 'SUM(coupon_discount_fee) AS total_discount_amount', 'COUNT(DISTINCT user_id) AS total_users_used_coupon', 'COUNT(*) AS order_count_with_coupon', '(SUM(real_price) / COUNT(*)) AS average_order_value', '(SUM(coupon_discount_fee) / SUM(real_price)) AS fee_effect_ratio', ]) ->find(); // 领券总用户数 $totalUsersWithCoupon = \app\admin\model\app\coupon\CouponUser::where('coupon_id', $coupon_id) ->group('user_id') ->count(); // 添加领券总用户数到结果数组中 $result['total_users_with_coupon'] = $totalUsersWithCoupon; return $this->success("获取成功",$result); } }