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.

147 lines
4.3 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
  1. <?php
  2. namespace App\Http\Logic;
  3. use App\Common\ReturnData;
  4. use App\Http\Model\Comment;
  5. use App\Http\Requests\CommentRequest;
  6. use Validator;
  7. class CommentLogic extends BaseLogic
  8. {
  9. public function __construct()
  10. {
  11. parent::__construct();
  12. }
  13. public function getModel()
  14. {
  15. return new Comment();
  16. }
  17. public function getValidate($data, $scene_name)
  18. {
  19. //数据验证
  20. $validate = new CommentRequest();
  21. return Validator::make($data, $validate->getSceneRules($scene_name), $validate->getSceneRulesMessages());
  22. }
  23. //列表
  24. public function getList($where = array(), $order = '', $field = '*', $offset = '', $limit = '')
  25. {
  26. $res = $this->getModel()->getList($where, $order, $field, $offset, $limit);
  27. if($res['count'] > 0)
  28. {
  29. foreach($res['list'] as $k=>$v)
  30. {
  31. $res['list'][$k] = $this->getDataView($v);
  32. }
  33. }
  34. return $res;
  35. }
  36. //分页html
  37. public function getPaginate($where = array(), $order = '', $field = '*', $limit = '')
  38. {
  39. $res = $this->getModel()->getPaginate($where, $order, $field, $limit);
  40. return $res;
  41. }
  42. //全部列表
  43. public function getAll($where = array(), $order = '', $field = '*', $limit = '')
  44. {
  45. $res = $this->getModel()->getAll($where, $order, $field, $limit);
  46. /* if($res)
  47. {
  48. foreach($res as $k=>$v)
  49. {
  50. $res[$k] = $this->getDataView($v);
  51. }
  52. } */
  53. return $res;
  54. }
  55. //详情
  56. public function getOne($where = array(), $field = '*')
  57. {
  58. $res = $this->getModel()->getOne($where, $field);
  59. if(!$res){return false;}
  60. $res = $this->getDataView($res);
  61. return $res;
  62. }
  63. //添加
  64. public function add($data = array(), $type=0)
  65. {
  66. if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  67. $validator = $this->getValidate($data, 'add');
  68. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  69. $comment = $this->getModel()->getOne(['comment_type'=>$data['comment_type'],'id_value'=>$data['id_value'],'user_id'=>$data['user_id']]);
  70. if($comment){return ReturnData::create(ReturnData::FAIL,null,'您已经评论过了');}
  71. $res = $this->getModel()->add($data,$type);
  72. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  73. return ReturnData::create(ReturnData::FAIL);
  74. }
  75. //修改
  76. public function edit($data, $where = array())
  77. {
  78. if(empty($data)){return ReturnData::create(ReturnData::SUCCESS);}
  79. $validator = $this->getValidate($data, 'edit');
  80. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  81. $res = $this->getModel()->edit($data,$where);
  82. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  83. return ReturnData::create(ReturnData::FAIL);
  84. }
  85. //删除
  86. public function del($where)
  87. {
  88. if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  89. $validator = $this->getValidate($where,'del');
  90. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  91. $res = $this->getModel()->del($where);
  92. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  93. return ReturnData::create(ReturnData::FAIL);
  94. }
  95. /**
  96. * 数据获取器
  97. * @param array $data 要转化的数据
  98. * @return array
  99. */
  100. private function getDataView($data = array())
  101. {
  102. return getDataAttr($this->getModel(),$data);
  103. }
  104. //批量添加
  105. public function batchAdd(array $data)
  106. {
  107. if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  108. DB::beginTransaction();
  109. foreach($data as $k=>$v)
  110. {
  111. $res = $this->add($v);
  112. if($res['code'] == ReturnData::SUCCESS){}else{DB::rollBack();return $res;}
  113. }
  114. DB::commit();
  115. return ReturnData::create(ReturnData::SUCCESS);
  116. }
  117. }