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.

299 lines
9.2 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
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正常 1已删除 2下架 3申请上架
  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(isset($brand_id)){$where['brand_id'] = $brand_id;}
  52. if($where !== '')
  53. {
  54. $model = $model->where($where);
  55. }
  56. //关键词搜索
  57. if(isset($max_price) && isset($min_price)){$model = $model->where("price", ">=", $min_price)->where("price", "<=", $max_price);} //价格区间搜索
  58. if(isset($keyword))
  59. {
  60. $model = $model->where(function ($query) use ($keyword) {$query->where("title", "like", "%$keyword%")->orWhere("sn", "like", "%$keyword%");});
  61. //添加搜索关键词
  62. GoodsSearchword::add(array('name'=>$keyword));
  63. }
  64. //return $model->toSql();//打印sql语句
  65. $res['count'] = $model->count();
  66. $res['list'] = array();
  67. //排序
  68. if(isset($orderby))
  69. {
  70. switch ($orderby)
  71. {
  72. case 1:
  73. $model = $model->orderBy('sale','desc'); //销量从高到低
  74. break;
  75. case 2:
  76. $model = $model->orderBy('comments','desc'); //评论从高到低
  77. break;
  78. case 3:
  79. $model = $model->orderBy('price','desc'); //价格从高到低
  80. break;
  81. case 4:
  82. $model = $model->orderBy('price','asc'); //价格从低到高
  83. break;
  84. case 5:
  85. $timestamp = time();
  86. $model = $model->where('promote_start_date','<=',$timestamp)->where('promote_end_date','>=',$timestamp); //促销商品
  87. break;
  88. default:
  89. $model = $model->orderBy('pubdate','desc'); //最新
  90. }
  91. }
  92. if($res['count']>0)
  93. {
  94. $res['list'] = $model->select(self::$common_field)->skip($offset)->take($limit)->orderBy('id','desc')->get();
  95. if($res['list'])
  96. {
  97. foreach($res['list'] as $k=>$v)
  98. {
  99. $res['list'][$k]->goods_detail_url = route('weixin_goods_detail',array('id'=>$v->id));
  100. $res['list'][$k]->price = self::get_final_price($v->id);
  101. $res['list'][$k]->is_promote_goods = self::bargain_price($v->promote_price,$v->promote_start_date,$v->promote_end_date); //is_promote_goods等于0,说明不是促销商品
  102. }
  103. }
  104. }
  105. return $res;
  106. }
  107. public static function getOne(array $param)
  108. {
  109. extract($param);
  110. $model = new Goods;
  111. $where['id'] = $id;
  112. if(isset($where)){$model = $model->where($where);}
  113. if(isset($field)){$model = $model->select($field);}
  114. $goods = $model->first();
  115. if($goods)
  116. {
  117. $goods['goods_detail_url'] = route('weixin_goods_detail',array('id'=>$goods->id));
  118. $goods['price'] = self::get_final_price($id);
  119. $goods['is_promote_goods'] = self::bargain_price($goods->promote_price,$goods->promote_start_date,$goods->promote_end_date); //is_promote_goods等于0,说明不是促销商品
  120. }
  121. return $goods;
  122. }
  123. public static function add(array $data)
  124. {
  125. if ($id = self::insertGetId($data))
  126. {
  127. return $id;
  128. }
  129. return false;
  130. }
  131. public static function modify($where, array $data)
  132. {
  133. if (self::where($where)->update($data))
  134. {
  135. return true;
  136. }
  137. return false;
  138. }
  139. //删除一条记录
  140. public static function remove($id)
  141. {
  142. if (!self::whereIn('id', explode(',', $id))->delete())
  143. {
  144. return false;
  145. }
  146. return true;
  147. }
  148. /**
  149. * 取得商品最终使用价格
  150. *
  151. * @param string $goods_id 商品编号
  152. * @param string $goods_num 购买数量
  153. *
  154. * @return 商品最终购买价格
  155. */
  156. public static function get_final_price($goods_id)
  157. {
  158. $final_price = '0'; //商品最终购买价格
  159. $promote_price = '0'; //商品促销价格
  160. $user_price = '0'; //商品会员价格,预留
  161. //取得商品促销价格列表
  162. $goods = Goods::where('id',$goods_id)->where('status',0)->first(['promote_price','promote_start_date','promote_end_date','price']);
  163. $final_price = $goods->price;
  164. // 计算商品的促销价格
  165. if ($goods->promote_price > 0)
  166. {
  167. $promote_price = self::bargain_price($goods->promote_price, $goods->promote_start_date, $goods->promote_end_date);
  168. }
  169. else
  170. {
  171. $promote_price = 0;
  172. }
  173. if ($promote_price != 0)
  174. {
  175. $final_price = $promote_price;
  176. }
  177. //返回商品最终购买价格
  178. return $final_price;
  179. }
  180. /**
  181. * 判断某个商品是否正在特价促销期
  182. *
  183. * @access public
  184. * @param float $price 促销价格
  185. * @param string $start 促销开始日期
  186. * @param string $end 促销结束日期
  187. * @return float 如果还在促销期则返回促销价,否则返回0
  188. */
  189. public static function bargain_price($price, $start, $end)
  190. {
  191. if ($price <= 0)
  192. {
  193. return 0;
  194. }
  195. else
  196. {
  197. $time = time();
  198. if ($time >= $start && $time <= $end)
  199. {
  200. return $price;
  201. }
  202. else
  203. {
  204. return 0;
  205. }
  206. }
  207. }
  208. //获取商品详情
  209. public static function goodsDetail(array $param)
  210. {
  211. extract($param); //参数:limit,offset
  212. $model = new Goods;
  213. if(isset($id)){$where['id'] = $id;}
  214. if(isset($where))
  215. {
  216. $model = $model->where($where);
  217. }
  218. else
  219. {
  220. return false;
  221. }
  222. $res = $model->first();
  223. if($res)
  224. {
  225. $where2['comment_type'] = Comment::GOODS_COMMENT_TYPE;
  226. $where2['status'] = Comment::SHOW_COMMENT;
  227. $where2['id_value'] = $id;
  228. $res->goods_comments_num = Comment::where($where2)->count();
  229. $res->price = self::get_final_price($res->id); //商品最终价格
  230. $res->is_promote_goods = self::bargain_price($res->promote_price,$res->promote_start_date,$res->promote_end_date); //is_promote_goods等于0,说明不是促销商品
  231. }
  232. return $res;
  233. }
  234. //增加或减少商品库存
  235. public static function changeGoodsStock(array $param)
  236. {
  237. //$param['type']=1减库存
  238. extract($param);
  239. if(isset($type) && $type==1)
  240. {
  241. //增加库存
  242. DB::table('goods')->where(array('id'=>$goods_id))->increment('goods_number', $goods_number);
  243. }
  244. else
  245. {
  246. //减少库存
  247. DB::table('goods')->where(array('id'=>$goods_id))->decrement('goods_number', $goods_number);
  248. }
  249. }
  250. }