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.

173 lines
5.0 KiB

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