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.

230 lines
6.6 KiB

7 years ago
4 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 Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Facades\Log;
  5. class Comment extends BaseModel
  6. {
  7. //评价
  8. protected $table = 'comment';
  9. public $timestamps = false;
  10. protected $hidden = array();
  11. protected $guarded = array(); //$guarded包含你不想被赋值的字段数组。
  12. const UNSHOW_COMMENT = 0; //评论未批准显示
  13. const SHOW_COMMENT = 1; //评论批准显示
  14. const GOODS_COMMENT_TYPE = 0; //商品评论
  15. const ARTICLE_COMMENT_TYPE = 1; //文章评论
  16. public function getDb()
  17. {
  18. return DB::table($this->table);
  19. }
  20. /**
  21. * 列表
  22. * @param array $where 查询条件
  23. * @param string $order 排序
  24. * @param string $field 字段
  25. * @param int $offset 偏移量
  26. * @param int $limit 取多少条
  27. * @return array
  28. */
  29. public function getList($where = array(), $order = '', $field = '*', $offset = 0, $limit = 15)
  30. {
  31. $model = $this->getDb();
  32. if($where){$model = $model->where($where);}
  33. $res['count'] = $model->count();
  34. $res['list'] = array();
  35. if($res['count'] > 0)
  36. {
  37. if($field){if(is_array($field)){$model = $model->select($field);}else{$model = $model->select(\DB::raw($field));}}
  38. if($order){$model = parent::getOrderByData($model, $order);}
  39. if($offset){}else{$offset = 0;}
  40. if($limit){}else{$limit = 15;}
  41. $res['list'] = $model->skip($offset)->take($limit)->get();
  42. }
  43. return $res;
  44. }
  45. /**
  46. * 分页,用于前端html输出
  47. * @param array $where 查询条件
  48. * @param string $order 排序
  49. * @param string $field 字段
  50. * @param int $limit 每页几条
  51. * @param int $page 当前第几页
  52. * @return array
  53. */
  54. public function getPaginate($where = array(), $order = '', $field = '*', $limit = 15)
  55. {
  56. $res = $this->getDb();
  57. if($where){$res = $res->where($where);}
  58. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  59. if($order){$res = parent::getOrderByData($res, $order);}
  60. if($limit){}else{$limit = 15;}
  61. return $res->paginate($limit);
  62. }
  63. /**
  64. * 查询全部
  65. * @param array $where 查询条件
  66. * @param string $order 排序
  67. * @param string $field 字段
  68. * @param int $limit 取多少条
  69. * @return array
  70. */
  71. public function getAll($where = array(), $order = '', $field = '*', $limit = '', $offset = '')
  72. {
  73. $res = $this->getDb();
  74. if($where){$res = $res->where($where);}
  75. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  76. if($order){$res = parent::getOrderByData($res, $order);}
  77. if($offset){$res = $res->skip($offset);}
  78. if($limit){$res = $res->take($limit);}
  79. $res = $res->get();
  80. return $res;
  81. }
  82. /**
  83. * 获取一条
  84. * @param array $where 条件
  85. * @param string $field 字段
  86. * @return array
  87. */
  88. public function getOne($where, $field = '*')
  89. {
  90. $res = $this->getDb();
  91. if($where){$res = $res->where($where);}
  92. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  93. $res = $res->first();
  94. return $res;
  95. }
  96. /**
  97. * 添加
  98. * @param array $data 数据
  99. * @return int
  100. */
  101. public function add(array $data,$type = 0)
  102. {
  103. if($type==0)
  104. {
  105. // 新增单条数据并返回主键值
  106. return self::insertGetId(parent::filterTableColumn($data,$this->table));
  107. }
  108. elseif($type==1)
  109. {
  110. /**
  111. * 添加单条数据
  112. * $data = ['foo' => 'bar', 'bar' => 'foo'];
  113. * 添加多条数据
  114. * $data = [
  115. * ['foo' => 'bar', 'bar' => 'foo'],
  116. * ['foo' => 'bar1', 'bar' => 'foo1'],
  117. * ['foo' => 'bar2', 'bar' => 'foo2']
  118. * ];
  119. */
  120. return self::insert($data);
  121. }
  122. }
  123. /**
  124. * 修改
  125. * @param array $data 数据
  126. * @param array $where 条件
  127. * @return int
  128. */
  129. public function edit($data, $where = array())
  130. {
  131. $res = $this->getDb();
  132. return $res->where($where)->update(parent::filterTableColumn($data, $this->table));
  133. }
  134. /**
  135. * 删除
  136. * @param array $where 条件
  137. * @return bool
  138. */
  139. public function del($where)
  140. {
  141. $res = $this->getDb();
  142. $res = $res->where($where)->delete();
  143. return $res;
  144. }
  145. /**
  146. * 统计
  147. * @param array $where 条件
  148. * @return bool
  149. */
  150. public function getCount($where)
  151. {
  152. $res = $this->getDb();
  153. $res = $res->where($where)->count();
  154. return $res;
  155. }
  156. /*
  157. public static function add(array $data)
  158. {
  159. if(!isset($data['comment_type']) || !isset($data['id_value']) || !isset($data['user_id']) || $data['comment_type']===null || $data['id_value']===null || $data['user_id']===null)
  160. {
  161. return ReturnData::create(ReturnData::PARAMS_ERROR);
  162. }
  163. if(!isset($data['content']) && !isset($data['comment_rank']))
  164. {
  165. return ReturnData::create(ReturnData::PARAMS_ERROR);
  166. }
  167. else
  168. {
  169. if($data['content']===null && $data['comment_rank']===null)
  170. {
  171. return ReturnData::create(ReturnData::PARAMS_ERROR);
  172. }
  173. }
  174. if(self::where(array('user_id'=>$data['user_id'],'id_value'=>$data['id_value'],'comment_type'=>$data['comment_type']))->first()){return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'亲,您已经评价啦!');}
  175. if ($id = self::insertGetId($data))
  176. {
  177. return ReturnData::create(ReturnData::SUCCESS,$id);
  178. }
  179. return ReturnData::create(ReturnData::SYSTEM_FAIL);
  180. }
  181. //批量添加
  182. public static function batchAdd(array $data)
  183. {
  184. $res = '';
  185. if($data)
  186. {
  187. foreach($data as $k=>$v)
  188. {
  189. $id = self::add($v);
  190. if($id['code']==0){$res[] = $id['data'];}else{return $id;}
  191. }
  192. return ReturnData::create(ReturnData::SUCCESS,$res);
  193. }
  194. return ReturnData::create(ReturnData::SYSTEM_FAIL);
  195. }*/
  196. }