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.

218 lines
6.3 KiB

8 years ago
7 years ago
8 years ago
7 years ago
8 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
  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 $common_field = [
  29. 'id', 'typeid', 'tuijian', 'click', 'title', 'goods_sn', 'price','litpic', 'pubdate', 'addtime', '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%");} //关键词搜索
  55. if(isset($goods_sn)){$model = $model->where("goods_sn", "like", "%$goods_sn%");} //货号搜索
  56. if(isset($max_price) && isset($min_price)){$model = $model->where("price", ">=", $min_price)->where("price", "<=", $max_price);} //价格区间搜索
  57. $res['count'] = $model->count();
  58. $res['list'] = array();
  59. //排序
  60. if(isset($orderby))
  61. {
  62. switch ($orderby)
  63. {
  64. case 1:
  65. $model = $model->orderBy('sale','desc'); //销量从高到低
  66. break;
  67. case 2:
  68. $model = $model->orderBy('comments','desc'); //评论从高到低
  69. break;
  70. case 3:
  71. $model = $model->orderBy('price','desc'); //价格从高到低
  72. break;
  73. case 4:
  74. $model = $model->orderBy('price','asc'); //价格从低到高
  75. break;
  76. default:
  77. $model = $model->orderBy('pubdate','desc'); //价格从低到高
  78. }
  79. }
  80. if($res['count']>0)
  81. {
  82. $res['list'] = $model->select(self::$common_field)->skip($offset)->take($limit)->orderBy('id','desc')->get()->toArray();
  83. }
  84. else
  85. {
  86. return false;
  87. }
  88. return $res;
  89. }
  90. public static function getOne($id)
  91. {
  92. if(isset($status)){$where['status'] = $status;}else{$where['status'] = self::STATUS;}
  93. $where['id'] = $id;
  94. $goods = self::where($where)->first()->toArray();
  95. $goods['price'] = self::get_final_price($id);
  96. return $goods;
  97. }
  98. public static function add(array $data)
  99. {
  100. if ($id = self::insertGetId($data))
  101. {
  102. return $id;
  103. }
  104. return false;
  105. }
  106. public static function modify($where, array $data)
  107. {
  108. if (self::where($where)->update($data))
  109. {
  110. return true;
  111. }
  112. return false;
  113. }
  114. //删除一条记录
  115. public static function remove($id)
  116. {
  117. if (!self::whereIn('id', explode(',', $id))->delete())
  118. {
  119. return false;
  120. }
  121. return true;
  122. }
  123. /**
  124. * 取得商品最终使用价格
  125. *
  126. * @param string $goods_id 商品编号
  127. * @param string $goods_num 购买数量
  128. *
  129. * @return 商品最终购买价格
  130. */
  131. public static function get_final_price($goods_id)
  132. {
  133. $final_price = '0'; //商品最终购买价格
  134. $promote_price = '0'; //商品促销价格
  135. $user_price = '0'; //商品会员价格,预留
  136. //取得商品促销价格列表
  137. $goods = Goods::where('id',$goods_id)->where('status',0)->first(['promote_price','promote_start_date','promote_end_date','price']);
  138. $final_price = $goods->price;
  139. // 计算商品的促销价格
  140. if ($goods->promote_price > 0)
  141. {
  142. $promote_price = self::bargain_price($goods->promote_price, $goods->promote_start_date, $goods->promote_end_date);
  143. }
  144. else
  145. {
  146. $promote_price = 0;
  147. }
  148. if ($promote_price != 0)
  149. {
  150. $final_price = $promote_price;
  151. }
  152. //返回商品最终购买价格
  153. return $final_price;
  154. }
  155. /**
  156. * 判断某个商品是否正在特价促销期
  157. *
  158. * @access public
  159. * @param float $price 促销价格
  160. * @param string $start 促销开始日期
  161. * @param string $end 促销结束日期
  162. * @return float 如果还在促销期则返回促销价,否则返回0
  163. */
  164. public static function bargain_price($price, $start, $end)
  165. {
  166. if ($price == 0)
  167. {
  168. return 0;
  169. }
  170. else
  171. {
  172. $time = time();
  173. if ($time >= $start && $time <= $end)
  174. {
  175. return $price;
  176. }
  177. else
  178. {
  179. return 0;
  180. }
  181. }
  182. }
  183. }