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.

262 lines
7.4 KiB

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
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
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 = array(
  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(array $param)
  93. {
  94. extract($param);
  95. $model = new Goods;
  96. $where['id'] = $id;
  97. if(isset($where)){$model = $model->where($where);}
  98. if(isset($field)){$model = $model->select($field);}
  99. $goods = $model->first();
  100. if($goods)
  101. {
  102. $goods['goods_detail_url'] = route('weixin_goods_detail',array('id'=>$goods->id));
  103. $goods['price'] = self::get_final_price($id);
  104. }
  105. return $goods;
  106. }
  107. public static function add(array $data)
  108. {
  109. if ($id = self::insertGetId($data))
  110. {
  111. return $id;
  112. }
  113. return false;
  114. }
  115. public static function modify($where, array $data)
  116. {
  117. if (self::where($where)->update($data))
  118. {
  119. return true;
  120. }
  121. return false;
  122. }
  123. //删除一条记录
  124. public static function remove($id)
  125. {
  126. if (!self::whereIn('id', explode(',', $id))->delete())
  127. {
  128. return false;
  129. }
  130. return true;
  131. }
  132. /**
  133. * 取得商品最终使用价格
  134. *
  135. * @param string $goods_id 商品编号
  136. * @param string $goods_num 购买数量
  137. *
  138. * @return 商品最终购买价格
  139. */
  140. public static function get_final_price($goods_id)
  141. {
  142. $final_price = '0'; //商品最终购买价格
  143. $promote_price = '0'; //商品促销价格
  144. $user_price = '0'; //商品会员价格,预留
  145. //取得商品促销价格列表
  146. $goods = Goods::where('id',$goods_id)->where('status',0)->first(['promote_price','promote_start_date','promote_end_date','price']);
  147. $final_price = $goods->price;
  148. // 计算商品的促销价格
  149. if ($goods->promote_price > 0)
  150. {
  151. $promote_price = self::bargain_price($goods->promote_price, $goods->promote_start_date, $goods->promote_end_date);
  152. }
  153. else
  154. {
  155. $promote_price = 0;
  156. }
  157. if ($promote_price != 0)
  158. {
  159. $final_price = $promote_price;
  160. }
  161. //返回商品最终购买价格
  162. return $final_price;
  163. }
  164. /**
  165. * 判断某个商品是否正在特价促销期
  166. *
  167. * @access public
  168. * @param float $price 促销价格
  169. * @param string $start 促销开始日期
  170. * @param string $end 促销结束日期
  171. * @return float 如果还在促销期则返回促销价,否则返回0
  172. */
  173. public static function bargain_price($price, $start, $end)
  174. {
  175. if ($price <= 0)
  176. {
  177. return 0;
  178. }
  179. else
  180. {
  181. $time = time();
  182. if ($time >= $start && $time <= $end)
  183. {
  184. return $price;
  185. }
  186. else
  187. {
  188. return 0;
  189. }
  190. }
  191. }
  192. //获取商品详情
  193. public static function goodsDetail(array $param)
  194. {
  195. extract($param); //参数:limit,offset
  196. $model = new Goods;
  197. if(isset($id)){$where['id'] = $id;}
  198. if(isset($where))
  199. {
  200. $model = $model->where($where);
  201. }
  202. else
  203. {
  204. return false;
  205. }
  206. $res = $model->first();
  207. if($res)
  208. {
  209. $where2['comment_type'] = Comment::GOODS_COMMENT_TYPE;
  210. $where2['status'] = Comment::SHOW_COMMENT;
  211. $where2['id_value'] = $id;
  212. $res->goods_comments_num = Comment::where($where2)->count();
  213. }
  214. return $res;
  215. }
  216. }