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.

336 lines
9.7 KiB

7 years ago
4 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
6 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Model;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Facades\Log;
  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 $hidden = array();
  22. //protected $guarded = []; //$guarded包含你不想被赋值的字段数组。
  23. //protected $fillable = ['name']; //定义哪些字段是可以进行赋值的,与$guarded相反
  24. /**
  25. * The connection name for the model.
  26. * 默认情况下,所有的 Eloquent 模型使用应用配置中的默认数据库连接,如果你想要为模型指定不同的连接,可以通过 $connection 属性来设置
  27. * @var string
  28. */
  29. //protected $connection = 'connection-name';
  30. //常用字段
  31. public $common_field = array(
  32. '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'
  33. );
  34. const GOODS_NORMAL_STATUS = 0; //商品状态 0正常 1已删除 2下架 3申请上架
  35. /**
  36. * 获取关联到产品的分类
  37. */
  38. public function goodstype()
  39. {
  40. return $this->belongsTo(GoodsType::class, 'typeid', 'id');
  41. }
  42. public function getDb()
  43. {
  44. return DB::table($this->table);
  45. }
  46. /**
  47. * 列表
  48. * @param array $where 查询条件
  49. * @param string $order 排序
  50. * @param string $field 字段
  51. * @param int $offset 偏移量
  52. * @param int $limit 取多少条
  53. * @return array
  54. */
  55. public function getList($where = array(), $order = '', $field = '*', $offset = 0, $limit = 15)
  56. {
  57. $model = $this->getDb();
  58. if($where){$model = $model->where($where);}
  59. $res['count'] = $model->count();
  60. $res['list'] = array();
  61. if($res['count'] > 0)
  62. {
  63. if($field){if(is_array($field)){$model = $model->select($field);}else{$model = $model->select(\DB::raw($field));}}
  64. if($order){$model = parent::getOrderByData($model, $order);}
  65. if($offset){}else{$offset = 0;}
  66. if($limit){}else{$limit = 15;}
  67. $res['list'] = $model->skip($offset)->take($limit)->get();
  68. }
  69. //return $model->toSql();//打印sql语句
  70. return $res;
  71. }
  72. /**
  73. * 分页,用于前端html输出
  74. * @param array $where 查询条件
  75. * @param string $order 排序
  76. * @param string $field 字段
  77. * @param int $limit 每页几条
  78. * @param int $page 当前第几页
  79. * @return array
  80. */
  81. public function getPaginate($where = array(), $order = '', $field = '*', $limit = 15)
  82. {
  83. $res = $this->getDb();
  84. if($where){$res = $res->where($where);}
  85. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  86. if($order){$res = parent::getOrderByData($res, $order);}
  87. if($limit){}else{$limit = 15;}
  88. return $res->paginate($limit);
  89. }
  90. /**
  91. * 查询全部
  92. * @param array $where 查询条件
  93. * @param string $order 排序
  94. * @param string $field 字段
  95. * @param int $limit 取多少条
  96. * @return array
  97. */
  98. public function getAll($where = array(), $order = '', $field = '*', $limit = '', $offset = '')
  99. {
  100. $res = $this->getDb();
  101. if($where){$res = $res->where($where);}
  102. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  103. if($order){$res = parent::getOrderByData($res, $order);}
  104. if($offset){$res = $res->skip($offset);}
  105. if($limit){$res = $res->take($limit);}
  106. $res = $res->get();
  107. return $res;
  108. }
  109. /**
  110. * 获取一条
  111. * @param array $where 条件
  112. * @param string $field 字段
  113. * @return array
  114. */
  115. public function getOne($where, $field = '*')
  116. {
  117. $res = $this->getDb();
  118. if($where){$res = $res->where($where);}
  119. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  120. $res = $res->first();
  121. return $res;
  122. }
  123. /**
  124. * 添加
  125. * @param array $data 数据
  126. * @return int
  127. */
  128. public function add(array $data,$type = 0)
  129. {
  130. if($type==0)
  131. {
  132. // 新增单条数据并返回主键值
  133. return self::insertGetId(parent::filterTableColumn($data,$this->table));
  134. }
  135. elseif($type==1)
  136. {
  137. /**
  138. * 添加单条数据
  139. * $data = ['foo' => 'bar', 'bar' => 'foo'];
  140. * 添加多条数据
  141. * $data = [
  142. * ['foo' => 'bar', 'bar' => 'foo'],
  143. * ['foo' => 'bar1', 'bar' => 'foo1'],
  144. * ['foo' => 'bar2', 'bar' => 'foo2']
  145. * ];
  146. */
  147. return self::insert($data);
  148. }
  149. }
  150. /**
  151. * 修改
  152. * @param array $data 数据
  153. * @param array $where 条件
  154. * @return int
  155. */
  156. public function edit($data, $where = array())
  157. {
  158. $res = $this->getDb();
  159. return $res->where($where)->update(parent::filterTableColumn($data, $this->table));
  160. }
  161. /**
  162. * 删除
  163. * @param array $where 条件
  164. * @return bool
  165. */
  166. public function del($where)
  167. {
  168. $res = $this->getDb();
  169. $res = $res->where($where)->delete();
  170. return $res;
  171. }
  172. /**
  173. * 取得商品最终使用价格
  174. *
  175. * @param string $goods_id 商品编号
  176. * @param string $goods_num 购买数量
  177. *
  178. * @return 商品最终购买价格
  179. */
  180. /* public function get_final_price($goods_id)
  181. {
  182. $final_price = '0'; //商品最终购买价格
  183. $promote_price = '0'; //商品促销价格
  184. $user_price = '0'; //商品会员价格,预留
  185. //取得商品促销价格列表
  186. $goods = Goods::where('id',$goods_id)->where('status', self::GOODS_NORMAL_STATUS)->first(['promote_price','promote_start_date','promote_end_date','price']);
  187. $final_price = $goods->price;
  188. // 计算商品的促销价格
  189. if ($goods->promote_price > 0)
  190. {
  191. $promote_price = $this->bargain_price($goods->promote_price, $goods->promote_start_date, $goods->promote_end_date);
  192. }
  193. else
  194. {
  195. $promote_price = 0;
  196. }
  197. if ($promote_price != 0)
  198. {
  199. $final_price = $promote_price;
  200. }
  201. //返回商品最终购买价格
  202. return $final_price;
  203. } */
  204. /**
  205. * 取得商品最终使用价格
  206. *
  207. * @param string $goods_id 商品编号
  208. * @param string $goods_num 购买数量
  209. *
  210. * @return 商品最终购买价格
  211. */
  212. public function get_goods_final_price($goods)
  213. {
  214. $final_price = '0'; //商品最终购买价格
  215. $promote_price = '0'; //商品促销价格
  216. $user_price = '0'; //商品会员价格,预留
  217. //取得商品促销价格列表
  218. $final_price = $goods->price;
  219. // 计算商品的促销价格
  220. if ($goods->promote_price > 0)
  221. {
  222. $promote_price = $this->bargain_price($goods->promote_price, $goods->promote_start_date, $goods->promote_end_date);
  223. }
  224. else
  225. {
  226. $promote_price = 0;
  227. }
  228. if ($promote_price != 0)
  229. {
  230. $final_price = $promote_price;
  231. }
  232. //返回商品最终购买价格
  233. return $final_price;
  234. }
  235. /**
  236. * 判断某个商品是否正在特价促销期
  237. *
  238. * @access public
  239. * @param float $price 促销价格
  240. * @param string $start 促销开始日期
  241. * @param string $end 促销结束日期
  242. * @return float 如果还在促销期则返回促销价,否则返回0
  243. */
  244. public function bargain_price($price, $start, $end)
  245. {
  246. if ($price <= 0)
  247. {
  248. return 0;
  249. }
  250. else
  251. {
  252. $time = time();
  253. if ($time >= $start && $time <= $end)
  254. {
  255. return $price;
  256. }
  257. else
  258. {
  259. return 0;
  260. }
  261. }
  262. }
  263. /**
  264. * 增加或减少商品库存
  265. *
  266. * @access public
  267. * @param int $id 商品ID
  268. * @param int $type 1增加库存
  269. * @return bool
  270. */
  271. public function changeGoodsStock($where)
  272. {
  273. if(isset($where['type']) && $where['type']==1)
  274. {
  275. //增加库存
  276. return $this->getDb()->where(array('id'=>$where['goods_id']))->increment('goods_number', $where['goods_number']);
  277. }
  278. //减少库存
  279. return $this->getDb()->where(array('id'=>$where['goods_id']))->decrement('goods_number', $where['goods_number']);
  280. }
  281. //获取栏目名称
  282. public function getTypenameAttr($data)
  283. {
  284. return DB::table('goods_type')->where(array('id'=>$data['typeid']))->value('name');
  285. }
  286. /**
  287. * 打印sql
  288. */
  289. public function toSql($where)
  290. {
  291. return $this->getDb()->where($where)->toSql();
  292. }
  293. }