Users.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace app\common\model;
  3. use app\api\model\taxi\MotorRecord;
  4. use app\common\model\UsersBalanceRecord;
  5. class Users extends BaseModel
  6. {
  7. /**
  8. * @var string
  9. */
  10. protected $name = 'users';
  11. /**
  12. * @var array
  13. */
  14. protected $auto = [];
  15. /**
  16. * @var array
  17. */
  18. protected $insert = ['created_at','updated_at'];
  19. /**
  20. * @var array
  21. */
  22. protected $update = ['updated_at'];
  23. /**
  24. * @var array
  25. */
  26. protected $hidden = ['updated_at'];
  27. /**
  28. * 自关联推荐用户
  29. * @return \think\model\relation\BelongsTo
  30. */
  31. public function recommend()
  32. {
  33. return $this->belongsTo(self::class,'parent_id','id');
  34. }
  35. /**
  36. * 用户资金变动
  37. *
  38. * @author 许祖兴 < zuxing.xu@lettered.cn>
  39. * @date 2020/7/14 10:34
  40. *
  41. * @param int $userId 用户ID
  42. * @param float $amount 变动金额
  43. * @param string $remark 备注说明
  44. * @param bool $inc 是否增长
  45. * @throws \think\exception\PDOException
  46. */
  47. public function changeBalance($userId,$amount, $remark = "", $inc = false)
  48. {
  49. $action = $inc ? 'setInc' : 'setDec';
  50. $this->startTrans();
  51. try {
  52. // 查余额
  53. $user = $this->lock(true)->where(['id' => $userId])->find();
  54. // 资金变动
  55. $this->where(['id' => $userId])->{$action}('balance',$amount);
  56. // 记录写入
  57. $data = [
  58. 'user_id' => $userId,
  59. 'action' => $inc ? 1 : 0,
  60. 'remark' => $remark
  61. ];
  62. // 加减
  63. if ($inc){
  64. $data['inc_amount'] = $amount;
  65. $data['aft_amount'] = sprintf("%.2f", round($user['balance'] + $amount, 2));
  66. }else{
  67. $data['dec_amount'] = $amount;
  68. $data['aft_amount'] = sprintf("%.2f", round($user['balance'] - $amount, 2));
  69. }
  70. UsersBalanceRecord::create($data,true);
  71. $this->commit();
  72. }catch (\Exception $e){
  73. $this->rollback();
  74. }
  75. }
  76. /**
  77. * 用户资产变动
  78. *
  79. * @author 许祖兴 < zuxing.xu@lettered.cn>
  80. * @date 2020/7/14 10:34
  81. *
  82. * @param int $userId 用户ID
  83. * @param float $amount 变动金额
  84. * @param string $remark 备注说明
  85. * @param bool $inc 是否增长
  86. * @throws \think\exception\PDOException
  87. */
  88. public function changeProperty($userId,$amount, $remark = "", $inc = false)
  89. {
  90. $action = $inc ? 'setInc' : 'setDec';
  91. $this->startTrans();
  92. try {
  93. // 查余额
  94. $user = $this->lock(true)->where(['id' => $userId])->find();
  95. // 资金变动
  96. $this->where(['id' => $userId])->{$action}('property',$amount);
  97. // 记录写入
  98. $data = [
  99. 'user_id' => $userId,
  100. 'action' => $inc ? 1 : 0,
  101. 'remark' => $remark
  102. ];
  103. // 加减
  104. if ($inc){
  105. $data['inc_amount'] = $amount;
  106. $data['aft_amount'] = sprintf("%.3f", round($user['property'] + $amount, 3));
  107. }else{
  108. $data['dec_amount'] = $amount;
  109. $data['aft_amount'] = sprintf("%.3f", round($user['property'] - $amount, 3));
  110. }
  111. UsersPropertyRecord::create($data,true);
  112. $this->commit();
  113. }catch (\Exception $e){
  114. $this->rollback();
  115. }
  116. }
  117. /**
  118. * 用户资金变动
  119. *
  120. * @author 许祖兴 < zuxing.xu@lettered.cn>
  121. * @date 2020/7/14 10:34
  122. *
  123. * @param int $userId 用户ID
  124. * @param float $amount 变动金额
  125. * @param string $remark 备注说明
  126. * @param string $type 类型:10-分红,20-提现,30-资产司机收入
  127. * @param bool $inc 是否增长
  128. * @throws \think\exception\PDOException
  129. */
  130. public function changePartnership($userId,$amount, $remark = "", $type=30, $inc = false)
  131. {
  132. $action = $inc ? 'setInc' : 'setDec';
  133. $this->startTrans();
  134. try {
  135. // 查余额
  136. $user = $this->lock(true)->where(['id' => $userId])->find();
  137. // 资金变动
  138. $this->where(['id' => $userId])->{$action}('partnership',$amount);
  139. // 记录写入
  140. $data = [
  141. 'type' => $type,
  142. 'money' => $amount,
  143. 'user_id' => $userId,
  144. 'tag' => $inc ? 10 : 20,
  145. 'created_at' => time(),
  146. 'updated_at' => time(),
  147. 'memo' => $remark
  148. ];
  149. // 加减
  150. if ($inc){
  151. $data['before'] = $user['partnership'];
  152. $data['after'] = sprintf("%.2f", round($user['partnership'] + $amount, 2));
  153. }else{
  154. $data['before'] = $user['partnership'];
  155. $data['after'] = sprintf("%.2f", round($user['partnership'] - $amount, 2));
  156. }
  157. MotorRecord::create($data,true);
  158. $this->commit();
  159. return true;
  160. }catch (\Exception $e){
  161. $this->rollback();
  162. return false;
  163. }
  164. }
  165. }