123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- <?php
- namespace app\models\user;
- use crmeb\services\ZtPayService;
- 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)->find();
- if ($data) {
- $money_types = sys_data('money_type');
- $update = [];
- foreach ($money_types as $v) {
- if ($v['code'] == $money_type) {
- if ($v['charge']) {
- $ways = explode(',', $v['way']);
- if (count($ways)) {
- foreach ($ways as $vv) {
- if ($vv != 'NO') {
- if (!$data['address_' . $vv]) {
- $address = ZtPayService::instance()->get_address($money_type . '_' . $vv);
- $update['address_' . $vv] = $address['data']['address'];
- }
- } else {
- if (!$data['address']) {
- $address = ZtPayService::instance()->get_address($money_type);
- $update['address'] = $address['data']['address'];
- }
- }
- }
- }
- }
- break;
- }
- }
- 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']) {
- $ways = explode(',', $v['way']);
- if (count($ways)) {
- foreach ($ways as $vv) {
- if ($vv != 'NO') {
- $address = ZtPayService::instance()->get_address($money_type . '_' . $vv);
- $where['address_' . $vv] = $address['data']['address'];
- } else {
- $address = ZtPayService::instance()->get_address($money_type);
- $where['address'] = $address['data']['address'];
- }
- }
- }
- }
- 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 ($user['money'] < $money) {
- return self::setErrorInfo('余额不足');
- }
- $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 {
- $res1 = UserMoneyOrder::create(['uid' => $uid, 'to_uid' => $to_uid, 'money_type' => $money_type, 'money' => $money, 'add_time' => time()]);
- $res2 = UserBill::expend('会员转出', $uid, $money_type, 'expend', $money, $res1->id, $balance, $mark, 1);
- $res3 = UserBill::income('会员转入', $to_uid, $money_type, 'income', $money, $res1->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]);
- $res1 = UserMoneyOrder::where('id', $res1->id)->update(['status' => 1]);
- $res = $res1 && $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 = '', $from_vote = 0, $from_sub_vote = 0)
- {
- $user = self::initialUserMoney($uid, $money_type);
- if ($user['money'] < $money) {
- return self::setErrorInfo('余额不足');
- }
- $balance = bcsub($user['money'], $money, 8);
- try {
- $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]);
- $res2 = UserBill::expend($title, $uid, $money_type, $type, $money, $res1->id, $balance, $mark, 1);
- $res4 = self::where('uid', $uid)->where('money_type', $money_type)->update(['money' => $balance]);
- $res1 = UserMoneyOrder::where('id', $res1->id)->update(['status' => 1]);
- $res = $res1 && $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 = '', $from_vote = 0, $from_sub_vote = 0)
- {
- $user = self::initialUserMoney($uid, $money_type);
- $balance = bcadd($user['money'], $money, 8);
- try {
- $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]);
- $res2 = UserBill::income($title, $uid, $money_type, $type, $money, $res1->id, $balance, $mark, 1);
- $res4 = self::where('uid', $uid)->where('money_type', $money_type)->update(['money' => $balance]);
- $res1 = UserMoneyOrder::where('id', $res1->id)->update(['status' => 1]);
- $res = $res1 && $res2 && $res4;
- if ($res) {
- return true;
- } else {
- return self::setErrorInfo('交易失败');
- }
- } catch (\Exception $e) {
- return self::setErrorInfo($e->getMessage());
- }
- }
- public static function getComission($uid, $money_type, $vote = 0, $date = '')
- {
- if (!$date) {
- $model = new UserMoneyOrder();
- } else {
- $model = UserMoneyOrder::whereTime('add_time', $date);
- }
- if ($vote > 0) {
- $model = $model->where('from_vote', $vote);
- } else {
- $model = $model->where('from_vote', '>', 0);
- }
- $a = $model->where('uid', 0)->where('to_uid', $uid)->where('money_type', $money_type)->where('status', 1)->sum('money');
- // var_dump(UserMoneyOrder::getLastSql());
- if (!$date) {
- $model = new UserMoneyOrder();
- } else {
- $model = UserMoneyOrder::whereTime('add_time', $date);
- }
- if ($vote > 0) {
- $model = $model->where('from_vote', $vote);
- } else {
- $model = $model->where('from_vote', '>', 0);
- }
- $b = $model->where('uid', $uid)->where('to_uid', 0)->where('money_type', $money_type)->where('status', 1)->sum('money');
- // var_dump(UserMoneyOrder::getLastSql());
- return $a - $b;
- }
- }
|