getPk(); if (empty($order)) { $order = (empty($data[$primary_key_name]) ? 'id' : $data[$primary_key_name]) . ' desc'; } $array = explode(',', $field); if ($field != '*') { if (count($array) <= 1) { return $this->with(['mallSpikeProductSku'])->where($where)->order($order)->value($field); } else { return $this->with(['mallSpikeProductSku'])->field($field)->where($where)->order($order)->find(); } } else { return $this->with(['mallSpikeProductSku'])->where($where)->order($order)->find(); } } /** * 获取商品列表 * @date 2021-03-01 */ public function getProductList($where, $page, $field = '*', $order = '', $per_page_number = 10) { $notice_model = new MallSpikeNotice(); $spike_ids = []; if ($this->userId) { //已登录,查看已设置秒杀ID $spike_ids = $notice_model->where([ ['user_agent', '=', $this->userAgent], ['user_id', '=', $this->userId], ['start_time', '>=', time()] ])->column('spike_id'); } $primary_key_name = $this->getPk(); if (empty($order)) { $order = (empty($data[$primary_key_name]) ? 'id' : $data[$primary_key_name]) . ' desc'; } $dataList = $this->hasWhere('mallProduct', [ ['is_publish', '=', 1] ], $field)->with(['mallProduct'])->where($where)->order($order) ->paginate(['list_rows' => $per_page_number, 'page' => $page], false)->each(function ($item, $key) use ($spike_ids) { if (in_array($item['id'], $spike_ids)) { $item['has_appoint_notice'] = 1; } else { $item['has_appoint_notice'] = 0; } return $item; }); return $dataList; } /** * API-一对多关联秒杀SKU * @date 2021-03-01 */ public function mallSpikeProductSku() { return $this->hasMany('MallSpikeProductSku', 'spike_product_id', 'id')->field('spike_product_id,product_id,sku,price_spike as price,number as stock'); } /** * 一对一关联产品 * @date 2021-05-01 */ public function mallProduct() { return $this->hasOne('MallProduct', 'id', 'product_id')->bind([ 'name', 'cover_img', 'price_original' ]); } /** * 秒杀时段 */ public function spikeTimes() { return $this->hasMany('MallSpikeProductTime', 'spike_product_id', 'id'); } }