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.

356 lines
11 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
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. use Validator;
  5. class Goods extends BaseModel
  6. {
  7. //产品模型
  8. /**
  9. * 关联到模型的数据表
  10. *
  11. * @var string
  12. */
  13. protected $table = 'goods';
  14. /**
  15. * 表明模型是否应该被打上时间戳
  16. * 默认情况下,Eloquent 期望 created_at 和updated_at 已经存在于数据表中,如果你不想要这些 Laravel 自动管理的数据列,在模型类中设置 $timestamps 属性为 false
  17. *
  18. * @var bool
  19. */
  20. public $timestamps = false;
  21. //protected $guarded = []; //$guarded包含你不想被赋值的字段数组。
  22. //protected $fillable = ['name']; //定义哪些字段是可以进行赋值的,与$guarded相反
  23. /**
  24. * The connection name for the model.
  25. * 默认情况下,所有的 Eloquent 模型使用应用配置中的默认数据库连接,如果你想要为模型指定不同的连接,可以通过 $connection 属性来设置
  26. * @var string
  27. */
  28. //protected $connection = 'connection-name';
  29. //常用字段
  30. protected static $common_field = array(
  31. 'id', 'typeid', 'tuijian', 'click', 'title', 'description', 'sn', 'price','litpic', 'pubdate', 'add_time', 'market_price', 'goods_number', 'sale', 'comments','promote_start_date','promote_price','promote_end_date','goods_img','spec','point'
  32. );
  33. const STATUS = 0; //商品状态 0正常 1已删除 2下架 3申请上架
  34. /**
  35. * 获取关联到产品的分类
  36. */
  37. public function goodstype()
  38. {
  39. return $this->belongsTo(GoodsType::class, 'typeid', 'id');
  40. }
  41. //获取列表
  42. public static function getList(array $param)
  43. {
  44. extract($param); //参数:limit,offset
  45. $where = '';
  46. $limit = isset($limit) ? $limit : 10;
  47. $offset = isset($offset) ? $offset : 0;
  48. $model = new Goods;
  49. if(isset($typeid)){$where['typeid'] = $typeid;}
  50. if(isset($tuijian)){$where['tuijian'] = $tuijian;}
  51. if(isset($status)){$where['status'] = $status;}else{$where['status'] = self::STATUS;}
  52. if(isset($brand_id)){$where['brand_id'] = $brand_id;}
  53. if($where !== '')
  54. {
  55. $model = $model->where($where);
  56. }
  57. //关键词搜索
  58. if(isset($max_price) && isset($min_price)){$model = $model->where("price", ">=", $min_price)->where("price", "<=", $max_price);} //价格区间搜索
  59. if(isset($keyword))
  60. {
  61. $model = $model->where(function ($query) use ($keyword) {$query->where("title", "like", "%$keyword%")->orWhere("sn", "like", "%$keyword%");});
  62. //添加搜索关键词
  63. GoodsSearchword::add(array('name'=>$keyword));
  64. }
  65. //return $model->toSql();//打印sql语句
  66. $res['count'] = $model->count();
  67. $res['list'] = array();
  68. //排序
  69. if(isset($orderby))
  70. {
  71. switch ($orderby)
  72. {
  73. case 1:
  74. $model = $model->orderBy('sale','desc'); //销量从高到低
  75. break;
  76. case 2:
  77. $model = $model->orderBy('comments','desc'); //评论从高到低
  78. break;
  79. case 3:
  80. $model = $model->orderBy('price','desc'); //价格从高到低
  81. break;
  82. case 4:
  83. $model = $model->orderBy('price','asc'); //价格从低到高
  84. break;
  85. case 5:
  86. $timestamp = time();
  87. $model = $model->where('promote_start_date','<=',$timestamp)->where('promote_end_date','>=',$timestamp); //促销商品
  88. break;
  89. default:
  90. $model = $model->orderBy('pubdate','desc'); //最新
  91. }
  92. }
  93. if($res['count']>0)
  94. {
  95. $res['list'] = $model->select(self::$common_field)->skip($offset)->take($limit)->orderBy('id','desc')->get();
  96. if($res['list'])
  97. {
  98. foreach($res['list'] as $k=>$v)
  99. {
  100. $res['list'][$k]->goods_detail_url = route('weixin_goods_detail',array('id'=>$v->id));
  101. $res['list'][$k]->price = self::get_goods_final_price($v);
  102. if(!empty($res['list'][$k]->litpic)){$res['list'][$k]->litpic = http_host().$res['list'][$k]->litpic;}
  103. $res['list'][$k]->is_promote_goods = self::bargain_price($v->promote_price,$v->promote_start_date,$v->promote_end_date); //is_promote_goods等于0,说明不是促销商品
  104. }
  105. }
  106. }
  107. return $res;
  108. }
  109. public static function getOne(array $param)
  110. {
  111. extract($param);
  112. $model = new Goods;
  113. $where['id'] = $id;
  114. if(isset($where)){$model = $model->where($where);}
  115. if(isset($field)){$model = $model->select($field);}
  116. $goods = $model->first();
  117. if($goods)
  118. {
  119. $goods['goods_detail_url'] = route('weixin_goods_detail',array('id'=>$goods->id));
  120. $goods['price'] = self::get_goods_final_price($goods);
  121. $goods['is_promote_goods'] = self::bargain_price($goods->promote_price,$goods->promote_start_date,$goods->promote_end_date); //is_promote_goods等于0,说明不是促销商品
  122. }
  123. return $goods;
  124. }
  125. public static function add(array $data)
  126. {
  127. $validator = Validator::make($data, [
  128. 'title' => 'required|unique:posts|max:255',
  129. 'body' => 'required',
  130. ]);
  131. if ($validator->fails()) {
  132. return redirect('post/create')
  133. ->withErrors($validator)
  134. ->withInput();
  135. }
  136. if ($id = self::insertGetId($data))
  137. {
  138. return $id;
  139. }
  140. return false;
  141. }
  142. public static function modify($where, array $data)
  143. {
  144. if (self::where($where)->update($data))
  145. {
  146. return true;
  147. }
  148. return false;
  149. }
  150. //删除一条记录
  151. public static function remove($id)
  152. {
  153. if (!self::whereIn('id', explode(',', $id))->delete())
  154. {
  155. return false;
  156. }
  157. return true;
  158. }
  159. /**
  160. * 取得商品最终使用价格
  161. *
  162. * @param string $goods_id 商品编号
  163. * @param string $goods_num 购买数量
  164. *
  165. * @return 商品最终购买价格
  166. */
  167. public static function get_final_price($goods_id)
  168. {
  169. $final_price = '0'; //商品最终购买价格
  170. $promote_price = '0'; //商品促销价格
  171. $user_price = '0'; //商品会员价格,预留
  172. //取得商品促销价格列表
  173. $goods = Goods::where('id',$goods_id)->where('status',0)->first(['promote_price','promote_start_date','promote_end_date','price']);
  174. $final_price = $goods->price;
  175. // 计算商品的促销价格
  176. if ($goods->promote_price > 0)
  177. {
  178. $promote_price = self::bargain_price($goods->promote_price, $goods->promote_start_date, $goods->promote_end_date);
  179. }
  180. else
  181. {
  182. $promote_price = 0;
  183. }
  184. if ($promote_price != 0)
  185. {
  186. $final_price = $promote_price;
  187. }
  188. //返回商品最终购买价格
  189. return $final_price;
  190. }
  191. /**
  192. * 取得商品最终使用价格
  193. *
  194. * @param string $goods_id 商品编号
  195. * @param string $goods_num 购买数量
  196. *
  197. * @return 商品最终购买价格
  198. */
  199. public static function get_goods_final_price($goods)
  200. {
  201. $final_price = '0'; //商品最终购买价格
  202. $promote_price = '0'; //商品促销价格
  203. $user_price = '0'; //商品会员价格,预留
  204. //取得商品促销价格列表
  205. $final_price = $goods->price;
  206. // 计算商品的促销价格
  207. if ($goods->promote_price > 0)
  208. {
  209. $promote_price = self::bargain_price($goods->promote_price, $goods->promote_start_date, $goods->promote_end_date);
  210. }
  211. else
  212. {
  213. $promote_price = 0;
  214. }
  215. if ($promote_price != 0)
  216. {
  217. $final_price = $promote_price;
  218. }
  219. //返回商品最终购买价格
  220. return $final_price;
  221. }
  222. /**
  223. * 判断某个商品是否正在特价促销期
  224. *
  225. * @access public
  226. * @param float $price 促销价格
  227. * @param string $start 促销开始日期
  228. * @param string $end 促销结束日期
  229. * @return float 如果还在促销期则返回促销价,否则返回0
  230. */
  231. public static function bargain_price($price, $start, $end)
  232. {
  233. if ($price <= 0)
  234. {
  235. return 0;
  236. }
  237. else
  238. {
  239. $time = time();
  240. if ($time >= $start && $time <= $end)
  241. {
  242. return $price;
  243. }
  244. else
  245. {
  246. return 0;
  247. }
  248. }
  249. }
  250. //获取商品详情
  251. public static function goodsDetail(array $param)
  252. {
  253. extract($param); //参数:limit,offset
  254. $model = new Goods;
  255. if(isset($id)){$where['id'] = $id;}
  256. if(isset($where))
  257. {
  258. $model = $model->where($where);
  259. }
  260. else
  261. {
  262. return false;
  263. }
  264. $res = $model->first();
  265. if($res)
  266. {
  267. $where2['comment_type'] = Comment::GOODS_COMMENT_TYPE;
  268. $where2['status'] = Comment::SHOW_COMMENT;
  269. $where2['id_value'] = $id;
  270. $res->goods_comments_num = Comment::where($where2)->count();
  271. $res->price = self::get_final_price($res->id); //商品最终价格
  272. $res->is_promote_goods = self::bargain_price($res->promote_price,$res->promote_start_date,$res->promote_end_date); //is_promote_goods等于0,说明不是促销商品
  273. }
  274. return $res;
  275. }
  276. //增加或减少商品库存
  277. public static function changeGoodsStock(array $param)
  278. {
  279. //$param['type']=1减库存
  280. extract($param);
  281. if(isset($type) && $type==1)
  282. {
  283. //增加库存
  284. DB::table('goods')->where(array('id'=>$goods_id))->increment('goods_number', $goods_number);
  285. }
  286. else
  287. {
  288. //减少库存
  289. DB::table('goods')->where(array('id'=>$goods_id))->decrement('goods_number', $goods_number);
  290. }
  291. }
  292. //获取栏目名称
  293. public static function getTypenameAttr($data)
  294. {
  295. return DB::table('goods_type')->where(array('id'=>$data['typeid']))->value('name');
  296. }
  297. }