belongsTo(GoodsType::class, 'typeid', 'id'); } //获取列表 public static function getList(array $param) { extract($param); //参数:limit,offset $where = ''; $limit = isset($limit) ? $limit : 10; $offset = isset($offset) ? $offset : 0; $model = new Goods; if(isset($typeid)){$where['typeid'] = $typeid;} if(isset($tuijian)){$where['tuijian'] = $tuijian;} if(isset($status)){$where['status'] = $status;}else{$where['status'] = self::STATUS;} if(isset($brand_id)){$where['brand_id'] = $brand_id;} if($where !== '') { $model = $model->where($where); } //关键词搜索 if(isset($max_price) && isset($min_price)){$model = $model->where("price", ">=", $min_price)->where("price", "<=", $max_price);} //价格区间搜索 if(isset($keyword)) { $model = $model->where(function ($query) use ($keyword) {$query->where("title", "like", "%$keyword%")->orWhere("sn", "like", "%$keyword%");}); //添加搜索关键词 GoodsSearchword::add(array('name'=>$keyword)); } //return $model->toSql();//打印sql语句 $res['count'] = $model->count(); $res['list'] = array(); //排序 if(isset($orderby)) { switch ($orderby) { case 1: $model = $model->orderBy('sale','desc'); //销量从高到低 break; case 2: $model = $model->orderBy('comments','desc'); //评论从高到低 break; case 3: $model = $model->orderBy('price','desc'); //价格从高到低 break; case 4: $model = $model->orderBy('price','asc'); //价格从低到高 break; case 5: $timestamp = time(); $model = $model->where('promote_start_date','<=',$timestamp)->where('promote_end_date','>=',$timestamp); //促销商品 break; default: $model = $model->orderBy('pubdate','desc'); //最新 } } if($res['count']>0) { $res['list'] = $model->select(self::$common_field)->skip($offset)->take($limit)->orderBy('id','desc')->get(); if($res['list']) { foreach($res['list'] as $k=>$v) { $res['list'][$k]->goods_detail_url = route('weixin_goods_detail',array('id'=>$v->id)); $res['list'][$k]->price = self::get_goods_final_price($v); if(!empty($res['list'][$k]->litpic)){$res['list'][$k]->litpic = http_host().$res['list'][$k]->litpic;} $res['list'][$k]->is_promote_goods = self::bargain_price($v->promote_price,$v->promote_start_date,$v->promote_end_date); //is_promote_goods等于0,说明不是促销商品 } } } return $res; } public static function getOne(array $param) { extract($param); $model = new Goods; $where['id'] = $id; if(isset($where)){$model = $model->where($where);} if(isset($field)){$model = $model->select($field);} $goods = $model->first(); if($goods) { $goods['goods_detail_url'] = route('weixin_goods_detail',array('id'=>$goods->id)); $goods['price'] = self::get_goods_final_price($goods); $goods['is_promote_goods'] = self::bargain_price($goods->promote_price,$goods->promote_start_date,$goods->promote_end_date); //is_promote_goods等于0,说明不是促销商品 } return $goods; } public static function add(array $data) { $validator = Validator::make($data, [ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); if ($validator->fails()) { return redirect('post/create') ->withErrors($validator) ->withInput(); } if ($id = self::insertGetId($data)) { return $id; } return false; } public static function modify($where, array $data) { if (self::where($where)->update($data)) { return true; } return false; } //删除一条记录 public static function remove($id) { if (!self::whereIn('id', explode(',', $id))->delete()) { return false; } return true; } /** * 取得商品最终使用价格 * * @param string $goods_id 商品编号 * @param string $goods_num 购买数量 * * @return 商品最终购买价格 */ public static function get_final_price($goods_id) { $final_price = '0'; //商品最终购买价格 $promote_price = '0'; //商品促销价格 $user_price = '0'; //商品会员价格,预留 //取得商品促销价格列表 $goods = Goods::where('id',$goods_id)->where('status',0)->first(['promote_price','promote_start_date','promote_end_date','price']); $final_price = $goods->price; // 计算商品的促销价格 if ($goods->promote_price > 0) { $promote_price = self::bargain_price($goods->promote_price, $goods->promote_start_date, $goods->promote_end_date); } else { $promote_price = 0; } if ($promote_price != 0) { $final_price = $promote_price; } //返回商品最终购买价格 return $final_price; } /** * 取得商品最终使用价格 * * @param string $goods_id 商品编号 * @param string $goods_num 购买数量 * * @return 商品最终购买价格 */ public static function get_goods_final_price($goods) { $final_price = '0'; //商品最终购买价格 $promote_price = '0'; //商品促销价格 $user_price = '0'; //商品会员价格,预留 //取得商品促销价格列表 $final_price = $goods->price; // 计算商品的促销价格 if ($goods->promote_price > 0) { $promote_price = self::bargain_price($goods->promote_price, $goods->promote_start_date, $goods->promote_end_date); } else { $promote_price = 0; } if ($promote_price != 0) { $final_price = $promote_price; } //返回商品最终购买价格 return $final_price; } /** * 判断某个商品是否正在特价促销期 * * @access public * @param float $price 促销价格 * @param string $start 促销开始日期 * @param string $end 促销结束日期 * @return float 如果还在促销期则返回促销价,否则返回0 */ public static function bargain_price($price, $start, $end) { if ($price <= 0) { return 0; } else { $time = time(); if ($time >= $start && $time <= $end) { return $price; } else { return 0; } } } //获取商品详情 public static function goodsDetail(array $param) { extract($param); //参数:limit,offset $model = new Goods; if(isset($id)){$where['id'] = $id;} if(isset($where)) { $model = $model->where($where); } else { return false; } $res = $model->first(); if($res) { $where2['comment_type'] = Comment::GOODS_COMMENT_TYPE; $where2['status'] = Comment::SHOW_COMMENT; $where2['id_value'] = $id; $res->goods_comments_num = Comment::where($where2)->count(); $res->price = self::get_final_price($res->id); //商品最终价格 $res->is_promote_goods = self::bargain_price($res->promote_price,$res->promote_start_date,$res->promote_end_date); //is_promote_goods等于0,说明不是促销商品 } return $res; } //增加或减少商品库存 public static function changeGoodsStock(array $param) { //$param['type']=1减库存 extract($param); if(isset($type) && $type==1) { //增加库存 DB::table('goods')->where(array('id'=>$goods_id))->increment('goods_number', $goods_number); } else { //减少库存 DB::table('goods')->where(array('id'=>$goods_id))->decrement('goods_number', $goods_number); } } //获取栏目名称 public static function getTypenameAttr($data) { return DB::table('goods_type')->where(array('id'=>$data['typeid']))->value('name'); } }