UserMoney.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace app\models\user;
  3. use crmeb\traits\ModelTrait;
  4. use crmeb\basic\BaseModel;
  5. use think\db\exception\DataNotFoundException;
  6. use think\db\exception\DbException;
  7. use think\db\exception\ModelNotFoundException;
  8. use think\Model;
  9. class UserMoney extends BaseModel
  10. {
  11. /**
  12. * 数据表主键
  13. * @var string
  14. */
  15. protected $pk = 'id';
  16. /**
  17. * 模型名称
  18. * @var string
  19. */
  20. protected $name = 'user_money';
  21. use ModelTrait;
  22. /**
  23. * @param $uid
  24. * @param $money_type
  25. * @param int $money
  26. * @return array|Model|null
  27. * @throws DataNotFoundException
  28. * @throws DbException
  29. * @throws ModelNotFoundException
  30. */
  31. public static function initialUserMoney($uid, $money_type, $money = 0)
  32. {
  33. $where = compact('uid', 'money_type');
  34. if (self::where($where)->find()) return self::where($where)->find();
  35. else return self::create(array_merge($where, ['money' => $money]));
  36. }
  37. /**
  38. * @param $uid
  39. * @param $to_uid
  40. * @param $money_type
  41. * @param $money
  42. * @return bool
  43. * @throws DataNotFoundException
  44. * @throws DbException
  45. * @throws ModelNotFoundException
  46. */
  47. public static function tradeMoney($uid, $to_uid, $money_type, $money)
  48. {
  49. $user = self::initialUserMoney($uid, $money_type);
  50. $user_info = User::getUserInfo($uid);
  51. $to_user = self::initialUserMoney($to_uid, $money_type);
  52. $to_user_info = User::getUserInfo($to_uid);
  53. if ($user['money'] < $money) {
  54. return self::setErrorInfo('余额不足');
  55. }
  56. $balance = bcsub($user['money'], $money, 8);
  57. $to_balance = bcadd($to_user['money'], $money, 8);
  58. $mark = '付款给' . $to_user_info['nickname'] . "({$to_user_info['uid']})" . $money . ' ' . $money_type;
  59. $to_mark = '收到' . $user_info['nickname'] . "({$user_info['uid']})支付" . $money . ' ' . $money_type;
  60. self::beginTrans();
  61. try {
  62. $res1 = UserMoneyOrder::create(['uid' => $uid, 'to_uid' => $to_uid, 'money_type' => $money_type, 'money' => $money, 'add_time' => time()]);
  63. $res2 = UserBill::expend('用户付款', $uid, $money_type, 'expend', $money, $res1->id, $balance, $mark, 1);
  64. $res3 = UserBill::income('用户收款', $to_uid, $money_type, 'income', $money, $res1->id, $to_balance, $to_mark, 1);
  65. $res4 = self::where('uid', $uid)->where('money_type', $money_type)->update(['money' => $balance]);
  66. $res5 = self::where('uid', $to_uid)->where('money_type', $money_type)->update(['money' => $to_balance]);
  67. $res1 = UserMoneyOrder::where('id', $res1->id)->update(['status' => 1]);
  68. $res = $res1 && $res2 && $res3 && $res4 && $res5;
  69. if ($res) {
  70. self::commitTrans();
  71. return true;
  72. } else {
  73. self::rollbackTrans();
  74. return self::setErrorInfo('交易失败');
  75. }
  76. } catch (\Exception $e) {
  77. self::rollbackTrans();
  78. return self::setErrorInfo($e->getMessage());
  79. }
  80. }
  81. /**
  82. * @param $uid
  83. * @param $money_type
  84. * @param $money
  85. * @param $type
  86. * @param string $title
  87. * @param string $mark
  88. * @param int $from_vote
  89. * @param int $from_sub_vote
  90. * @return bool
  91. * @throws DataNotFoundException
  92. * @throws DbException
  93. * @throws ModelNotFoundException
  94. */
  95. public static function expendMoney($uid, $money_type, $money, $type, $title = '用户付款', $mark = '', $from_vote = 0, $from_sub_vote = 0)
  96. {
  97. $user = self::initialUserMoney($uid, $money_type);
  98. if ($user['money'] < $money) {
  99. return self::setErrorInfo('余额不足');
  100. }
  101. $balance = bcsub($user['money'], $money, 8);
  102. try {
  103. $res1 = UserMoneyOrder::create(['uid' => $uid, 'to_uid' => 0, 'money_type' => $money_type, 'money' => $money, 'add_time' => time(), 'from_vote' => $from_vote, 'from_sub_vote' => $from_sub_vote]);
  104. $res2 = UserBill::expend($title, $uid, $money_type, $type, $money, $res1->id, $balance, $mark, 1);
  105. $res4 = self::where('uid', $uid)->where('money_type', $money_type)->update(['money' => $balance]);
  106. $res1 = UserMoneyOrder::where('id', $res1->id)->update(['status' => 1]);
  107. $res = $res1 && $res2 && $res4;
  108. if ($res) {
  109. return true;
  110. } else {
  111. return self::setErrorInfo('交易失败');
  112. }
  113. } catch (\Exception $e) {
  114. return self::setErrorInfo($e->getMessage());
  115. }
  116. }
  117. /**
  118. * @param $uid
  119. * @param $money_type
  120. * @param $money
  121. * @param $type
  122. * @param string $title
  123. * @param string $mark
  124. * @param int $from_vote
  125. * @param int $from_sub_vote
  126. * @return bool
  127. * @throws DataNotFoundException
  128. * @throws DbException
  129. * @throws ModelNotFoundException
  130. */
  131. public static function incomeMoney($uid, $money_type, $money, $type, $title = '用户获得', $mark = '', $from_vote = 0, $from_sub_vote = 0)
  132. {
  133. $user = self::initialUserMoney($uid, $money_type);
  134. $balance = bcadd($user['money'], $money, 8);
  135. try {
  136. $res1 = UserMoneyOrder::create(['uid' => 0, 'to_uid' => $uid, 'money_type' => $money_type, 'money' => $money, 'add_time' => time(), 'from_vote' => $from_vote, 'from_sub_vote' => $from_sub_vote]);
  137. $res2 = UserBill::income($title, $uid, $money_type, $type, $money, $res1->id, $balance, $mark, 1);
  138. $res4 = self::where('uid', $uid)->where('money_type', $money_type)->update(['money' => $balance]);
  139. $res1 = UserMoneyOrder::where('id', $res1->id)->update(['status' => 1]);
  140. $res = $res1 && $res2 && $res4;
  141. if ($res) {
  142. return true;
  143. } else {
  144. return self::setErrorInfo('交易失败');
  145. }
  146. } catch (\Exception $e) {
  147. return self::setErrorInfo($e->getMessage());
  148. }
  149. }
  150. public static function getComission($uid, $money_type, $vote = 0, $date = '')
  151. {
  152. if (!$date) {
  153. $model = new UserMoneyOrder();
  154. } else {
  155. $model = UserMoneyOrder::whereTime('add_time', $date);
  156. }
  157. if ($vote > 0) {
  158. $model = $model->where('from_vote', $vote);
  159. } else {
  160. $model = $model->where('from_vote', '>', 0);
  161. }
  162. $a = $model->where('uid', 0)->where('to_uid', $uid)->where('money_type', $money_type)->where('status', 1)->sum('money');
  163. // var_dump(UserMoneyOrder::getLastSql());
  164. if (!$date) {
  165. $model = new UserMoneyOrder();
  166. } else {
  167. $model = UserMoneyOrder::whereTime('add_time', $date);
  168. }
  169. if ($vote > 0) {
  170. $model = $model->where('from_vote', $vote);
  171. } else {
  172. $model = $model->where('from_vote', '>', 0);
  173. }
  174. $b = $model->where('uid', $uid)->where('to_uid', 0)->where('money_type', $money_type)->where('status', 1)->sum('money');
  175. // var_dump(UserMoneyOrder::getLastSql());
  176. return $a - $b;
  177. }
  178. }