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.

243 lines
6.4 KiB

7 years ago
4 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
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 GoodsSearchword extends BaseModel
  6. {
  7. //用户消息
  8. protected $table = 'goods_searchword';
  9. public $timestamps = false;
  10. protected $hidden = array();
  11. protected $guarded = array(); //$guarded包含你不想被赋值的字段数组。
  12. public function getDb()
  13. {
  14. return DB::table($this->table);
  15. }
  16. /**
  17. * 列表
  18. * @param array $where 查询条件
  19. * @param string $order 排序
  20. * @param string $field 字段
  21. * @param int $offset 偏移量
  22. * @param int $limit 取多少条
  23. * @return array
  24. */
  25. public function getList($where = array(), $order = '', $field = '*', $offset = 0, $limit = 15)
  26. {
  27. $model = $this->getDb();
  28. if($where){$model = $model->where($where);}
  29. $res['count'] = $model->count();
  30. $res['list'] = array();
  31. if($res['count'] > 0)
  32. {
  33. if($field){if(is_array($field)){$model = $model->select($field);}else{$model = $model->select(\DB::raw($field));}}
  34. if($order){$model = parent::getOrderByData($model, $order);}
  35. if($offset){}else{$offset = 0;}
  36. if($limit){}else{$limit = 15;}
  37. $res['list'] = $model->skip($offset)->take($limit)->get();
  38. }
  39. return $res;
  40. }
  41. /**
  42. * 分页,用于前端html输出
  43. * @param array $where 查询条件
  44. * @param string $order 排序
  45. * @param string $field 字段
  46. * @param int $limit 每页几条
  47. * @param int $page 当前第几页
  48. * @return array
  49. */
  50. public function getPaginate($where = array(), $order = '', $field = '*', $limit = 15)
  51. {
  52. $res = $this->getDb();
  53. if($where){$res = $res->where($where);}
  54. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  55. if($order){$res = parent::getOrderByData($res, $order);}
  56. if($limit){}else{$limit = 15;}
  57. return $res->paginate($limit);
  58. }
  59. /**
  60. * 查询全部
  61. * @param array $where 查询条件
  62. * @param string $order 排序
  63. * @param string $field 字段
  64. * @param int $limit 取多少条
  65. * @return array
  66. */
  67. public function getAll($where = array(), $order = '', $field = '*', $limit = '', $offset = '')
  68. {
  69. $res = $this->getDb();
  70. if($where){$res = $res->where($where);}
  71. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  72. if($order){$res = parent::getOrderByData($res, $order);}
  73. if($offset){$res = $res->skip($offset);}
  74. if($limit){$res = $res->take($limit);}
  75. $res = $res->get();
  76. return $res;
  77. }
  78. /**
  79. * 获取一条
  80. * @param array $where 条件
  81. * @param string $field 字段
  82. * @return array
  83. */
  84. public function getOne($where, $field = '*')
  85. {
  86. $res = $this->getDb();
  87. if($where){$res = $res->where($where);}
  88. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  89. $res = $res->first();
  90. return $res;
  91. }
  92. /**
  93. * 添加
  94. * @param array $data 数据
  95. * @return int
  96. */
  97. public function add(array $data,$type = 0)
  98. {
  99. if($type==0)
  100. {
  101. // 新增单条数据并返回主键值
  102. return self::insertGetId(parent::filterTableColumn($data,$this->table));
  103. }
  104. elseif($type==1)
  105. {
  106. /**
  107. * 添加单条数据
  108. * $data = ['foo' => 'bar', 'bar' => 'foo'];
  109. * 添加多条数据
  110. * $data = [
  111. * ['foo' => 'bar', 'bar' => 'foo'],
  112. * ['foo' => 'bar1', 'bar' => 'foo1'],
  113. * ['foo' => 'bar2', 'bar' => 'foo2']
  114. * ];
  115. */
  116. return self::insert($data);
  117. }
  118. }
  119. /**
  120. * 修改
  121. * @param array $data 数据
  122. * @param array $where 条件
  123. * @return int
  124. */
  125. public function edit($data, $where = array())
  126. {
  127. $res = $this->getDb();
  128. return $res->where($where)->update(parent::filterTableColumn($data, $this->table));
  129. }
  130. /**
  131. * 删除
  132. * @param array $where 条件
  133. * @return bool
  134. */
  135. public function del($where)
  136. {
  137. $res = $this->getDb();
  138. $res = $res->where($where)->delete();
  139. return $res;
  140. }
  141. /*
  142. //获取列表
  143. public static function getList(array $param)
  144. {
  145. extract($param); //参数:limit,offset
  146. $limit = isset($limit) ? $limit : 10;
  147. $offset = isset($offset) ? $offset : 0;
  148. $model = new self;
  149. $where['status'] = 0;
  150. $model = $model->where($where);
  151. $res['count'] = $model->count();
  152. $res['list'] = array();
  153. if($res['count']>0)
  154. {
  155. $res['list'] = $model->skip($offset)->take($limit)->orderBy('click','desc')->orderBy('listorder','asc')->get();
  156. }
  157. else
  158. {
  159. return false;
  160. }
  161. return $res;
  162. }
  163. public static function getOne($where)
  164. {
  165. return self::where($where)->first();
  166. }
  167. public static function add(array $data)
  168. {
  169. //如果关键词存在,就增加点击量
  170. if(isset($data['name']))
  171. {
  172. if(self::getOne(array('name'=>$data['name'])))
  173. {
  174. \DB::table('goods_searchword')->where(array('name'=>$data['name']))->increment('click', 1);
  175. }
  176. else
  177. {
  178. if ($id = self::insertGetId($data))
  179. {
  180. return $id;
  181. }
  182. }
  183. }
  184. else
  185. {
  186. return false;
  187. }
  188. return false;
  189. }
  190. public static function modify($where, array $data)
  191. {
  192. if (self::where($where)->update($data))
  193. {
  194. return true;
  195. }
  196. return false;
  197. }
  198. //删除一条记录
  199. public static function remove($id)
  200. {
  201. if (!self::whereIn('id', explode(',', $id))->delete())
  202. {
  203. return false;
  204. }
  205. return true;
  206. } */
  207. }