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.

280 lines
7.9 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
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. use DB;
  4. class Goods extends BaseModel
  5. {
  6. //产品模型
  7. /**
  8. * 关联到模型的数据表
  9. *
  10. * @var string
  11. */
  12. protected $table = 'goods';
  13. /**
  14. * 表明模型是否应该被打上时间戳
  15. * 默认情况下,Eloquent 期望 created_at 和updated_at 已经存在于数据表中,如果你不想要这些 Laravel 自动管理的数据列,在模型类中设置 $timestamps 属性为 false
  16. *
  17. * @var bool
  18. */
  19. public $timestamps = false;
  20. //protected $guarded = []; //$guarded包含你不想被赋值的字段数组。
  21. //protected $fillable = ['name']; //定义哪些字段是可以进行赋值的,与$guarded相反
  22. /**
  23. * The connection name for the model.
  24. * 默认情况下,所有的 Eloquent 模型使用应用配置中的默认数据库连接,如果你想要为模型指定不同的连接,可以通过 $connection 属性来设置
  25. * @var string
  26. */
  27. //protected $connection = 'connection-name';
  28. //常用字段
  29. protected static $common_field = array(
  30. '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'
  31. );
  32. const STATUS = 0; //商品是否删除,0未删除
  33. /**
  34. * 获取关联到产品的分类
  35. */
  36. public function goodstype()
  37. {
  38. return $this->belongsTo(GoodsType::class, 'typeid', 'id');
  39. }
  40. //获取列表
  41. public static function getList(array $param)
  42. {
  43. extract($param); //参数:limit,offset
  44. $where = '';
  45. $limit = isset($limit) ? $limit : 10;
  46. $offset = isset($offset) ? $offset : 0;
  47. $model = new Goods;
  48. if(isset($typeid)){$where['typeid'] = $typeid;}
  49. if(isset($tuijian)){$where['tuijian'] = $tuijian;}
  50. if(isset($status)){$where['status'] = $status;}else{$where['status'] = self::STATUS;}
  51. if($where !== '')
  52. {
  53. $model = $model->where($where);
  54. }
  55. if(isset($keyword)){$model = $model->where("title", "like", "%$keyword%")->orWhere("sn", "like", "%$keyword%");} //关键词搜索
  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();
  83. if($res['list'])
  84. {
  85. foreach($res['list'] as $k=>$v)
  86. {
  87. $res['list'][$k]->goods_detail_url = route('weixin_goods_detail',array('id'=>$v->id));
  88. }
  89. }
  90. }
  91. return $res;
  92. }
  93. public static function getOne(array $param)
  94. {
  95. extract($param);
  96. $model = new Goods;
  97. $where['id'] = $id;
  98. if(isset($where)){$model = $model->where($where);}
  99. if(isset($field)){$model = $model->select($field);}
  100. $goods = $model->first();
  101. if($goods)
  102. {
  103. $goods['goods_detail_url'] = route('weixin_goods_detail',array('id'=>$goods->id));
  104. $goods['price'] = self::get_final_price($id);
  105. }
  106. return $goods;
  107. }
  108. public static function add(array $data)
  109. {
  110. if ($id = self::insertGetId($data))
  111. {
  112. return $id;
  113. }
  114. return false;
  115. }
  116. public static function modify($where, array $data)
  117. {
  118. if (self::where($where)->update($data))
  119. {
  120. return true;
  121. }
  122. return false;
  123. }
  124. //删除一条记录
  125. public static function remove($id)
  126. {
  127. if (!self::whereIn('id', explode(',', $id))->delete())
  128. {
  129. return false;
  130. }
  131. return true;
  132. }
  133. /**
  134. * 取得商品最终使用价格
  135. *
  136. * @param string $goods_id 商品编号
  137. * @param string $goods_num 购买数量
  138. *
  139. * @return 商品最终购买价格
  140. */
  141. public static function get_final_price($goods_id)
  142. {
  143. $final_price = '0'; //商品最终购买价格
  144. $promote_price = '0'; //商品促销价格
  145. $user_price = '0'; //商品会员价格,预留
  146. //取得商品促销价格列表
  147. $goods = Goods::where('id',$goods_id)->where('status',0)->first(['promote_price','promote_start_date','promote_end_date','price']);
  148. $final_price = $goods->price;
  149. // 计算商品的促销价格
  150. if ($goods->promote_price > 0)
  151. {
  152. $promote_price = self::bargain_price($goods->promote_price, $goods->promote_start_date, $goods->promote_end_date);
  153. }
  154. else
  155. {
  156. $promote_price = 0;
  157. }
  158. if ($promote_price != 0)
  159. {
  160. $final_price = $promote_price;
  161. }
  162. //返回商品最终购买价格
  163. return $final_price;
  164. }
  165. /**
  166. * 判断某个商品是否正在特价促销期
  167. *
  168. * @access public
  169. * @param float $price 促销价格
  170. * @param string $start 促销开始日期
  171. * @param string $end 促销结束日期
  172. * @return float 如果还在促销期则返回促销价,否则返回0
  173. */
  174. public static function bargain_price($price, $start, $end)
  175. {
  176. if ($price <= 0)
  177. {
  178. return 0;
  179. }
  180. else
  181. {
  182. $time = time();
  183. if ($time >= $start && $time <= $end)
  184. {
  185. return $price;
  186. }
  187. else
  188. {
  189. return 0;
  190. }
  191. }
  192. }
  193. //获取商品详情
  194. public static function goodsDetail(array $param)
  195. {
  196. extract($param); //参数:limit,offset
  197. $model = new Goods;
  198. if(isset($id)){$where['id'] = $id;}
  199. if(isset($where))
  200. {
  201. $model = $model->where($where);
  202. }
  203. else
  204. {
  205. return false;
  206. }
  207. $res = $model->first();
  208. if($res)
  209. {
  210. $where2['comment_type'] = Comment::GOODS_COMMENT_TYPE;
  211. $where2['status'] = Comment::SHOW_COMMENT;
  212. $where2['id_value'] = $id;
  213. $res->goods_comments_num = Comment::where($where2)->count();
  214. }
  215. return $res;
  216. }
  217. //增加或减少商品库存
  218. public static function changeGoodsStock(array $param)
  219. {
  220. extract($param);
  221. if(isset($type))
  222. {
  223. //增加库存
  224. DB::table('goods')->where(array('id'=>$goods_id))->increment('goods_number', $goods_number);
  225. }
  226. else
  227. {
  228. //减少库存
  229. DB::table('goods')->where(array('id'=>$goods_id))->decrement('goods_number', $goods_number);
  230. }
  231. }
  232. }