123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- <?php
- namespace app\models\user;
- use app\models\activity\Activity;
- use app\models\lala\LalaRatio;
- use app\models\system\SystemBill;
- use crmeb\services\blockchain\BlockChianService;
- use crmeb\traits\ModelTrait;
- use crmeb\basic\BaseModel;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\Model;
- class UserMoney extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'user_money';
- use ModelTrait;
- /**
- * @param $uid
- * @param $money_type
- * @param int $money
- * @return array|Model|null
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function initialUserMoney($uid, $money_type, $money = 0)
- {
- $where = compact('uid', 'money_type');
- $data = self::where($where)->order('id', 'asc')->find();
- if (self::where($where)->count() > 1) {
- self::where($where)->where('id', '<>', $data['id'])->delete();
- }
- if ($data) {
- // $money_types = sys_data('money_type');
- // $update = [];
- // foreach ($money_types as $v) {
- // if ($v['code'] == $money_type) {
- // if ($v['charge']) {
- // //TODO 接入链上充值
- // if (!$data['address']) {
- // $update = BlockChianService::createAddress($v['code']);
- // }
- // }
- // break;
- // }
- // }
- // if ($update)
- // self::where($where)->update($update);
- return self::where($where)->find();
- } else {
- // $money_types = sys_data('money_type');
- // foreach ($money_types as $v) {
- // if ($v['code'] == $money_type) {
- // if ($v['charge']) {
- // //TODO 接入链上充值
- // $address = BlockChianService::createAddress($v['code']);
- // $where['address'] = $address['address'];
- // $where['privateKey'] = $address['privateKey'];
- // $where['hexAddress'] = $address['hexAddress'];
- // }
- // break;
- // }
- // }
- return self::create(array_merge($where, ['money' => $money]));
- }
- }
- /**
- * @param $uid
- * @param $to_uid
- * @param $money_type
- * @param $money
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function tradeMoney($uid, $to_uid, $money_type, $money)
- {
- $user = self::initialUserMoney($uid, $money_type);
- $user_info = User::getUserInfo($uid);
- $to_user = self::initialUserMoney($to_uid, $money_type);
- $to_user_info = User::getUserInfo($to_uid);
- if ($to_uid == $uid) {
- return self::setErrorInfo('不能转账给自己');
- }
- if (bcsub($user['money'], $user['only_pink'], 8) < $money) {
- return self::setErrorInfo('可转账' . $money_type . '不足');
- }
- $balance = bcsub($user['money'], $money, 8);
- $to_balance = bcadd($to_user['money'], $money, 8);
- $mark = '付款给' . $to_user_info['nickname'] . "({$to_user_info['uid']})" . $money . ' ' . $money_type;
- $to_mark = '收到' . $user_info['nickname'] . "({$user_info['uid']})支付" . $money . ' ' . $money_type;
- self::beginTrans();
- try {
- $res2 = UserBill::expend('会员转出', $uid, $money_type, 'expend', $money, 0, $balance, $mark, 1);
- $res3 = UserBill::income('会员转入', $to_uid, $money_type, 'income', $money, $res2->id, $to_balance, $to_mark, 1);
- $res4 = self::where('uid', $uid)->where('money_type', $money_type)->update(['money' => $balance]);
- $res5 = self::where('uid', $to_uid)->where('money_type', $money_type)->update(['money' => $to_balance]);
- $res = $res2 && $res3 && $res4 && $res5;
- if ($res) {
- self::commitTrans();
- return true;
- } else {
- self::rollbackTrans();
- return self::setErrorInfo('交易失败');
- }
- } catch (\Exception $e) {
- self::rollbackTrans();
- return self::setErrorInfo($e->getMessage());
- }
- }
- /**
- * @param $aid
- * @param $uid
- * @param $to_uid
- * @param $layer
- * @param $money_type
- * @param $money
- * @param int $status
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function activityTradeMoney($aid, $uid, $to_uid, $link_id, $layer, $money_type, $money, int $status = 1)
- {
- $activity = Activity::get($aid);
- if (!$activity) return self::setErrorInfo('找不到活动');
- $user = self::initialUserMoney($uid, $money_type);
- $user_info = User::getUserInfo($uid);
- if (bcsub($user['money'], $user['only_pink'], 8) < $money) {
- return self::setErrorInfo('可转账' . $money_type . '不足');
- }
- $balance = bcsub($user['money'], $money, 8);
- if ($to_uid > 0) {
- $to_user = self::initialUserMoney($to_uid, $money_type);
- $to_user_info = User::getUserInfo($to_uid);
- $to_balance = bcadd($to_user['money'], $money, 8);
- $to_mark = '收到' . $user_info['nickname'] . "({$user_info['uid']})" . $activity['name'] . '升级V' . $layer . "支付" . $money . ' ' . $money_type;
- }
- $mark = $activity['name'] . '升级V' . $layer . ('付款给' . ($to_user_info['nickname'] ?? '系统')) . "({$to_uid})" . $money . ' ' . $money_type;
- self::beginTrans();
- try {
- $res2 = UserBill::expend('互助升级转出', $uid, $money_type, 'activity_expend', $money, $link_id, $balance, $mark, 1, '', $aid, $layer);
- if ($to_uid > 0)
- $res3 = UserBill::income('互助升级转入', $to_uid, $money_type, 'activity_income', $money, $link_id, $to_balance ?? 0, $to_mark ?? '', $status, '', $aid, $layer);
- else $res3 = true;
- $res4 = self::where('uid', $uid)->where('money_type', $money_type)->update(['money' => $balance]);
- if ($status && $to_uid > 0) $res5 = self::where('uid', $to_uid)->where('money_type', $money_type)->update(['money' => $to_balance ?? 0]);
- else $res5 = true;
- $res = $res2 && $res3 && $res4 && $res5;
- if ($res) {
- self::commitTrans();
- return true;
- } else {
- self::rollbackTrans();
- return self::setErrorInfo('交易失败');
- }
- } catch (\Exception $e) {
- self::rollbackTrans();
- return self::setErrorInfo($e->getMessage());
- }
- }
- /**
- * @param $uid
- * @param $money_type
- * @param $money
- * @param $type
- * @param string $title
- * @param string $mark
- * @param int $from_vote
- * @param int $from_sub_vote
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function expendMoney($uid, $money_type, $money, $type, $title = '会员转出', $mark = '', $link_id = 0, $not_pink = true, $admin_mark = '')
- {
- $user = self::initialUserMoney($uid, $money_type);
- if ($not_pink) {
- if (bcsub($user['money'], $user['only_pink'], 8) < $money) {
- return self::setErrorInfo('可用余额不足');
- }
- } else {
- if ($user['money'] < $money) {
- return self::setErrorInfo('余额不足');
- }
- }
- $balance = bcsub($user['money'], $money, 8);
- try {
- $res2 = UserBill::expend($title, $uid, $money_type, $type, $money, $link_id, $balance, $mark, 1, $admin_mark);
- $res4 = self::where('uid', $uid)->where('money_type', $money_type)->update(['money' => $balance]);
- if (!$not_pink && $user['only_pink'] > 0) {
- if ($user['only_pink'] >= $money) {
- $res4 = $res4 && self::where('uid', $uid)->where('money_type', $money_type)->update(['only_pink' => bcsub($user['only_pink'], $money, 8)]);
- } else {
- $res4 = $res4 && self::where('uid', $uid)->where('money_type', $money_type)->update(['only_pink' => 0]);
- }
- }
- $res = $res2 && $res4;
- if ($res) {
- return true;
- } else {
- return self::setErrorInfo('交易失败');
- }
- } catch (\Exception $e) {
- return self::setErrorInfo($e->getMessage());
- }
- }
- /**
- * @param $uid
- * @param $money_type
- * @param $money
- * @param $type
- * @param string $title
- * @param string $mark
- * @param int $from_vote
- * @param int $from_sub_vote
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function incomeMoney($uid, $money_type, $money, $type, $title = '用户获得', $mark = '', $link_id = 0, $status = 1, $not_pink = true, $admin_mark = '')
- {
- $user = self::initialUserMoney($uid, $money_type);
- $balance = bcadd($user['money'], $money, 8);
- try {
- $res2 = UserBill::income($title, $uid, $money_type, $type, $money, $link_id, $balance, $mark, $status, $admin_mark);
- if ($status) {
- $res4 = self::where('uid', $uid)->where('money_type', $money_type)->update(['money' => $balance]);
- if (!$not_pink) {
- $res4 = $res4 && self::where('uid', $uid)->where('money_type', $money_type)->update(['only_pink' => bcadd($user['only_pink'], $money, 8)]);
- }
- } else
- $res4 = true;
- $res = $res2 && $res4;
- if ($res) {
- return true;
- } else {
- return self::setErrorInfo('交易失败');
- }
- } catch (\Exception $e) {
- return self::setErrorInfo($e->getMessage());
- }
- }
- public static function sendMoney($id)
- {
- $info = UserBill::get($id);
- if (!$info || $info['status'] != 0) {
- return self::setErrorInfo('状态异常');
- }
- $user = self::initialUserMoney($info['uid'], $info['category']);
- $balance = bcadd($user['money'], $info['number'], 8);
- try {
- $res2 = UserBill::edit(['status' => 1, 'balance' => $balance, 'add_time' => time()], $id);
- $res4 = self::where('uid', $info['uid'])->where('money_type', $info['category'])->update(['money' => $balance]);
- $res = $res2 && $res4;
- if ($res) {
- return true;
- } else {
- return self::setErrorInfo('交易失败');
- }
- } catch (\Exception $e) {
- return self::setErrorInfo($e->getMessage());
- }
- }
- /**
- * @param $uid
- * @param $origin_money_type
- * @param $target_money_type
- * @param $money
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function exchangeMoney($uid, $origin_money_type, $target_money_type, $money)
- {
- if ($origin_money_type == $target_money_type) return self::setErrorInfo('相同币种不可兑换');
- $origin_money = self::initialUserMoney($uid, $origin_money_type);
- $target_money = self::initialUserMoney($uid, $target_money_type);
- if (bcsub($origin_money['money'], $origin_money['only_pink'], 8) < $money) {
- return self::setErrorInfo('可用' . $origin_money_type . '不足');
- }
- $money_types = sys_data('money_type');
- $origin_ratio = 0;
- $target_ratio = 0;
- foreach ($money_types as $v) {
- if ($v['code'] == $origin_ratio) {
- $origin_ratio = $v['usdt_price'];
- if ($origin_money_type == 'LALA') {
- $origin_ratio = get_lala_ratio();
- }
- }
- if ($v['code'] == $target_money_type) {
- $target_ratio = $v['usdt_price'];
- if ($target_money_type == 'LALA') {
- $target_ratio = get_lala_ratio();
- }
- }
- }
- $ratio = bcdiv($origin_ratio, $target_ratio, 14);
- $add_money = bcmul($money, $ratio, 8);
- $balance = bcsub($origin_money['money'], $money, 8);
- $to_balance = bcadd($target_money['money'], $add_money, 8);
- $mark = '使用' . $money . ' ' . $origin_money_type . '兑换' . $add_money . ' ' . $target_money_type;
- self::beginTrans();
- try {
- $res2 = UserBill::expend('闪兑', $uid, $origin_money_type, 'exchange', $money, 0, $balance, $mark, 1);
- $res3 = UserBill::income('闪兑', $uid, $target_money_type, 'exchange', $add_money, $res2->id, $to_balance, $mark, 1);
- $res4 = self::where('uid', $uid)->where('money_type', $origin_money_type)->update(['money' => $balance]);
- $res5 = self::where('uid', $uid)->where('money_type', $target_money_type)->update(['money' => $to_balance]);
- $res = $res2 && $res3 && $res4 && $res5;
- if ($res) {
- self::commitTrans();
- return true;
- } else {
- self::rollbackTrans();
- return self::setErrorInfo('兑换失败');
- }
- } catch (\Exception $e) {
- self::rollbackTrans();
- return self::setErrorInfo($e->getMessage());
- }
- }
- }
|