TradeOrderService.php 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\CapitalLogModel;
  13. use App\Models\MemberModel;
  14. use App\Models\MemberPaymentModel;
  15. use App\Models\TradeOrderModel;
  16. use App\Services\Api\MemberPaymentService;
  17. use App\Services\BaseService;
  18. use App\Services\ChatMessageService;
  19. use App\Services\ConfigService;
  20. use App\Services\RedisService;
  21. use App\Services\UsdtWalletService;
  22. /**
  23. * 用户交易订单-服务类
  24. * Class TradeOrderService
  25. * @package App\Services\Common
  26. */
  27. class TradeOrderService extends BaseService
  28. {
  29. // 静态对象
  30. protected static $instance = null;
  31. /**
  32. * 构造函数
  33. * @since 2020/11/10
  34. * TradeOrderService constructor.
  35. */
  36. public function __construct()
  37. {
  38. $this->model = new TradeOrderModel();
  39. $this->memberModel = new MemberModel();
  40. $this->capitalModel = new CapitalLogModel();
  41. $this->paymentModel = new MemberPaymentModel();
  42. }
  43. /**
  44. * 静态入口
  45. * @return static|null
  46. */
  47. public static function make()
  48. {
  49. if (!self::$instance) {
  50. self::$instance = (new static());
  51. }
  52. return self::$instance;
  53. }
  54. /**
  55. * 订单列表
  56. * @param $params
  57. * @param int $pageSize
  58. * @return array
  59. */
  60. public function getDataList($params, $pageSize = 15)
  61. {
  62. $list = $this->model->from('trade_order as a')
  63. ->leftJoin('member as b', 'b.id', '=', 'a.business_id')
  64. ->leftJoin('member as c', 'c.id', '=', 'a.user_id')
  65. ->where(function ($query) use ($params) {
  66. $query->where(['a.mark' => 1])->where('a.status', '>', 0);
  67. $orderNo = isset($params['order_no']) && $params['order_no'] ? trim($params['order_no']) : '';
  68. if ($orderNo) {
  69. $query->where('a.order_no', 'like', "%{$orderNo}%");
  70. }
  71. $type = isset($params['type']) ? intval($params['type']) : 0;
  72. if ($type > 0) {
  73. $query->where(['a.type' => $type]);
  74. }
  75. $businessName = isset($params['business_name']) ? trim($params['business_name']) : '';
  76. if ($businessName) {
  77. $query->where('b.username','like',"%{$businessName}%");
  78. }
  79. $userName = isset($params['username']) ? trim($params['username']) : '';
  80. if ($userName) {
  81. $query->where('c.username','like',"%{$userName}%");
  82. }
  83. $apiId = isset($params['api_id']) ? intval($params['api_id']) : 0;
  84. if ($apiId > 0) {
  85. $query->where(['a.api_id' => $apiId]);
  86. }
  87. $tradeType = isset($params['trade_type']) ? intval($params['trade_type']) : 0;
  88. if ($tradeType > 0) {
  89. $query->where(['a.trade_type' => $tradeType]);
  90. }
  91. // 日期
  92. $date = isset($params['date']) ? $params['date'] : [];
  93. $start = isset($date[0]) ? $date[0] : '';
  94. $end = isset($date[1]) ? $date[1] : '';
  95. $end = $start >= $end ? '' : $end;
  96. if ($start) {
  97. $query->where('a.create_time', '>=', strtotime($start));
  98. }
  99. if ($end) {
  100. $query->where('a.create_time', '<', strtotime($end));
  101. }
  102. $refundStatus = isset($params['refund_status']) ? intval($params['refund_status']) : 0;
  103. if ($refundStatus > 0) {
  104. $query->where(['a.refund_status' => $refundStatus]);
  105. }
  106. $exceptionStatus = isset($params['exception_status']) ? intval($params['exception_status']) : 0;
  107. if ($exceptionStatus > 0) {
  108. $query->where(['a.exception_status' => $exceptionStatus]);
  109. }
  110. $status = isset($params['status']) ? intval($params['status']) : 0;
  111. if ($status > 0) {
  112. $query->where(['a.status' => $status]);
  113. }
  114. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  115. if ($userId > 0) {
  116. $query->where('a.user_id', $userId);
  117. }
  118. $businessId = isset($params['business_id']) ? $params['business_id'] : 0;
  119. if ($businessId > 0) {
  120. $query->where('a.business_id', $businessId);
  121. }
  122. })
  123. ->select(['a.*', 'b.username','b.realname as realname', 'c.username as c_username','c.realname as c_realname'])
  124. ->orderBy('a.create_time', 'desc')
  125. ->orderBy('a.id', 'desc')
  126. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  127. $list = $list ? $list->toArray() : [];
  128. if ($list) {
  129. $payTypes = [1 => '银行卡', 2 => '微信', 3 => '支付宝', 4 => '其他'];
  130. $overTime = ConfigService::make()->getConfigByCode('trade_order_overtime');
  131. $overTime = $overTime ? $overTime : 0;
  132. foreach ($list['data'] as &$item) {
  133. $item['idcardData'] = $item['idcard_data'] ? json_decode($item['idcard_data'], true) : [];
  134. $item['paymentData'] = $item['payment_data'] ? json_decode($item['payment_data'], true) : [];
  135. $item['create_time_text'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
  136. $item['update_time_text'] = $item['update_time'] ? datetime($item['update_time'], 'Y-m-d H:i:s') : '';
  137. $item['time_text'] = $item['create_time'] ? datetime($item['create_time'], 'H:i') : '';
  138. $item['utime_text'] = $item['update_time']>0? datetime($item['update_time'], 'm-d H:i') : datetime($item['create_time'], 'm-d H:i');
  139. $item['pay_time_text'] = $item['pay_time'] ? datetime($item['pay_time'], 'Y-m-d H:i') : '';
  140. $item['username_text'] = $item['username'] ? format_account($item['username']) : '';
  141. $item['c_username_text'] = $item['c_username'] ? format_account($item['c_username']) : '';
  142. $item['exception_img'] = $item['exception_img'] ? get_image_url($item['exception_img']) : '';
  143. $item['exception_img1'] = $item['exception_img1'] ? get_image_url($item['exception_img1']) : '';
  144. $item['exception_img2'] = $item['exception_img2'] ? get_image_url($item['exception_img2']) : '';
  145. $item['refund_img'] = $item['refund_img'] ? get_image_url($item['refund_img']) : '';
  146. $item['refund_img1'] = $item['refund_img1'] ? get_image_url($item['refund_img1']) : '';
  147. $item['refund_img2'] = $item['refund_img2'] ? get_image_url($item['refund_img2']) : '';
  148. $item['pay_img'] = $item['pay_img'] ? get_image_url($item['pay_img']) : '';
  149. $item['paymentData']['qrcode'] = isset($item['paymentData']['qrcode']) && $item['paymentData']['qrcode'] ? get_image_url($item['paymentData']['qrcode']) : '';
  150. $overTime = max(0, intval(strtotime($item['create_time'])) + $overTime * 60 - time());
  151. $item['overtime_text'] = in_array($item['status'], [1, 2]) && $overTime ? date('i:s', $overTime) : '';
  152. $payType = isset($item['pay_type']) ? $item['pay_type'] : 0;
  153. $item['pay_name'] = isset($payTypes[$payType]) ? $payTypes[$payType] : '其他';
  154. $item['chat_key'] = getChatKey($item['user_id'], $item['business_id']);
  155. }
  156. }
  157. return [
  158. 'pageSize' => $pageSize,
  159. 'total' => isset($list['total']) ? $list['total'] : 0,
  160. 'list' => isset($list['data']) ? $list['data'] : []
  161. ];
  162. }
  163. /**
  164. * 用户交易量
  165. * @param $userId
  166. * @param $date
  167. * @return mixed
  168. */
  169. public function getTotalByDate($userId, $date)
  170. {
  171. $total = $this->model->where(['business_id'=> $userId, 'status'=> 4, 'mark'=>1])
  172. ->where(function($query) use($date){
  173. $start = isset($date[0]) ? $date[0] : '';
  174. $end = isset($date[1]) ? $date[1] : '';
  175. $end = $start >= $end ? '' : $end;
  176. if ($start) {
  177. $query->where('pay_time', '>=', strtotime($start));
  178. }
  179. if ($end) {
  180. $query->where('pay_time', '<', strtotime($end));
  181. }
  182. })->sum('total');
  183. return $total;
  184. }
  185. /**
  186. * 用户交易量
  187. * @param $userId
  188. * @param $date
  189. * @return mixed
  190. */
  191. public function getTotalByApiAndDate($apiId, $type=1, $date=[])
  192. {
  193. $where = ['api_id'=> $apiId, 'status'=> 4, 'mark'=>1];
  194. if($type){
  195. $where['type'] = $type;
  196. }
  197. $total = $this->model->where($where)
  198. ->where(function($query) use($date){
  199. $start = isset($date[0]) ? $date[0] : '';
  200. $end = isset($date[1]) ? $date[1] : '';
  201. $end = $start >= $end ? '' : $end;
  202. if ($start) {
  203. $query->where('pay_time', '>=', strtotime($start));
  204. }
  205. if ($end) {
  206. $query->where('pay_time', '<', strtotime($end));
  207. }
  208. })->sum('total');
  209. return $total;
  210. }
  211. /**
  212. * @param $orderNo
  213. * @return array
  214. */
  215. public function getInfoByNo($orderNo, $userId = 0)
  216. {
  217. $info = $this->model->from('trade_order as a')
  218. ->leftJoin('member as b', 'b.id', '=', 'a.business_id')
  219. ->leftJoin('member as c', 'c.id', '=', 'a.user_id')
  220. ->leftJoin('user as u', 'u.user_id', '=', 'a.business_id')
  221. ->where(['order_no' => $orderNo])
  222. ->where(function ($query) use ($userId) {
  223. $query->where(['a.mark' => 1])->where('a.status', '>', 0);
  224. if ($userId > 0) {
  225. $query->where(['a.user_id' => $userId]);
  226. }
  227. })
  228. ->select(['a.*', 'b.username', 'b.idcard_check', 'b.credit', 'u.bond', 'c.username as c_username'])
  229. ->first();
  230. $info = $info ? $info->toArray() : [];
  231. if ($info) {
  232. $overTime = ConfigService::make()->getConfigByCode('trade_order_overtime');
  233. $overTime = $overTime ? $overTime : 5;
  234. $info['idcardData'] = $info['idcard_data'] ? json_decode($info['idcard_data'], true) : [];
  235. $info['paymentData'] = $info['payment_data'] ? json_decode($info['payment_data'], true) : [];
  236. $info['create_time_text'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
  237. $info['update_time_text'] = $info['update_time'] ? datetime($info['update_time'], 'Y-m-d H:i:s') : '';
  238. $info['time_text'] = $info['create_time'] ? datetime($info['create_time'], 'H:i') : '';
  239. $info['utime_text'] = $info['update_time'] ? datetime($info['update_time'], 'H:i') : '';
  240. $info['pay_time_text'] = $info['pay_time'] ? datetime($info['pay_time'], 'Y-m-d H:i') : '';
  241. $info['username_text'] = $info['username'] ? format_account($info['username']) : '';
  242. $info['c_username_text'] = $info['c_username'] ? format_account($info['c_username']) : '';
  243. $info['exception_img'] = $info['exception_img'] ? get_image_url($info['exception_img']) : '';
  244. $info['pay_img'] = $info['pay_img'] ? get_image_url($info['pay_img']) : '';
  245. $info['paymentData']['qrcode'] = isset($info['paymentData']['qrcode']) && $info['paymentData']['qrcode'] ? get_image_url($info['paymentData']['qrcode']) : '';
  246. $payType = isset($info['pay_type']) ? $info['pay_type'] : 0;
  247. $info['pay_name'] = isset($payTypes[$payType]) ? $payTypes[$payType] : '其他';
  248. $info['chat_key'] = getChatKey($info['user_id'], $info['business_id']);
  249. $overTime = max(0, intval(strtotime($info['create_time'])) + $overTime * 60 - time());
  250. $info['overtime_text'] = in_array($info['status'], [1, 2]) && $overTime ? date('i:s', $overTime) : '';
  251. $info['overtime'] = $overTime;
  252. }
  253. return $info;
  254. }
  255. /**
  256. * 获取待处理订单
  257. * @param $params
  258. * @return array
  259. */
  260. public function getWaitOrder($params)
  261. {
  262. $info = $this->model->from('trade_order as a')
  263. ->leftJoin('member as b', 'b.id', '=', 'a.business_id')
  264. ->where(function ($query) use ($params) {
  265. $query->where(['a.mark' => 1])->whereIn('a.status', [1, 2, 3, 7])->where('a.status', '>', 0);
  266. $orderNo = isset($params['order_no']) && $params['order_no'] ? trim($params['order_no']) : '';
  267. if ($orderNo) {
  268. $query->where('a.order_no', 'like', "%{$orderNo}%");
  269. }
  270. $type = isset($params['type']) ? intval($params['type']) : 0;
  271. if ($type > 0) {
  272. $query->where(['a.type' => $type]);
  273. }
  274. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  275. if ($userId > 0) {
  276. $query->where('a.user_id', $userId);
  277. }
  278. $businessId = isset($params['business_id']) ? $params['business_id'] : 0;
  279. if ($businessId > 0) {
  280. $query->where('a.business_id', $businessId);
  281. }
  282. })
  283. ->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'])
  284. ->orderBy('a.create_time', 'desc')
  285. ->first();
  286. $info = $info ? $info->toArray() : [];
  287. if ($info) {
  288. $overTime = ConfigService::make()->getConfigByCode('trade_order_overtime');
  289. $overTime = $overTime ? $overTime : 5;
  290. $info['paymentData'] = $info['payment_data'] ? json_decode($info['payment_data'], true) : [];
  291. $info['create_time_text'] = $info['create_time'] ? format_time(strtotime($info['create_time'])) : '';
  292. $info['time_text'] = $info['create_time'] ? datetime($info['create_time'], 'H:i') : '';
  293. $info['username_text'] = $info['username'] ? format_account($info['username']) : '';
  294. $info['paymentData']['qrcode'] = isset($info['paymentData']['qrcode']) && $info['paymentData']['qrcode'] ? get_image_url($info['paymentData']['qrcode']) : '';
  295. $info['paymentData']['bank_card_text'] = isset($info['paymentData']['bank_card']) && $info['paymentData']['bank_card'] ? format_bank_card($info['paymentData']['bank_card']) : '未设置';
  296. $info['paymentData']['account_text'] = isset($info['paymentData']['account']) && $info['paymentData']['account'] ? format_account($info['paymentData']['account']) : '未设置';
  297. $overTime = max(0, intval(strtotime($info['create_time'])) + $overTime * 60 - time());
  298. $info['overtime_text'] = in_array($info['status'], [1, 2]) && $overTime ? date('H:i', $overTime) : '';
  299. }
  300. return $info;
  301. }
  302. /**
  303. * 成交交易笔数
  304. * @param int $type 类型:1-买单,2-卖单
  305. * @param int $bsid 承兑商用户ID
  306. * @param int $status 状态:4-成交
  307. * @return mixed
  308. */
  309. public function getCompleteCount($type = 1, $bsid = 0, $status = 4)
  310. {
  311. return (int)$this->model->where(['type' => $type, 'status' => $status, 'mark' => 1])
  312. ->where(function ($query) use ($bsid) {
  313. if ($bsid > 0) {
  314. $query->where('business_id', '=', $bsid);
  315. }
  316. })->count('id');
  317. }
  318. /**
  319. * 成交交易金额
  320. * @param int $type 类型:1-买单,2-卖单
  321. * @param int $bsid 承兑商用户ID
  322. * @param int $status 状态:4-成交
  323. * @param string $field 统计字段:total-总金额(默认),num-交易USDT数量
  324. * @return mixed
  325. */
  326. public function getCompleteTotal($type = 1, $bsid = 0, $status = 4, $field = 'total')
  327. {
  328. $total = $this->model->where(['type' => $type, 'status' => $status, 'mark' => 1])
  329. ->where(function ($query) use ($bsid) {
  330. if ($bsid > 0) {
  331. $query->where('business_id', '=', $bsid);
  332. }
  333. })->sum($field);
  334. return moneyFormat($total, 2);
  335. }
  336. /**
  337. * 今日成交交易金额
  338. * @param int $type 类型:1-买单,2-卖单
  339. * @param int $bsid 承兑商用户ID
  340. * @param int $status 状态:4-成交
  341. * @param string $field 统计字段:total-总金额(默认),num-交易USDT数量
  342. * @return mixed
  343. */
  344. public function getCompleteTotalByDay($type = 1, $bsid = 0, $status = 4, $field = 'total')
  345. {
  346. $total = $this->model->where(['type' => $type, 'status' => $status, 'mark' => 1])
  347. ->where('pay_time', '>=', strtotime(date('Y-m-d')))
  348. ->where(function ($query) use ($bsid) {
  349. if ($bsid > 0) {
  350. $query->where('business_id', '=', $bsid);
  351. }
  352. })->sum($field);
  353. return moneyFormat($total, 6);
  354. }
  355. /**
  356. * 24小时成交交易金额
  357. * @param int $time 时间/小时
  358. * @param int $bsid 承兑商用户ID
  359. * @param int $status 状态:4-成交
  360. * @param string $field 统计字段:total-总金额(默认),num-交易USDT数量
  361. * @return mixed
  362. */
  363. public function getCompleteTotalByTime($time = 24, $bsid = 0, $status = 4, $field = 'total')
  364. {
  365. $total = $this->model->where(['status' => $status, 'mark' => 1])
  366. ->where('pay_time', '>=', time() - $time * 3600)
  367. ->where(function ($query) use ($bsid) {
  368. if ($bsid > 0) {
  369. $query->where('business_id', '=', $bsid);
  370. }
  371. })->sum($field);
  372. return moneyFormat($total, 4);
  373. }
  374. /**
  375. * 24小时成交交易金额
  376. * @param int $time 时间/小时
  377. * @param int $bsid 承兑商用户ID
  378. * @param int $status 状态:4-成交
  379. * @param string $field 统计字段:total-总金额(默认),num-交易USDT数量
  380. * @return mixed
  381. */
  382. public function getCompleteCountByTime($time = 24, $bsid = 0, $status = 4, $field = 'id')
  383. {
  384. $total = $this->model->where(['status' => $status, 'mark' => 1])
  385. ->where('pay_time', '>=', time() - $time * 3600)
  386. ->where(function ($query) use ($bsid) {
  387. if ($bsid > 0) {
  388. $query->where('business_id', '=', $bsid);
  389. }
  390. })->count($field);
  391. return intval($total);
  392. }
  393. /**
  394. * 获取某个时间段的买卖交易量比
  395. * @param int $type 状态类型:1-成交的
  396. * @param int $bsid 承兑商用户ID
  397. * @param int $time 时间/分钟,默认15分钟
  398. * @return array
  399. */
  400. public function getCountRateByTime($type = 1, $bsid = 0, $time = 15)
  401. {
  402. $where = ['mark' => 1];
  403. if ($type == 1) {
  404. $where['status'] = 4;
  405. }
  406. $buyCount = $this->model->where(['type' => 1])->where($where)
  407. ->where(function ($query) use ($bsid) {
  408. if ($bsid > 0) {
  409. $query->where('business_id', '=', $bsid);
  410. }
  411. })
  412. ->where('pay_time', '>=', time() - $time * 60)
  413. ->count('id');
  414. $sellCount = $this->model->where(['type' => 2])->where($where)
  415. ->where(function ($query) use ($bsid) {
  416. if ($bsid > 0) {
  417. $query->where('business_id', '=', $bsid);
  418. }
  419. })
  420. ->where('pay_time', '>=', time() - $time * 60)
  421. ->count('id');
  422. $rate = ($buyCount || $sellCount) > 0 ? moneyFormat($buyCount / ($buyCount + $sellCount)) * 100 : 0;
  423. return ['buy' => $buyCount, 'sell' => $sellCount, 'buy_rate' => $rate, 'sell_rate' => ($sellCount > 0 ? 100 - $rate : 0)];
  424. }
  425. /**
  426. * 获取某个时间段的交易成功率
  427. * @param int $type 状态类型:1-成交的
  428. * @param int $bsid 承兑商用户ID
  429. * @param int $time 时间/小时,默认24小时
  430. * @return array
  431. */
  432. public function getRateByTime($bsid = 0, $time = 24)
  433. {
  434. $successCount = $this->model->where(['status' => 4, 'mark' => 1])
  435. ->where(function ($query) use ($bsid) {
  436. if ($bsid > 0) {
  437. $query->where('business_id', '=', $bsid);
  438. }
  439. })
  440. ->where('pay_time', '>=', time() - $time * 3600)
  441. ->count('id');
  442. $count = $this->model->where(['mark' => 1])
  443. ->where(function ($query) use ($bsid) {
  444. if ($bsid > 0) {
  445. $query->where('business_id', '=', $bsid);
  446. }
  447. })
  448. ->where(function ($query) use ($time) {
  449. $time = time() - $time * 3600;
  450. $query->where('pay_time', '>=', $time)
  451. ->orWhereRaw("(pay_time = 0 and create_time >={$time})");
  452. })
  453. ->count('id');
  454. $rate = $count > 0 ? moneyFormat($successCount / $count * 100) : 0;
  455. return ['rate' => $rate, 'success' => $successCount, 'count' => $count];
  456. }
  457. /**
  458. * 获取未支付或处理的订单数
  459. * @param $userId
  460. * @param int $type
  461. * @return mixed
  462. */
  463. public function checkOrderNoCatch($userId, $type = 1)
  464. {
  465. return $this->model->where(['user_id' => $userId, 'type' => $type, 'mark' => 1])->whereIn('status', [1, 2, 7])->count('id');
  466. }
  467. /**
  468. * 客户买入
  469. * @param $userId
  470. * @param $params
  471. * @return false|int|number
  472. */
  473. public function buy($userId, $params)
  474. {
  475. $num = isset($params['num']) ? floatval($params['num']) : 0;
  476. $numType = isset($params['num_type']) ? intval($params['num_type']) : 1;
  477. if ($userId <= 0) {
  478. $this->error = '1013';
  479. return false;
  480. }
  481. // 验证参数
  482. $config = ConfigService::make()->getConfigOptionByGroup(5);
  483. $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 0;
  484. $tradeMinNum = isset($config['trade_min_num']) ? $config['trade_min_num'] : 0;
  485. $tradeMaxNum = isset($config['trade_max_num']) ? $config['trade_max_num'] : 0;
  486. $trademinMoney = isset($config['trade_min_money']) ? $config['trade_min_money'] : 0;
  487. $tradeMaxMoney = isset($config['trade_max_money']) ? $config['trade_max_money'] : 0;
  488. $tradePrice = isset($config['usdt_buy_price']) ? $config['usdt_buy_price'] : 0;
  489. $tradeLimitNum = isset($config['trade_no_catch']) ? $config['trade_no_catch'] : 0;
  490. // 是否开启交易
  491. if ($tradeOpen != 1) {
  492. $this->error = '1013';
  493. return false;
  494. }
  495. if ($tradePrice <= 0) {
  496. $this->error = '3002';
  497. return false;
  498. }
  499. // 验证数量或金额
  500. $total = 0;
  501. if ($numType == 1) {
  502. if ($num < $tradeMinNum || $num > $tradeMaxNum) {
  503. $this->error = '3003';
  504. return false;
  505. }
  506. $total = moneyFormat($num * $tradePrice, 6);
  507. } else {
  508. if ($num < $trademinMoney || $num > $tradeMaxMoney) {
  509. $this->error = '3003';
  510. return false;
  511. }
  512. $total = moneyFormat($num, 6);
  513. $num = moneyFormat($num / $tradePrice, 6);
  514. }
  515. // 用户信息
  516. $userInfo = MemberService::make()->getInfo($userId);
  517. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  518. $idcardCheck = isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0;
  519. $username = isset($userInfo['username']) && $userInfo['username'] ? format_account($userInfo['username']) : '';
  520. if ($status != 1) {
  521. $this->error = '2009';
  522. return false;
  523. }
  524. if ($idcardCheck != 1) {
  525. $this->error = '2014';
  526. return false;
  527. }
  528. // 未处理订单
  529. $noCatchOrder = $this->checkOrderNoCatch($userId, 1);
  530. if ($tradeLimitNum > 0 && $noCatchOrder >= $tradeLimitNum) {
  531. $this->error = lang(3005, ['num' => $tradeLimitNum]);
  532. return false;
  533. }
  534. // 匹配交易商家
  535. $businessInfo = \App\Services\Api\MemberService::make()->getTradeMember($num, 1, $userId);
  536. if (empty($businessInfo)) {
  537. $this->error = '3004';
  538. return false;
  539. }
  540. if ($businessInfo['usdt_num'] < $num) {
  541. $this->error = '3004';
  542. return false;
  543. }
  544. // 客户身份信息
  545. $idcardData = [
  546. 'idcard' => isset($userInfo['idcard']) ? $userInfo['idcard'] : '',
  547. 'idcard_check' => isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0,
  548. 'idcard_front_img' => isset($userInfo['idcard_front_img']) ? $userInfo['idcard_front_img'] : '',
  549. 'idcard_back_img' => isset($userInfo['idcard_back_img']) ? $userInfo['idcard_back_img'] : '',
  550. 'idcard_hand_img' => isset($userInfo['idcard_hand_img']) ? $userInfo['idcard_hand_img'] : '',
  551. ];
  552. // 币商收款方式
  553. $payment = MemberPaymentService::make()->getPayment($businessInfo['id']);
  554. if (empty($payment)) {
  555. $this->error = '3015';
  556. return false;
  557. }
  558. if($payment['type'] == 1){
  559. $idcardData['bank_name'] = isset($payment['bank_name'])? $payment['bank_name'] : '';
  560. $idcardData['bank_card'] = isset($payment['bank_card'])? $payment['bank_card'] : '';
  561. }else{
  562. $idcardData['bank_name'] = $payment['type']==2? '微信':'支付宝';
  563. $idcardData['bank_card'] = isset($payment['account'])? $payment['account'] : '';
  564. }
  565. $this->model->startTrans();
  566. $orderNo = get_order_num('OT');
  567. $data = [
  568. 'user_id' => $userId,
  569. 'business_id' => isset($businessInfo['id']) ? $businessInfo['id'] : 0,
  570. 'order_no' => $orderNo,
  571. 'type' => 1,
  572. 'pay_type' => isset($params['pay_type']) ? floatval($params['pay_type']) : 1,
  573. 'price' => $tradePrice,
  574. 'num' => $num,
  575. 'total' => $total,
  576. 'payment_id' => isset($payment['id']) ? intval($payment['id']) : 0,
  577. 'idcard_data' => $idcardData ? json_encode($idcardData, 256) : '',
  578. 'payment_data' => $payment ? json_encode($payment, 256) : '',
  579. 'create_time' => time(),
  580. 'update_time' => time(),
  581. 'status' => 1,
  582. 'mark' => 1,
  583. ];
  584. if (!$order = $this->model->edit($data)) {
  585. $this->model->rollBack();
  586. $this->error = '3023';
  587. return false;
  588. }
  589. if (!$this->memberModel->where(['id' => $businessInfo['id']])->decrement('usdt_num', $num)) {
  590. $this->model->rollBack();
  591. $this->error = '3020';
  592. return false;
  593. }
  594. $data = [
  595. 'order_no' => $orderNo,
  596. 'user_id' => $businessInfo['id'],
  597. 'type' => 2,
  598. 'pay_type' => 1,
  599. 'change_type' => 2,
  600. 'num' => $num,
  601. 'total' => $total,
  602. 'balance' => floatval($businessInfo['usdt_num'] - $num),
  603. 'create_time' => time(),
  604. 'update_time' => time(),
  605. 'status' => 1,
  606. 'mark' => 1,
  607. 'remark' => '交易员卖出',
  608. ];
  609. if (!$this->capitalModel->edit($data)) {
  610. $this->model->rollBack();
  611. $this->error = '3014';
  612. return false;
  613. }
  614. // 订单通知
  615. $data = [
  616. 'from_uid' => $userId,
  617. 'to_uid' => $businessInfo['id'],
  618. 'type' => 2,
  619. 'order_no' => $orderNo,
  620. 'chat_key' => getChatKey($userId, $businessInfo['id']),
  621. 'message' => "您有来自客户买单订单:{$orderNo}的消息,请尽快回复!",
  622. 'message_type' => 1,
  623. 'data_type' => 2,
  624. 'create_time' => time(),
  625. 'update_time' => time(),
  626. 'status' => 1,
  627. 'mark' => 1,
  628. ];
  629. if (!ChatMessageService::make()->pushMessage($data)) {
  630. $this->model->rollBack();
  631. $this->error = '3031';
  632. return false;
  633. }
  634. $this->model->commit();
  635. $data['id'] = $order;
  636. return $data;
  637. }
  638. /**
  639. * 客户卖出
  640. * @param $userId
  641. * @param $params
  642. * @return false|int|number
  643. */
  644. public function sell($userId, $params)
  645. {
  646. $num = isset($params['num']) ? floatval($params['num']) : 0;
  647. if ($userId <= 0) {
  648. $this->error = '1013';
  649. return false;
  650. }
  651. // 验证参数
  652. $config = ConfigService::make()->getConfigOptionByGroup(5);
  653. $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 0;
  654. $tradeMinNum = isset($config['trade_min']) ? $config['trade_min'] : 0;
  655. $tradeMaxNum = isset($config['trade_max']) ? $config['trade_max'] : 0;
  656. $tradePrice = isset($config['usdt_sell_price']) ? $config['usdt_sell_price'] : 0;
  657. $tradeLimitNum = isset($config['trade_no_catch']) ? $config['trade_no_catch'] : 0;
  658. // 是否开启交易
  659. if ($tradeOpen != 1) {
  660. $this->error = '1013';
  661. return false;
  662. }
  663. if ($tradePrice <= 0) {
  664. $this->error = '3002';
  665. return false;
  666. }
  667. // 验证数量或金额
  668. $total = moneyFormat($num * $tradePrice, 6);
  669. if ($num < $tradeMinNum || ($tradeMaxNum && $num > $tradeMaxNum)) {
  670. $this->error = '3003';
  671. return false;
  672. }
  673. // 用户信息
  674. $userInfo = MemberService::make()->getInfo($userId);
  675. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  676. $idcardCheck = isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0;
  677. $usdtNum = isset($userInfo['usdt_num']) ? $userInfo['usdt_num'] : 0;
  678. $username = isset($userInfo['username']) && $userInfo['username'] ? format_account($userInfo['username']) : '';
  679. if ($status != 1) {
  680. $this->error = '2009';
  681. return false;
  682. }
  683. if ($idcardCheck != 1) {
  684. $this->error = '2014';
  685. return false;
  686. }
  687. if ($usdtNum < $num) {
  688. $this->error = '3011';
  689. return false;
  690. }
  691. // 未处理订单
  692. $noCatchOrder = $this->checkOrderNoCatch($userId, 2);
  693. if ($tradeLimitNum > 0 && $noCatchOrder >= $tradeLimitNum) {
  694. $this->error = lang(3005, ['num' => $tradeLimitNum]);
  695. return false;
  696. }
  697. // 匹配交易商家
  698. $businessInfo = \App\Services\Api\MemberService::make()->getTradeMember($num, 2, $userId);
  699. if (empty($businessInfo)) {
  700. $this->error = '3004';
  701. return false;
  702. }
  703. $idcardData = [
  704. 'idcard' => isset($userInfo['idcard']) ? $userInfo['idcard'] : '',
  705. 'idcard_check' => isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0,
  706. 'idcard_front_img' => isset($userInfo['idcard_front_img']) ? $userInfo['idcard_front_img'] : '',
  707. 'idcard_back_img' => isset($userInfo['idcard_back_img']) ? $userInfo['idcard_back_img'] : '',
  708. 'idcard_hand_img' => isset($userInfo['idcard_hand_img']) ? $userInfo['idcard_hand_img'] : '',
  709. ];
  710. // 收款方式
  711. $paymentId = isset($params['payment_id']) ? $params['payment_id'] : 0;
  712. $paymentInfo = MemberPaymentService::make()->getInfo($paymentId);
  713. if (empty($paymentInfo)) {
  714. $this->error = '3010';
  715. return false;
  716. }
  717. $paymentData = [
  718. 'payment_id' => $paymentInfo['id'],
  719. 'type' => $paymentInfo['type'],
  720. 'logo' => $paymentInfo['logo'] ? get_image_url($paymentInfo['logo']) : '',
  721. 'real_name' => $paymentInfo['real_name'],
  722. 'bank_name' => $paymentInfo['bank_name'],
  723. 'bank_card' => $paymentInfo['bank_card'],
  724. 'branch_name' => $paymentInfo['branch_name'],
  725. 'qrcode' => $paymentInfo['qrcode'] ? get_image_url($paymentInfo['qrcode']) : '',
  726. 'account' => $paymentInfo['account'],
  727. ];
  728. if($paymentData['type'] == 1){
  729. $idcardData['bank_name'] = isset($paymentInfo['bank_name'])? $paymentInfo['bank_name'] : '';
  730. $idcardData['bank_card'] = isset($paymentInfo['bank_card'])? $paymentInfo['bank_card'] : '';
  731. }else{
  732. $idcardData['bank_name'] = $paymentData['type']==2? '微信':'支付宝';
  733. $idcardData['bank_card'] = isset($paymentInfo['account'])? $paymentInfo['account'] : '';
  734. }
  735. $orderNo = get_order_num('OT');
  736. $data = [
  737. 'user_id' => $userId,
  738. 'business_id' => isset($businessInfo['id']) ? $businessInfo['id'] : 0,
  739. 'order_no' => $orderNo,
  740. 'type' => 2,
  741. 'pay_type' => isset($params['pay_type']) ? floatval($params['pay_type']) : 1,
  742. 'price' => $tradePrice,
  743. 'num' => $num,
  744. 'payment_id' => $paymentId,
  745. 'idcard_data' => json_encode($idcardData, 256),
  746. 'payment_data' => json_encode($paymentData, 256),
  747. 'total' => $total,
  748. 'create_time' => time(),
  749. 'status' => 1,
  750. 'mark' => 1,
  751. ];
  752. $this->model->startTrans();
  753. if (!$order = $this->model->edit($data)) {
  754. $this->error = '3012';
  755. $this->model->rollBack();
  756. return false;
  757. }
  758. // 扣除币
  759. if (!$this->memberModel->where(['id' => $userId])->decrement('usdt_num', $num)) {
  760. $this->error = '3013';
  761. $this->model->rollBack();
  762. return false;
  763. }
  764. // 账户明细
  765. $data = [
  766. 'order_no' => $data['order_no'],
  767. 'user_id' => $userId,
  768. 'type' => 2,
  769. 'change_type' => 2,
  770. 'num' => $num,
  771. 'total' => $total,
  772. 'balance' => floatval($userInfo['usdt_num'] - $num),
  773. 'create_time' => time(),
  774. 'update_time' => time(),
  775. 'remark' => '客户卖出',
  776. 'status' => 1,
  777. 'mark' => 1,
  778. ];
  779. if (!$this->capitalModel->edit($data)) {
  780. $this->error = '3014';
  781. $this->model->rollBack();
  782. return false;
  783. }
  784. // 订单通知
  785. $data = [
  786. 'from_uid' => $userId,
  787. 'to_uid' => $businessInfo['id'],
  788. 'type' => 2,
  789. 'order_no' => $orderNo,
  790. 'chat_key' => getChatKey($userId, $businessInfo['id']),
  791. 'message' => "您有来自客户卖单订单:{$orderNo}的消息,请尽快回复!",
  792. 'message_type' => 1,
  793. 'data_type' => 3,
  794. 'create_time' => time(),
  795. 'update_time' => time(),
  796. 'status' => 1,
  797. 'mark' => 1,
  798. ];
  799. if (!ChatMessageService::make()->pushMessage($data)) {
  800. $this->model->rollBack();
  801. $this->error = '3023';
  802. return false;
  803. }
  804. $this->model->commit();
  805. $data['id'] = $order;
  806. return $data;
  807. }
  808. /**
  809. * 订单打款处理
  810. * @param $userId 用户ID
  811. * @param $params 打款参数
  812. * @return false
  813. */
  814. public function pay($userId, $params)
  815. {
  816. $orderId = isset($params['id']) ? $params['id'] : 0;
  817. if ($orderId <= 0) {
  818. $this->error = '1013';
  819. return false;
  820. }
  821. $orderInfo = $this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1, 'type' => 1])
  822. ->whereIn('status', [1, 2, 5, 7])
  823. ->select(['id', 'order_no', 'business_id', 'type', 'payment_id', 'num', 'total', 'status'])
  824. ->first();
  825. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  826. if (empty($orderInfo)) {
  827. $this->error = '3016';
  828. return false;
  829. }
  830. if ($tradeType != 1) {
  831. $this->error = '3024';
  832. return false;
  833. }
  834. // 用户信息
  835. $userInfo = MemberService::make()->getInfo($userId);
  836. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  837. if ($status != 1) {
  838. $this->error = '2009';
  839. return false;
  840. }
  841. // 交易密码
  842. $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
  843. $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
  844. if (empty($password)) {
  845. $this->error = '2015';
  846. return false;
  847. }
  848. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  849. $this->error = '2016';
  850. return false;
  851. }
  852. $data = [
  853. 'status' => 3,
  854. 'pay_type' => $params['pay_type'],
  855. 'pay_img' => $params['pay_img'],
  856. 'pay_remark' => $params['pay_remark'],
  857. 'pay_time' => time(),
  858. ];
  859. if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update($data)) {
  860. $this->error = '3018';
  861. return false;
  862. }
  863. return true;
  864. }
  865. /**
  866. * 商家订单打款处理
  867. * @param $userId 用户ID
  868. * @param $params 打款参数
  869. * @return false
  870. */
  871. public function businessPay($businessId, $params)
  872. {
  873. $orderId = isset($params['id']) ? $params['id'] : 0;
  874. if ($orderId <= 0) {
  875. $this->error = '1013';
  876. return false;
  877. }
  878. $orderInfo = $this->model->where(['business_id' => $businessId, 'id' => $orderId, 'mark' => 1, 'type' => 2])
  879. ->whereIn('status', [1, 2, 5, 7])
  880. ->select(['id', 'user_id', 'order_no', 'business_id', 'type', 'payment_id', 'num', 'total', 'status'])
  881. ->first();
  882. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  883. if (empty($orderInfo)) {
  884. $this->error = '3016';
  885. return false;
  886. }
  887. if ($tradeType != 2) {
  888. $this->error = '3025';
  889. return false;
  890. }
  891. // 用户信息
  892. $userInfo = MemberService::make()->getInfo($businessId);
  893. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  894. $username = isset($userInfo['username']) ? $userInfo['username'] : 0;
  895. if ($status != 1) {
  896. $this->error = '2009';
  897. return false;
  898. }
  899. // 交易密码
  900. $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
  901. $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
  902. if (empty($password)) {
  903. $this->error = '2015';
  904. return false;
  905. }
  906. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  907. $this->error = '2016';
  908. return false;
  909. }
  910. if (strpos($params['pay_img'], "temp")) {
  911. $params['pay_img'] = save_image($params['pay_img'], 'images');
  912. } else {
  913. $params['pay_img'] = str_replace(IMG_URL, "", $params['pay_img']);
  914. }
  915. $data = [
  916. 'status' => 3,
  917. 'pay_type' => $params['pay_type'],
  918. 'pay_img' => $params['pay_img'],
  919. 'pay_remark' => $params['pay_remark'],
  920. 'pay_time' => time(),
  921. ];
  922. if (!$this->model->where(['business_id' => $businessId, 'id' => $orderId, 'mark' => 1])->update($data)) {
  923. $this->error = '3018';
  924. return false;
  925. }
  926. $this->error = '3017';
  927. return true;
  928. }
  929. /**
  930. * 订单确认处理
  931. * @param $userId 用户ID
  932. * @param $params 打款参数
  933. * @return false
  934. */
  935. public function collection($userId, $params)
  936. {
  937. $orderId = isset($params['id']) ? $params['id'] : 0;
  938. if ($orderId <= 0) {
  939. $this->error = '1013';
  940. return false;
  941. }
  942. $orderInfo = $this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1, 'type' => 2])
  943. ->select(['id', 'user_id', 'order_no', 'business_id', 'payment_id', 'type', 'num', 'total', 'status'])
  944. ->first();
  945. $businessId = isset($orderInfo['business_id']) ? $orderInfo['business_id'] : 0;
  946. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  947. if (empty($orderInfo) || empty($businessId)) {
  948. $this->error = '3016';
  949. return false;
  950. }
  951. if ($orderInfo['status'] != 3) {
  952. $this->error = '3026';
  953. return false;
  954. }
  955. if ($tradeType != 2) {
  956. $this->error = '3024';
  957. return false;
  958. }
  959. $this->model->startTrans();
  960. // 订单状态更新
  961. if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update(['status' => 4, 'update_time' => time()])) {
  962. $this->model->rollBack();
  963. $this->error = '3023';
  964. return false;
  965. }
  966. // 交易处理
  967. if ($orderInfo['num'] > 0) {
  968. $info = $this->memberModel->where(['id' => $businessId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  969. if (empty($info)) {
  970. $this->model->rollBack();
  971. $this->error = '3019';
  972. return false;
  973. }
  974. // 商家进币
  975. if (!$this->memberModel->where(['id' => $businessId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  976. $this->model->rollBack();
  977. $this->error = '3019';
  978. return false;
  979. }
  980. // 明细处理
  981. $data = [
  982. 'order_no' => $orderInfo['order_no'],
  983. 'user_id' => $businessId,
  984. 'type' => 1,
  985. 'pay_type' => 1,
  986. 'change_type' => 1,
  987. 'num' => $orderInfo['num'],
  988. 'total' => $orderInfo['total'],
  989. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  990. 'create_time' => time(),
  991. 'remark' => '交易员买入',
  992. 'status' => 1,
  993. 'mark' => 1,
  994. ];
  995. if (!$this->capitalModel->edit($data)) {
  996. $this->error = '3014';
  997. $this->model->rollBack();
  998. return false;
  999. }
  1000. }
  1001. $this->model->commit();
  1002. return true;
  1003. }
  1004. /**
  1005. * 商家订单确认处理
  1006. * @param $businessId 币商用户ID,平台处理时为0
  1007. * @param $params 打款参数
  1008. * @return false
  1009. */
  1010. public function businessCollection($businessId, $params)
  1011. {
  1012. $orderId = isset($params['id']) ? $params['id'] : 0;
  1013. $catchUid = isset($params['catch_uid']) ? $params['catch_uid'] : 0;
  1014. $type = isset($params['type']) ? $params['type'] : 0;
  1015. if ($orderId <= 0) {
  1016. $this->error = '1013';
  1017. return false;
  1018. }
  1019. // 平台或币商处理
  1020. $where = ['id' => $orderId, 'mark' => 1];
  1021. if ($businessId) {
  1022. $where['business_id'] = $businessId;
  1023. }
  1024. if ($type) {
  1025. $where['type'] = $type;
  1026. }
  1027. $orderInfo = $this->model->where($where)
  1028. ->select(['id', 'user_id','api_id','pt_order_no', 'order_no', 'business_id', 'type','contact_type','is_cancel','usdt_address','notify_url', 'trade_type', 'num', 'total','pay_time', 'status'])
  1029. ->first();
  1030. $userId = isset($orderInfo['user_id']) ? $orderInfo['user_id'] : 0;
  1031. $orderType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  1032. $tradeType = isset($orderInfo['trade_type']) ? $orderInfo['trade_type'] : 0;
  1033. if (empty($orderInfo) || (empty($userId) && $tradeType != 2)) {
  1034. $this->error = '3016';
  1035. return false;
  1036. }
  1037. $status = isset($params['status']) ? intval($params['status']) : 4;
  1038. if ($status == 4 && $orderInfo['status'] != 3 && $businessId > 0) {
  1039. $this->error = '3026';
  1040. return false;
  1041. }
  1042. // if ($status == 5 && $orderInfo['status'] == 8 && $businessId > 0) {
  1043. // $this->error = '3026';
  1044. // return false;
  1045. // }
  1046. if ($status == 5 && $businessId <= 0 && $orderInfo['status'] == 4) {
  1047. $this->error = '3026';
  1048. return false;
  1049. }
  1050. if ($orderType != 1 && $status != 5) {
  1051. $this->error = '3024';
  1052. return false;
  1053. }
  1054. // 用户信息
  1055. if ($businessId > 0) {
  1056. $userInfo = MemberService::make()->getInfo($businessId);
  1057. $userStatus = isset($userInfo['status']) ? $userInfo['status'] : 0;
  1058. $username = isset($userInfo['username']) ? $userInfo['username'] : 0;
  1059. if ($userStatus != 1) {
  1060. $this->error = '2009';
  1061. return false;
  1062. }
  1063. // 交易密码
  1064. $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
  1065. $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
  1066. if (empty($password)) {
  1067. $this->error = '2015';
  1068. return false;
  1069. }
  1070. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  1071. $this->error = '2016';
  1072. return false;
  1073. }
  1074. }
  1075. $this->model->startTrans();
  1076. // 订单状态更新,异常或确认
  1077. $updateData = ['status' => $status, 'exception_catch_user' => $catchUid, 'update_time' => time()];
  1078. // 异常处理数据
  1079. if (isset($params['exception_type'])) {
  1080. $updateData['exception_type'] = intval($params['exception_type']);
  1081. }
  1082. if (isset($params['exception_sub_type'])) {
  1083. $updateData['exception_sub_type'] = intval($params['exception_sub_type']);
  1084. }
  1085. if (isset($params['exception_img'])) {
  1086. $updateData['exception_img'] = trim($params['exception_img']);
  1087. if (strpos($updateData['exception_img'], "temp")) {
  1088. $updateData['exception_img'] = save_image($updateData['exception_img'], 'images');
  1089. } else {
  1090. $updateData['exception_img'] = str_replace(IMG_URL, "", $updateData['exception_img']);
  1091. }
  1092. }
  1093. if (isset($params['exception_img1'])) {
  1094. $updateData['exception_img1'] = trim($params['exception_img1']);
  1095. if (strpos($updateData['exception_img1'], "temp")) {
  1096. $updateData['exception_img1'] = save_image($updateData['exception_img1'], 'images');
  1097. } else {
  1098. $updateData['exception_img1'] = str_replace(IMG_URL, "", $updateData['exception_img1']);
  1099. }
  1100. }
  1101. if (isset($params['exception_img2'])) {
  1102. $updateData['exception_img2'] = trim($params['exception_img2']);
  1103. if (strpos($updateData['exception_img2'], "temp")) {
  1104. $updateData['exception_img2'] = save_image($updateData['exception_img2'], 'images');
  1105. } else {
  1106. $updateData['exception_img2'] = str_replace(IMG_URL, "", $updateData['exception_img2']);
  1107. }
  1108. }
  1109. if (isset($params['refund_img'])) {
  1110. $updateData['refund_img'] = trim($params['refund_img']);
  1111. if (strpos($updateData['refund_img'], "temp")) {
  1112. $updateData['refund_img'] = save_image($updateData['refund_img'], 'images');
  1113. } else {
  1114. $updateData['refund_img'] = str_replace(IMG_URL, "", $updateData['refund_img']);
  1115. }
  1116. }
  1117. if (isset($params['refund_img1'])) {
  1118. $updateData['refund_img1'] = trim($params['refund_img1']);
  1119. if (strpos($updateData['refund_img1'], "temp")) {
  1120. $updateData['refund_img1'] = save_image($updateData['refund_img1'], 'images');
  1121. } else {
  1122. $updateData['refund_img1'] = str_replace(IMG_URL, "", $updateData['refund_img1']);
  1123. }
  1124. }
  1125. if (isset($params['refund_img2'])) {
  1126. $updateData['refund_img2'] = trim($params['refund_img2']);
  1127. if (strpos($updateData['refund_img2'], "temp")) {
  1128. $updateData['refund_img2'] = save_image($updateData['refund_img2'], 'images');
  1129. } else {
  1130. $updateData['refund_img2'] = str_replace(IMG_URL, "", $updateData['refund_img2']);
  1131. }
  1132. }
  1133. if (isset($params['refund_payment'])) {
  1134. $updateData['refund_payment'] = trim($params['refund_payment']);
  1135. }
  1136. if (isset($params['refund_status'])) {
  1137. $updateData['refund_status'] = intval($params['refund_status']);
  1138. }
  1139. if (isset($params['exception_remark'])) {
  1140. $updateData['exception_remark'] = trim($params['exception_remark']);
  1141. }
  1142. if (isset($params['exception_status'])) {
  1143. $updateData['exception_status'] = intval($params['exception_status']);
  1144. }
  1145. if (isset($params['refund_money'])) {
  1146. $updateData['refund_money'] = intval($params['refund_money']);
  1147. }
  1148. if (!$this->model->where($where)->update($updateData)) {
  1149. $this->model->rollBack();
  1150. $this->error = '3023';
  1151. return false;
  1152. }
  1153. // 交易处理,如果是确认状态
  1154. $exceptionType = isset($params['exception_type'])? $params['exception_type'] : 0;
  1155. $exceptionStatus = isset($params['exception_status'])? $params['exception_status'] : 0;
  1156. $refundStatus = isset($params['refund_status'])? $params['refund_status'] : 0;
  1157. $isCancel = isset($orderInfo['is_cancel']) ? $orderInfo['is_cancel'] : 0;
  1158. if ($orderInfo['num'] > 0 && ($status == 4 || ($exceptionStatus==2 && $refundStatus==0)) && $userId > 0 && $tradeType == 1) {
  1159. // 若订单被系统取消过,重新扣除
  1160. if($isCancel == 1){
  1161. // 商家
  1162. $info = $this->memberModel->where(['id' => $orderInfo['business_id'], 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1163. if (empty($info)) {
  1164. $this->model->rollBack();
  1165. $this->error = '3048';
  1166. return false;
  1167. }
  1168. if($info['usdt_num'] < $orderInfo['num']){
  1169. $this->model->rollBack();
  1170. $this->error = '3004';
  1171. return false;
  1172. }
  1173. // 商家重新扣除币
  1174. if (!$this->memberModel->where(['id' => $orderInfo['business_id'], 'mark' => 1])->decrement('usdt_num', $orderInfo['num'])) {
  1175. $this->model->rollBack();
  1176. $this->error = '3047';
  1177. return false;
  1178. }
  1179. // 明细处理
  1180. $data = [
  1181. 'order_no' => $orderInfo['order_no'],
  1182. 'user_id' => $orderInfo['business_id'],
  1183. 'type' => 2,
  1184. 'pay_type' => 1,
  1185. 'change_type' => 2,
  1186. 'num' => $orderInfo['num'],
  1187. 'total' => $orderInfo['total'],
  1188. 'balance' => floatval($info['usdt_num'] - $orderInfo['num']),
  1189. 'create_time' => time(),
  1190. 'update_time' => time(),
  1191. 'remark' => '交易员卖出重新扣除',
  1192. 'status' => 1,
  1193. 'mark' => 1,
  1194. ];
  1195. if (!$this->capitalModel->edit($data)) {
  1196. $this->error = '3014';
  1197. $this->model->rollBack();
  1198. return false;
  1199. }
  1200. }
  1201. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1202. if (empty($info)) {
  1203. $this->model->rollBack();
  1204. $this->error = '3019';
  1205. return false;
  1206. }
  1207. // 客户币进账
  1208. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1209. $this->model->rollBack();
  1210. $this->error = '3019';
  1211. return false;
  1212. }
  1213. // 明细处理
  1214. $data = [
  1215. 'order_no' => $orderInfo['order_no'],
  1216. 'user_id' => $userId,
  1217. 'type' => 1,
  1218. 'pay_type' => 1,
  1219. 'change_type' => 1,
  1220. 'num' => $orderInfo['num'],
  1221. 'total' => $orderInfo['total'],
  1222. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1223. 'create_time' => time(),
  1224. 'update_time' => time(),
  1225. 'remark' => '客户买入',
  1226. 'status' => 1,
  1227. 'mark' => 1,
  1228. ];
  1229. if (!$this->capitalModel->edit($data)) {
  1230. $this->error = '3014';
  1231. $this->model->rollBack();
  1232. return false;
  1233. }
  1234. // 更新收款地址数据
  1235. $paymentId = isset($orderInfo['payment_id']) ? $orderInfo['payment_id'] : 0;
  1236. $info = $this->paymentModel->where(['id' => $paymentId, 'mark' => 1])->select(['id', 'used_at'])->first();
  1237. if ($paymentId && $info) {
  1238. $usedAt = isset($info['used_at']) ? $info['used_at'] : '';
  1239. if ($usedAt < date('Y-m-d')) {
  1240. if (!$this->paymentModel->where(['id' => $paymentId])->update(['used_num' => 1, 'used_quota' => $orderInfo['total'], 'update_time' => time()])) {
  1241. $this->model->rollBack();
  1242. $this->error = '3018';
  1243. return false;
  1244. }
  1245. } else {
  1246. $res1 = $this->paymentModel->where(['id' => $paymentId])->increment('used_num', 1);
  1247. $res2 = $this->paymentModel->where(['id' => $paymentId])->increment('used_quota', floatval($orderInfo['total']));
  1248. if (!$res1 || !$res2) {
  1249. $this->model->rollBack();
  1250. $this->error = '3014';
  1251. return false;
  1252. }
  1253. }
  1254. }
  1255. }
  1256. // 外来平台需要出币和回调通知
  1257. else if(in_array($tradeType, [2,3]) && ($status == 4 || ($refundStatus==0 && $exceptionStatus==2))){
  1258. $usdtAddress = isset($orderInfo['usdt_address'])? $orderInfo['usdt_address'] : '';
  1259. $contactType = isset($orderInfo['contact_type'])? $orderInfo['contact_type'] : '';
  1260. $notifyUrl = isset($orderInfo['notify_url'])? $orderInfo['notify_url'] : '';
  1261. if($usdtAddress && $contactType && $notifyUrl){
  1262. // TRC 出币
  1263. if($contactType == 1 && !$result = UsdtWalletService::make()->usdtTrcTransfer($usdtAddress,$orderInfo['num'])){
  1264. $this->model->rollBack();
  1265. $this->error = '3045';
  1266. return false;
  1267. }
  1268. // ERC 出币
  1269. else if($contactType == 2 && !$result = UsdtWalletService::make()->usdtErcTransfer($usdtAddress,$orderInfo['num'])){
  1270. $this->model->rollBack();
  1271. $this->error = '3045';
  1272. return false;
  1273. }
  1274. // 更新出币数据和发送回调
  1275. $txid = isset($result['txID'])? $result['txID'] : '';
  1276. if($txid) {
  1277. if (!$this->model->where($where)->update(['txid' => $txid, 'notify_num' => 1, 'notify_time' => date('Y-m-d H:i:s')])) {
  1278. $this->model->rollBack();
  1279. $this->error = '3045';
  1280. return false;
  1281. }
  1282. // 发送回调通知
  1283. $notifyData = [
  1284. 'transaction_id' => $orderInfo['order_no'],
  1285. 'order_no' => $orderInfo['pt_order_no'],
  1286. 'usdt' => $orderInfo['num'],
  1287. 'amount' => $orderInfo['total'],
  1288. 'contact_type' => $orderInfo['contact_type'],
  1289. 'usdt_address' => $orderInfo['usdt_address'],
  1290. 'hash' => $txid,
  1291. 'status'=> 1,
  1292. 'pay_time'=> $orderInfo['pay_time']? $orderInfo['pay_time'] : time(),
  1293. 'service_time'=> time(),
  1294. ];
  1295. \App\Services\Oapi\TradeOrderService::make()->notify($notifyUrl, $notifyData);
  1296. }
  1297. }
  1298. }
  1299. $this->model->commit();
  1300. $this->error = $status == 5 ? 3032 : 3021;
  1301. return true;
  1302. }
  1303. /**
  1304. * 取消订单
  1305. * @param $userId
  1306. * @param $params
  1307. * @return false
  1308. */
  1309. public function cancel($userId, $params)
  1310. {
  1311. $orderId = isset($params['id']) ? intval($params['id']) : 0;
  1312. if ($orderId <= 0) {
  1313. $this->error = '1013';
  1314. return false;
  1315. }
  1316. $orderInfo = $this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])
  1317. ->select(['id', 'order_no', 'business_id', 'type', 'num', 'total', 'status'])
  1318. ->first();
  1319. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  1320. $businessId = isset($orderInfo['business_id']) ? $orderInfo['business_id'] : 0;
  1321. if (empty($orderInfo) || $businessId <= 0) {
  1322. $this->error = '3016';
  1323. return false;
  1324. }
  1325. if ($orderInfo['status'] == 3) {
  1326. $this->error = '3027';
  1327. return false;
  1328. }
  1329. if ($orderInfo['status'] == 4) {
  1330. $this->error = '3028';
  1331. return false;
  1332. }
  1333. if ($orderInfo['status'] == 7) {
  1334. $this->error = '3030';
  1335. return false;
  1336. }
  1337. if (!in_array($orderInfo['status'], [1, 2])) {
  1338. $this->error = '3029';
  1339. return false;
  1340. }
  1341. $this->model->startTrans();
  1342. // 订单状态更新
  1343. $updateData = ['status' => 8, 'update_time' => time(), 'exception_remark' => '客户取消'];
  1344. if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update($updateData)) {
  1345. $this->model->rollBack();
  1346. $this->error = '3023';
  1347. return false;
  1348. }
  1349. // 出售订单,USDT退回
  1350. if ($tradeType == 2 && $orderInfo['num'] > 0) {
  1351. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1352. if (empty($info)) {
  1353. $this->model->rollBack();
  1354. $this->error = '3019';
  1355. return false;
  1356. }
  1357. // 退还币给客户
  1358. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1359. $this->model->rollBack();
  1360. $this->error = '3019';
  1361. return false;
  1362. }
  1363. // 明细处理
  1364. $data = [
  1365. 'order_no' => $orderInfo['order_no'],
  1366. 'user_id' => $userId,
  1367. 'type' => 3,
  1368. 'pay_type' => 1,
  1369. 'change_type' => 1,
  1370. 'num' => $orderInfo['num'],
  1371. 'total' => $orderInfo['total'],
  1372. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1373. 'create_time' => time(),
  1374. 'update_time' => time(),
  1375. 'remark' => '客户取消退还',
  1376. 'status' => 1,
  1377. 'mark' => 1,
  1378. ];
  1379. if (!$this->capitalModel->edit($data)) {
  1380. $this->error = '3014';
  1381. $this->model->rollBack();
  1382. return false;
  1383. }
  1384. } // 买入取消
  1385. else if ($tradeType == 1 && $orderInfo['num'] > 0) {
  1386. $info = $this->memberModel->where(['id' => $businessId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1387. if (empty($info)) {
  1388. $this->model->rollBack();
  1389. $this->error = '3019';
  1390. return false;
  1391. }
  1392. // 退还币给客户
  1393. if (!$this->memberModel->where(['id' => $businessId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1394. $this->model->rollBack();
  1395. $this->error = '3019';
  1396. return false;
  1397. }
  1398. // 明细处理
  1399. $data = [
  1400. 'order_no' => $orderInfo['order_no'],
  1401. 'user_id' => $businessId,
  1402. 'type' => 3,
  1403. 'pay_type' => 1,
  1404. 'change_type' => 1,
  1405. 'num' => $orderInfo['num'],
  1406. 'total' => $orderInfo['total'],
  1407. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1408. 'create_time' => time(),
  1409. 'update_time' => time(),
  1410. 'remark' => '客户取消退还',
  1411. 'status' => 1,
  1412. 'mark' => 1,
  1413. ];
  1414. if (!$this->capitalModel->edit($data)) {
  1415. $this->error = '3014';
  1416. $this->model->rollBack();
  1417. return false;
  1418. }
  1419. }
  1420. $this->model->commit();
  1421. return true;
  1422. }
  1423. /**
  1424. * 取消订单
  1425. * @param $businessId 币商用户,平台为0
  1426. * @param $params
  1427. * @return false
  1428. */
  1429. public function businessCancel($businessId, $params)
  1430. {
  1431. $orderId = isset($params['id']) ? intval($params['id']) : 0;
  1432. $catchUid = isset($params['catch_uid']) ? intval($params['catch_uid']) : 0;
  1433. if ($orderId <= 0) {
  1434. $this->error = '1013';
  1435. return false;
  1436. }
  1437. $where = ['id' => $orderId, 'mark' => 1];
  1438. if ($businessId) {
  1439. $where['business_id'] = $businessId;
  1440. }
  1441. $orderInfo = $this->model->where($where)
  1442. ->select(['id', 'user_id', 'order_no', 'business_id', 'type','contact_type','trade_type', 'num', 'total', 'status'])
  1443. ->first();
  1444. $type = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  1445. $tradeType = isset($orderInfo['trade_type']) ? $orderInfo['trade_type'] : 0;
  1446. $userId = isset($orderInfo['user_id']) ? $orderInfo['user_id'] : 0;
  1447. $orderBusinessId = isset($orderInfo['business_id']) ? $orderInfo['business_id'] : 0;
  1448. if (empty($orderInfo) || $orderBusinessId <= 0) {
  1449. $this->error = '3016';
  1450. return false;
  1451. }
  1452. if (empty($userId) && $tradeType != 2) {
  1453. $this->error = '3019';
  1454. return false;
  1455. }
  1456. if ($orderInfo['status'] == 4) {
  1457. $this->error = '3028';
  1458. return false;
  1459. }
  1460. if (!in_array($orderInfo['status'], [1, 2, 3, 5, 7])) {
  1461. $this->error = '3029';
  1462. return false;
  1463. }
  1464. // 用户信息
  1465. $status = isset($params['status']) ? $params['status'] : 8;
  1466. if ($businessId > 0 && $status != 8) {
  1467. $userInfo = MemberService::make()->getInfo($businessId);
  1468. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  1469. $username = isset($userInfo['username']) ? $userInfo['username'] : 0;
  1470. if ($status != 1) {
  1471. $this->error = '2009';
  1472. return false;
  1473. }
  1474. // 交易密码
  1475. $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
  1476. $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
  1477. if (empty($password)) {
  1478. $this->error = '2015';
  1479. return false;
  1480. }
  1481. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  1482. $this->error = '2016';
  1483. return false;
  1484. }
  1485. }
  1486. $this->model->startTrans();
  1487. // 订单状态更新,或异常处理
  1488. $updateData = ['status' => $status, 'exception_catch_user' => $catchUid, 'update_time' => time(), 'exception_remark' => $businessId? '交易员取消':'客服取消'];
  1489. // 异常处理数据
  1490. if (isset($params['exception_type'])) {
  1491. $updateData['exception_type'] = intval($params['exception_type']);
  1492. }
  1493. if (isset($params['exception_sub_type'])) {
  1494. $updateData['exception_sub_type'] = intval($params['exception_sub_type']);
  1495. }
  1496. if (isset($params['exception_img'])) {
  1497. $updateData['exception_img'] = trim($params['exception_img']);
  1498. if (strpos($updateData['exception_img'], "temp")) {
  1499. $updateData['exception_img'] = save_image($updateData['exception_img'], 'images');
  1500. } else {
  1501. $updateData['exception_img'] = str_replace(IMG_URL, "", $updateData['exception_img']);
  1502. }
  1503. }
  1504. if (isset($params['exception_img1'])) {
  1505. $updateData['exception_img1'] = trim($params['exception_img1']);
  1506. if (strpos($updateData['exception_img1'], "temp")) {
  1507. $updateData['exception_img1'] = save_image($updateData['exception_img1'], 'images');
  1508. } else {
  1509. $updateData['exception_img1'] = str_replace(IMG_URL, "", $updateData['exception_img1']);
  1510. }
  1511. }
  1512. if (isset($params['exception_img2'])) {
  1513. $updateData['exception_img2'] = trim($params['exception_img2']);
  1514. if (strpos($updateData['exception_img2'], "temp")) {
  1515. $updateData['exception_img2'] = save_image($updateData['exception_img2'], 'images');
  1516. } else {
  1517. $updateData['exception_img2'] = str_replace(IMG_URL, "", $updateData['exception_img2']);
  1518. }
  1519. }
  1520. if (isset($params['refund_img'])) {
  1521. $updateData['refund_img'] = trim($params['refund_img']);
  1522. if (strpos($updateData['refund_img'], "temp")) {
  1523. $updateData['refund_img'] = save_image($updateData['refund_img'], 'images');
  1524. } else {
  1525. $updateData['refund_img'] = str_replace(IMG_URL, "", $updateData['refund_img']);
  1526. }
  1527. }
  1528. if (isset($params['refund_img1'])) {
  1529. $updateData['refund_img1'] = trim($params['refund_img1']);
  1530. if (strpos($updateData['refund_img1'], "temp")) {
  1531. $updateData['refund_img1'] = save_image($updateData['refund_img1'], 'images');
  1532. } else {
  1533. $updateData['refund_img1'] = str_replace(IMG_URL, "", $updateData['refund_img1']);
  1534. }
  1535. }
  1536. if (isset($params['refund_img2'])) {
  1537. $updateData['refund_img2'] = trim($params['refund_img2']);
  1538. if (strpos($updateData['refund_img2'], "temp")) {
  1539. $updateData['refund_img2'] = save_image($updateData['refund_img2'], 'images');
  1540. } else {
  1541. $updateData['refund_img2'] = str_replace(IMG_URL, "", $updateData['refund_img2']);
  1542. }
  1543. }
  1544. if (isset($params['refund_payment'])) {
  1545. $updateData['refund_payment'] = trim($params['refund_payment']);
  1546. }
  1547. if (isset($params['exception_remark'])) {
  1548. $updateData['exception_remark'] = trim($params['exception_remark']);
  1549. }
  1550. if (isset($params['exception_status'])) {
  1551. $updateData['exception_status'] = intval($params['exception_status']);
  1552. }
  1553. if (isset($params['refund_status'])) {
  1554. $updateData['refund_status'] = intval($params['refund_status']);
  1555. }
  1556. if (isset($params['refund_money'])) {
  1557. $updateData['refund_money'] = intval($params['refund_money']);
  1558. }
  1559. if (!$this->model->where($where)->update($updateData)) {
  1560. $this->model->rollBack();
  1561. $this->error = '3023';
  1562. return false;
  1563. }
  1564. // 平台内部订单取消
  1565. if($tradeType == 1){
  1566. // 出售订单,状态已取消,USDT退回
  1567. if ($type == 2 && $status == 8 && $orderInfo['num'] > 0) {
  1568. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1569. if (empty($info)) {
  1570. $this->model->rollBack();
  1571. $this->error = '3019';
  1572. return false;
  1573. }
  1574. // 退还币给客户
  1575. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1576. $this->model->rollBack();
  1577. $this->error = '3019';
  1578. return false;
  1579. }
  1580. // 明细处理
  1581. $data = [
  1582. 'order_no' => $orderInfo['order_no'],
  1583. 'user_id' => $userId,
  1584. 'type' => 3,
  1585. 'pay_type' => 1,
  1586. 'change_type' => 1,
  1587. 'num' => $orderInfo['num'],
  1588. 'total' => $orderInfo['total'],
  1589. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1590. 'create_time' => time(),
  1591. 'update_time' => time(),
  1592. 'remark' => '交易员取消买入退还',
  1593. 'status' => 1,
  1594. 'mark' => 1,
  1595. ];
  1596. if (!$this->capitalModel->edit($data)) {
  1597. $this->error = '3014';
  1598. $this->model->rollBack();
  1599. return false;
  1600. }
  1601. } // 客户买入订单取消
  1602. else if ($type == 1 && $status == 8 && $orderInfo['num'] > 0) {
  1603. $info = $this->memberModel->where(['id' => $orderBusinessId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1604. if (empty($info)) {
  1605. $this->model->rollBack();
  1606. $this->error = '3019';
  1607. return false;
  1608. }
  1609. // 退还币给客户
  1610. if (!$this->memberModel->where(['id' => $orderBusinessId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1611. $this->model->rollBack();
  1612. $this->error = '3019';
  1613. return false;
  1614. }
  1615. // 明细处理
  1616. $data = [
  1617. 'order_no' => $orderInfo['order_no'],
  1618. 'user_id' => $orderBusinessId,
  1619. 'type' => 3,
  1620. 'pay_type' => 1,
  1621. 'change_type' => 1,
  1622. 'num' => $orderInfo['num'],
  1623. 'total' => $orderInfo['total'],
  1624. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1625. 'create_time' => time(),
  1626. 'update_time' => time(),
  1627. 'remark' => '交易员取消卖出退还',
  1628. 'status' => 1,
  1629. 'mark' => 1,
  1630. ];
  1631. if (!$this->capitalModel->edit($data)) {
  1632. $this->error = '3014';
  1633. $this->model->rollBack();
  1634. return false;
  1635. }
  1636. }
  1637. }else{
  1638. // 卖出取消不需要退平台USDT,需要客服处理,线上退还币给平台方
  1639. // 客户买入取消
  1640. if ($type == 1 && $status == 8 && $orderInfo['num'] > 0) {
  1641. $info = $this->memberModel->where(['id' => $orderBusinessId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1642. if (empty($info)) {
  1643. $this->model->rollBack();
  1644. $this->error = '3019';
  1645. return false;
  1646. }
  1647. // 退还币给客户
  1648. if (!$this->memberModel->where(['id' => $orderBusinessId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1649. $this->model->rollBack();
  1650. $this->error = '3019';
  1651. return false;
  1652. }
  1653. // 明细处理
  1654. $data = [
  1655. 'order_no' => $orderInfo['order_no'],
  1656. 'user_id' => $orderBusinessId,
  1657. 'type' => 3,
  1658. 'pay_type' => 1,
  1659. 'change_type' => 1,
  1660. 'num' => $orderInfo['num'],
  1661. 'total' => $orderInfo['total'],
  1662. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1663. 'create_time' => time(),
  1664. 'update_time' => time(),
  1665. 'remark' => '交易员取消卖出退还',
  1666. 'status' => 1,
  1667. 'mark' => 1,
  1668. ];
  1669. if (!$this->capitalModel->edit($data)) {
  1670. $this->error = '3014';
  1671. $this->model->rollBack();
  1672. return false;
  1673. }
  1674. }
  1675. }
  1676. $this->model->commit();
  1677. $this->error = $status == 5 ? 3032 : 3034;
  1678. return true;
  1679. }
  1680. /**
  1681. * 无效订单处理
  1682. * @return false
  1683. */
  1684. public function catchInvalidOrder()
  1685. {
  1686. $cacheKey = "caches:trades:cancels:";
  1687. if (RedisService::get($cacheKey . 'lock')) {
  1688. return false;
  1689. }
  1690. RedisService::set($cacheKey . 'lock', 1, rand(3, 5));
  1691. $overtime = ConfigService::make()->getConfigByCode('trade_order_overtime');
  1692. $cancelTime = ConfigService::make()->getConfigByCode('trade_order_cancel');
  1693. $catchNum = ConfigService::make()->getConfigByCode('trade_order_catch_num');
  1694. $catchNum = $catchNum > 0 ? $catchNum : 200;
  1695. // 处理超时订单
  1696. if ($overtime > 0) {
  1697. $this->model->where(['mark' => 1])
  1698. ->where('status', '<=', 2)
  1699. ->where('status', '>', 0)
  1700. ->where('create_time', '<=', time() - $overtime * 60)
  1701. ->update(['status' => 7, 'catch_at' => time()]);
  1702. }
  1703. if ($cancelTime <= 0) {
  1704. $this->error = '1023';
  1705. return false;
  1706. }
  1707. $fail = 0;
  1708. $success = 0;
  1709. $this->model->where(function ($query) use ($cancelTime) {
  1710. // 已更新为超时的订单
  1711. $query->where(['mark' => 1, 'status' => 7])
  1712. ->where('catch_at', '<=', time() - $cancelTime * 60);
  1713. })
  1714. ->orWhere(function ($query) use ($cancelTime, $overtime) {
  1715. $query->where('mark', '=', 1)
  1716. ->where('status', '<=', 2)
  1717. ->where('status', '>', 0)
  1718. ->where('create_time', '<=', time() - ($cancelTime + $overtime) * 60);
  1719. })
  1720. ->select(['id', 'user_id', 'business_id', 'order_no', 'type', 'trade_type', 'num', 'total'])
  1721. ->take($catchNum)
  1722. ->get()
  1723. ->each(function ($item, $k) use ($cacheKey, &$fail, &$success) {
  1724. // 客户卖出订单退还
  1725. $date = date('Y-m-d H:i:s');
  1726. $type = isset($item['type']) ? $item['type'] : 0;
  1727. if ($type == 2) {
  1728. // 内部订单退还USDT给客户
  1729. if ($item['trade_type'] == 1) {
  1730. if (!$this->orderReback($item['user_id'], $item)) {
  1731. $fail++;
  1732. RedisService::set($cacheKey . "order_{$item['order_no']}:u{$item['user_id']}_fail", ['order' => $item, 'msg' => lang($this->error), 'date' => $date], 3600);
  1733. } else {
  1734. $success++;
  1735. RedisService::set($cacheKey . "order_{$item['order_no']}:u{$item['user_id']}_success", ['order' => $item, 'msg' => lang($this->error), 'date' => $date], 3600);
  1736. }
  1737. }
  1738. } else {
  1739. if (!$this->orderReback($item['business_id'], $item)) {
  1740. $fail++;
  1741. RedisService::set($cacheKey . "order_{$item['order_no']}:b{$item['business_id']}_fail", ['order' => $item, 'msg' => lang($this->error), 'date' => $date], 3600);
  1742. } else {
  1743. $success++;
  1744. RedisService::set($cacheKey . "order_{$item['order_no']}:b{$item['business_id']}_success", ['order' => $item, 'msg' => lang($this->error), 'date' => $date], 3600);
  1745. }
  1746. }
  1747. });
  1748. return ['success' => $success, 'fail' => $fail];
  1749. }
  1750. /**
  1751. * 订单取消退还处理
  1752. * @param $userId
  1753. * @param $orderInfo
  1754. * @return bool
  1755. */
  1756. protected function orderReback($userId, $orderInfo)
  1757. {
  1758. try {
  1759. if ($orderInfo['num'] <= 0) {
  1760. return false;
  1761. }
  1762. $this->model->startTrans();
  1763. $updateData = ['status' => 8, 'update_time' => time(),'is_cancel'=>1, 'catch_at' => time(),'exception_status'=> 0, 'exception_remark' => '系统取消'];
  1764. if (!$this->model->where(['id' => $orderInfo['id']])->update($updateData)) {
  1765. $this->model->rollBack();
  1766. $this->error = '3043';
  1767. return false;
  1768. }
  1769. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1770. if (empty($info)) {
  1771. $this->model->rollBack();
  1772. $this->error = '3019';
  1773. return false;
  1774. }
  1775. // 退还币给客户
  1776. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1777. $this->model->rollBack();
  1778. $this->error = '3019';
  1779. return false;
  1780. }
  1781. // 明细处理
  1782. $data = [
  1783. 'order_no' => $orderInfo['order_no'],
  1784. 'user_id' => $userId,
  1785. 'type' => 3,
  1786. 'pay_type' => 1,
  1787. 'change_type' => 1,
  1788. 'num' => $orderInfo['num'],
  1789. 'total' => $orderInfo['total'],
  1790. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1791. 'create_time' => time(),
  1792. 'update_time' => time(),
  1793. 'remark' => '系统自动取消退还',
  1794. 'status' => 1,
  1795. 'mark' => 1,
  1796. ];
  1797. if (!$this->capitalModel->edit($data)) {
  1798. $this->error = '3014';
  1799. $this->model->rollBack();
  1800. return false;
  1801. }
  1802. $this->model->commit();
  1803. $this->error = '3044';
  1804. return true;
  1805. } catch (\Exception $exception) {
  1806. $this->error = $exception->getMessage();
  1807. return false;
  1808. }
  1809. }
  1810. /**
  1811. * 重新派单
  1812. * @param $id 订单ID
  1813. * @param $businessId 新派单用户
  1814. * @return false
  1815. */
  1816. public function reassign($id, $businessId)
  1817. {
  1818. if (empty($id) || empty($businessId)) {
  1819. $this->error = '1013';
  1820. return false;
  1821. }
  1822. $orderInfo = $this->model->where(['id' => $id])
  1823. ->select(['id', 'user_id', 'order_no', 'business_id', 'type', 'num', 'total', 'status'])
  1824. ->first();
  1825. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  1826. if (empty($orderInfo)) {
  1827. $this->error = '3016';
  1828. return false;
  1829. }
  1830. // 状态
  1831. if (!in_array($orderInfo['status'], [1, 2, 3, 5, 7])) {
  1832. $this->error = '3039';
  1833. return false;
  1834. }
  1835. if ($businessId == $orderInfo['business_id']) {
  1836. $this->error = '3035';
  1837. return false;
  1838. }
  1839. // 派单用户信息
  1840. $businessInfo = $this->memberModel->from('member as a')
  1841. ->leftJoin('member_setting as ms', 'ms.user_id', '=', 'a.id')
  1842. ->where(['a.id' => $businessId, 'a.status' => 1, 'a.mark' => 1])
  1843. ->select(['a.id', 'a.username', 'a.usdt_num', 'ms.buy_online', 'ms.buy_online_time', 'ms.sell_online', 'ms.sell_online_time'])
  1844. ->first();
  1845. if (empty($businessInfo)) {
  1846. $this->error = '3036';
  1847. return false;
  1848. }
  1849. // 余额
  1850. if ($businessInfo['usdt_num'] < $orderInfo['num']) {
  1851. $this->error = '3036';
  1852. return false;
  1853. }
  1854. // 在线状态
  1855. if ($tradeType == 1) {
  1856. if ($businessInfo['buy_online'] != 1 || $businessInfo['buy_online_time'] <= time()) {
  1857. $this->error = '3036';
  1858. return false;
  1859. }
  1860. } else if ($tradeType == 2) {
  1861. if ($businessInfo['sell_online'] != 1 || $businessInfo['sell_online_time'] <= time()) {
  1862. $this->error = '3036';
  1863. return false;
  1864. }
  1865. }
  1866. $payment = MemberPaymentService::make()->getPayment($businessId);
  1867. if (empty($payment)) {
  1868. $this->error = '3040';
  1869. return false;
  1870. }
  1871. $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' => '平台重新派单'];
  1872. if ($this->model->where(['id' => $id])->update($updateData)) {
  1873. $this->error = '3041';
  1874. return true;
  1875. }
  1876. $this->error = '3042';
  1877. return false;
  1878. }
  1879. /**
  1880. * 派单交易完成通知
  1881. * @return int[]
  1882. */
  1883. public function catchOrderNotify()
  1884. {
  1885. $fail = 0;
  1886. $success = 0;
  1887. $times = [1=>'30',2=>'60',3=>'120',4=>'300',5=>'600',6=>'3600'];
  1888. $this->model->from('trade_order as a')
  1889. ->leftJoin('member as b','b.id','=','a.business_id')
  1890. ->leftJoin('member as c','c.id','=','a.user_id')
  1891. ->where(function ($query) {
  1892. // 已更新为超时的订单
  1893. $query->where(['a.mark' => 1])
  1894. ->where(function($query){
  1895. $query->where('a.status',4)->orWhere(['a.status'=>5,'a.exception_status'=>2,'a.refund_status'=>0]);
  1896. })
  1897. ->where('a.notify_time', '<=', time() - 3700);
  1898. })
  1899. ->where('a.api_id','>', 0)
  1900. ->where('a.trade_type','>', 1)
  1901. ->select(['a.id','a.user_id','a.business_id', 'order_no','a.txid','a.pt_order_no','a.usdt_address','a.type','a.trade_type','a.contact_type','a.status','a.price','a.num','a.total','a.notify_num','a.notify_time','a.notify_url','a.create_time','a.pay_type','a.pay_time','b.username','c.username as c_username'])
  1902. ->take(200)
  1903. ->get()
  1904. ->each(function ($item, $k) use (&$fail, &$success, $times) {
  1905. $cacheKey = "caches:task:orderNotify:".$item['order_no'].':';
  1906. // 客户卖出订单退还
  1907. $date = date('Y-m-d H:i:s');
  1908. $type = isset($item['type']) ? $item['type'] : 0;
  1909. $notifyTime = isset($item['notify_time']) && $item['notify_time']? strtotime($item['notify_time']) : 0;
  1910. $notifyNum = isset($item['notify_num'])? $item['notify_num'] : '';
  1911. $notifyUrl = isset($item['notify_url'])? $item['notify_url'] : '';
  1912. $txid = isset($item['txid'])? $item['txid'] : '';
  1913. $time = isset($times[$notifyNum])? $times[$notifyNum] : 0;
  1914. // 已交易完成且,出币
  1915. if($txid && (($time>0 && $notifyTime>=time()-$time) || ($time==0 && $notifyNum==0))){
  1916. RedisService::set($cacheKey.'temp', ['data'=> $item,'time'=> $time,'date'=> $date], 7200);
  1917. if ($this->model->where(['id'=> $item['id']])->update(['notify_num' => $notifyNum+1, 'notify_time' => date('Y-m-d H:i:s')])) {
  1918. // 发送回调通知
  1919. $notifyData = [
  1920. 'transaction_id' => $item['order_no'],
  1921. 'order_no' => $item['pt_order_no'],
  1922. 'usdt' => $item['num'],
  1923. 'amount' => $item['total'],
  1924. 'contact_type' => $item['contact_type'],
  1925. 'usdt_address' => $item['usdt_address'],
  1926. 'hash' => $txid,
  1927. 'status'=> 1,
  1928. 'pay_time'=> $item['pay_time']? $item['pay_time'] : time(),
  1929. 'service_time'=> time(),
  1930. ];
  1931. $success++;
  1932. \App\Services\Oapi\TradeOrderService::make()->notify($notifyUrl, $notifyData);
  1933. }
  1934. $fail++;
  1935. }
  1936. });
  1937. return ['success' => $success, 'fail' => $fail];
  1938. }
  1939. }