| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Common;
- use App\Models\AdvertModel;
- use App\Models\AdvertOrderModel;
- use App\Models\CapitalLogModel;
- use App\Models\MemberModel;
- use App\Models\MemberPaymentModel;
- use App\Services\Api\MemberPaymentService;
- use App\Services\BaseService;
- use App\Services\ConfigService;
- use App\Services\ChatMessageService;
- use App\Services\RedisService;
- use App\Services\UsdtWalletService;
- /**
- * 用户广告订单-服务类
- * Class AdvertOrderService
- * @package App\Services\Common
- */
- class AdvertOrderService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * @since 2020/11/10
- * LoginService constructor.
- */
- public function __construct()
- {
- $this->model = new AdvertOrderModel();
- $this->advertModel = new AdvertModel();
- $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('advert_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]);
- }
- $businessId = isset($params['business_id']) ? $params['business_id'] : 0;
- $userType = isset($params['user_type']) ? intval($params['user_type']) : 0;
- if ($userType > 0 && $businessId<=0) {
- $query->where(['c.user_type' => $userType]);
- }
- // 日期
- $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);
- }
- if ($businessId > 0) {
- $query->where('a.business_id', $businessId);
- }
- })
- ->select(['a.*', 'b.username','b.realname','b.idcard','b.idcard_check','b.idcard_front_img','b.idcard_back_img','b.idcard_hand_img', 'c.username as c_username','c.realname as c_realname'])
- ->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['utime_text'] = $item['update_time'] ? datetime($item['update_time'], 'm-d 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['exception_img1'] = $item['exception_img1'] ? get_image_url($item['exception_img1']) : '';
- $item['exception_img2'] = $item['exception_img2'] ? get_image_url($item['exception_img2']) : '';
- $item['refund_img'] = $item['refund_img'] ? get_image_url($item['refund_img']) : '';
- $item['refund_img1'] = $item['refund_img1'] ? get_image_url($item['refund_img1']) : '';
- $item['refund_img2'] = $item['refund_img2'] ? get_image_url($item['refund_img2']) : '';
- $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] : '其他';
- $item['chat_key'] = getChatKey($item['user_id'],$item['business_id']);
- }
- }
- return [
- 'pageSize' => $pageSize,
- 'total' => isset($list['total']) ? $list['total'] : 0,
- 'list' => isset($list['data']) ? $list['data'] : []
- ];
- }
- /**
- * 用户交易量
- * @param $userId
- * @param $date
- * @return mixed
- */
- public function getTotalByDate($userId, $date)
- {
- $total = $this->model->where(['business_id'=> $userId, 'status'=> 4, 'mark'=>1])
- ->where(function($query) use($date){
- $start = isset($date[0]) ? $date[0] : '';
- $end = isset($date[1]) ? $date[1] : '';
- $end = $start >= $end ? '' : $end;
- if ($start) {
- $query->where('pay_time', '>=', strtotime($start));
- }
- if ($end) {
- $query->where('pay_time', '<', strtotime($end));
- }
- })->sum('total');
- return $total;
- }
- /**
- * 购买
- * @param $userId
- * @param $params
- * @return false|int|number
- */
- public function buy($userId, $params)
- {
- $id = isset($params['id']) ? intval($params['id']) : 0;
- $num = isset($params['num']) ? intval($params['num']) : 0;
- if ($id <= 0 || $num<=0) {
- $this->error = '1013';
- return false;
- }
- // 验证参数
- $config = \App\Services\ConfigService::make()->getConfigOptionByGroup(5);
- $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 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;
- }
- $info = AdvertService::make()->getInfo($id);
- $tradeType = isset($info['type'])? $info['type'] : 0;
- $priceType = isset($info['price_type'])? $info['price_type'] : 0;
- $price = isset($info['price'])? $info['price'] : 0;
- $businessId = isset($info['user_id'])? $info['user_id'] : 0;
- if(empty($info) || $info['status'] != 1 || $businessId<=0){
- $this->error = '4001';
- return false;
- }
- if($userId == $businessId){
- $this->error = '4008';
- return false;
- }
- $expiredAt = isset($info['expired_at']) && $info['expired_at']? strtotime($info['expired_at']) : 0;
- if($expiredAt <= time()){
- $this->error = '4007';
- return false;
- }
- if($tradeType != 2){
- $this->error = '4003';
- return false;
- }
- if ($tradePrice <= 0 && $priceType == 2) {
- $this->error = '3002';
- return false;
- }
- // 浮动价格计算
- if ($priceType == 2) {
- $price = floatval($tradePrice + $price);
- }
- // 总价
- $total = floatval($price * $num);
- if($total<=0){
- $this->error = '4002';
- return false;
- }
- // 单笔限额
- if($total< $info['limit_min'] || $total> $info['limit_max']){
- $this->error = lang('4005',['min'=> $info['limit_min'],'max'=>$info['limit_max']]);
- return false;
- }
- // 购买用户信息
- $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']) ? format_account($userInfo['username']) : '';
- if (empty($userInfo) || $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 = MemberService::make()->getInfo($businessId);
- $usdtNum = isset($businessInfo['usdt_num'])? $businessInfo['usdt_num'] : 0;
- if (empty($businessInfo)) {
- $this->error = '3004';
- return false;
- }
- if($num > $usdtNum){
- $this->error = '4009';
- return false;
- }
- $setting = MemberSettingService::make()->getInfo($businessId);
- $advertOnline = isset($setting['advert_online'])? $setting['advert_online'] : 0;
- if($advertOnline != 1){
- $this->error = '4006';
- return false;
- }
- // 购买者身份信息
- $idcardData = [
- 'realname' => isset($userInfo['realname']) ? $userInfo['realname'] : '',
- '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'] : '',
- 'bank_front_img' => isset($userInfo['bank_front_img']) ? $userInfo['bank_front_img'] : '',
- 'bank_back_img' => isset($userInfo['bank_back_img']) ? $userInfo['bank_back_img'] : '',
- ];
- // 收款方式
- $payment = MemberPaymentService::make()->getPayment($businessInfo['id']);
- if (empty($payment)) {
- $this->error = '3015';
- return false;
- }
- $this->model->startTrans();
- $orderNo = get_order_num('TA');
- $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' => $price,
- '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,
- 'trade_type'=> 2,
- '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' => 3,
- 'order_no' => $orderNo,
- 'chat_key' => getChatKey($userId, $businessInfo['id']),
- 'message' => "您有来自客户购买广告订单:{$orderNo}的消息,请尽快回复!",
- 'message_type' => 1,
- 'data_type' => 2,
- '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();
- $this->error = '1002';
- return $order;
- }
- /**
- * 购买
- * @param $userId
- * @param $params
- * @return false|int|number
- */
- public function sell($userId, $params)
- {
- $id = isset($params['id']) ? intval($params['id']) : 0;
- $num = isset($params['num']) ? intval($params['num']) : 0;
- $paymentId = isset($params['payment_id']) ? intval($params['payment_id']) : 0;
- if ($id <= 0 || $num<=0 || $paymentId<=0) {
- $this->error = '1013';
- return false;
- }
- // 验证参数
- $config = \App\Services\ConfigService::make()->getConfigOptionByGroup(5);
- $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 0;
- $tradePrice = isset($config['usdt_buy_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;
- }
- $info = AdvertService::make()->getInfo($id);
- $tradeType = isset($info['type'])? $info['type'] : 0;
- $priceType = isset($info['price_type'])? $info['price_type'] : 0;
- $price = isset($info['price'])? $info['price'] : 0;
- $businessId = isset($info['user_id'])? $info['user_id'] : 0;
- if(empty($info) || $info['status'] != 1){
- $this->error = '4001';
- return false;
- }
- if($userId == $businessId){
- $this->error = '4008';
- return false;
- }
- $expiredAt = isset($info['expired_at']) && $info['expired_at']? strtotime($info['expired_at']) : 0;
- if($expiredAt <= time()){
- $this->error = '4007';
- return false;
- }
- if($tradeType != 1){
- $this->error = '4004';
- return false;
- }
- if ($tradePrice <= 0 && $priceType == 2) {
- $this->error = '3002';
- return false;
- }
- // 验证数量或金额
- if ($priceType == 2) {
- $price = floatval($tradePrice + $price);
- }
- // 单笔限额
- $total = floatval($price * $num);
- if($total<=0){
- $this->error = '4002';
- return false;
- }
- if($total< $info['limit_min'] || $total> $info['limit_max']){
- $this->error = lang('4005',['min'=> $info['limit_min'],'max'=>$info['limit_max']]);
- return false;
- }
- // 用户信息
- $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;
- }
- $usdtNum = isset($userInfo['usdt_num'])? $userInfo['usdt_num'] : 0;
- if($num > $usdtNum){
- $this->error = '4010';
- return false;
- }
- // 未处理订单
- $noCatchOrder = $this->checkOrderNoCatch($userId, 2);
- if ($tradeLimitNum > 0 && $noCatchOrder >= $tradeLimitNum) {
- $this->error = lang(3005, ['num' => $tradeLimitNum]);
- return false;
- }
- // 交易商家
- $businessInfo = MemberService::make()->getInfo($businessId);
- if (empty($businessInfo)) {
- $this->error = '3004';
- return false;
- }
- $setting = MemberSettingService::make()->getInfo($businessId);
- $advertOnline = isset($setting['advert_online'])? $setting['advert_online'] : 0;
- if($advertOnline != 1){
- $this->error = '4006';
- 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'] : '',
- ];
- // 收款方式
- $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'],
- ];
- $this->model->startTrans();
- $orderNo = get_order_num('TA');
- $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' => $price,
- 'num' => $num,
- 'total' => $total,
- 'payment_id' => $paymentId,
- 'idcard_data' => $idcardData ? json_encode($idcardData, 256) : '',
- 'payment_data' => $paymentData ? json_encode($paymentData, 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;
- }
- // 扣除出售用户的USDT
- if(!$this->memberModel->where(['id'=> $userId])->decrement('usdt_num', $num)){
- $this->model->rollBack();
- $this->error = '3020';
- return false;
- }
- $data = [
- 'order_no'=> $orderNo,
- 'user_id'=> $userId,
- 'type'=> 2,
- 'pay_type'=> 1,
- 'trade_type'=> 2,
- 'change_type'=> 2,
- 'num'=> $num,
- 'total'=> $total,
- 'balance'=> floatval($userInfo['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' => 3,
- 'order_no' => $orderNo,
- 'chat_key' => getChatKey($userId, $businessInfo['id']),
- 'message' => "您有来自客户出售广告订单:{$orderNo}的消息,请尽快回复!",
- 'message_type' => 1,
- 'data_type' => 3,
- '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();
- $this->error = '1002';
- 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;
- var_dump($orderInfo);
- 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;
- $catchUid = isset($params['catch_uid']) ? $params['catch_uid'] : 0;
- if ($orderId <= 0) {
- $this->error = '1013';
- return false;
- }
- $status = isset($params['status']) ? intval($params['status']) : 4;
- $where = ['user_id' => $userId, 'id' => $orderId, 'mark' => 1];
- if($status==4){
- $where['type'] = 2;
- }
- $orderInfo = $this->model->where($where)
- ->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 ($status == 2 && $tradeType != 2) {
- $this->error = '3024';
- return false;
- }
- // 订单状态更新,异常或确认
- $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_img1'])) {
- $updateData['exception_img1'] = trim($params['exception_img1']);
- if (strpos($updateData['exception_img1'], "temp")) {
- $updateData['exception_img1'] = save_image($updateData['exception_img'], 'images');
- } else {
- $updateData['exception_img1'] = str_replace(IMG_URL, "", $updateData['exception_img1']);
- }
- }
- if (isset($params['exception_img2'])) {
- $updateData['exception_img2'] = trim($params['exception_img2']);
- if (strpos($updateData['exception_img2'], "temp")) {
- $updateData['exception_img2'] = save_image($updateData['exception_img2'], 'images');
- } else {
- $updateData['exception_img2'] = str_replace(IMG_URL, "", $updateData['exception_img2']);
- }
- }
- if (isset($params['exception_remark'])) {
- $updateData['exception_remark'] = trim($params['exception_remark']);
- }
- if (isset($params['exception_status'])) {
- $updateData['exception_status'] = intval($params['exception_status']);
- }
- $this->model->startTrans();
- // 订单状态更新
- if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update($updateData)) {
- $this->model->rollBack();
- $this->error = '3023';
- return false;
- }
- // 交易处理
- if ($orderInfo['num'] > 0 && $status == 4 && $userId > 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,
- 'trade_type' => 2,
- '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();
- $this->error = $status == 5? 3032: '1002';
- 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','user_id', 'business_id', 'type', 'num', 'total','pay_time', 'status'])
- ->first();
- $userId = isset($orderInfo['user_id']) ? $orderInfo['user_id'] : 0;
- $orderType = 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 ($orderType != 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_img1'])) {
- $updateData['exception_img1'] = trim($params['exception_img1']);
- if (strpos($updateData['exception_img1'], "temp")) {
- $updateData['exception_img1'] = save_image($updateData['exception_img'], 'images');
- } else {
- $updateData['exception_img1'] = str_replace(IMG_URL, "", $updateData['exception_img1']);
- }
- }
- if (isset($params['exception_img2'])) {
- $updateData['exception_img2'] = trim($params['exception_img2']);
- if (strpos($updateData['exception_img2'], "temp")) {
- $updateData['exception_img2'] = save_image($updateData['exception_img2'], 'images');
- } else {
- $updateData['exception_img2'] = str_replace(IMG_URL, "", $updateData['exception_img2']);
- }
- }
- 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 && $userId > 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' => 1,
- 'pay_type' => 1,
- 'trade_type' => 2,
- '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,
- 'trade_type' => 2,
- '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,
- 'trade_type' => 2,
- '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 = '1002';
- 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_img1'])) {
- $updateData['exception_img'] = trim($params['exception_img1']);
- if (strpos($updateData['exception_img1'], "temp")) {
- $updateData['exception_img1'] = save_image($updateData['exception_img'], 'images');
- } else {
- $updateData['exception_img1'] = str_replace(IMG_URL, "", $updateData['exception_img1']);
- }
- }
- if (isset($params['exception_img2'])) {
- $updateData['exception_img2'] = trim($params['exception_img2']);
- if (strpos($updateData['exception_img2'], "temp")) {
- $updateData['exception_img2'] = save_image($updateData['exception_img2'], 'images');
- } else {
- $updateData['exception_img2'] = str_replace(IMG_URL, "", $updateData['exception_img2']);
- }
- }
- 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,
- 'trade_type' => 2,
- '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,
- 'trade_type' => 2,
- '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;
- }
- /**
- * 获取未支付或处理的订单数
- * @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, 5, 7])
- ->count('id');
- }
- /**
- * 自动取消广告订单处理
- * @return false
- */
- public function catchInvalidOrder(){
- $cacheKey = "caches:adverts: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','advert_id','order_no', 'type', 'num','total'])
- ->take($catchNum)
- ->get()
- ->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_status'=> 0,'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,
- 'trade_type' => 2,
- '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;
- }
- }
- }
|