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.

213 lines
6.2 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
  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()->toArray();
  82. }
  83. return $res;
  84. }
  85. public static function getOne($id)
  86. {
  87. if(isset($status)){$where['status'] = $status;}else{$where['status'] = self::STATUS;}
  88. $where['id'] = $id;
  89. $goods = self::where($where)->first()->toArray();
  90. $goods['price'] = self::get_final_price($id);
  91. return $goods;
  92. }
  93. public static function add(array $data)
  94. {
  95. if ($id = self::insertGetId($data))
  96. {
  97. return $id;
  98. }
  99. return false;
  100. }
  101. public static function modify($where, array $data)
  102. {
  103. if (self::where($where)->update($data))
  104. {
  105. return true;
  106. }
  107. return false;
  108. }
  109. //删除一条记录
  110. public static function remove($id)
  111. {
  112. if (!self::whereIn('id', explode(',', $id))->delete())
  113. {
  114. return false;
  115. }
  116. return true;
  117. }
  118. /**
  119. * 取得商品最终使用价格
  120. *
  121. * @param string $goods_id 商品编号
  122. * @param string $goods_num 购买数量
  123. *
  124. * @return 商品最终购买价格
  125. */
  126. public static function get_final_price($goods_id)
  127. {
  128. $final_price = '0'; //商品最终购买价格
  129. $promote_price = '0'; //商品促销价格
  130. $user_price = '0'; //商品会员价格,预留
  131. //取得商品促销价格列表
  132. $goods = Goods::where('id',$goods_id)->where('status',0)->first(['promote_price','promote_start_date','promote_end_date','price']);
  133. $final_price = $goods->price;
  134. // 计算商品的促销价格
  135. if ($goods->promote_price > 0)
  136. {
  137. $promote_price = self::bargain_price($goods->promote_price, $goods->promote_start_date, $goods->promote_end_date);
  138. }
  139. else
  140. {
  141. $promote_price = 0;
  142. }
  143. if ($promote_price != 0)
  144. {
  145. $final_price = $promote_price;
  146. }
  147. //返回商品最终购买价格
  148. return $final_price;
  149. }
  150. /**
  151. * 判断某个商品是否正在特价促销期
  152. *
  153. * @access public
  154. * @param float $price 促销价格
  155. * @param string $start 促销开始日期
  156. * @param string $end 促销结束日期
  157. * @return float 如果还在促销期则返回促销价,否则返回0
  158. */
  159. public static function bargain_price($price, $start, $end)
  160. {
  161. if ($price == 0)
  162. {
  163. return 0;
  164. }
  165. else
  166. {
  167. $time = time();
  168. if ($time >= $start && $time <= $end)
  169. {
  170. return $price;
  171. }
  172. else
  173. {
  174. return 0;
  175. }
  176. }
  177. }
  178. }