Users.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace app\common\model;
  3. use app\common\model\UsersBalanceRecord;
  4. class Users extends BaseModel
  5. {
  6. /**
  7. * @var string
  8. */
  9. protected $name = 'users';
  10. /**
  11. * @var array
  12. */
  13. protected $auto = [];
  14. /**
  15. * @var array
  16. */
  17. protected $insert = ['created_at','updated_at'];
  18. /**
  19. * @var array
  20. */
  21. protected $update = ['updated_at'];
  22. /**
  23. * @var array
  24. */
  25. protected $hidden = ['updated_at'];
  26. /**
  27. * 自关联推荐用户
  28. * @return \think\model\relation\BelongsTo
  29. */
  30. public function recommend()
  31. {
  32. return $this->belongsTo(self::class,'parent_id','id');
  33. }
  34. /**
  35. * 用户资金变动
  36. *
  37. * @author 许祖兴 < zuxing.xu@lettered.cn>
  38. * @date 2020/7/14 10:34
  39. *
  40. * @param int $userId 用户ID
  41. * @param float $amount 变动金额
  42. * @param string $remark 备注说明
  43. * @param bool $inc 是否增长
  44. * @throws \think\exception\PDOException
  45. */
  46. public function changeBalance($userId,$amount, $remark = "", $inc = false)
  47. {
  48. $action = $inc ? 'setInc' : 'setDec';
  49. $this->startTrans();
  50. try {
  51. // 查余额
  52. $user = $this->lock(true)->where(['id' => $userId])->find();
  53. // 资金变动
  54. $this->where(['id' => $userId])->{$action}('balance',$amount);
  55. // 记录写入
  56. $data = [
  57. 'user_id' => $userId,
  58. 'action' => $inc ? 1 : 0,
  59. 'remark' => $remark
  60. ];
  61. // 加减
  62. if ($inc){
  63. $data['inc_amount'] = $amount;
  64. $data['aft_amount'] = sprintf("%.2f", round($user['balance'] + $amount, 2));
  65. }else{
  66. $data['dec_amount'] = $amount;
  67. $data['aft_amount'] = sprintf("%.2f", round($user['balance'] - $amount, 2));
  68. }
  69. UsersBalanceRecord::create($data,true);
  70. $this->commit();
  71. }catch (\Exception $e){
  72. $this->rollback();
  73. }
  74. }
  75. /**
  76. * 用户资产变动
  77. *
  78. * @author 许祖兴 < zuxing.xu@lettered.cn>
  79. * @date 2020/7/14 10:34
  80. *
  81. * @param int $userId 用户ID
  82. * @param float $amount 变动金额
  83. * @param string $remark 备注说明
  84. * @param bool $inc 是否增长
  85. * @throws \think\exception\PDOException
  86. */
  87. public function changeProperty($userId,$amount, $remark = "", $inc = false)
  88. {
  89. $action = $inc ? 'setInc' : 'setDec';
  90. $this->startTrans();
  91. try {
  92. // 查余额
  93. $user = $this->lock(true)->where(['id' => $userId])->find();
  94. // 资金变动
  95. $this->where(['id' => $userId])->{$action}('property',$amount);
  96. // 记录写入
  97. $data = [
  98. 'user_id' => $userId,
  99. 'action' => $inc ? 1 : 0,
  100. 'remark' => $remark
  101. ];
  102. // 加减
  103. if ($inc){
  104. $data['inc_amount'] = $amount;
  105. $data['aft_amount'] = sprintf("%.3f", round($user['property'] + $amount, 3));
  106. }else{
  107. $data['dec_amount'] = $amount;
  108. $data['aft_amount'] = sprintf("%.3f", round($user['property'] - $amount, 3));
  109. }
  110. UsersPropertyRecord::create($data,true);
  111. $this->commit();
  112. }catch (\Exception $e){
  113. $this->rollback();
  114. }
  115. }
  116. }