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.

180 lines
5.5 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
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\UserMoney;
  6. use App\Http\Requests\UserMoneyRequest;
  7. use Validator;
  8. class UserMoneyLogic extends BaseLogic
  9. {
  10. public function __construct()
  11. {
  12. parent::__construct();
  13. }
  14. public function getModel()
  15. {
  16. return new UserMoney();
  17. }
  18. public function getValidate($data, $scene_name)
  19. {
  20. //数据验证
  21. $validate = new UserMoneyRequest();
  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'] 用户id
  74. * @param int $data['type'] 0增加,1减少
  75. * @param float $data['money'] 金额
  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['money']<=0){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  85. $user = model('User')->getOne(['id'=>$data['user_id']]);
  86. if(!$user){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'用户不存在');}
  87. $data['add_time'] = time();
  88. DB::beginTransaction(); //启动事务
  89. if($data['type'] == UserMoney::USER_MONEY_INCREMENT)
  90. {
  91. //增加用户余额
  92. model('User')->getDb()->where(array('id'=>$data['user_id']))->increment('money', $data['money']);
  93. }
  94. elseif($data['type'] == UserMoney::USER_MONEY_DECREMENT)
  95. {
  96. //判断用户余额是否足够
  97. if($data['money']>$user->money){return ReturnData::create(ReturnData::FAIL,null,'余额不足');}
  98. //减少用户余额
  99. model('User')->getDb()->where(array('id'=>$data['user_id']))->decrement('money', $data['money']);
  100. }
  101. else
  102. {
  103. DB::rollBack(); //事务回滚
  104. return ReturnData::create(ReturnData::FAIL);
  105. }
  106. $user_money = model('User')->getDb()->where(array('id'=>$data['user_id']))->value('money'); //用户余额
  107. $data['user_money'] = $user_money;
  108. $res = $this->getModel()->add($data, $type);
  109. if($res)
  110. {
  111. DB::commit(); //事务提交
  112. return ReturnData::create(ReturnData::SUCCESS,$res);
  113. }
  114. DB::rollBack(); //事务回滚
  115. return ReturnData::create(ReturnData::FAIL);
  116. }
  117. //修改
  118. public function edit($data, $where = array())
  119. {
  120. if(empty($data)){return ReturnData::create(ReturnData::SUCCESS);}
  121. $validator = $this->getValidate($data, 'edit');
  122. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  123. $res = $this->getModel()->edit($data,$where);
  124. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  125. return ReturnData::create(ReturnData::FAIL);
  126. }
  127. //删除
  128. public function del($where)
  129. {
  130. if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  131. $validator = $this->getValidate($where,'del');
  132. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  133. $res = $this->getModel()->del($where);
  134. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  135. return ReturnData::create(ReturnData::FAIL);
  136. }
  137. /**
  138. * 数据获取器
  139. * @param array $data 要转化的数据
  140. * @return array
  141. */
  142. private function getDataView($data = array())
  143. {
  144. return getDataAttr($this->getModel(),$data);
  145. }
  146. }