You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

311 lines
9.5 KiB

8 years ago
7 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
  1. <?php
  2. namespace App\Http\Model;
  3. use DB;
  4. use Validator;
  5. class Goods extends BaseModel
  6. {
  7. //产品模型
  8. /**
  9. * 关联到模型的数据表
  10. *
  11. * @var string
  12. */
  13. protected $table = 'goods';
  14. /**
  15. * 表明模型是否应该被打上时间戳
  16. * 默认情况下,Eloquent 期望 created_at 和updated_at 已经存在于数据表中,如果你不想要这些 Laravel 自动管理的数据列,在模型类中设置 $timestamps 属性为 false
  17. *
  18. * @var bool
  19. */
  20. public $timestamps = false;
  21. //protected $guarded = []; //$guarded包含你不想被赋值的字段数组。
  22. //protected $fillable = ['name']; //定义哪些字段是可以进行赋值的,与$guarded相反
  23. /**
  24. * The connection name for the model.
  25. * 默认情况下,所有的 Eloquent 模型使用应用配置中的默认数据库连接,如果你想要为模型指定不同的连接,可以通过 $connection 属性来设置
  26. * @var string
  27. */
  28. //protected $connection = 'connection-name';
  29. //常用字段
  30. protected static $common_field = array(
  31. 'id', 'typeid', 'tuijian', 'click', 'title', 'description', 'sn', 'price','litpic', 'pubdate', 'add_time', 'market_price', 'goods_number', 'sale', 'comments','promote_start_date','promote_price','promote_end_date','goods_img','spec','point'
  32. );
  33. const STATUS = 0; //商品状态 0正常 1已删除 2下架 3申请上架
  34. /**
  35. * 获取关联到产品的分类
  36. */
  37. public function goodstype()
  38. {
  39. return $this->belongsTo(GoodsType::class, 'typeid', 'id');
  40. }
  41. //获取列表
  42. public static function getList(array $param)
  43. {
  44. extract($param); //参数:limit,offset
  45. $where = '';
  46. $limit = isset($limit) ? $limit : 10;
  47. $offset = isset($offset) ? $offset : 0;
  48. $model = new Goods;
  49. if(isset($typeid)){$where['typeid'] = $typeid;}
  50. if(isset($tuijian)){$where['tuijian'] = $tuijian;}
  51. if(isset($status)){$where['status'] = $status;}else{$where['status'] = self::STATUS;}
  52. if(isset($brand_id)){$where['brand_id'] = $brand_id;}
  53. if($where !== '')
  54. {
  55. $model = $model->where($where);
  56. }
  57. //关键词搜索
  58. if(isset($max_price) && isset($min_price)){$model = $model->where("price", ">=", $min_price)->where("price", "<=", $max_price);} //价格区间搜索
  59. if(isset($keyword))
  60. {
  61. $model = $model->where(function ($query) use ($keyword) {$query->where("title", "like", "%$keyword%")->orWhere("sn", "like", "%$keyword%");});
  62. //添加搜索关键词
  63. GoodsSearchword::add(array('name'=>$keyword));
  64. }
  65. //return $model->toSql();//打印sql语句
  66. $res['count'] = $model->count();
  67. $res['list'] = array();
  68. //排序
  69. if(isset($orderby))
  70. {
  71. switch ($orderby)
  72. {
  73. case 1:
  74. $model = $model->orderBy('sale','desc'); //销量从高到低
  75. break;
  76. case 2:
  77. $model = $model->orderBy('comments','desc'); //评论从高到低
  78. break;
  79. case 3:
  80. $model = $model->orderBy('price','desc'); //价格从高到低
  81. break;
  82. case 4:
  83. $model = $model->orderBy('price','asc'); //价格从低到高
  84. break;
  85. case 5:
  86. $timestamp = time();
  87. $model = $model->where('promote_start_date','<=',$timestamp)->where('promote_end_date','>=',$timestamp); //促销商品
  88. break;
  89. default:
  90. $model = $model->orderBy('pubdate','desc'); //最新
  91. }
  92. }
  93. if($res['count']>0)
  94. {
  95. $res['list'] = $model->select(self::$common_field)->skip($offset)->take($limit)->orderBy('id','desc')->get();
  96. if($res['list'])
  97. {
  98. foreach($res['list'] as $k=>$v)
  99. {
  100. $res['list'][$k]->goods_detail_url = route('weixin_goods_detail',array('id'=>$v->id));
  101. $res['list'][$k]->price = self::get_final_price($v->id);
  102. $res['list'][$k]->is_promote_goods = self::bargain_price($v->promote_price,$v->promote_start_date,$v->promote_end_date); //is_promote_goods等于0,说明不是促销商品
  103. }
  104. }
  105. }
  106. return $res;
  107. }
  108. public static function getOne(array $param)
  109. {
  110. extract($param);
  111. $model = new Goods;
  112. $where['id'] = $id;
  113. if(isset($where)){$model = $model->where($where);}
  114. if(isset($field)){$model = $model->select($field);}
  115. $goods = $model->first();
  116. if($goods)
  117. {
  118. $goods['goods_detail_url'] = route('weixin_goods_detail',array('id'=>$goods->id));
  119. $goods['price'] = self::get_final_price($id);
  120. $goods['is_promote_goods'] = self::bargain_price($goods->promote_price,$goods->promote_start_date,$goods->promote_end_date); //is_promote_goods等于0,说明不是促销商品
  121. }
  122. return $goods;
  123. }
  124. public static function add(array $data)
  125. {
  126. $validator = Validator::make($data, [
  127. 'title' => 'required|unique:posts|max:255',
  128. 'body' => 'required',
  129. ]);
  130. if ($validator->fails()) {
  131. return redirect('post/create')
  132. ->withErrors($validator)
  133. ->withInput();
  134. }
  135. if ($id = self::insertGetId($data))
  136. {
  137. return $id;
  138. }
  139. return false;
  140. }
  141. public static function modify($where, array $data)
  142. {
  143. if (self::where($where)->update($data))
  144. {
  145. return true;
  146. }
  147. return false;
  148. }
  149. //删除一条记录
  150. public static function remove($id)
  151. {
  152. if (!self::whereIn('id', explode(',', $id))->delete())
  153. {
  154. return false;
  155. }
  156. return true;
  157. }
  158. /**
  159. * 取得商品最终使用价格
  160. *
  161. * @param string $goods_id 商品编号
  162. * @param string $goods_num 购买数量
  163. *
  164. * @return 商品最终购买价格
  165. */
  166. public static function get_final_price($goods_id)
  167. {
  168. $final_price = '0'; //商品最终购买价格
  169. $promote_price = '0'; //商品促销价格
  170. $user_price = '0'; //商品会员价格,预留
  171. //取得商品促销价格列表
  172. $goods = Goods::where('id',$goods_id)->where('status',0)->first(['promote_price','promote_start_date','promote_end_date','price']);
  173. $final_price = $goods->price;
  174. // 计算商品的促销价格
  175. if ($goods->promote_price > 0)
  176. {
  177. $promote_price = self::bargain_price($goods->promote_price, $goods->promote_start_date, $goods->promote_end_date);
  178. }
  179. else
  180. {
  181. $promote_price = 0;
  182. }
  183. if ($promote_price != 0)
  184. {
  185. $final_price = $promote_price;
  186. }
  187. //返回商品最终购买价格
  188. return $final_price;
  189. }
  190. /**
  191. * 判断某个商品是否正在特价促销期
  192. *
  193. * @access public
  194. * @param float $price 促销价格
  195. * @param string $start 促销开始日期
  196. * @param string $end 促销结束日期
  197. * @return float 如果还在促销期则返回促销价,否则返回0
  198. */
  199. public static function bargain_price($price, $start, $end)
  200. {
  201. if ($price <= 0)
  202. {
  203. return 0;
  204. }
  205. else
  206. {
  207. $time = time();
  208. if ($time >= $start && $time <= $end)
  209. {
  210. return $price;
  211. }
  212. else
  213. {
  214. return 0;
  215. }
  216. }
  217. }
  218. //获取商品详情
  219. public static function goodsDetail(array $param)
  220. {
  221. extract($param); //参数:limit,offset
  222. $model = new Goods;
  223. if(isset($id)){$where['id'] = $id;}
  224. if(isset($where))
  225. {
  226. $model = $model->where($where);
  227. }
  228. else
  229. {
  230. return false;
  231. }
  232. $res = $model->first();
  233. if($res)
  234. {
  235. $where2['comment_type'] = Comment::GOODS_COMMENT_TYPE;
  236. $where2['status'] = Comment::SHOW_COMMENT;
  237. $where2['id_value'] = $id;
  238. $res->goods_comments_num = Comment::where($where2)->count();
  239. $res->price = self::get_final_price($res->id); //商品最终价格
  240. $res->is_promote_goods = self::bargain_price($res->promote_price,$res->promote_start_date,$res->promote_end_date); //is_promote_goods等于0,说明不是促销商品
  241. }
  242. return $res;
  243. }
  244. //增加或减少商品库存
  245. public static function changeGoodsStock(array $param)
  246. {
  247. //$param['type']=1减库存
  248. extract($param);
  249. if(isset($type) && $type==1)
  250. {
  251. //增加库存
  252. DB::table('goods')->where(array('id'=>$goods_id))->increment('goods_number', $goods_number);
  253. }
  254. else
  255. {
  256. //减少库存
  257. DB::table('goods')->where(array('id'=>$goods_id))->decrement('goods_number', $goods_number);
  258. }
  259. }
  260. }