SystemMoney.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace app\models\system;
  3. use app\models\user\User;
  4. use app\models\user\UserBill;
  5. use app\models\user\UserMoney;
  6. use crmeb\traits\ModelTrait;
  7. use crmeb\basic\BaseModel;
  8. use think\db\exception\DataNotFoundException;
  9. use think\db\exception\DbException;
  10. use think\db\exception\ModelNotFoundException;
  11. use think\Model;
  12. class SystemMoney extends BaseModel
  13. {
  14. /**
  15. * 数据表主键
  16. * @var string
  17. */
  18. protected $pk = 'id';
  19. /**
  20. * 模型名称
  21. * @var string
  22. */
  23. protected $name = 'system_money';
  24. use ModelTrait;
  25. /**
  26. * @param $uid
  27. * @param $money_type
  28. * @param int $money
  29. * @return array|Model|null
  30. * @throws DataNotFoundException
  31. * @throws DbException
  32. * @throws ModelNotFoundException
  33. */
  34. public static function initialMoney($site_id, $money_type, $money = 0)
  35. {
  36. $where = compact('site_id', 'money_type');
  37. $data = self::where($where)->find();
  38. if ($data) {
  39. return self::where($where)->find();
  40. } else {
  41. return self::create(array_merge($where, ['money' => $money]));
  42. }
  43. }
  44. /**
  45. * @param $uid
  46. * @param $to_uid
  47. * @param $money_type
  48. * @param $money
  49. * @return bool
  50. * @throws DataNotFoundException
  51. * @throws DbException
  52. * @throws ModelNotFoundException
  53. */
  54. public static function tradeMoney($uid, $to_uid, $money_type, $money)
  55. {
  56. $user = self::initialMoney($uid, $money_type);
  57. $user_info = Site::get($uid);
  58. $to_user = UserMoney::initialUserMoney($to_uid, $money_type);
  59. $to_user_info = User::getUserInfo($to_uid);
  60. if ($user['money'] < $money) {
  61. return self::setErrorInfo('奖池余额不足');
  62. }
  63. $balance = bcsub($user['money'], $money, 8);
  64. $to_balance = bcadd($to_user['money'], $money, 8);
  65. $mark = '转至' . $to_user_info['nickname'] . "({$to_user_info['uid']})" . $money . ' ' . $money_type;
  66. $to_mark = '收到' . $user_info['name'] . "平台奖池转入" . $money . ' ' . $money_type;
  67. self::beginTrans();
  68. try {
  69. $res2 = SystemBill::expend('奖池转出', $uid, $money_type, 'expend', $money, 0, $balance, $mark, 1);
  70. $res3 = UserBill::income('奖池转入', $to_uid, $money_type, 'system_income', $money, $res2->id, $to_balance, $to_mark, 1);
  71. $res4 = self::where('site_id', $uid)->where('money_type', $money_type)->update(['money' => $balance]);
  72. $res5 = UserMoney::where('uid', $to_uid)->where('money_type', $money_type)->update(['money' => $to_balance]);
  73. $res = $res2 && $res3 && $res4 && $res5;
  74. if ($res) {
  75. self::commitTrans();
  76. return true;
  77. } else {
  78. self::rollbackTrans();
  79. return self::setErrorInfo('转账失败');
  80. }
  81. } catch (\Exception $e) {
  82. self::rollbackTrans();
  83. return self::setErrorInfo($e->getMessage());
  84. }
  85. }
  86. /**
  87. * @param $uid
  88. * @param $money_type
  89. * @param $money
  90. * @param $type
  91. * @param string $title
  92. * @param string $mark
  93. * @param int $from_vote
  94. * @param int $from_sub_vote
  95. * @return bool
  96. * @throws DataNotFoundException
  97. * @throws DbException
  98. * @throws ModelNotFoundException
  99. */
  100. public static function expendMoney($uid, $money_type, $money, $type, $title = '奖池转出', $mark = '', $link_id = 0)
  101. {
  102. $user = self::initialMoney($uid, $money_type);
  103. if ($user['money'] < $money) {
  104. return self::setErrorInfo('余额不足');
  105. }
  106. $balance = bcsub($user['money'], $money, 8);
  107. try {
  108. $res2 = SystemBill::expend($title, $uid, $money_type, $type, $money, $link_id, $balance, $mark, 1);
  109. $res4 = self::where('site_uid', $uid)->where('money_type', $money_type)->update(['money' => $balance]);
  110. $res = $res2 && $res4;
  111. if ($res) {
  112. return true;
  113. } else {
  114. return self::setErrorInfo('交易失败');
  115. }
  116. } catch (\Exception $e) {
  117. return self::setErrorInfo($e->getMessage());
  118. }
  119. }
  120. /**
  121. * @param $uid
  122. * @param $money_type
  123. * @param $money
  124. * @param $type
  125. * @param string $title
  126. * @param string $mark
  127. * @param int $from_vote
  128. * @param int $from_sub_vote
  129. * @return bool
  130. * @throws DataNotFoundException
  131. * @throws DbException
  132. * @throws ModelNotFoundException
  133. */
  134. public static function incomeMoney($uid, $money_type, $money, $type, $title = '奖池转入', $mark = '', $link_id = 0)
  135. {
  136. $user = self::initialMoney($uid, $money_type);
  137. $balance = bcadd($user['money'], $money, 8);
  138. try {
  139. $res2 = SystemBill::income($title, $uid, $money_type, $type, $money, $link_id, $balance, $mark, 1);
  140. $res4 = self::where('site_id', $uid)->where('money_type', $money_type)->update(['money' => $balance]);
  141. $res = $res2 && $res4;
  142. if ($res) {
  143. return true;
  144. } else {
  145. return self::setErrorInfo('交易失败');
  146. }
  147. } catch (\Exception $e) {
  148. return self::setErrorInfo($e->getMessage());
  149. }
  150. }
  151. }