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.

221 lines
6.5 KiB

8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
7 years ago
7 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
  1. <?php
  2. namespace App\Http\Model;
  3. class Goods extends BaseModel
  4. {
  5. //产品模型
  6. /**
  7. * 关联到模型的数据表
  8. *
  9. * @var string
  10. */
  11. protected $table = 'goods';
  12. /**
  13. * 表明模型是否应该被打上时间戳
  14. * 默认情况下,Eloquent 期望 created_at 和updated_at 已经存在于数据表中,如果你不想要这些 Laravel 自动管理的数据列,在模型类中设置 $timestamps 属性为 false
  15. *
  16. * @var bool
  17. */
  18. public $timestamps = false;
  19. //protected $guarded = []; //$guarded包含你不想被赋值的字段数组。
  20. //protected $fillable = ['name']; //定义哪些字段是可以进行赋值的,与$guarded相反
  21. /**
  22. * The connection name for the model.
  23. * 默认情况下,所有的 Eloquent 模型使用应用配置中的默认数据库连接,如果你想要为模型指定不同的连接,可以通过 $connection 属性来设置
  24. * @var string
  25. */
  26. //protected $connection = 'connection-name';
  27. //常用字段
  28. protected static $common_field = [
  29. 'id', 'typeid', 'tuijian', 'click', 'title', 'sn', 'price','litpic', 'pubdate', 'add_time', 'market_price', 'goods_number', 'sale', 'comments','promote_start_date','promote_price','promote_end_date','goods_img','spec','point'
  30. ];
  31. const STATUS = 0; //商品是否删除,0未删除
  32. /**
  33. * 获取关联到产品的分类
  34. */
  35. public function goodstype()
  36. {
  37. return $this->belongsTo(GoodsType::class, 'typeid', 'id');
  38. }
  39. //获取列表
  40. public static function getList(array $param)
  41. {
  42. extract($param); //参数:limit,offset
  43. $where = '';
  44. $limit = isset($limit) ? $limit : 10;
  45. $offset = isset($offset) ? $offset : 0;
  46. $model = new Goods;
  47. if(isset($typeid)){$where['typeid'] = $typeid;}
  48. if(isset($tuijian)){$where['tuijian'] = $tuijian;}
  49. if(isset($status)){$where['status'] = $status;}else{$where['status'] = self::STATUS;}
  50. if($where !== '')
  51. {
  52. $model = $model->where($where);
  53. }
  54. if(isset($keyword)){$model = $model->where("title", "like", "%$keyword%")->orWhere("sn", "like", "%$keyword%");} //关键词搜索
  55. if(isset($max_price) && isset($min_price)){$model = $model->where("price", ">=", $min_price)->where("price", "<=", $max_price);} //价格区间搜索
  56. $res['count'] = $model->count();
  57. $res['list'] = array();
  58. //排序
  59. if(isset($orderby))
  60. {
  61. switch ($orderby)
  62. {
  63. case 1:
  64. $model = $model->orderBy('sale','desc'); //销量从高到低
  65. break;
  66. case 2:
  67. $model = $model->orderBy('comments','desc'); //评论从高到低
  68. break;
  69. case 3:
  70. $model = $model->orderBy('price','desc'); //价格从高到低
  71. break;
  72. case 4:
  73. $model = $model->orderBy('price','asc'); //价格从低到高
  74. break;
  75. default:
  76. $model = $model->orderBy('pubdate','desc'); //价格从低到高
  77. }
  78. }
  79. if($res['count']>0)
  80. {
  81. $res['list'] = $model->select(self::$common_field)->skip($offset)->take($limit)->orderBy('id','desc')->get();
  82. if($res['list'])
  83. {
  84. foreach($res['list'] as $k=>$v)
  85. {
  86. $res['list'][$k]->goods_detail_url = route('weixin_goods_detail',array('id'=>$v->id));
  87. }
  88. }
  89. }
  90. return $res;
  91. }
  92. public static function getOne($id)
  93. {
  94. if(isset($status)){$where['status'] = $status;}else{$where['status'] = self::STATUS;}
  95. $where['id'] = $id;
  96. $goods = self::where($where)->first();
  97. $goods['price'] = self::get_final_price($id);
  98. return $goods;
  99. }
  100. public static function add(array $data)
  101. {
  102. if ($id = self::insertGetId($data))
  103. {
  104. return $id;
  105. }
  106. return false;
  107. }
  108. public static function modify($where, array $data)
  109. {
  110. if (self::where($where)->update($data))
  111. {
  112. return true;
  113. }
  114. return false;
  115. }
  116. //删除一条记录
  117. public static function remove($id)
  118. {
  119. if (!self::whereIn('id', explode(',', $id))->delete())
  120. {
  121. return false;
  122. }
  123. return true;
  124. }
  125. /**
  126. * 取得商品最终使用价格
  127. *
  128. * @param string $goods_id 商品编号
  129. * @param string $goods_num 购买数量
  130. *
  131. * @return 商品最终购买价格
  132. */
  133. public static function get_final_price($goods_id)
  134. {
  135. $final_price = '0'; //商品最终购买价格
  136. $promote_price = '0'; //商品促销价格
  137. $user_price = '0'; //商品会员价格,预留
  138. //取得商品促销价格列表
  139. $goods = Goods::where('id',$goods_id)->where('status',0)->first(['promote_price','promote_start_date','promote_end_date','price']);
  140. $final_price = $goods->price;
  141. // 计算商品的促销价格
  142. if ($goods->promote_price > 0)
  143. {
  144. $promote_price = self::bargain_price($goods->promote_price, $goods->promote_start_date, $goods->promote_end_date);
  145. }
  146. else
  147. {
  148. $promote_price = 0;
  149. }
  150. if ($promote_price != 0)
  151. {
  152. $final_price = $promote_price;
  153. }
  154. //返回商品最终购买价格
  155. return $final_price;
  156. }
  157. /**
  158. * 判断某个商品是否正在特价促销期
  159. *
  160. * @access public
  161. * @param float $price 促销价格
  162. * @param string $start 促销开始日期
  163. * @param string $end 促销结束日期
  164. * @return float 如果还在促销期则返回促销价,否则返回0
  165. */
  166. public static function bargain_price($price, $start, $end)
  167. {
  168. if ($price == 0)
  169. {
  170. return 0;
  171. }
  172. else
  173. {
  174. $time = time();
  175. if ($time >= $start && $time <= $end)
  176. {
  177. return $price;
  178. }
  179. else
  180. {
  181. return 0;
  182. }
  183. }
  184. }
  185. }