123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- namespace app\models\system;
- use app\models\user\User;
- use app\models\user\UserBill;
- use app\models\user\UserMoney;
- 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 SystemMoney extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'system_money';
- use ModelTrait;
- /**
- * @param $uid
- * @param $money_type
- * @param int $money
- * @return array|Model|null
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function initialMoney($site_id, $money_type, $money = 0)
- {
- $where = compact('site_id', 'money_type');
- $data = self::where($where)->find();
- if ($data) {
- return self::where($where)->find();
- } else {
- 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::initialMoney($uid, $money_type);
- $user_info = Site::get($uid);
- $to_user = UserMoney::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['name'] . "平台奖池转入" . $money . ' ' . $money_type;
- self::beginTrans();
- try {
- $res2 = SystemBill::expend('奖池转出', $uid, $money_type, 'expend', $money, 0, $balance, $mark, 1);
- $res3 = UserBill::income('奖池转入', $to_uid, $money_type, 'system_income', $money, $res2->id, $to_balance, $to_mark, 1);
- $res4 = self::where('site_id', $uid)->where('money_type', $money_type)->update(['money' => $balance]);
- $res5 = UserMoney::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 $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)
- {
- $user = self::initialMoney($uid, $money_type);
- if ($user['money'] < $money) {
- return self::setErrorInfo('余额不足');
- }
- $balance = bcsub($user['money'], $money, 8);
- try {
- $res2 = SystemBill::expend($title, $uid, $money_type, $type, $money, $link_id, $balance, $mark, 1);
- $res4 = self::where('site_uid', $uid)->where('money_type', $money_type)->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 $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)
- {
- $user = self::initialMoney($uid, $money_type);
- $balance = bcadd($user['money'], $money, 8);
- try {
- $res2 = SystemBill::income($title, $uid, $money_type, $type, $money, $link_id, $balance, $mark, 1);
- $res4 = self::where('site_id', $uid)->where('money_type', $money_type)->update(['money' => $balance]);
- $res = $res2 && $res4;
- if ($res) {
- return true;
- } else {
- return self::setErrorInfo('交易失败');
- }
- } catch (\Exception $e) {
- return self::setErrorInfo($e->getMessage());
- }
- }
- }
|