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.

165 lines
4.8 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
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 Log;
  4. use 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. return $res;
  44. }
  45. //全部列表
  46. public function getAll($where = array(), $order = '', $field = '*', $limit = '')
  47. {
  48. $res = $this->getModel()->getAll($where, $order, $field, $limit);
  49. /* if($res)
  50. {
  51. foreach($res as $k=>$v)
  52. {
  53. $res[$k] = $this->getDataView($v);
  54. }
  55. } */
  56. return $res;
  57. }
  58. //详情
  59. public function getOne($where = array(), $field = '*')
  60. {
  61. $res = $this->getModel()->getOne($where, $field);
  62. if(!$res){return false;}
  63. $res = $this->getDataView($res);
  64. return $res;
  65. }
  66. //添加
  67. public function add($data = array(), $type=0)
  68. {
  69. if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  70. $validator = $this->getValidate($data, 'add');
  71. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  72. $comment = $this->getModel()->getOne(['comment_type'=>$data['comment_type'],'id_value'=>$data['id_value'],'user_id'=>$data['user_id'],'order_id'=>$data['order_id']]);
  73. if($comment){return ReturnData::create(ReturnData::FAIL,null,'您已经评论过了');}
  74. $res = $this->getModel()->add($data,$type);
  75. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  76. return ReturnData::create(ReturnData::FAIL);
  77. }
  78. //修改
  79. public function edit($data, $where = array())
  80. {
  81. if(empty($data)){return ReturnData::create(ReturnData::SUCCESS);}
  82. $validator = $this->getValidate($data, 'edit');
  83. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  84. $res = $this->getModel()->edit($data,$where);
  85. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  86. return ReturnData::create(ReturnData::FAIL);
  87. }
  88. //删除
  89. public function del($where)
  90. {
  91. if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  92. $validator = $this->getValidate($where,'del');
  93. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  94. $res = $this->getModel()->del($where);
  95. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  96. return ReturnData::create(ReturnData::FAIL);
  97. }
  98. /**
  99. * 数据获取器
  100. * @param array $data 要转化的数据
  101. * @return array
  102. */
  103. private function getDataView($data = array())
  104. {
  105. return getDataAttr($this->getModel(),$data);
  106. }
  107. /**
  108. * 评价-批量添加
  109. * @return array
  110. */
  111. public function batchAdd($data)
  112. {
  113. if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  114. $order_id = 0;
  115. DB::beginTransaction();
  116. foreach($data as $k=>$v)
  117. {
  118. $res = $this->add($v);
  119. $order_id = $v['order_id'];
  120. if($res['code'] != ReturnData::SUCCESS){DB::rollBack();return $res;}
  121. }
  122. //设为已评价
  123. $data2['is_comment'] = Order::ORDER_IS_COMMENT;
  124. $data2['updated_at'] = time();
  125. if(model('Order')->edit($data2,['id'=>$order_id]))
  126. {
  127. DB::commit();
  128. return ReturnData::create(ReturnData::SUCCESS,null,'评价成功');
  129. }
  130. DB::rollBack();
  131. return ReturnData::create(ReturnData::FAIL);
  132. }
  133. }