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.1 KiB

7 years ago
4 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 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 Illuminate\Support\Facades\DB;
  4. use App\Common\ReturnData;
  5. use App\Http\Model\UserPoint;
  6. use App\Http\Requests\UserPointRequest;
  7. use Validator;
  8. class UserPointLogic extends BaseLogic
  9. {
  10. public function __construct()
  11. {
  12. parent::__construct();
  13. }
  14. public function getModel()
  15. {
  16. return new UserPoint();
  17. }
  18. public function getValidate($data, $scene_name)
  19. {
  20. //数据验证
  21. $validate = new UserPointRequest();
  22. return Validator::make($data, $validate->getSceneRules($scene_name), $validate->getSceneRulesMessages());
  23. }
  24. //列表
  25. public function getList($where = array(), $order = '', $field = '*', $offset = '', $limit = '')
  26. {
  27. $res = $this->getModel()->getList($where, $order, $field, $offset, $limit);
  28. if($res['count'] > 0)
  29. {
  30. foreach($res['list'] as $k=>$v)
  31. {
  32. $res['list'][$k] = $this->getDataView($v);
  33. }
  34. }
  35. return $res;
  36. }
  37. //分页html
  38. public function getPaginate($where = array(), $order = '', $field = '*', $limit = '')
  39. {
  40. $res = $this->getModel()->getPaginate($where, $order, $field, $limit);
  41. if($res->count() > 0)
  42. {
  43. foreach($res as $k=>$v)
  44. {
  45. $res[$k] = $this->getDataView($v);
  46. }
  47. }
  48. return $res;
  49. }
  50. //全部列表
  51. public function getAll($where = array(), $order = '', $field = '*', $limit = '')
  52. {
  53. $res = $this->getModel()->getAll($where, $order, $field, $limit);
  54. if($res)
  55. {
  56. foreach($res as $k=>$v)
  57. {
  58. $res[$k] = $this->getDataView($v);
  59. }
  60. }
  61. return $res;
  62. }
  63. //详情
  64. public function getOne($where = array(), $field = '*')
  65. {
  66. $res = $this->getModel()->getOne($where, $field);
  67. if(!$res){return false;}
  68. $res = $this->getDataView($res);
  69. return $res;
  70. }
  71. /**
  72. * 添加一条记录,并增加或减少用户余额,会操作用户积分,谨慎使用
  73. * @param int $data['user_id'] 排序
  74. * @param int $data['type'] 0增加,1减少
  75. * @param float $data['point'] 积分
  76. * @param string $data['des'] 描述
  77. * @return array
  78. */
  79. public function add($data = array(), $type=0)
  80. {
  81. if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  82. $validator = $this->getValidate($data, 'add');
  83. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  84. if($data['point']<=0){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  85. $data['add_time'] = time();
  86. DB::beginTransaction(); //启动事务
  87. if($data['type'] == UserPoint::USER_POINT_INCREMENT)
  88. {
  89. //增加用户余额
  90. model('User')->getDb()->where(array('id'=>$data['user_id']))->increment('point', $data['point']);
  91. }
  92. elseif($data['type'] == UserPoint::USER_POINT_DECREMENT)
  93. {
  94. //减少用户余额
  95. model('User')->getDb()->where(array('id'=>$data['user_id']))->decrement('point', $data['point']);
  96. }
  97. else
  98. {
  99. DB::rollBack(); //事务回滚
  100. return ReturnData::create(ReturnData::FAIL);
  101. }
  102. $user_point = model('User')->getDb()->where(array('id'=>$data['user_id']))->value('point'); //用户余额
  103. $data['user_point'] = $user_point;
  104. $res = $this->getModel()->add($data,$type);
  105. if($res)
  106. {
  107. DB::commit(); //事务提交
  108. return ReturnData::create(ReturnData::SUCCESS,$res);
  109. }
  110. return ReturnData::create(ReturnData::FAIL);
  111. }
  112. //修改
  113. public function edit($data, $where = array())
  114. {
  115. if(empty($data)){return ReturnData::create(ReturnData::SUCCESS);}
  116. $validator = $this->getValidate($data, 'edit');
  117. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  118. $res = $this->getModel()->edit($data,$where);
  119. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  120. return ReturnData::create(ReturnData::FAIL);
  121. }
  122. //删除
  123. public function del($where)
  124. {
  125. if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  126. $validator = $this->getValidate($where,'del');
  127. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  128. $res = $this->getModel()->del($where);
  129. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  130. return ReturnData::create(ReturnData::FAIL);
  131. }
  132. /**
  133. * 数据获取器
  134. * @param array $data 要转化的数据
  135. * @return array
  136. */
  137. private function getDataView($data = array())
  138. {
  139. return getDataAttr($this->getModel(),$data);
  140. }
  141. }