UserMoney.php 9.4 KB

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