123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697 |
- <?php
- namespace app\models\trade;
- use app\models\user\User;
- use app\models\user\UserBanks;
- use app\models\user\UserBill;
- use app\models\user\UserMoney;
- use app\models\user\UserMoneyOrder;
- use crmeb\repositories\ShortLetterRepositories;
- 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\Exception;
- use think\Model;
- class CashTradeOrder extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'cash_trade_order';
- use ModelTrait;
- /**
- * @param $money_type
- * @return int|string|null
- */
- public static function averagePrice($money_type)
- {
- $all_money = self::whereTime('trade_time', 'yesterday')->where('status', 2)->where('money_type', $money_type)->sum('money_num');
- $all_cash = self::whereTime('trade_time', 'yesterday')->where('status', 2)->where('money_type', $money_type)->sum('all_price');
- if ($all_money > 0)
- return bcdiv($all_cash, $all_money, 8);
- else
- return "0";
- }
- /**
- * @param $where
- * @return self
- */
- public static function setStatus($where)
- {
- if (is_array($where)) {
- if (isset($where['status'])) {
- $status = $where['status'];
- } else {
- return new self();
- }
- } else {
- $status = $where;
- }
- switch ($status) {
- case 1://挂出中
- return self::where('status', 'in', [0, -1])->where('end_time', '>', time())->where('trade_uid', null);
- case 2://已完成
- return self::where('status', 1);
- case 3://挂出时间结束
- return self::where('status', 'in', [0, -1, 2])->where('end_time', '<', time());
- case 4://挂出时间结束,未领回
- return self::where('status', 'in', [0, -1])->where('end_time', '<', time());
- case 5://已领回
- return self::where('status', 2);
- case 6://交易中,已上传凭证
- return self::where('status', 3);
- case 7://未结束 但是关闭
- return self::where('status', 'in', [0, -1])->where('end_time', '>', time())->where('close_time', '<', time());
- default:
- return new self();
- }
- }
- /**
- * @param $where
- * @return array
- */
- public static function getList($where)
- {
- $model = self::setStatus($where);
- // if (isset($where['uid']) && $where['uid']) {
- // $model = $model->where('uid|trade_uid', $where['uid']);
- // }
- if (isset($where['trade_type']) && $where['trade_type'] != '') {
- if (isset($where['uid']) && $where['uid']) {
- $model = $model->where(function ($query) use ($where) {
- $query->where('trade_uid', $where['uid'])
- ->where('trade_type', $where['trade_type']);
- })->whereOr(function ($query) use ($where) {
- $query->where('uid', $where['uid'])
- ->where('trade_type', '<>', $where['trade_type']);
- });
- } else {
- $model = $model->where('trade_type', $where['trade_type']);
- }
- }
- if (isset($where['money_type']) && $where['money_type'] != '') {
- $model = $model->where('money_type', $where['money_type']);
- }
- if (isset($where['order_id']) && $where['order_id'] != '') {
- $model = $model->where('order_id', $where['order_id']);
- }
- if (isset($where['trade_uid']) && $where['trade_uid'] != '') {
- $model = $model->where('trade_uid', $where['trade_uid']);
- }
- $count = $model->count();
- // var_dump(self::getLastSql());
- $list = $model->order('trade_start_time desc,add_time desc')->page((int)$where['page'], (int)$where['limit'])->select()
- // var_dump(self::getLastSql());
- ->each(function ($item) {
- $item['user'] = User::getUserInfo($item['uid']);
- $item['_trade_user'] = $item['trade_uid'] === 0 ? '系统/0' : ($item['trade_uid'] ? User::getUserInfo($item['trade_uid'])['nickname'] . '/' . $item['trade_uid'] : '--');
- $item['trade_user'] = $item['trade_uid'] === 0 ? [] : User::getUserInfo($item['trade_uid']);
- // $item['_add_time'] = time_format($item['add_time']);
- // $item['_end_time'] = time_format($item['end_time']);
- // $item['_trade_time'] = time_format($item['trade_time']);
- $money_type = init_money_type();
- $item['_user'] = User::getUserInfo($item['uid'])['nickname'] . '/' . $item['uid'];
- $item['bank'] = json_decode($item['bank_info'], true);
- $item['_name_text'] = '--';
- $item['_account_text'] = '--';
- $item['_code_text'] = '--';
- $item['_name'] = '--';
- $item['_account'] = '--';
- $item['_code'] = '--';
- if ($item['bank']) {
- if ($item['bank']['type'] == 1) {
- $item['_name_text'] = '支付宝用户';
- $item['_account_text'] = '支付宝账号';
- $item['_code_text'] = '支付宝二维码';
- $item['_name'] = $item['bank']['name'];
- $item['_account'] = $item['bank']['account'];
- $item['_code'] = "<img src='" . $item['bank']['code'] . "'>";
- } else if ($item['bank']['type'] == 2) {
- $item['_name_text'] = '微信用户';
- $item['_account_text'] = '微信账号';
- $item['_code_text'] = '微信二维码';
- $item['_name'] = $item['bank']['name'];
- $item['_account'] = $item['bank']['account'];
- $item['_code'] = "<img src='" . $item['bank']['code'] . "'>";
- } else {
- $item['_name_text'] = '开户人';
- $item['_account_text'] = '银行卡号';
- $item['_code_text'] = '开户行';
- $item['_name'] = $item['bank']['name'];
- $item['_account'] = $item['bank']['account'];
- $item['_code'] = $item['bank']['code'];
- }
- }
- $item['_add_time'] = time_format($item['add_time']);
- $item['_end_time'] = $item['end_time'] ? time_format($item['end_time']) : '--';
- $item['_trade_time'] = $item['trade_time'] ? time_format($item['trade_time']) : '--';
- $item['_money_type'] = $money_type[$item['money_type']];
- $item['_trade_type'] = $item['trade_type'] == 2 ? '挂买' : '挂卖';
- $item['_proof'] = $item['proof'] ? "<img src='" . $item['proof'] . "'>" : '--';
- switch ($item['status']) {
- case -1:
- $item['_status'] = '未支付';
- break;
- case 0:
- $item['_status'] = '未完成';
- break;
- case 1:
- $item['_status'] = '已完成';
- break;
- case 2:
- $item['_status'] = '已收回';
- break;
- case 3:
- $item['_status'] = '交易中【‘已提交交易凭证’】';
- break;
- default:
- $item['_status'] = '未知';
- break;
- }
- });
- // var_dump($list);
- return compact('count', 'list');
- }
- public static function getInfo($id)
- {
- $item = self::get($id);
- $item['user'] = User::getUserInfo($item['uid']);
- $item['_trade_user'] = $item['trade_uid'] === 0 ? '系统/0' : ($item['trade_uid'] ? User::getUserInfo($item['trade_uid'])['nickname'] . '/' . $item['trade_uid'] : '--');
- $item['trade_user'] = $item['trade_uid'] === 0 ? [] : User::getUserInfo($item['trade_uid']);
- // $item['_add_time'] = time_format($item['add_time']);
- // $item['_end_time'] = time_format($item['end_time']);
- // $item['_trade_time'] = time_format($item['trade_time']);
- $money_type = init_money_type();
- $item['_user'] = User::getUserInfo($item['uid'])['nickname'] . '/' . $item['uid'];
- $item['bank'] = json_decode($item['bank_info'], true);
- $item['_name_text'] = '--';
- $item['_account_text'] = '--';
- $item['_code_text'] = '--';
- $item['_name'] = '--';
- $item['_account'] = '--';
- $item['_code'] = '--';
- if ($item['bank']) {
- if ($item['bank']['type'] == 1) {
- $item['_name_text'] = '支付宝用户';
- $item['_account_text'] = '支付宝账号';
- $item['_code_text'] = '支付宝二维码';
- $item['_name'] = $item['bank']['name'];
- $item['_account'] = $item['bank']['account'];
- $item['_code'] = "<img src='" . $item['bank']['code'] . "'>";
- } else if ($item['bank']['type'] == 2) {
- $item['_name_text'] = '微信用户';
- $item['_account_text'] = '微信账号';
- $item['_code_text'] = '微信二维码';
- $item['_name'] = $item['bank']['name'];
- $item['_account'] = $item['bank']['account'];
- $item['_code'] = "<img src='" . $item['bank']['code'] . "'>";
- } else {
- $item['_name_text'] = '开户人';
- $item['_account_text'] = '银行卡号';
- $item['_code_text'] = '开户行';
- $item['_name'] = $item['bank']['name'];
- $item['_account'] = $item['bank']['account'];
- $item['_code'] = $item['bank']['code'];
- }
- }
- $item['_add_time'] = time_format($item['add_time']);
- $item['_end_time'] = $item['end_time'] ? time_format($item['end_time']) : '--';
- $item['_trade_time'] = $item['trade_time'] ? time_format($item['trade_time']) : '--';
- $item['_money_type'] = $money_type[$item['money_type']];
- $item['_trade_type'] = $item['trade_type'] == 2 ? '挂买' : '挂卖';
- $item['_proof'] = $item['proof'] ? "<img src='" . $item['proof'] . "'>" : '--';
- switch ($item['status']) {
- case -1:
- $item['_status'] = '未支付';
- break;
- case 0:
- $item['_status'] = '未完成';
- break;
- case 1:
- $item['_status'] = '已完成';
- break;
- case 2:
- $item['_status'] = '已收回';
- break;
- case 3:
- $item['_status'] = '交易中【‘已提交交易凭证’】';
- break;
- default:
- $item['_status'] = '未知';
- break;
- }
- return $item;
- }
- public static function getOneUserList($where)
- {
- $model = self::setStatus($where);
- $model = $model->where('uid|trade_uid', $where['uid']);
- // if (isset($where['trade_type']) && $where['trade_type'] != '') {
- // if (isset($where['uid']) && $where['uid']) {
- // $model = $model->where(function ($query) use ($where) {
- // $query->where('trade_uid', $where['uid'])
- // ->where('trade_type', $where['trade_type']);
- // })->whereOr(function ($query) use ($where) {
- // $query->where('uid', $where['uid'])
- // ->where('trade_type', '<>', $where['trade_type']);
- // });
- // } else {
- // $model = $model->where('trade_type', $where['trade_type']);
- // }
- // }
- // if (isset($where['money_type']) && $where['money_type'] != '') {
- // $model = $model->where('money_type', $where['money_type']);
- // }
- // $count = $model->count();
- // var_dump(self::getLastSql());
- $list = $model->page((int)$where['page'], (int)$where['limit'])->select()
- // var_dump(self::getLastSql());
- ->each(function ($item) use ($where) {
- $money_type = init_money_type();
- $item['_user'] = User::getUserInfo($item['uid'])['nickname'] . '/' . $item['uid'];
- $item['_trade_user'] = $item['trade_uid'] ? User::getUserInfo($item['trade_uid'])['nickname'] . '/' . $item['trade_uid'] : '--';
- $item['_add_time'] = time_format($item['add_time']);
- $item['_end_time'] = $item['_end_time'] ? time_format($item['end_time']) : '--';
- $item['_trade_time'] = $item['trade_time'] ? time_format($item['trade_time']) : '--';
- $item['_money_type'] = $money_type[$item['money_type']];
- $item['_trade_type'] = $item['trade_type'] == 2 ? '挂买' : '挂卖';
- $item['_proof'] = $item['proof'] ? "<img src='" . $item['proof'] . "'>" : '--';
- switch ($item['status']) {
- case -1:
- $item['_status'] = '未支付';
- break;
- case 0:
- $item['_status'] = '未完成';
- break;
- case 1:
- $item['_status'] = '已完成';
- break;
- case 2:
- $item['_status'] = '已收回';
- break;
- case 3:
- $item['_status'] = '交易中【‘已提交交易凭证’】';
- break;
- default:
- $item['_status'] = '未知';
- break;
- }
- });
- return $list;
- }
- public static function hangSale($uid, $money_type, $money_num, $price, $hang_time, $bank_id)
- {
- $user = User::getUserInfo($uid);
- if (!$user['sale_switch']) {
- return self::setErrorInfo('当前用户不可挂卖!');
- }
- if (!$bank_id) {
- return self::setErrorInfo('请选择收款方式!');
- }
- $bank = UserBanks::get($bank_id);
- if (!$bank || $bank['uid'] != $uid) {
- return self::setErrorInfo('收款方式错误!');
- }
- $money = UserMoney::initialUserMoney($uid, $money_type);
- if ($money['money'] < $money_num) return self::setErrorInfo('账户不足!');
- $sum = bcmul($money_num, $price, 2);
- BaseModel::beginTrans();
- try {
- $res = UserMoney::expendMoney($uid, $money_type, $money_num, 'hang_sale', '挂卖', '挂卖' . $money_num);
- if (!$res) {
- return self::setErrorInfo(UserMoney::getErrorInfo('支付失败'), true);
- }
- $res = self::create([
- 'order_id' => self::getOrderId(),
- 'uid' => $uid,
- 'money_type' => $money_type,
- 'money_num' => $money_num,
- 'price' => $price,
- 'all_price' => $sum,
- 'trade_type' => 1,
- 'add_time' => time(),
- 'hang_time' => $hang_time,
- 'end_time' => bcadd(time(), bcmul($hang_time, 3600)),
- 'bank_info' => json_encode($bank)
- ]);
- if ($res) {
- BaseModel::commitTrans();
- return $res;
- } else {
- return self::setErrorInfo('挂卖失败', true);
- }
- } catch (Exception $e) {
- return self::setErrorInfo($e->getMessage(), true);
- }
- }
- public static function fastSale($uid, $money_type, $money_num, $price, $bank_id)
- {
- // $user = User::getUserInfo($uid);
- // if (!$user['bank_code'] && !$user['alipay_code'] && !$user['alipay_account'] && !$user['wechat_code'] && !$user['wechat_account']) {
- // return self::setErrorInfo('请先设置收款方式!');
- // }
- if (!$bank_id) {
- return self::setErrorInfo('请选择收款方式!');
- }
- $bank = UserBanks::get($bank_id);
- if (!$bank || $bank['uid'] != $uid) {
- return self::setErrorInfo('收款方式错误!');
- }
- $money = UserMoney::initialUserMoney($uid, $money_type);
- if ($money['money'] < $money_num) return self::setErrorInfo('账户不足!');
- $sum = bcmul($money_num, $price, 2);
- BaseModel::beginTrans();
- try {
- $res = UserMoney::expendMoney($uid, $money_type, $money_num, 'hang_sale', '挂卖', '挂卖' . $money_num);
- if (!$res) {
- return self::setErrorInfo(UserMoney::getErrorInfo('支付失败'), true);
- }
- $res = self::create([
- 'order_id' => self::getOrderId(),
- 'uid' => $uid,
- 'money_type' => $money_type,
- 'money_num' => $money_num,
- 'price' => $price,
- 'all_price' => $sum,
- 'trade_type' => 1,
- 'add_time' => time(),
- 'hang_time' => 0,
- 'trade_uid' => 0,
- 'status' => -1,
- 'end_time' => PHP_INT_MAX,
- 'bank_info' => json_encode($bank)
- ]);
- if ($res) {
- self::commitTrans();
- return true;
- } else {
- return self::setErrorInfo('挂卖失败', true);
- }
- } catch (Exception $e) {
- return self::setErrorInfo($e->getMessage(), true);
- }
- }
- public static function hangBuy2($uid, $money_type, $money_num, $price, $hang_time)
- {
- $sum = bcmul($money_num, $price, 2);
- BaseModel::beginTrans();
- try {
- // $res = UserMoney::expendMoney($uid, $money_type, $money_num, 'hang_sale', '挂卖', '挂卖' . $money_num);
- // if (!$res) {
- // return self::setErrorInfo(UserMoney::getErrorInfo('支付失败'), true);
- // }
- $res = self::create([
- 'order_id' => self::getOrderId(),
- 'uid' => $uid,
- 'money_type' => $money_type,
- 'money_num' => $money_num,
- 'price' => $price,
- 'all_price' => $sum,
- 'trade_type' => 2,
- 'add_time' => time(),
- 'hang_time' => $hang_time,
- 'status' => -1,//未支付虚拟货币
- 'end_time' => bcadd(time(), bcmul($hang_time, 3600)),
- ]);
- if ($res) {
- BaseModel::commitTrans();
- return $res;
- } else {
- return self::setErrorInfo('挂买失败', true);
- }
- } catch (Exception $e) {
- return self::setErrorInfo($e->getMessage(), true);
- }
- }
- public static function hangBuy($uid, $money_type, $money_num, $price, $hang_time)
- {
- $sum = bcmul($money_num, $price, 2);
- BaseModel::beginTrans();
- try {
- $res = CashTradeOrderPayment::create([
- 'uid' => $uid,
- 'order_id' => CashTradeOrderPayment::getOrderId(),
- 'order_type' => 1,
- 'pay_money' => $sum,
- 'add_time' => time(),
- 'order_info' => json_encode([
- 'uid' => $uid,
- 'money_type' => $money_type,
- 'money_num' => $money_num,
- 'price' => $price,
- 'all_price' => $sum,
- 'trade_type' => 2,
- 'hang_time' => $hang_time,]),
- ]);
- if ($res) {
- BaseModel::commitTrans();
- return $res;
- } else {
- return self::setErrorInfo('订单生成失败', true);
- }
- } catch (Exception $e) {
- return self::setErrorInfo($e->getMessage(), true);
- }
- }
- public static function getOrderId()
- {
- list($msec, $sec) = explode(' ', microtime());
- $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
- do {
- $str = 'ct' . $msectime . mt_rand(10000, 99999);
- } while (self::be(['order_id' => $str]));
- return $str;
- }
- public static function sale($uid, $id, $bank_id)
- {
- if (!$bank_id) {
- return self::setErrorInfo('请选择收款方式!');
- }
- $bank = UserBanks::get($bank_id);
- if (!$bank || $bank['uid'] != $uid) {
- return self::setErrorInfo('收款方式错误!');
- }
- $order = self::setStatus(1)->where('id', $id)->find();
- if (!$order) {
- return self::setErrorInfo('订单不存在或已结束挂出');
- }
- // $user = User::getUserInfo($uid);
- // if (!$user['bank_code'] && !$user['alipay_code'] && !$user['alipay_account'] && !$user['wechat_code'] && !$user['wechat_account']) {
- // return self::setErrorInfo('请先设置收款方式!');
- // }
- $money = UserMoney::initialUserMoney($uid, $order['money_type']);
- if ($money['money'] < $order['money_num']) return self::setErrorInfo('账户不足!');
- BaseModel::beginTrans();
- try {
- $res = UserMoney::expendMoney($uid, $order['money_type'], $order['money_num'], 'sale', '售币成功', '售币成功,购买方:' . $order['uid']);
- if (!$res) {
- return self::setErrorInfo(UserMoney::getErrorInfo('支付失败'), true);
- }
- $res = self::where('id', $id)->update([
- 'status' => 0,
- 'trade_uid' => $uid,
- 'bank_info' => json_encode($bank),
- 'trade_start_time' => time(),
- //'trade_way' => $order['pay_type'],
- // 'trade_time' => time(),
- ]);
- // $userInfo = User::getUserInfo($uid);
- // $res = $res && UserMoney::incomeMoney($order['uid'], $order['money_type'], $order['money_num'], 'buy_success', '购币成功', '购币成功,出售方:' . $uid);
- // $res = $res && (false !== User::bcInc($userInfo['uid'], 'now_money', $order['all_price'], 'uid'));
- // $res = $res && UserBill::income('售币成功', $userInfo['uid'], 'now_money', 'sale_success', $order['all_price'], $order['id'], $userInfo['now_money'] + $order['all_price'], '售币成功,购买方:' . $order['uid']);
- if ($res) {
- BaseModel::commitTrans();
- $user = User::getUserInfo($order['uid']);
- ShortLetterRepositories::SMSSend($user['phone'], ['code' => 0], 'START_TRADE');
- return $res;
- } else {
- return self::setErrorInfo('出售失败', true);
- }
- } catch (Exception $e) {
- return self::setErrorInfo($e->getMessage(), true);
- }
- }
- public static function buy($uid, $id)
- {
- $order = self::setStatus(1)->where('id', $id)->find();
- if (!$order) {
- return self::setErrorInfo('订单不存在或已结束挂出');
- }
- BaseModel::beginTrans();
- try {
- $res = CashTradeOrderPayment::create([
- 'uid' => $uid,
- 'order_id' => CashTradeOrderPayment::getOrderId(),
- 'bind_order' => $id,
- 'order_type' => 2,
- 'pay_money' => $order['all_price'],
- 'add_time' => time()
- ]);
- if ($res) {
- BaseModel::commitTrans();
- return $res;
- } else {
- return self::setErrorInfo('订单生成失败', true);
- }
- } catch (Exception $e) {
- return self::setErrorInfo($e->getMessage(), true);
- }
- }
- public static function buy2($uid, $id)
- {
- $order = self::setStatus(1)->where('id', $id)->find();
- if (!$order) {
- return self::setErrorInfo('订单不存在或已结束挂出');
- }
- // $money = UserMoney::initialUserMoney($uid, $order['money_type']);
- // if ($money['money'] < $order['money_num']) return self::setErrorInfo('账户不足!');
- BaseModel::beginTrans();
- try {
- // $res = UserMoney::expendMoney($uid, $order['money_type'], $order['money_num'], 'sale', '售币成功', '售币成功,购买方:' . $order['uid']);
- // if (!$res) {
- // return self::setErrorInfo(UserMoney::getErrorInfo('支付失败'), true);
- // }
- $res = self::where('id', $id)->update([
- 'status' => -1,
- 'trade_uid' => $uid,
- 'close_time' => time() + 30 * 60,
- 'trade_start_time' => time(),
- ]);
- if ($res) {
- BaseModel::commitTrans();
- return $res;
- } else {
- return self::setErrorInfo('申请购买失败', true);
- }
- } catch (Exception $e) {
- return self::setErrorInfo($e->getMessage(), true);
- }
- }
- public static function getBack()
- {
- $list = self::setStatus(4)->select();
- BaseModel::beginTrans();
- $res = true;
- foreach ($list as $v) {
- switch ($v['trade_type']) {
- case 1:
- //退回币
- $res = $res && UserMoney::incomeMoney($v['uid'], $v['money_type'], $v['money_num'], 'hang_sale_send_back', '挂卖退回', '挂卖时间结束,退回钱包');
- $res = $res && self::where('id', $v['id'])->update(['status' => 2]);
- break;
- case 2:
- // $userInfo = User::getUserInfo($v['uid']);
- // $res = $res && (false !== User::bcInc($v['uid'], 'now_money', $v['all_price'], 'uid'));
- // $res = $res && UserBill::income('售币成功', $v['uid'], 'now_money', 'hang_buy_send_back', $v['all_price'], $v['id'], $userInfo['now_money'] + $v['all_price'], '挂买时间结束,退回余额');
- // $res = $res && self::where('id', $v['id'])->update(['status' => 2]);
- if ($v['trade_uid'])
- $res = $res && UserMoney::incomeMoney($v['trade_uid'], $v['money_type'], $v['money_num'], 'hang_buy_send_back', '挂买退回', '挂买时间结束,退回钱包');
- $res = $res && self::where('id', $v['id'])->update(['status' => 2]);
- break;
- default:
- break;
- }
- }
- BaseModel::checkTrans($res);
- return $res;
- }
- public static function orderClose()
- {
- $list = self::setStatus(7)->select();
- // var_dump($list);
- BaseModel::beginTrans();
- $res = true;
- try {
- foreach ($list as $v) {
- switch ($v['trade_type']) {
- case 1:
- // var_dump(1111);
- //退回币
- // $res = $res && UserMoney::incomeMoney($v['uid'], $v['money_type'], $v['money_num'], 'hang_sale_send_back', '挂卖退回', '挂卖时间结束,退回钱包');
- $res = $res && self::where('id', $v['id'])->update(['trade_uid' => null, 'status' => 0, 'close_time' => null, 'trade_start_time' => null,]);
- break;
- case 2:
- // var_dump(2222);
- // $userInfo
- // = User::getUserInfo($v['uid']);
- // $res = $res && (false !== User::bcInc($v['uid'], 'now_money', $v['all_price'], 'uid'));
- // $res = $res && UserBill::income('售币成功', $v['uid'], 'now_money', 'hang_buy_send_back', $v['all_price'], $v['id'], $userInfo['now_money'] + $v['all_price'], '挂买时间结束,退回余额');
- // $res = $res && self::where('id', $v['id'])->update(['status' => 2]);
- if ($v['trade_uid'])
- $res = $res && UserMoney::incomeMoney($v['trade_uid'], $v['money_type'], $v['money_num'], 'hang_sale_send_back', '挂卖退回', '挂卖时间结束,退回钱包');
- $res = $res && self::where('id', $v['id'])->update(['trade_uid' => null, 'status' => -1, 'close_time' => null, 'trade_start_time' => null,]);
- break;
- default:
- break;
- }
- }
- } catch (Exception $e) {
- BaseModel::rollbackTrans();
- @file_put_contents('orderClose.error.log', '[' . date('Y-m-d H:i:s') . ']' . $e->getMessage() . PHP_EOL, FILE_APPEND);
- }
- BaseModel::commitTrans();
- return $res;
- }
- public static function brokerage($id)
- {
- $info = self::get($id);
- // if ($info['trade_uid'] !== 0) return true;
- if ($info['trade_type'] == 2)
- $user = User::getUserInfo($info['trade_uid']);
- else
- $user = User::getUserInfo($info['uid']);
- $uper = User::getUserInfo($user['spread_uid']);
- // var_dump($info);
- // var_dump($user['uid']);
- // var_dump($user['spread_uid']);
- // exit;
- $send = 0;
- $res = true;
- while ($uper) {
- $ratio = bcdiv($uper['return_uper'], 100, 2);
- if ($ratio > 0) {
- $brokerage = bcmul($ratio, $info['money_num'], 8);
- if ($brokerage > $send) {
- $brokerage = bcsub($brokerage, $send, 8);
- $res = $res && UserMoney::incomeMoney($uper['uid'], $info['money_type'], $brokerage, 'return_uper', '佣金', $user['nickname'] . '发生交易,获得佣金');
- $send = bcadd($brokerage, $send, 8);
- }
- }
- $uper = User::getUserInfo($uper['spread_uid']);
- }
- return $res;
- }
- }
|