| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Common;
- use App\Models\CapitalLogModel;
- use App\Models\MemberModel;
- use App\Models\MemberPaymentModel;
- use App\Models\TradeOrderModel;
- use App\Services\Api\MemberPaymentService;
- use App\Services\BaseService;
- use App\Services\ChatMessageService;
- use App\Services\ConfigService;
- use App\Services\RedisService;
- /**
- * 用户交易订单-服务类
- * Class TradeOrderService
- * @package App\Services\Common
- */
- class TradeOrderService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * @since 2020/11/10
- * TradeOrderService constructor.
- */
- public function __construct()
- {
- $this->model = new TradeOrderModel();
- $this->memberModel = new MemberModel();
- $this->capitalModel = new CapitalLogModel();
- $this->paymentModel = new MemberPaymentModel();
- }
- /**
- * 静态入口
- * @return static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = (new static());
- }
- return self::$instance;
- }
- /**
- * 订单列表
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function getDataList($params, $pageSize = 15)
- {
- $list = $this->model->from('trade_order as a')
- ->leftJoin('member as b', 'b.id', '=', 'a.business_id')
- ->leftJoin('member as c', 'c.id', '=', 'a.user_id')
- ->where(function ($query) use ($params) {
- $query->where(['a.mark' => 1])->where('a.status', '>', 0);
- $orderNo = isset($params['order_no']) && $params['order_no'] ? trim($params['order_no']) : '';
- if ($orderNo) {
- $query->where('a.order_no', 'like', "%{$orderNo}%");
- }
- $type = isset($params['type']) ? intval($params['type']) : 0;
- if ($type > 0) {
- $query->where(['a.type' => $type]);
- }
- // 日期
- $date = isset($params['date']) ? $params['date'] : [];
- $start = isset($date[0])? $date[0] : '';
- $end = isset($date[1])? $date[1] : '';
- $end = $start>=$end? '' : $end;
- if ($start) {
- $query->where('a.create_time','>=', strtotime($start));
- }
- if($end){
- $query->where('a.create_time','<', strtotime($end));
- }
- $exceptionStatus = isset($params['exception_status']) ? intval($params['exception_status']) : 0;
- if ($exceptionStatus > 0) {
- $query->where(['a.exception_status' => $exceptionStatus]);
- }
- $status = isset($params['status']) ? intval($params['status']) : 0;
- if ($status > 0) {
- $query->where(['a.status' => $status]);
- }
- $userId = isset($params['user_id']) ? $params['user_id'] : 0;
- if ($userId > 0) {
- $query->where('a.user_id', $userId);
- }
- $businessId = isset($params['business_id']) ? $params['business_id'] : 0;
- if ($businessId > 0) {
- $query->where('a.business_id', $businessId);
- }
- })
- ->select(['a.*', 'b.username', 'c.username as c_username'])
- ->orderBy('a.create_time', 'desc')
- ->orderBy('a.id', 'desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list ? $list->toArray() : [];
- if ($list) {
- $payTypes = [1 => '银行卡', 2 => '微信', 3 => '支付宝', 4 => '其他'];
- $overTime = ConfigService::make()->getConfigByCode('trade_order_overtime');
- $overTime = $overTime ? $overTime : 0;
- foreach ($list['data'] as &$item) {
- $item['idcardData'] = $item['idcard_data'] ? json_decode($item['idcard_data'], true) : [];
- $item['paymentData'] = $item['payment_data'] ? json_decode($item['payment_data'], true) : [];
- $item['create_time_text'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
- $item['update_time_text'] = $item['update_time'] ? datetime($item['update_time'], 'Y-m-d H:i:s') : '';
- $item['time_text'] = $item['create_time'] ? datetime($item['create_time'], 'H:i') : '';
- $item['pay_time_text'] = $item['pay_time'] ? datetime($item['pay_time'], 'Y-m-d H:i') : '';
- $item['username_text'] = $item['username'] ? format_account($item['username']) : '';
- $item['c_username_text'] = $item['c_username'] ? format_account($item['c_username']) : '';
- $item['exception_img'] = $item['exception_img'] ? get_image_url($item['exception_img']) : '';
- $item['pay_img'] = $item['pay_img'] ? get_image_url($item['pay_img']) : '';
- $item['paymentData']['qrcode'] = isset($item['paymentData']['qrcode']) && $item['paymentData']['qrcode'] ? get_image_url($item['paymentData']['qrcode']) : '';
- $overTime = max(0, intval($item['create_time']) + $overTime * 60 - time());
- $item['overtime_text'] = in_array($item['status'], [1, 2]) && $overTime ? date('H:i', $overTime) : '';
- $payType = isset($item['pay_type']) ? $item['pay_type'] : 0;
- $item['pay_name'] = isset($payTypes[$payType]) ? $payTypes[$payType] : '其他';
- }
- }
- return [
- 'pageSize' => $pageSize,
- 'total' => isset($list['total']) ? $list['total'] : 0,
- 'list' => isset($list['data']) ? $list['data'] : []
- ];
- }
- /**
- * 获取待处理订单
- * @param $params
- * @return array
- */
- public function getWaitOrder($params)
- {
- $info = $this->model->from('trade_order as a')
- ->leftJoin('member as b', 'b.id', '=', 'a.business_id')
- ->where(function ($query) use ($params) {
- $query->where(['a.mark' => 1])->whereIn('a.status', [1, 2, 3, 7])->where('a.status', '>', 0);
- $orderNo = isset($params['order_no']) && $params['order_no'] ? trim($params['order_no']) : '';
- if ($orderNo) {
- $query->where('a.order_no', 'like', "%{$orderNo}%");
- }
- $type = isset($params['type']) ? intval($params['type']) : 0;
- if ($type > 0) {
- $query->where(['a.type' => $type]);
- }
- $userId = isset($params['user_id']) ? $params['user_id'] : 0;
- if ($userId > 0) {
- $query->where('a.user_id', $userId);
- }
- $businessId = isset($params['business_id']) ? $params['business_id'] : 0;
- if ($businessId > 0) {
- $query->where('a.business_id', $businessId);
- }
- })
- ->select(['a.id', 'a.order_no', 'a.type', 'a.pay_type', 'a.user_id', 'a.business_id', 'a.num', 'a.total', 'a.payment_data', 'a.status', 'a.create_time', 'b.username'])
- ->orderBy('a.create_time', 'desc')
- ->first();
- $info = $info ? $info->toArray() : [];
- if ($info) {
- $overTime = ConfigService::make()->getConfigByCode('trade_order_overtime');
- $overTime = $overTime ? $overTime : 0;
- $info['paymentData'] = $info['payment_data'] ? json_decode($info['payment_data'], true) : [];
- $info['create_time_text'] = $info['create_time'] ? format_time(strtotime($info['create_time'])) : '';
- $info['time_text'] = $info['create_time'] ? datetime($info['create_time'], 'H:i') : '';
- $info['username_text'] = $info['username'] ? format_account($info['username']) : '';
- $info['paymentData']['qrcode'] = isset($info['paymentData']['qrcode']) && $info['paymentData']['qrcode'] ? get_image_url($info['paymentData']['qrcode']) : '';
- $info['paymentData']['bank_card_text'] = isset($info['paymentData']['bank_card']) && $info['paymentData']['bank_card'] ? format_bank_card($info['paymentData']['bank_card']) : '未设置';
- $info['paymentData']['account_text'] = isset($info['paymentData']['account']) && $info['paymentData']['account'] ? format_account($info['paymentData']['account']) : '未设置';
- $overTime = max(0, intval($info['create_time']) + $overTime * 60 - time());
- $item['overtime_text'] = in_array($info['status'], [1, 2]) && $overTime ? date('H:i', $overTime) : '';
- }
- return $info;
- }
- /**
- * 成交交易笔数
- * @param int $type 类型:1-买单,2-卖单
- * @param int $bsid 承兑商用户ID
- * @param int $status 状态:4-成交
- * @return mixed
- */
- public function getCompleteCount($type = 1, $bsid = 0, $status = 4)
- {
- return (int)$this->model->where(['type' => $type, 'status' => $status, 'mark' => 1])
- ->where(function ($query) use ($bsid) {
- if ($bsid > 0) {
- $query->where('business_id', '=', $bsid);
- }
- })->count('id');
- }
- /**
- * 成交交易金额
- * @param int $type 类型:1-买单,2-卖单
- * @param int $bsid 承兑商用户ID
- * @param int $status 状态:4-成交
- * @param string $field 统计字段:total-总金额(默认),num-交易USDT数量
- * @return mixed
- */
- public function getCompleteTotal($type = 1, $bsid = 0, $status = 4, $field = 'total')
- {
- $total = $this->model->where(['type' => $type, 'status' => $status, 'mark' => 1])
- ->where(function ($query) use ($bsid) {
- if ($bsid > 0) {
- $query->where('business_id', '=', $bsid);
- }
- })->sum($field);
- return moneyFormat($total, 2);
- }
- /**
- * 今日成交交易金额
- * @param int $type 类型:1-买单,2-卖单
- * @param int $bsid 承兑商用户ID
- * @param int $status 状态:4-成交
- * @param string $field 统计字段:total-总金额(默认),num-交易USDT数量
- * @return mixed
- */
- public function getCompleteTotalByDay($type = 1, $bsid = 0, $status = 4, $field = 'total')
- {
- $total = $this->model->where(['type' => $type, 'status' => $status, 'mark' => 1])
- ->where('pay_time', '>=', strtotime(date('Y-m-d')))
- ->where(function ($query) use ($bsid) {
- if ($bsid > 0) {
- $query->where('business_id', '=', $bsid);
- }
- })->sum($field);
- return moneyFormat($total, 6);
- }
- /**
- * 24小时成交交易金额
- * @param int $time 时间/小时
- * @param int $bsid 承兑商用户ID
- * @param int $status 状态:4-成交
- * @param string $field 统计字段:total-总金额(默认),num-交易USDT数量
- * @return mixed
- */
- public function getCompleteTotalByTime($time = 24, $bsid = 0, $status = 4, $field = 'total')
- {
- $total = $this->model->where(['status' => $status, 'mark' => 1])
- ->where('pay_time', '>=', time() - $time * 3600)
- ->where(function ($query) use ($bsid) {
- if ($bsid > 0) {
- $query->where('business_id', '=', $bsid);
- }
- })->sum($field);
- return moneyFormat($total, 4);
- }
- /**
- * 24小时成交交易金额
- * @param int $time 时间/小时
- * @param int $bsid 承兑商用户ID
- * @param int $status 状态:4-成交
- * @param string $field 统计字段:total-总金额(默认),num-交易USDT数量
- * @return mixed
- */
- public function getCompleteCountByTime($time = 24, $bsid = 0, $status = 4, $field = 'id')
- {
- $total = $this->model->where(['status' => $status, 'mark' => 1])
- ->where('pay_time', '>=', time() - $time * 3600)
- ->where(function ($query) use ($bsid) {
- if ($bsid > 0) {
- $query->where('business_id', '=', $bsid);
- }
- })->count($field);
- return intval($total);
- }
- /**
- * 获取某个时间段的买卖交易量比
- * @param int $type 状态类型:1-成交的
- * @param int $bsid 承兑商用户ID
- * @param int $time 时间/分钟,默认15分钟
- * @return array
- */
- public function getCountRateByTime($type = 1, $bsid = 0, $time = 15)
- {
- $where = ['mark' => 1];
- if ($type == 1) {
- $where['status'] = 4;
- }
- $buyCount = $this->model->where(['type' => 1])->where($where)
- ->where(function ($query) use ($bsid) {
- if ($bsid > 0) {
- $query->where('business_id', '=', $bsid);
- }
- })
- ->where('pay_time', '>=', time() - $time * 60)
- ->count('id');
- $sellCount = $this->model->where(['type' => 2])->where($where)
- ->where(function ($query) use ($bsid) {
- if ($bsid > 0) {
- $query->where('business_id', '=', $bsid);
- }
- })
- ->where('pay_time', '>=', time() - $time * 60)
- ->count('id');
- $rate = ($buyCount || $sellCount) > 0 ? moneyFormat($buyCount / ($buyCount + $sellCount)) * 100 : 0;
- return ['buy' => $buyCount, 'sell' => $sellCount, 'buy_rate' => $rate, 'sell_rate' => ($sellCount > 0 ? 100 - $rate : 0)];
- }
- /**
- * 获取某个时间段的交易成功率
- * @param int $type 状态类型:1-成交的
- * @param int $bsid 承兑商用户ID
- * @param int $time 时间/小时,默认24小时
- * @return array
- */
- public function getRateByTime($bsid = 0, $time = 24)
- {
- $successCount = $this->model->where(['status' => 4,'mark'=>1])
- ->where(function ($query) use ($bsid) {
- if ($bsid > 0) {
- $query->where('business_id', '=', $bsid);
- }
- })
- ->where('pay_time', '>=', time() - $time * 3600)
- ->count('id');
- $count = $this->model->where(['mark' => 1])
- ->where(function ($query) use ($bsid) {
- if ($bsid > 0) {
- $query->where('business_id', '=', $bsid);
- }
- })
- ->where(function($query) use($time){
- $time = time() - $time * 3600;
- $query->where('pay_time', '>=', $time)
- ->orWhereRaw("(pay_time = 0 and create_time >={$time})");
- })
- ->count('id');
- $rate = $count > 0 ? moneyFormat($successCount / $count * 100) : 0;
- return ['rate' => $rate, 'success' => $successCount, 'count' => $count];
- }
- /**
- * 获取未支付或处理的订单数
- * @param $userId
- * @param int $type
- * @return mixed
- */
- public function checkOrderNoCatch($userId, $type = 1)
- {
- return $this->model->where(['user_id' => $userId, 'type' => $type, 'mark' => 1])->whereIn('status', [1, 2, 7])->count('id');
- }
- /**
- * 客户买入
- * @param $userId
- * @param $params
- * @return false|int|number
- */
- public function buy($userId, $params)
- {
- $num = isset($params['num']) ? floatval($params['num']) : 0;
- $numType = isset($params['num_type']) ? intval($params['num_type']) : 1;
- if ($userId <= 0) {
- $this->error = '1013';
- return false;
- }
- // 验证参数
- $config = ConfigService::make()->getConfigOptionByGroup(5);
- $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 0;
- $tradeMinNum = isset($config['trade_min_num']) ? $config['trade_min_num'] : 0;
- $tradeMaxNum = isset($config['trade_max_num']) ? $config['trade_max_num'] : 0;
- $trademinMoney = isset($config['trade_min_money']) ? $config['trade_min_money'] : 0;
- $tradeMaxMoney = isset($config['trade_max_money']) ? $config['trade_max_money'] : 0;
- $tradePrice = isset($config['usdt_buy_price']) ? $config['usdt_buy_price'] : 0;
- $tradeLimitNum = isset($config['trade_no_catch']) ? $config['trade_no_catch'] : 0;
- // 是否开启交易
- if ($tradeOpen != 1) {
- $this->error = '1013';
- return false;
- }
- if ($tradePrice <= 0) {
- $this->error = '3002';
- return false;
- }
- // 验证数量或金额
- $total = 0;
- if ($numType == 1) {
- if ($num < $tradeMinNum || $num > $tradeMaxNum) {
- $this->error = '3003';
- return false;
- }
- $total = moneyFormat($num * $tradePrice, 6);
- } else {
- if ($num < $trademinMoney || $num > $tradeMaxMoney) {
- $this->error = '3003';
- return false;
- }
- $total = moneyFormat($num, 6);
- $num = moneyFormat($num / $tradePrice, 6);
- }
- // 用户信息
- $userInfo = MemberService::make()->getInfo($userId);
- $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
- $idcardCheck = isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0;
- $username = isset($userInfo['username']) && $userInfo['username'] ? format_account($userInfo['username']) : '';
- if ($status != 1) {
- $this->error = '2009';
- return false;
- }
- if ($idcardCheck != 1) {
- $this->error = '2014';
- return false;
- }
- // 未处理订单
- $noCatchOrder = $this->checkOrderNoCatch($userId, 1);
- if ($tradeLimitNum > 0 && $noCatchOrder >= $tradeLimitNum) {
- $this->error = lang(3005, ['num' => $tradeLimitNum]);
- return false;
- }
- // 匹配交易商家
- $businessInfo = \App\Services\Api\MemberService::make()->getTradeMember($num, 1, $userId);
- if (empty($businessInfo)) {
- $this->error = '3004';
- return false;
- }
- if($businessInfo['usdt_num']<$num){
- $this->error = '3004';
- return false;
- }
- // 客户身份信息
- $idcardData = [
- 'idcard' => isset($userInfo['idcard']) ? $userInfo['idcard'] : '',
- 'idcard_check' => isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0,
- 'idcard_front_img' => isset($userInfo['idcard_front_img']) ? $userInfo['idcard_front_img'] : '',
- 'idcard_back_img' => isset($userInfo['idcard_back_img']) ? $userInfo['idcard_back_img'] : '',
- 'idcard_hand_img' => isset($userInfo['idcard_hand_img']) ? $userInfo['idcard_hand_img'] : '',
- ];
- // 币商收款方式
- $payment = MemberPaymentService::make()->getPayment($businessInfo['id']);
- if (empty($payment)) {
- $this->error = '3015';
- return false;
- }
- $this->model->startTrans();
- $orderNo = get_order_num('OT');
- $data = [
- 'user_id' => $userId,
- 'business_id' => isset($businessInfo['id']) ? $businessInfo['id'] : 0,
- 'order_no' => $orderNo,
- 'type' => 1,
- 'pay_type' => isset($params['pay_type']) ? floatval($params['pay_type']) : 1,
- 'price' => $tradePrice,
- 'num' => $num,
- 'total' => $total,
- 'payment_id' => isset($payment['id']) ? intval($payment['id']) : 0,
- 'idcard_data' => $idcardData ? json_encode($idcardData, 256) : '',
- 'payment_data' => $payment ? json_encode($payment, 256) : '',
- 'create_time' => time(),
- 'update_time' => time(),
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!$order = $this->model->edit($data)) {
- $this->model->rollBack();
- $this->error = '3023';
- return false;
- }
- if(!$this->memberModel->where(['id'=> $businessInfo['id']])->decrement('usdt_num', $num)){
- $this->model->rollBack();
- $this->error = '3020';
- return false;
- }
- $data = [
- 'order_no'=> $orderNo,
- 'user_id'=> $businessInfo['id'],
- 'type'=> 2,
- 'pay_type'=> 1,
- 'change_type'=> 2,
- 'num'=> $num,
- 'total'=> $total,
- 'balance'=> floatval($businessInfo['usdt_num']-$num),
- 'create_time'=> time(),
- 'update_time'=> time(),
- 'status'=> 1,
- 'mark'=>1,
- 'remark'=> '交易员卖出',
- ];
- if(!$this->capitalModel->edit($data)){
- $this->model->rollBack();
- $this->error = '3014';
- return false;
- }
- // 订单通知
- $data = [
- 'from_uid' => $userId,
- 'to_uid' => $businessInfo['id'],
- 'type' => 2,
- 'order_no' => $orderNo,
- 'chat_key' => ChatMessageService::make()->getChatKey($userId, $businessInfo['id']),
- 'message' => "来自用户{$username}的购买订单,金额{$total},单号{$orderNo},时间" . date('Y-m-d H:i:s'),
- 'message_type' => 1,
- 'create_time' => time(),
- 'update_time' => time(),
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!ChatMessageService::make()->pushMessage($data)) {
- $this->model->rollBack();
- $this->error = '3031';
- return false;
- }
- $this->model->commit();
- return $order;
- }
- /**
- * 客户卖出
- * @param $userId
- * @param $params
- * @return false|int|number
- */
- public function sell($userId, $params)
- {
- $num = isset($params['num']) ? floatval($params['num']) : 0;
- if ($userId <= 0) {
- $this->error = '1013';
- return false;
- }
- // 验证参数
- $config = ConfigService::make()->getConfigOptionByGroup(5);
- $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 0;
- $tradeMinNum = isset($config['trade_min']) ? $config['trade_min'] : 0;
- $tradeMaxNum = isset($config['trade_max']) ? $config['trade_max'] : 0;
- $tradePrice = isset($config['usdt_sell_price']) ? $config['usdt_sell_price'] : 0;
- $tradeLimitNum = isset($config['trade_no_catch']) ? $config['trade_no_catch'] : 0;
- // 是否开启交易
- if ($tradeOpen != 1) {
- $this->error = '1013';
- return false;
- }
- if ($tradePrice <= 0) {
- $this->error = '3002';
- return false;
- }
- // 验证数量或金额
- $total = moneyFormat($num * $tradePrice, 6);
- if ($num < $tradeMinNum || ($tradeMaxNum && $num > $tradeMaxNum)) {
- $this->error = '3003';
- return false;
- }
- // 用户信息
- $userInfo = MemberService::make()->getInfo($userId);
- $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
- $usdtNum = isset($userInfo['usdt_num']) ? $userInfo['usdt_num'] : 0;
- $username = isset($userInfo['username']) && $userInfo['username'] ? format_account($userInfo['username']) : '';
- if ($status != 1) {
- $this->error = '2009';
- return false;
- }
- if ($usdtNum < $num) {
- $this->error = '3011';
- return false;
- }
- // 未处理订单
- $noCatchOrder = $this->checkOrderNoCatch($userId, 2);
- if ($tradeLimitNum > 0 && $noCatchOrder >= $tradeLimitNum) {
- $this->error = lang(3005, ['num' => $tradeLimitNum]);
- return false;
- }
- // 匹配交易商家
- $businessInfo = \App\Services\Api\MemberService::make()->getTradeMember($num, 2, $userId);
- if (empty($businessInfo)) {
- $this->error = '3004';
- return false;
- }
- $idcardData = [
- 'idcard' => isset($userInfo['idcard']) ? $userInfo['idcard'] : '',
- 'idcard_check' => isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0,
- 'idcard_front_img' => isset($userInfo['idcard_front_img']) ? $userInfo['idcard_front_img'] : '',
- 'idcard_back_img' => isset($userInfo['idcard_back_img']) ? $userInfo['idcard_back_img'] : '',
- 'idcard_hand_img' => isset($userInfo['idcard_hand_img']) ? $userInfo['idcard_hand_img'] : '',
- ];
- // 收款方式
- $paymentId = isset($params['payment_id']) ? $params['payment_id'] : 0;
- $paymentInfo = MemberPaymentService::make()->getInfo($paymentId);
- if (empty($paymentInfo)) {
- $this->error = '3010';
- return false;
- }
- $paymentData = [
- 'payment_id' => $paymentInfo['id'],
- 'type' => $paymentInfo['type'],
- 'logo' => $paymentInfo['logo'] ? get_image_url($paymentInfo['logo']) : '',
- 'real_name' => $paymentInfo['real_name'],
- 'bank_name' => $paymentInfo['bank_name'],
- 'bank_card' => $paymentInfo['bank_card'],
- 'branch_name' => $paymentInfo['branch_name'],
- 'qrcode' => $paymentInfo['qrcode'] ? get_image_url($paymentInfo['qrcode']) : '',
- 'account' => $paymentInfo['account'],
- ];
- $orderNo = get_order_num('OT');
- $data = [
- 'user_id' => $userId,
- 'business_id' => isset($businessInfo['id']) ? $businessInfo['id'] : 0,
- 'order_no' => $orderNo,
- 'type' => 2,
- 'pay_type' => isset($params['pay_type']) ? floatval($params['pay_type']) : 1,
- 'price' => $tradePrice,
- 'num' => $num,
- 'payment_id' => $paymentId,
- 'idcard_data' => json_encode($idcardData, 256),
- 'payment_data' => json_encode($paymentData, 256),
- 'total' => $total,
- 'create_time' => time(),
- 'status' => 1,
- 'mark' => 1,
- ];
- $this->model->startTrans();
- if (!$order = $this->model->edit($data)) {
- $this->error = '3012';
- $this->model->rollBack();
- return false;
- }
- // 扣除币
- if (!$this->memberModel->where(['id' => $userId])->decrement('usdt_num', $num)) {
- $this->error = '3013';
- $this->model->rollBack();
- return false;
- }
- // 账户明细
- $data = [
- 'order_no' => $data['order_no'],
- 'user_id' => $userId,
- 'type' => 2,
- 'change_type' => 2,
- 'num' => $num,
- 'total' => $total,
- 'balance' => floatval($userInfo['usdt_num'] - $num),
- 'create_time' => time(),
- 'update_time' => time(),
- 'remark' => '客户卖出',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!$this->capitalModel->edit($data)) {
- $this->error = '3014';
- $this->model->rollBack();
- return false;
- }
- // 订单通知
- $data = [
- 'from_uid' => $userId,
- 'to_uid' => $businessInfo['id'],
- 'type' => 2,
- 'order_no' => $orderNo,
- 'chat_key' => ChatMessageService::make()->getChatKey($userId, $businessInfo['id']),
- 'message' => "来自用户{$username}的出售订单,金额{$total},单号{$orderNo},时间" . date('Y-m-d H:i:s'),
- 'message_type' => 1,
- 'create_time' => time(),
- 'update_time' => time(),
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!ChatMessageService::make()->pushMessage($data)) {
- $this->model->rollBack();
- $this->error = '3023';
- return false;
- }
- $this->model->commit();
- return $order;
- }
- /**
- * 订单打款处理
- * @param $userId 用户ID
- * @param $params 打款参数
- * @return false
- */
- public function pay($userId, $params)
- {
- $orderId = isset($params['id']) ? $params['id'] : 0;
- if ($orderId <= 0) {
- $this->error = '1013';
- return false;
- }
- $orderInfo = $this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1, 'type' => 1])
- ->whereIn('status', [1, 2, 5, 7])
- ->select(['id', 'order_no', 'business_id', 'type', 'payment_id', 'num', 'total', 'status'])
- ->first();
- $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
- if (empty($orderInfo)) {
- $this->error = '3016';
- return false;
- }
- if ($tradeType != 1) {
- $this->error = '3024';
- return false;
- }
- // 用户信息
- $userInfo = MemberService::make()->getInfo($userId);
- $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
- if ($status != 1) {
- $this->error = '2009';
- return false;
- }
- // 交易密码
- $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
- $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
- if (empty($password)) {
- $this->error = '2015';
- return false;
- }
- if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
- $this->error = '2016';
- return false;
- }
- $data = [
- 'status' => 3,
- 'pay_type' => $params['pay_type'],
- 'pay_img' => $params['pay_img'],
- 'pay_remark' => $params['pay_remark'],
- 'pay_time' => time(),
- ];
- if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update($data)) {
- $this->error = '3018';
- return false;
- }
- return true;
- }
- /**
- * 商家订单打款处理
- * @param $userId 用户ID
- * @param $params 打款参数
- * @return false
- */
- public function businessPay($businessId, $params)
- {
- $orderId = isset($params['id']) ? $params['id'] : 0;
- if ($orderId <= 0) {
- $this->error = '1013';
- return false;
- }
- $orderInfo = $this->model->where(['business_id' => $businessId, 'id' => $orderId, 'mark' => 1, 'type' => 2])
- ->whereIn('status', [1, 2, 5, 7])
- ->select(['id', 'user_id', 'order_no', 'business_id', 'type', 'payment_id', 'num', 'total', 'status'])
- ->first();
- $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
- if (empty($orderInfo)) {
- $this->error = '3016';
- return false;
- }
- if ($tradeType != 2) {
- $this->error = '3025';
- return false;
- }
- // 用户信息
- $userInfo = MemberService::make()->getInfo($businessId);
- $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
- $username = isset($userInfo['username']) ? $userInfo['username'] : 0;
- if ($status != 1) {
- $this->error = '2009';
- return false;
- }
- // 交易密码
- $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
- $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
- if (empty($password)) {
- $this->error = '2015';
- return false;
- }
- if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
- $this->error = '2016';
- return false;
- }
- if (strpos($params['pay_img'], "temp")) {
- $params['pay_img'] = save_image($params['pay_img'], 'images');
- } else {
- $params['pay_img'] = str_replace(IMG_URL, "", $params['pay_img']);
- }
- $data = [
- 'status' => 3,
- 'pay_type' => $params['pay_type'],
- 'pay_img' => $params['pay_img'],
- 'pay_remark' => $params['pay_remark'],
- 'pay_time' => time(),
- ];
- if (!$this->model->where(['business_id' => $businessId, 'id' => $orderId, 'mark' => 1])->update($data)) {
- $this->error = '3018';
- return false;
- }
- $this->error = '3017';
- return true;
- }
- /**
- * 订单确认处理
- * @param $userId 用户ID
- * @param $params 打款参数
- * @return false
- */
- public function collection($userId, $params)
- {
- $orderId = isset($params['id']) ? $params['id'] : 0;
- if ($orderId <= 0) {
- $this->error = '1013';
- return false;
- }
- $orderInfo = $this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1, 'type' => 2])
- ->select(['id', 'user_id', 'order_no', 'business_id', 'payment_id', 'type', 'num', 'total', 'status'])
- ->first();
- $businessId = isset($orderInfo['business_id']) ? $orderInfo['business_id'] : 0;
- $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
- if (empty($orderInfo) || empty($businessId)) {
- $this->error = '3016';
- return false;
- }
- if ($orderInfo['status'] != 3) {
- $this->error = '3026';
- return false;
- }
- if ($tradeType != 2) {
- $this->error = '3024';
- return false;
- }
- $this->model->startTrans();
- // 订单状态更新
- if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update(['status' => 4, 'update_time' => time()])) {
- $this->model->rollBack();
- $this->error = '3023';
- return false;
- }
- // 交易处理
- if ($orderInfo['num'] > 0) {
- $info = $this->memberModel->where(['id' => $businessId, 'status' => 1, 'mark' => 1])->select(['id', 'username','usdt_num', 'user_type'])->first();
- if (empty($info)) {
- $this->model->rollBack();
- $this->error = '3019';
- return false;
- }
- // 商家进币
- if (!$this->memberModel->where(['id' => $businessId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
- $this->model->rollBack();
- $this->error = '3019';
- return false;
- }
- // 明细处理
- $data = [
- 'order_no' => $orderInfo['order_no'],
- 'user_id' => $businessId,
- 'type' => 1,
- 'pay_type' => 1,
- 'change_type' => 1,
- 'num' => $orderInfo['num'],
- 'total' => $orderInfo['total'],
- 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
- 'create_time' => time(),
- 'remark' => '交易员买入',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!$this->capitalModel->edit($data)) {
- $this->error = '3014';
- $this->model->rollBack();
- return false;
- }
- }
- $this->model->commit();
- return true;
- }
- /**
- * 商家订单确认处理
- * @param $businessId 币商用户ID,平台处理时为0
- * @param $params 打款参数
- * @return false
- */
- public function businessCollection($businessId, $params)
- {
- $orderId = isset($params['id']) ? $params['id'] : 0;
- $catchUid = isset($params['catch_uid']) ? $params['catch_uid'] : 0;
- $type = isset($params['type']) ? $params['type'] : 0;
- if ($orderId <= 0) {
- $this->error = '1013';
- return false;
- }
- // 平台或币商处理
- $where = ['id' => $orderId, 'mark' => 1];
- if ($businessId) {
- $where['business_id'] = $businessId;
- }
- if($type){
- $where['type'] = $type;
- }
- $orderInfo = $this->model->where($where)
- ->select(['id', 'user_id', 'order_no', 'business_id', 'type', 'num', 'total', 'status'])
- ->first();
- $userId = isset($orderInfo['user_id']) ? $orderInfo['user_id'] : 0;
- $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
- if (empty($orderInfo) || empty($userId)) {
- $this->error = '3016';
- return false;
- }
- $status = isset($params['status']) ? intval($params['status']) : 4;
- if ($status == 4 && $orderInfo['status'] != 3 && $businessId > 0) {
- $this->error = '3026';
- return false;
- }
- if($status == 5 && $orderInfo['status'] == 8 && $businessId>0){
- $this->error = '3026';
- return false;
- }
- if($status == 5 && $businessId<=0 && $orderInfo['status'] == 4){
- $this->error = '3026';
- return false;
- }
- if ($tradeType != 1 && $status != 5) {
- $this->error = '3024';
- return false;
- }
- // 用户信息
- if($businessId>0){
- $userInfo = MemberService::make()->getInfo($businessId);
- $userStatus = isset($userInfo['status']) ? $userInfo['status'] : 0;
- $username = isset($userInfo['username']) ? $userInfo['username'] : 0;
- if ($userStatus != 1) {
- $this->error = '2009';
- return false;
- }
- // 交易密码
- $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
- $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
- if (empty($password)) {
- $this->error = '2015';
- return false;
- }
- if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
- $this->error = '2016';
- return false;
- }
- }
- $this->model->startTrans();
- // 订单状态更新,异常或确认
- $updateData = ['status' => $status, 'exception_catch_user' => $catchUid, 'update_time' => time()];
- // 异常处理数据
- if (isset($params['exception_type'])) {
- $updateData['exception_type'] = intval($params['exception_type']);
- }
- if (isset($params['exception_sub_type'])) {
- $updateData['exception_sub_type'] = intval($params['exception_sub_type']);
- }
- if (isset($params['exception_img'])) {
- $updateData['exception_img'] = trim($params['exception_img']);
- if (strpos($updateData['exception_img'], "temp")) {
- $updateData['exception_img'] = save_image($updateData['exception_img'], 'images');
- } else {
- $updateData['exception_img'] = str_replace(IMG_URL, "", $updateData['exception_img']);
- }
- }
- if (isset($params['exception_remark'])) {
- $updateData['exception_remark'] = trim($params['exception_remark']);
- }
- if (isset($params['exception_status'])) {
- $updateData['exception_status'] = intval($params['exception_status']);
- }
- if (!$this->model->where($where)->update($updateData)) {
- $this->model->rollBack();
- $this->error = '3023';
- return false;
- }
- // 交易处理,如果是确认状态
- if ($orderInfo['num'] > 0 && $status == 4) {
- $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
- if (empty($info)) {
- $this->model->rollBack();
- $this->error = '3019';
- return false;
- }
- // 客户币进账
- if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
- $this->model->rollBack();
- $this->error = '3019';
- return false;
- }
- // 明细处理
- $data = [
- 'order_no' => $orderInfo['order_no'],
- 'user_id' => $userId,
- 'type' => 1,
- 'pay_type' => 1,
- 'change_type' => 1,
- 'num' => $orderInfo['num'],
- 'total' => $orderInfo['total'],
- 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
- 'create_time' => time(),
- 'update_time' => time(),
- 'remark' => '客户买入',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!$this->capitalModel->edit($data)) {
- $this->error = '3014';
- $this->model->rollBack();
- return false;
- }
- // 更新收款地址数据
- $paymentId = isset($orderInfo['payment_id']) ? $orderInfo['payment_id'] : 0;
- $info = $this->paymentModel->where(['id' => $paymentId, 'mark' => 1])->select(['id', 'used_at'])->first();
- if ($paymentId && $info) {
- $usedAt = isset($info['used_at']) ? $info['used_at'] : '';
- if ($usedAt < date('Y-m-d')) {
- if (!$this->paymentModel->where(['id' => $paymentId])->update(['used_num' => 1, 'used_quota' => $orderInfo['total'], 'update_time' => time()])) {
- $this->model->rollBack();
- $this->error = '3018';
- return false;
- }
- } else {
- $res1 = $this->paymentModel->where(['id' => $paymentId])->increment('used_num', 1);
- $res2 = $this->paymentModel->where(['id' => $paymentId])->increment('used_quota', floatval($orderInfo['total']));
- if (!$res1 || !$res2) {
- $this->model->rollBack();
- $this->error = '3014';
- return false;
- }
- }
- }
- }
- $this->model->commit();
- $this->error = $status == 5 ? 3032 : 3021;
- return true;
- }
- /**
- * 取消订单
- * @param $userId
- * @param $params
- * @return false
- */
- public function cancel($userId, $params)
- {
- $orderId = isset($params['id']) ? intval($params['id']) : 0;
- if ($orderId <= 0) {
- $this->error = '1013';
- return false;
- }
- $orderInfo = $this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])
- ->select(['id', 'order_no', 'business_id', 'type', 'num', 'total', 'status'])
- ->first();
- $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
- $businessId = isset($orderInfo['business_id']) ? $orderInfo['business_id'] : 0;
- if (empty($orderInfo) || $businessId<=0) {
- $this->error = '3016';
- return false;
- }
- if ($orderInfo['status'] == 3) {
- $this->error = '3027';
- return false;
- }
- if ($orderInfo['status'] == 4) {
- $this->error = '3028';
- return false;
- }
- if ($orderInfo['status'] == 7) {
- $this->error = '3030';
- return false;
- }
- if (!in_array($orderInfo['status'], [1, 2])) {
- $this->error = '3029';
- return false;
- }
- $this->model->startTrans();
- // 订单状态更新
- $updateData = ['status' => 8, 'update_time' => time(), 'exception_remark' => '客户取消'];
- if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update($updateData)) {
- $this->model->rollBack();
- $this->error = '3023';
- return false;
- }
- // 出售订单,USDT退回
- if ($tradeType == 2 && $orderInfo['num']>0) {
- $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
- if (empty($info)) {
- $this->model->rollBack();
- $this->error = '3019';
- return false;
- }
- // 退还币给客户
- if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
- $this->model->rollBack();
- $this->error = '3019';
- return false;
- }
- // 明细处理
- $data = [
- 'order_no' => $orderInfo['order_no'],
- 'user_id' => $userId,
- 'type' => 3,
- 'pay_type' => 1,
- 'change_type' => 1,
- 'num' => $orderInfo['num'],
- 'total' => $orderInfo['total'],
- 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
- 'create_time' => time(),
- 'update_time' => time(),
- 'remark' => '客户取消退还',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!$this->capitalModel->edit($data)) {
- $this->error = '3014';
- $this->model->rollBack();
- return false;
- }
- }
- // 买入取消
- else if($tradeType == 1 && $orderInfo['num']>0){
- $info = $this->memberModel->where(['id' => $businessId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
- if (empty($info)) {
- $this->model->rollBack();
- $this->error = '3019';
- return false;
- }
- // 退还币给客户
- if (!$this->memberModel->where(['id' => $businessId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
- $this->model->rollBack();
- $this->error = '3019';
- return false;
- }
- // 明细处理
- $data = [
- 'order_no' => $orderInfo['order_no'],
- 'user_id' => $businessId,
- 'type' => 3,
- 'pay_type' => 1,
- 'change_type' => 1,
- 'num' => $orderInfo['num'],
- 'total' => $orderInfo['total'],
- 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
- 'create_time' => time(),
- 'update_time' => time(),
- 'remark' => '客户取消退还',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!$this->capitalModel->edit($data)) {
- $this->error = '3014';
- $this->model->rollBack();
- return false;
- }
- }
- $this->model->commit();
- return true;
- }
- /**
- * 取消订单
- * @param $businessId 币商用户,平台为0
- * @param $params
- * @return false
- */
- public function businessCancel($businessId, $params)
- {
- $orderId = isset($params['id']) ? intval($params['id']) : 0;
- $catchUid = isset($params['catch_uid']) ? intval($params['catch_uid']) : 0;
- if ($orderId <= 0) {
- $this->error = '1013';
- return false;
- }
- $where = ['id' => $orderId, 'mark' => 1];
- if ($businessId) {
- $where['business_id'] = $businessId;
- }
- $orderInfo = $this->model->where($where)
- ->select(['id', 'user_id', 'order_no', 'business_id', 'type', 'num', 'total', 'status'])
- ->first();
- $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
- $userId = isset($orderInfo['user_id']) ? $orderInfo['user_id'] : 0;
- $orderBusinessId = isset($orderInfo['business_id']) ? $orderInfo['business_id'] : 0;
- if (empty($orderInfo) || $orderBusinessId<=0) {
- $this->error = '3016';
- return false;
- }
- if (empty($userId)) {
- $this->error = '3019';
- return false;
- }
- if ($orderInfo['status'] == 4) {
- $this->error = '3028';
- return false;
- }
- if (!in_array($orderInfo['status'], [1, 2, 3, 5, 7])) {
- $this->error = '3029';
- return false;
- }
- // 用户信息
- $status = isset($params['status']) ? $params['status'] : 8;
- if($businessId>0 && $status != 8){
- $userInfo = MemberService::make()->getInfo($businessId);
- $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
- $username = isset($userInfo['username']) ? $userInfo['username'] : 0;
- if ($status != 1) {
- $this->error = '2009';
- return false;
- }
- // 交易密码
- $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
- $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
- if (empty($password)) {
- $this->error = '2015';
- return false;
- }
- if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
- $this->error = '2016';
- return false;
- }
- }
- $this->model->startTrans();
- // 订单状态更新,或异常处理
- $updateData = ['status' => $status, 'exception_catch_user' => $catchUid, 'update_time' => time(), 'exception_remark' => '交易员取消'];
- // 异常处理数据
- if (isset($params['exception_type'])) {
- $updateData['exception_type'] = intval($params['exception_type']);
- }
- if (isset($params['exception_sub_type'])) {
- $updateData['exception_sub_type'] = intval($params['exception_sub_type']);
- }
- if (isset($params['exception_img'])) {
- $updateData['exception_img'] = trim($params['exception_img']);
- if (strpos($updateData['exception_img'], "temp")) {
- $updateData['exception_img'] = save_image($updateData['exception_img'], 'images');
- } else {
- $updateData['exception_img'] = str_replace(IMG_URL, "", $updateData['exception_img']);
- }
- }
- if (isset($params['exception_remark'])) {
- $updateData['exception_remark'] = trim($params['exception_remark']);
- }
- if (isset($params['exception_status'])) {
- $updateData['exception_status'] = intval($params['exception_status']);
- }
- if (isset($params['refund_status'])) {
- $updateData['refund_status'] = intval($params['refund_status']);
- }
- if (isset($params['refund_money'])) {
- $updateData['refund_money'] = intval($params['refund_money']);
- }
- if (!$this->model->where($where)->update($updateData)) {
- $this->model->rollBack();
- $this->error = '3023';
- return false;
- }
- // 出售订单,状态已取消,USDT退回
- if ($tradeType == 2 && $status == 8 && $orderInfo['num']>0) {
- $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
- if (empty($info)) {
- $this->model->rollBack();
- $this->error = '3019';
- return false;
- }
- // 退还币给客户
- if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
- $this->model->rollBack();
- $this->error = '3019';
- return false;
- }
- // 明细处理
- $data = [
- 'order_no' => $orderInfo['order_no'],
- 'user_id' => $userId,
- 'type' => 3,
- 'pay_type' => 1,
- 'change_type' => 1,
- 'num' => $orderInfo['num'],
- 'total' => $orderInfo['total'],
- 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
- 'create_time' => time(),
- 'update_time' => time(),
- 'remark' => '交易员取消买入退还',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!$this->capitalModel->edit($data)) {
- $this->error = '3014';
- $this->model->rollBack();
- return false;
- }
- }
- // 客户买入订单取消
- else if($tradeType == 1 && $status == 8 && $orderInfo['num']>0){
- $info = $this->memberModel->where(['id' => $orderBusinessId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
- if (empty($info)) {
- $this->model->rollBack();
- $this->error = '3019';
- return false;
- }
- // 退还币给客户
- if (!$this->memberModel->where(['id' => $orderBusinessId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
- $this->model->rollBack();
- $this->error = '3019';
- return false;
- }
- // 明细处理
- $data = [
- 'order_no' => $orderInfo['order_no'],
- 'user_id' => $orderBusinessId,
- 'type' => 3,
- 'pay_type' => 1,
- 'change_type' => 1,
- 'num' => $orderInfo['num'],
- 'total' => $orderInfo['total'],
- 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
- 'create_time' => time(),
- 'update_time' => time(),
- 'remark' => '交易员取消卖出退还',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!$this->capitalModel->edit($data)) {
- $this->error = '3014';
- $this->model->rollBack();
- return false;
- }
- }
- $this->model->commit();
- $this->error = $status == 5 ? 3032 : 3034;
- return true;
- }
- /**
- * 无效订单处理
- * @return false
- */
- public function catchInvalidOrder()
- {
- $cacheKey = "caches:trades:cancels:";
- if(RedisService::get($cacheKey.'lock')){
- return false;
- }
- RedisService::set($cacheKey.'lock', 1, rand(3, 5));
- $overtime = ConfigService::make()->getConfigByCode('trade_order_overtime');
- $cancelTime = ConfigService::make()->getConfigByCode('trade_order_cancel');
- $catchNum = ConfigService::make()->getConfigByCode('trade_order_catch_num');
- $catchNum = $catchNum > 0 ? $catchNum : 200;
- // 处理超时订单
- if ($overtime > 0) {
- $this->model->where(['mark' => 1])
- ->where('status', '<=', 2)
- ->where('create_time', '<=', time() - $overtime * 60)
- ->update(['status' => 7, 'catch_at' => time()]);
- }
- if ($cancelTime <= 0) {
- $this->error = '1023';
- return false;
- }
- $fail = 0;
- $success = 0;
- $this->model->where(function ($query) use ($cancelTime) {
- // 已更新为超时的订单
- $query->where(['mark' => 1, 'status' => 7])
- ->where('catch_at', '<=', time() - $cancelTime * 60);
- })
- ->orWhere(function ($query) use ($cancelTime, $overtime) {
- $query->where('mark', '=', 1)
- ->where('status', '<=', 2)
- ->where('create_time', '<=', time() - ($cancelTime + $overtime) * 60);
- })
- ->select(['id', 'user_id', 'business_id','order_no', 'type', 'num','total'])
- ->get()
- ->limit($catchNum)
- ->each(function ($item, $k) use($cacheKey, &$fail, &$success){
- // 客户卖出订单退还
- $date = date('Y-m-d H:i:s');
- $type = isset($item['type']) ? $item['type'] : 0;
- if ($type == 2) {
- if(!$this->orderReback($item['user_id'], $item)){
- $fail++;
- RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
- }else{
- $success++;
- RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
- }
- }
- else{
- if(!$this->orderReback($item['business_id'], $item)){
- $fail++;
- RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
- }else{
- $success++;
- RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
- }
- }
- });
- return ['success'=> $success,'fail'=> $fail];
- }
- /**
- * 订单取消退还处理
- * @param $userId
- * @param $orderInfo
- * @return bool
- */
- protected function orderReback($userId, $orderInfo){
- try {
- if($orderInfo['num']<=0){
- return false;
- }
- $this->model->startTrans();
- $updateData = ['status'=> 8, 'update_time'=> time(),'catch_at'=>time(),'exception_remark'=>'系统取消'];
- if(!$this->model->where(['id'=> $orderInfo['id']])->update($updateData)){
- $this->model->rollBack();
- $this->error = '3043';
- return false;
- }
- $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
- if (empty($info)) {
- $this->model->rollBack();
- $this->error = '3019';
- return false;
- }
- // 退还币给客户
- if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
- $this->model->rollBack();
- $this->error = '3019';
- return false;
- }
- // 明细处理
- $data = [
- 'order_no' => $orderInfo['order_no'],
- 'user_id' => $userId,
- 'type' => 3,
- 'pay_type' => 1,
- 'change_type' => 1,
- 'num' => $orderInfo['num'],
- 'total' => $orderInfo['total'],
- 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
- 'create_time' => time(),
- 'update_time' => time(),
- 'remark' => '系统自动取消退还',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!$this->capitalModel->edit($data)) {
- $this->error = '3014';
- $this->model->rollBack();
- return false;
- }
- $this->model->commit();
- $this->error = '3044';
- return true;
- } catch (\Exception $exception){
- $this->error = $exception->getMessage();
- return false;
- }
- }
- /**
- * 重新派单
- * @param $id 订单ID
- * @param $businessId 新派单用户
- * @return false
- */
- public function reassign($id, $businessId)
- {
- if (empty($id) || empty($businessId)) {
- $this->error = '1013';
- return false;
- }
- $orderInfo = $this->model->where(['id' => $id])
- ->select(['id', 'user_id', 'order_no', 'business_id', 'type', 'num', 'total', 'status'])
- ->first();
- $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
- if (empty($orderInfo)) {
- $this->error = '3016';
- return false;
- }
- // 状态
- if (!in_array($orderInfo['status'], [1, 2, 3, 5, 7])) {
- $this->error = '3039';
- return false;
- }
- if ($businessId == $orderInfo['business_id']) {
- $this->error = '3035';
- return false;
- }
- // 派单用户信息
- $businessInfo = $this->memberModel->from('member as a')
- ->leftJoin('member_setting as ms', 'ms.user_id', '=', 'a.id')
- ->where(['a.id' => $businessId, 'a.status' => 1, 'a.mark' => 1])
- ->select(['a.id', 'a.username', 'a.usdt_num', 'ms.buy_online', 'ms.buy_online_time', 'ms.sell_online', 'ms.sell_online_time'])
- ->first();
- if (empty($businessInfo)) {
- $this->error = '3036';
- return false;
- }
- // 余额
- if ($businessInfo['usdt_num'] < $orderInfo['num']) {
- $this->error = '3036';
- return false;
- }
- // 在线状态
- if ($tradeType == 1) {
- if ($businessInfo['buy_online'] != 1 || $businessInfo['buy_online_time'] <= time()) {
- $this->error = '3036';
- return false;
- }
- } else if ($tradeType == 2) {
- if ($businessInfo['sell_online'] != 1 || $businessInfo['sell_online_time'] <= time()) {
- $this->error = '3036';
- return false;
- }
- }
- $payment = MemberPaymentService::make()->getPayment($businessId);
- if (empty($payment)) {
- $this->error = '3040';
- return false;
- }
- $updateData = ['business_id'=> $businessId,'status'=> 1,'mark'=> 1,'create_time'=>time(),'pay_time'=>0,'pay_img'=>'','payment_data'=>json_encode($payment, 256),'exception_type'=>0,'exception_remark'=>'平台重新派单'];
- if ($this->model->where(['id' => $id])->update($updateData)){
- $this->error = '3041';
- return true;
- }
- $this->error = '3042';
- return false;
- }
- }
|