count(); $ing = BargainActivity::where('status', BargainEnum::ACTIVITY_STATUS_ING)->count(); $end = BargainActivity::where('status', BargainEnum::ACTIVITY_STATUS_END)->count(); return [ 'all' => $all, 'wait' => $wait, 'ing' => $ing, 'end' => $end ]; } /** * @notes 设置搜索 * @return \string[][] * @author Tab * @date 2021/8/27 18:19 */ public function setSearch(): array { return [ '=' => ['status'] ]; } /** * @notes 附加搜索 * @author Tab * @date 2021/9/24 11:52 */ public function attachSearch() { // 活动信息 if (isset($this->params['activity_info']) && !empty($this->params['activity_info'])) { $this->searchWhere[] = ['sn|name', 'like', '%'. $this->params['activity_info'] .'%']; } } /** * @notes 列表 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @author Tab * @date 2021/8/27 18:26 */ public function lists() : array { $this->attachSearch(); $field = [ 'id', 'sn', 'name', 'start_time' => 'start_time_desc', 'end_time' => 'end_time_desc', 'visited', 'status', 'status' => 'status_desc', 'create_time', ]; $lists = BargainActivity::field($field) ->where($this->searchWhere) ->order('id','desc') ->withSearch(['activity_time', 'goods_info'], $this->params) ->limit($this->limitOffset, $this->limitLength) ->select() ->toArray(); foreach ($lists as &$item) { $item['goods_count'] = $this->goodsCount($item); $orderData= $this->orderData($item); $item['total_amount'] = $orderData['total_amount']; $item['total_num'] = $orderData['total_num']; $item['order_count'] = $orderData['order_count']; } return $lists; } /** * @notes 记录数 * @return int * @author Tab * @date 2021/8/27 18:25 */ public function count() : int { $this->attachSearch(); $count = BargainActivity::where($this->searchWhere) ->withSearch(['activity_time', 'goods_name', 'goods_code'], $this->params) ->count(); return $count; } /** * @notes 活动商品数量 * @param $item * @return int * @author Tab * @date 2021/9/24 9:10 */ public function goodsCount($item) { $goodsIds = BargainGoods::distinct(true) ->field('goods_id') ->where('activity_id', $item['id']) ->select() ->toArray(); return count($goodsIds); } /** * @notes 订单数据 * @param $item * @author Tab * @date 2021/9/24 9:22 */ public function orderData($item) { // 活动对应的订单id $orderIds = BargainInitiate::where('activity_id', $item['id'])->whereNotNull('order_id')->column('order_id'); // 成交订单数 $orderCount = Order::where([ ['order_type', '=' ,OrderEnum::BARGAIN_ORDER], ['pay_status', '=', YesNoEnum::YES], ['id', 'in', $orderIds] ])->count(); // 销量 $totalNum = Order::where([ ['order_type', '=' ,OrderEnum::BARGAIN_ORDER], ['pay_status', '=', YesNoEnum::YES], ['id', 'in', $orderIds] ])->sum('total_num'); // 销售额 $totalAmount = Order::where([ ['order_type', '=' ,OrderEnum::BARGAIN_ORDER], ['pay_status', '=', YesNoEnum::YES], ['id', 'in', $orderIds] ])->sum('order_amount'); return [ 'total_amount' => $totalAmount, 'total_num' => $totalNum, 'order_count' => $orderCount, ]; } }