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.

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