TradeOrderService.php 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703
  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. /**
  22. * 用户交易订单-服务类
  23. * Class TradeOrderService
  24. * @package App\Services\Common
  25. */
  26. class TradeOrderService extends BaseService
  27. {
  28. // 静态对象
  29. protected static $instance = null;
  30. /**
  31. * 构造函数
  32. * @since 2020/11/10
  33. * TradeOrderService constructor.
  34. */
  35. public function __construct()
  36. {
  37. $this->model = new TradeOrderModel();
  38. $this->memberModel = new MemberModel();
  39. $this->capitalModel = new CapitalLogModel();
  40. $this->paymentModel = new MemberPaymentModel();
  41. }
  42. /**
  43. * 静态入口
  44. * @return static|null
  45. */
  46. public static function make()
  47. {
  48. if (!self::$instance) {
  49. self::$instance = (new static());
  50. }
  51. return self::$instance;
  52. }
  53. /**
  54. * 订单列表
  55. * @param $params
  56. * @param int $pageSize
  57. * @return array
  58. */
  59. public function getDataList($params, $pageSize = 15)
  60. {
  61. $list = $this->model->from('trade_order as a')
  62. ->leftJoin('member as b', 'b.id', '=', 'a.business_id')
  63. ->leftJoin('member as c', 'c.id', '=', 'a.user_id')
  64. ->where(function ($query) use ($params) {
  65. $query->where(['a.mark' => 1])->where('a.status', '>', 0);
  66. $orderNo = isset($params['order_no']) && $params['order_no'] ? trim($params['order_no']) : '';
  67. if ($orderNo) {
  68. $query->where('a.order_no', 'like', "%{$orderNo}%");
  69. }
  70. $type = isset($params['type']) ? intval($params['type']) : 0;
  71. if ($type > 0) {
  72. $query->where(['a.type' => $type]);
  73. }
  74. // 日期
  75. $date = isset($params['date']) ? $params['date'] : [];
  76. $start = isset($date[0])? $date[0] : '';
  77. $end = isset($date[1])? $date[1] : '';
  78. $end = $start>=$end? '' : $end;
  79. if ($start) {
  80. $query->where('a.create_time','>=', strtotime($start));
  81. }
  82. if($end){
  83. $query->where('a.create_time','<', strtotime($end));
  84. }
  85. $exceptionStatus = isset($params['exception_status']) ? intval($params['exception_status']) : 0;
  86. if ($exceptionStatus > 0) {
  87. $query->where(['a.exception_status' => $exceptionStatus]);
  88. }
  89. $status = isset($params['status']) ? intval($params['status']) : 0;
  90. if ($status > 0) {
  91. $query->where(['a.status' => $status]);
  92. }
  93. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  94. if ($userId > 0) {
  95. $query->where('a.user_id', $userId);
  96. }
  97. $businessId = isset($params['business_id']) ? $params['business_id'] : 0;
  98. if ($businessId > 0) {
  99. $query->where('a.business_id', $businessId);
  100. }
  101. })
  102. ->select(['a.*', 'b.username', 'c.username as c_username'])
  103. ->orderBy('a.create_time', 'desc')
  104. ->orderBy('a.id', 'desc')
  105. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  106. $list = $list ? $list->toArray() : [];
  107. if ($list) {
  108. $payTypes = [1 => '银行卡', 2 => '微信', 3 => '支付宝', 4 => '其他'];
  109. $overTime = ConfigService::make()->getConfigByCode('trade_order_overtime');
  110. $overTime = $overTime ? $overTime : 0;
  111. foreach ($list['data'] as &$item) {
  112. $item['idcardData'] = $item['idcard_data'] ? json_decode($item['idcard_data'], true) : [];
  113. $item['paymentData'] = $item['payment_data'] ? json_decode($item['payment_data'], true) : [];
  114. $item['create_time_text'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
  115. $item['update_time_text'] = $item['update_time'] ? datetime($item['update_time'], 'Y-m-d H:i:s') : '';
  116. $item['time_text'] = $item['create_time'] ? datetime($item['create_time'], 'H:i') : '';
  117. $item['utime_text'] = $item['update_time'] ? datetime($item['update_time'], 'H:i') : '';
  118. $item['pay_time_text'] = $item['pay_time'] ? datetime($item['pay_time'], 'Y-m-d H:i') : '';
  119. $item['username_text'] = $item['username'] ? format_account($item['username']) : '';
  120. $item['c_username_text'] = $item['c_username'] ? format_account($item['c_username']) : '';
  121. $item['exception_img'] = $item['exception_img'] ? get_image_url($item['exception_img']) : '';
  122. $item['pay_img'] = $item['pay_img'] ? get_image_url($item['pay_img']) : '';
  123. $item['paymentData']['qrcode'] = isset($item['paymentData']['qrcode']) && $item['paymentData']['qrcode'] ? get_image_url($item['paymentData']['qrcode']) : '';
  124. $overTime = max(0, intval($item['create_time']) + $overTime * 60 - time());
  125. $item['overtime_text'] = in_array($item['status'], [1, 2]) && $overTime ? date('H:i', $overTime) : '';
  126. $payType = isset($item['pay_type']) ? $item['pay_type'] : 0;
  127. $item['pay_name'] = isset($payTypes[$payType]) ? $payTypes[$payType] : '其他';
  128. $item['chat_key'] = getChatKey($item['user_id'],$item['business_id']);
  129. }
  130. }
  131. return [
  132. 'pageSize' => $pageSize,
  133. 'total' => isset($list['total']) ? $list['total'] : 0,
  134. 'list' => isset($list['data']) ? $list['data'] : []
  135. ];
  136. }
  137. /**
  138. * 获取待处理订单
  139. * @param $params
  140. * @return array
  141. */
  142. public function getWaitOrder($params)
  143. {
  144. $info = $this->model->from('trade_order as a')
  145. ->leftJoin('member as b', 'b.id', '=', 'a.business_id')
  146. ->where(function ($query) use ($params) {
  147. $query->where(['a.mark' => 1])->whereIn('a.status', [1, 2, 3, 7])->where('a.status', '>', 0);
  148. $orderNo = isset($params['order_no']) && $params['order_no'] ? trim($params['order_no']) : '';
  149. if ($orderNo) {
  150. $query->where('a.order_no', 'like', "%{$orderNo}%");
  151. }
  152. $type = isset($params['type']) ? intval($params['type']) : 0;
  153. if ($type > 0) {
  154. $query->where(['a.type' => $type]);
  155. }
  156. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  157. if ($userId > 0) {
  158. $query->where('a.user_id', $userId);
  159. }
  160. $businessId = isset($params['business_id']) ? $params['business_id'] : 0;
  161. if ($businessId > 0) {
  162. $query->where('a.business_id', $businessId);
  163. }
  164. })
  165. ->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'])
  166. ->orderBy('a.create_time', 'desc')
  167. ->first();
  168. $info = $info ? $info->toArray() : [];
  169. if ($info) {
  170. $overTime = ConfigService::make()->getConfigByCode('trade_order_overtime');
  171. $overTime = $overTime ? $overTime : 0;
  172. $info['paymentData'] = $info['payment_data'] ? json_decode($info['payment_data'], true) : [];
  173. $info['create_time_text'] = $info['create_time'] ? format_time(strtotime($info['create_time'])) : '';
  174. $info['time_text'] = $info['create_time'] ? datetime($info['create_time'], 'H:i') : '';
  175. $info['username_text'] = $info['username'] ? format_account($info['username']) : '';
  176. $info['paymentData']['qrcode'] = isset($info['paymentData']['qrcode']) && $info['paymentData']['qrcode'] ? get_image_url($info['paymentData']['qrcode']) : '';
  177. $info['paymentData']['bank_card_text'] = isset($info['paymentData']['bank_card']) && $info['paymentData']['bank_card'] ? format_bank_card($info['paymentData']['bank_card']) : '未设置';
  178. $info['paymentData']['account_text'] = isset($info['paymentData']['account']) && $info['paymentData']['account'] ? format_account($info['paymentData']['account']) : '未设置';
  179. $overTime = max(0, intval($info['create_time']) + $overTime * 60 - time());
  180. $item['overtime_text'] = in_array($info['status'], [1, 2]) && $overTime ? date('H:i', $overTime) : '';
  181. }
  182. return $info;
  183. }
  184. /**
  185. * 成交交易笔数
  186. * @param int $type 类型:1-买单,2-卖单
  187. * @param int $bsid 承兑商用户ID
  188. * @param int $status 状态:4-成交
  189. * @return mixed
  190. */
  191. public function getCompleteCount($type = 1, $bsid = 0, $status = 4)
  192. {
  193. return (int)$this->model->where(['type' => $type, 'status' => $status, 'mark' => 1])
  194. ->where(function ($query) use ($bsid) {
  195. if ($bsid > 0) {
  196. $query->where('business_id', '=', $bsid);
  197. }
  198. })->count('id');
  199. }
  200. /**
  201. * 成交交易金额
  202. * @param int $type 类型:1-买单,2-卖单
  203. * @param int $bsid 承兑商用户ID
  204. * @param int $status 状态:4-成交
  205. * @param string $field 统计字段:total-总金额(默认),num-交易USDT数量
  206. * @return mixed
  207. */
  208. public function getCompleteTotal($type = 1, $bsid = 0, $status = 4, $field = 'total')
  209. {
  210. $total = $this->model->where(['type' => $type, 'status' => $status, 'mark' => 1])
  211. ->where(function ($query) use ($bsid) {
  212. if ($bsid > 0) {
  213. $query->where('business_id', '=', $bsid);
  214. }
  215. })->sum($field);
  216. return moneyFormat($total, 2);
  217. }
  218. /**
  219. * 今日成交交易金额
  220. * @param int $type 类型:1-买单,2-卖单
  221. * @param int $bsid 承兑商用户ID
  222. * @param int $status 状态:4-成交
  223. * @param string $field 统计字段:total-总金额(默认),num-交易USDT数量
  224. * @return mixed
  225. */
  226. public function getCompleteTotalByDay($type = 1, $bsid = 0, $status = 4, $field = 'total')
  227. {
  228. $total = $this->model->where(['type' => $type, 'status' => $status, 'mark' => 1])
  229. ->where('pay_time', '>=', strtotime(date('Y-m-d')))
  230. ->where(function ($query) use ($bsid) {
  231. if ($bsid > 0) {
  232. $query->where('business_id', '=', $bsid);
  233. }
  234. })->sum($field);
  235. return moneyFormat($total, 6);
  236. }
  237. /**
  238. * 24小时成交交易金额
  239. * @param int $time 时间/小时
  240. * @param int $bsid 承兑商用户ID
  241. * @param int $status 状态:4-成交
  242. * @param string $field 统计字段:total-总金额(默认),num-交易USDT数量
  243. * @return mixed
  244. */
  245. public function getCompleteTotalByTime($time = 24, $bsid = 0, $status = 4, $field = 'total')
  246. {
  247. $total = $this->model->where(['status' => $status, 'mark' => 1])
  248. ->where('pay_time', '>=', time() - $time * 3600)
  249. ->where(function ($query) use ($bsid) {
  250. if ($bsid > 0) {
  251. $query->where('business_id', '=', $bsid);
  252. }
  253. })->sum($field);
  254. return moneyFormat($total, 4);
  255. }
  256. /**
  257. * 24小时成交交易金额
  258. * @param int $time 时间/小时
  259. * @param int $bsid 承兑商用户ID
  260. * @param int $status 状态:4-成交
  261. * @param string $field 统计字段:total-总金额(默认),num-交易USDT数量
  262. * @return mixed
  263. */
  264. public function getCompleteCountByTime($time = 24, $bsid = 0, $status = 4, $field = 'id')
  265. {
  266. $total = $this->model->where(['status' => $status, 'mark' => 1])
  267. ->where('pay_time', '>=', time() - $time * 3600)
  268. ->where(function ($query) use ($bsid) {
  269. if ($bsid > 0) {
  270. $query->where('business_id', '=', $bsid);
  271. }
  272. })->count($field);
  273. return intval($total);
  274. }
  275. /**
  276. * 获取某个时间段的买卖交易量比
  277. * @param int $type 状态类型:1-成交的
  278. * @param int $bsid 承兑商用户ID
  279. * @param int $time 时间/分钟,默认15分钟
  280. * @return array
  281. */
  282. public function getCountRateByTime($type = 1, $bsid = 0, $time = 15)
  283. {
  284. $where = ['mark' => 1];
  285. if ($type == 1) {
  286. $where['status'] = 4;
  287. }
  288. $buyCount = $this->model->where(['type' => 1])->where($where)
  289. ->where(function ($query) use ($bsid) {
  290. if ($bsid > 0) {
  291. $query->where('business_id', '=', $bsid);
  292. }
  293. })
  294. ->where('pay_time', '>=', time() - $time * 60)
  295. ->count('id');
  296. $sellCount = $this->model->where(['type' => 2])->where($where)
  297. ->where(function ($query) use ($bsid) {
  298. if ($bsid > 0) {
  299. $query->where('business_id', '=', $bsid);
  300. }
  301. })
  302. ->where('pay_time', '>=', time() - $time * 60)
  303. ->count('id');
  304. $rate = ($buyCount || $sellCount) > 0 ? moneyFormat($buyCount / ($buyCount + $sellCount)) * 100 : 0;
  305. return ['buy' => $buyCount, 'sell' => $sellCount, 'buy_rate' => $rate, 'sell_rate' => ($sellCount > 0 ? 100 - $rate : 0)];
  306. }
  307. /**
  308. * 获取某个时间段的交易成功率
  309. * @param int $type 状态类型:1-成交的
  310. * @param int $bsid 承兑商用户ID
  311. * @param int $time 时间/小时,默认24小时
  312. * @return array
  313. */
  314. public function getRateByTime($bsid = 0, $time = 24)
  315. {
  316. $successCount = $this->model->where(['status' => 4,'mark'=>1])
  317. ->where(function ($query) use ($bsid) {
  318. if ($bsid > 0) {
  319. $query->where('business_id', '=', $bsid);
  320. }
  321. })
  322. ->where('pay_time', '>=', time() - $time * 3600)
  323. ->count('id');
  324. $count = $this->model->where(['mark' => 1])
  325. ->where(function ($query) use ($bsid) {
  326. if ($bsid > 0) {
  327. $query->where('business_id', '=', $bsid);
  328. }
  329. })
  330. ->where(function($query) use($time){
  331. $time = time() - $time * 3600;
  332. $query->where('pay_time', '>=', $time)
  333. ->orWhereRaw("(pay_time = 0 and create_time >={$time})");
  334. })
  335. ->count('id');
  336. $rate = $count > 0 ? moneyFormat($successCount / $count * 100) : 0;
  337. return ['rate' => $rate, 'success' => $successCount, 'count' => $count];
  338. }
  339. /**
  340. * 获取未支付或处理的订单数
  341. * @param $userId
  342. * @param int $type
  343. * @return mixed
  344. */
  345. public function checkOrderNoCatch($userId, $type = 1)
  346. {
  347. return $this->model->where(['user_id' => $userId, 'type' => $type, 'mark' => 1])->whereIn('status', [1, 2, 5, 7])->count('id');
  348. }
  349. /**
  350. * 客户买入
  351. * @param $userId
  352. * @param $params
  353. * @return false|int|number
  354. */
  355. public function buy($userId, $params)
  356. {
  357. $num = isset($params['num']) ? floatval($params['num']) : 0;
  358. $numType = isset($params['num_type']) ? intval($params['num_type']) : 1;
  359. if ($userId <= 0) {
  360. $this->error = '1013';
  361. return false;
  362. }
  363. // 验证参数
  364. $config = ConfigService::make()->getConfigOptionByGroup(5);
  365. $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 0;
  366. $tradeMinNum = isset($config['trade_min_num']) ? $config['trade_min_num'] : 0;
  367. $tradeMaxNum = isset($config['trade_max_num']) ? $config['trade_max_num'] : 0;
  368. $trademinMoney = isset($config['trade_min_money']) ? $config['trade_min_money'] : 0;
  369. $tradeMaxMoney = isset($config['trade_max_money']) ? $config['trade_max_money'] : 0;
  370. $tradePrice = isset($config['usdt_buy_price']) ? $config['usdt_buy_price'] : 0;
  371. $tradeLimitNum = isset($config['trade_no_catch']) ? $config['trade_no_catch'] : 0;
  372. // 是否开启交易
  373. if ($tradeOpen != 1) {
  374. $this->error = '1013';
  375. return false;
  376. }
  377. if ($tradePrice <= 0) {
  378. $this->error = '3002';
  379. return false;
  380. }
  381. // 验证数量或金额
  382. $total = 0;
  383. if ($numType == 1) {
  384. if ($num < $tradeMinNum || $num > $tradeMaxNum) {
  385. $this->error = '3003';
  386. return false;
  387. }
  388. $total = moneyFormat($num * $tradePrice, 6);
  389. } else {
  390. if ($num < $trademinMoney || $num > $tradeMaxMoney) {
  391. $this->error = '3003';
  392. return false;
  393. }
  394. $total = moneyFormat($num, 6);
  395. $num = moneyFormat($num / $tradePrice, 6);
  396. }
  397. // 用户信息
  398. $userInfo = MemberService::make()->getInfo($userId);
  399. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  400. $idcardCheck = isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0;
  401. $username = isset($userInfo['username']) && $userInfo['username'] ? format_account($userInfo['username']) : '';
  402. if ($status != 1) {
  403. $this->error = '2009';
  404. return false;
  405. }
  406. if ($idcardCheck != 1) {
  407. $this->error = '2014';
  408. return false;
  409. }
  410. // 未处理订单
  411. $noCatchOrder = $this->checkOrderNoCatch($userId, 1);
  412. if ($tradeLimitNum > 0 && $noCatchOrder >= $tradeLimitNum) {
  413. $this->error = lang(3005, ['num' => $tradeLimitNum]);
  414. return false;
  415. }
  416. // 匹配交易商家
  417. $businessInfo = \App\Services\Api\MemberService::make()->getTradeMember($num, 1, $userId);
  418. if (empty($businessInfo)) {
  419. $this->error = '3004';
  420. return false;
  421. }
  422. if($businessInfo['usdt_num']<$num){
  423. $this->error = '3004';
  424. return false;
  425. }
  426. // 客户身份信息
  427. $idcardData = [
  428. 'idcard' => isset($userInfo['idcard']) ? $userInfo['idcard'] : '',
  429. 'idcard_check' => isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0,
  430. 'idcard_front_img' => isset($userInfo['idcard_front_img']) ? $userInfo['idcard_front_img'] : '',
  431. 'idcard_back_img' => isset($userInfo['idcard_back_img']) ? $userInfo['idcard_back_img'] : '',
  432. 'idcard_hand_img' => isset($userInfo['idcard_hand_img']) ? $userInfo['idcard_hand_img'] : '',
  433. ];
  434. // 币商收款方式
  435. $payment = MemberPaymentService::make()->getPayment($businessInfo['id']);
  436. if (empty($payment)) {
  437. $this->error = '3015';
  438. return false;
  439. }
  440. $this->model->startTrans();
  441. $orderNo = get_order_num('OT');
  442. $data = [
  443. 'user_id' => $userId,
  444. 'business_id' => isset($businessInfo['id']) ? $businessInfo['id'] : 0,
  445. 'order_no' => $orderNo,
  446. 'type' => 1,
  447. 'pay_type' => isset($params['pay_type']) ? floatval($params['pay_type']) : 1,
  448. 'price' => $tradePrice,
  449. 'num' => $num,
  450. 'total' => $total,
  451. 'payment_id' => isset($payment['id']) ? intval($payment['id']) : 0,
  452. 'idcard_data' => $idcardData ? json_encode($idcardData, 256) : '',
  453. 'payment_data' => $payment ? json_encode($payment, 256) : '',
  454. 'create_time' => time(),
  455. 'update_time' => time(),
  456. 'status' => 1,
  457. 'mark' => 1,
  458. ];
  459. if (!$order = $this->model->edit($data)) {
  460. $this->model->rollBack();
  461. $this->error = '3023';
  462. return false;
  463. }
  464. if(!$this->memberModel->where(['id'=> $businessInfo['id']])->decrement('usdt_num', $num)){
  465. $this->model->rollBack();
  466. $this->error = '3020';
  467. return false;
  468. }
  469. $data = [
  470. 'order_no'=> $orderNo,
  471. 'user_id'=> $businessInfo['id'],
  472. 'type'=> 2,
  473. 'pay_type'=> 1,
  474. 'change_type'=> 2,
  475. 'num'=> $num,
  476. 'total'=> $total,
  477. 'balance'=> floatval($businessInfo['usdt_num']-$num),
  478. 'create_time'=> time(),
  479. 'update_time'=> time(),
  480. 'status'=> 1,
  481. 'mark'=>1,
  482. 'remark'=> '交易员卖出',
  483. ];
  484. if(!$this->capitalModel->edit($data)){
  485. $this->model->rollBack();
  486. $this->error = '3014';
  487. return false;
  488. }
  489. // 订单通知
  490. $data = [
  491. 'from_uid' => $userId,
  492. 'to_uid' => $businessInfo['id'],
  493. 'type' => 2,
  494. 'order_no' => $orderNo,
  495. 'chat_key' => getChatKey($userId, $businessInfo['id']),
  496. 'message' => "您有来自客户买单订单:{$orderNo}的消息,请尽快回复!",
  497. 'message_type' => 1,
  498. 'create_time' => time(),
  499. 'update_time' => time(),
  500. 'status' => 1,
  501. 'mark' => 1,
  502. ];
  503. if (!ChatMessageService::make()->pushMessage($data)) {
  504. $this->model->rollBack();
  505. $this->error = '3031';
  506. return false;
  507. }
  508. $this->model->commit();
  509. return $order;
  510. }
  511. /**
  512. * 客户卖出
  513. * @param $userId
  514. * @param $params
  515. * @return false|int|number
  516. */
  517. public function sell($userId, $params)
  518. {
  519. $num = isset($params['num']) ? floatval($params['num']) : 0;
  520. if ($userId <= 0) {
  521. $this->error = '1013';
  522. return false;
  523. }
  524. // 验证参数
  525. $config = ConfigService::make()->getConfigOptionByGroup(5);
  526. $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 0;
  527. $tradeMinNum = isset($config['trade_min']) ? $config['trade_min'] : 0;
  528. $tradeMaxNum = isset($config['trade_max']) ? $config['trade_max'] : 0;
  529. $tradePrice = isset($config['usdt_sell_price']) ? $config['usdt_sell_price'] : 0;
  530. $tradeLimitNum = isset($config['trade_no_catch']) ? $config['trade_no_catch'] : 0;
  531. // 是否开启交易
  532. if ($tradeOpen != 1) {
  533. $this->error = '1013';
  534. return false;
  535. }
  536. if ($tradePrice <= 0) {
  537. $this->error = '3002';
  538. return false;
  539. }
  540. // 验证数量或金额
  541. $total = moneyFormat($num * $tradePrice, 6);
  542. if ($num < $tradeMinNum || ($tradeMaxNum && $num > $tradeMaxNum)) {
  543. $this->error = '3003';
  544. return false;
  545. }
  546. // 用户信息
  547. $userInfo = MemberService::make()->getInfo($userId);
  548. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  549. $usdtNum = isset($userInfo['usdt_num']) ? $userInfo['usdt_num'] : 0;
  550. $username = isset($userInfo['username']) && $userInfo['username'] ? format_account($userInfo['username']) : '';
  551. if ($status != 1) {
  552. $this->error = '2009';
  553. return false;
  554. }
  555. if ($usdtNum < $num) {
  556. $this->error = '3011';
  557. return false;
  558. }
  559. // 未处理订单
  560. $noCatchOrder = $this->checkOrderNoCatch($userId, 2);
  561. if ($tradeLimitNum > 0 && $noCatchOrder >= $tradeLimitNum) {
  562. $this->error = lang(3005, ['num' => $tradeLimitNum]);
  563. return false;
  564. }
  565. // 匹配交易商家
  566. $businessInfo = \App\Services\Api\MemberService::make()->getTradeMember($num, 2, $userId);
  567. if (empty($businessInfo)) {
  568. $this->error = '3004';
  569. return false;
  570. }
  571. $idcardData = [
  572. 'idcard' => isset($userInfo['idcard']) ? $userInfo['idcard'] : '',
  573. 'idcard_check' => isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0,
  574. 'idcard_front_img' => isset($userInfo['idcard_front_img']) ? $userInfo['idcard_front_img'] : '',
  575. 'idcard_back_img' => isset($userInfo['idcard_back_img']) ? $userInfo['idcard_back_img'] : '',
  576. 'idcard_hand_img' => isset($userInfo['idcard_hand_img']) ? $userInfo['idcard_hand_img'] : '',
  577. ];
  578. // 收款方式
  579. $paymentId = isset($params['payment_id']) ? $params['payment_id'] : 0;
  580. $paymentInfo = MemberPaymentService::make()->getInfo($paymentId);
  581. if (empty($paymentInfo)) {
  582. $this->error = '3010';
  583. return false;
  584. }
  585. $paymentData = [
  586. 'payment_id' => $paymentInfo['id'],
  587. 'type' => $paymentInfo['type'],
  588. 'logo' => $paymentInfo['logo'] ? get_image_url($paymentInfo['logo']) : '',
  589. 'real_name' => $paymentInfo['real_name'],
  590. 'bank_name' => $paymentInfo['bank_name'],
  591. 'bank_card' => $paymentInfo['bank_card'],
  592. 'branch_name' => $paymentInfo['branch_name'],
  593. 'qrcode' => $paymentInfo['qrcode'] ? get_image_url($paymentInfo['qrcode']) : '',
  594. 'account' => $paymentInfo['account'],
  595. ];
  596. $orderNo = get_order_num('OT');
  597. $data = [
  598. 'user_id' => $userId,
  599. 'business_id' => isset($businessInfo['id']) ? $businessInfo['id'] : 0,
  600. 'order_no' => $orderNo,
  601. 'type' => 2,
  602. 'pay_type' => isset($params['pay_type']) ? floatval($params['pay_type']) : 1,
  603. 'price' => $tradePrice,
  604. 'num' => $num,
  605. 'payment_id' => $paymentId,
  606. 'idcard_data' => json_encode($idcardData, 256),
  607. 'payment_data' => json_encode($paymentData, 256),
  608. 'total' => $total,
  609. 'create_time' => time(),
  610. 'status' => 1,
  611. 'mark' => 1,
  612. ];
  613. $this->model->startTrans();
  614. if (!$order = $this->model->edit($data)) {
  615. $this->error = '3012';
  616. $this->model->rollBack();
  617. return false;
  618. }
  619. // 扣除币
  620. if (!$this->memberModel->where(['id' => $userId])->decrement('usdt_num', $num)) {
  621. $this->error = '3013';
  622. $this->model->rollBack();
  623. return false;
  624. }
  625. // 账户明细
  626. $data = [
  627. 'order_no' => $data['order_no'],
  628. 'user_id' => $userId,
  629. 'type' => 2,
  630. 'change_type' => 2,
  631. 'num' => $num,
  632. 'total' => $total,
  633. 'balance' => floatval($userInfo['usdt_num'] - $num),
  634. 'create_time' => time(),
  635. 'update_time' => time(),
  636. 'remark' => '客户卖出',
  637. 'status' => 1,
  638. 'mark' => 1,
  639. ];
  640. if (!$this->capitalModel->edit($data)) {
  641. $this->error = '3014';
  642. $this->model->rollBack();
  643. return false;
  644. }
  645. // 订单通知
  646. $data = [
  647. 'from_uid' => $userId,
  648. 'to_uid' => $businessInfo['id'],
  649. 'type' => 2,
  650. 'order_no' => $orderNo,
  651. 'chat_key' => getChatKey($userId, $businessInfo['id']),
  652. 'message' => "您有来自客户卖单订单:{$orderNo}的消息,请尽快回复!",
  653. 'message_type' => 1,
  654. 'create_time' => time(),
  655. 'update_time' => time(),
  656. 'status' => 1,
  657. 'mark' => 1,
  658. ];
  659. if (!ChatMessageService::make()->pushMessage($data)) {
  660. $this->model->rollBack();
  661. $this->error = '3023';
  662. return false;
  663. }
  664. $this->model->commit();
  665. return $order;
  666. }
  667. /**
  668. * 订单打款处理
  669. * @param $userId 用户ID
  670. * @param $params 打款参数
  671. * @return false
  672. */
  673. public function pay($userId, $params)
  674. {
  675. $orderId = isset($params['id']) ? $params['id'] : 0;
  676. if ($orderId <= 0) {
  677. $this->error = '1013';
  678. return false;
  679. }
  680. $orderInfo = $this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1, 'type' => 1])
  681. ->whereIn('status', [1, 2, 5, 7])
  682. ->select(['id', 'order_no', 'business_id', 'type', 'payment_id', 'num', 'total', 'status'])
  683. ->first();
  684. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  685. if (empty($orderInfo)) {
  686. $this->error = '3016';
  687. return false;
  688. }
  689. if ($tradeType != 1) {
  690. $this->error = '3024';
  691. return false;
  692. }
  693. // 用户信息
  694. $userInfo = MemberService::make()->getInfo($userId);
  695. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  696. if ($status != 1) {
  697. $this->error = '2009';
  698. return false;
  699. }
  700. // 交易密码
  701. $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
  702. $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
  703. if (empty($password)) {
  704. $this->error = '2015';
  705. return false;
  706. }
  707. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  708. $this->error = '2016';
  709. return false;
  710. }
  711. $data = [
  712. 'status' => 3,
  713. 'pay_type' => $params['pay_type'],
  714. 'pay_img' => $params['pay_img'],
  715. 'pay_remark' => $params['pay_remark'],
  716. 'pay_time' => time(),
  717. ];
  718. if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update($data)) {
  719. $this->error = '3018';
  720. return false;
  721. }
  722. return true;
  723. }
  724. /**
  725. * 商家订单打款处理
  726. * @param $userId 用户ID
  727. * @param $params 打款参数
  728. * @return false
  729. */
  730. public function businessPay($businessId, $params)
  731. {
  732. $orderId = isset($params['id']) ? $params['id'] : 0;
  733. if ($orderId <= 0) {
  734. $this->error = '1013';
  735. return false;
  736. }
  737. $orderInfo = $this->model->where(['business_id' => $businessId, 'id' => $orderId, 'mark' => 1, 'type' => 2])
  738. ->whereIn('status', [1, 2, 5, 7])
  739. ->select(['id', 'user_id', 'order_no', 'business_id', 'type', 'payment_id', 'num', 'total', 'status'])
  740. ->first();
  741. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  742. if (empty($orderInfo)) {
  743. $this->error = '3016';
  744. return false;
  745. }
  746. if ($tradeType != 2) {
  747. $this->error = '3025';
  748. return false;
  749. }
  750. // 用户信息
  751. $userInfo = MemberService::make()->getInfo($businessId);
  752. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  753. $username = isset($userInfo['username']) ? $userInfo['username'] : 0;
  754. if ($status != 1) {
  755. $this->error = '2009';
  756. return false;
  757. }
  758. // 交易密码
  759. $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
  760. $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
  761. if (empty($password)) {
  762. $this->error = '2015';
  763. return false;
  764. }
  765. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  766. $this->error = '2016';
  767. return false;
  768. }
  769. if (strpos($params['pay_img'], "temp")) {
  770. $params['pay_img'] = save_image($params['pay_img'], 'images');
  771. } else {
  772. $params['pay_img'] = str_replace(IMG_URL, "", $params['pay_img']);
  773. }
  774. $data = [
  775. 'status' => 3,
  776. 'pay_type' => $params['pay_type'],
  777. 'pay_img' => $params['pay_img'],
  778. 'pay_remark' => $params['pay_remark'],
  779. 'pay_time' => time(),
  780. ];
  781. if (!$this->model->where(['business_id' => $businessId, 'id' => $orderId, 'mark' => 1])->update($data)) {
  782. $this->error = '3018';
  783. return false;
  784. }
  785. $this->error = '3017';
  786. return true;
  787. }
  788. /**
  789. * 订单确认处理
  790. * @param $userId 用户ID
  791. * @param $params 打款参数
  792. * @return false
  793. */
  794. public function collection($userId, $params)
  795. {
  796. $orderId = isset($params['id']) ? $params['id'] : 0;
  797. if ($orderId <= 0) {
  798. $this->error = '1013';
  799. return false;
  800. }
  801. $orderInfo = $this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1, 'type' => 2])
  802. ->select(['id', 'user_id', 'order_no', 'business_id', 'payment_id', 'type', 'num', 'total', 'status'])
  803. ->first();
  804. $businessId = isset($orderInfo['business_id']) ? $orderInfo['business_id'] : 0;
  805. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  806. if (empty($orderInfo) || empty($businessId)) {
  807. $this->error = '3016';
  808. return false;
  809. }
  810. if ($orderInfo['status'] != 3) {
  811. $this->error = '3026';
  812. return false;
  813. }
  814. if ($tradeType != 2) {
  815. $this->error = '3024';
  816. return false;
  817. }
  818. $this->model->startTrans();
  819. // 订单状态更新
  820. if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update(['status' => 4, 'update_time' => time()])) {
  821. $this->model->rollBack();
  822. $this->error = '3023';
  823. return false;
  824. }
  825. // 交易处理
  826. if ($orderInfo['num'] > 0) {
  827. $info = $this->memberModel->where(['id' => $businessId, 'status' => 1, 'mark' => 1])->select(['id', 'username','usdt_num', 'user_type'])->first();
  828. if (empty($info)) {
  829. $this->model->rollBack();
  830. $this->error = '3019';
  831. return false;
  832. }
  833. // 商家进币
  834. if (!$this->memberModel->where(['id' => $businessId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  835. $this->model->rollBack();
  836. $this->error = '3019';
  837. return false;
  838. }
  839. // 明细处理
  840. $data = [
  841. 'order_no' => $orderInfo['order_no'],
  842. 'user_id' => $businessId,
  843. 'type' => 1,
  844. 'pay_type' => 1,
  845. 'change_type' => 1,
  846. 'num' => $orderInfo['num'],
  847. 'total' => $orderInfo['total'],
  848. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  849. 'create_time' => time(),
  850. 'remark' => '交易员买入',
  851. 'status' => 1,
  852. 'mark' => 1,
  853. ];
  854. if (!$this->capitalModel->edit($data)) {
  855. $this->error = '3014';
  856. $this->model->rollBack();
  857. return false;
  858. }
  859. }
  860. $this->model->commit();
  861. return true;
  862. }
  863. /**
  864. * 商家订单确认处理
  865. * @param $businessId 币商用户ID,平台处理时为0
  866. * @param $params 打款参数
  867. * @return false
  868. */
  869. public function businessCollection($businessId, $params)
  870. {
  871. $orderId = isset($params['id']) ? $params['id'] : 0;
  872. $catchUid = isset($params['catch_uid']) ? $params['catch_uid'] : 0;
  873. $type = isset($params['type']) ? $params['type'] : 0;
  874. if ($orderId <= 0) {
  875. $this->error = '1013';
  876. return false;
  877. }
  878. // 平台或币商处理
  879. $where = ['id' => $orderId, 'mark' => 1];
  880. if ($businessId) {
  881. $where['business_id'] = $businessId;
  882. }
  883. if($type){
  884. $where['type'] = $type;
  885. }
  886. $orderInfo = $this->model->where($where)
  887. ->select(['id', 'user_id', 'order_no', 'business_id', 'type', 'num', 'total', 'status'])
  888. ->first();
  889. $userId = isset($orderInfo['user_id']) ? $orderInfo['user_id'] : 0;
  890. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  891. if (empty($orderInfo) || empty($userId)) {
  892. $this->error = '3016';
  893. return false;
  894. }
  895. $status = isset($params['status']) ? intval($params['status']) : 4;
  896. if ($status == 4 && $orderInfo['status'] != 3 && $businessId > 0) {
  897. $this->error = '3026';
  898. return false;
  899. }
  900. if($status == 5 && $orderInfo['status'] == 8 && $businessId>0){
  901. $this->error = '3026';
  902. return false;
  903. }
  904. if($status == 5 && $businessId<=0 && $orderInfo['status'] == 4){
  905. $this->error = '3026';
  906. return false;
  907. }
  908. if ($tradeType != 1 && $status != 5) {
  909. $this->error = '3024';
  910. return false;
  911. }
  912. // 用户信息
  913. if($businessId>0){
  914. $userInfo = MemberService::make()->getInfo($businessId);
  915. $userStatus = isset($userInfo['status']) ? $userInfo['status'] : 0;
  916. $username = isset($userInfo['username']) ? $userInfo['username'] : 0;
  917. if ($userStatus != 1) {
  918. $this->error = '2009';
  919. return false;
  920. }
  921. // 交易密码
  922. $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
  923. $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
  924. if (empty($password)) {
  925. $this->error = '2015';
  926. return false;
  927. }
  928. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  929. $this->error = '2016';
  930. return false;
  931. }
  932. }
  933. $this->model->startTrans();
  934. // 订单状态更新,异常或确认
  935. $updateData = ['status' => $status, 'exception_catch_user' => $catchUid, 'update_time' => time()];
  936. // 异常处理数据
  937. if (isset($params['exception_type'])) {
  938. $updateData['exception_type'] = intval($params['exception_type']);
  939. }
  940. if (isset($params['exception_sub_type'])) {
  941. $updateData['exception_sub_type'] = intval($params['exception_sub_type']);
  942. }
  943. if (isset($params['exception_img'])) {
  944. $updateData['exception_img'] = trim($params['exception_img']);
  945. if (strpos($updateData['exception_img'], "temp")) {
  946. $updateData['exception_img'] = save_image($updateData['exception_img'], 'images');
  947. } else {
  948. $updateData['exception_img'] = str_replace(IMG_URL, "", $updateData['exception_img']);
  949. }
  950. }
  951. if (isset($params['exception_remark'])) {
  952. $updateData['exception_remark'] = trim($params['exception_remark']);
  953. }
  954. if (isset($params['exception_status'])) {
  955. $updateData['exception_status'] = intval($params['exception_status']);
  956. }
  957. if (!$this->model->where($where)->update($updateData)) {
  958. $this->model->rollBack();
  959. $this->error = '3023';
  960. return false;
  961. }
  962. // 交易处理,如果是确认状态
  963. if ($orderInfo['num'] > 0 && $status == 4) {
  964. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  965. if (empty($info)) {
  966. $this->model->rollBack();
  967. $this->error = '3019';
  968. return false;
  969. }
  970. // 客户币进账
  971. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  972. $this->model->rollBack();
  973. $this->error = '3019';
  974. return false;
  975. }
  976. // 明细处理
  977. $data = [
  978. 'order_no' => $orderInfo['order_no'],
  979. 'user_id' => $userId,
  980. 'type' => 1,
  981. 'pay_type' => 1,
  982. 'change_type' => 1,
  983. 'num' => $orderInfo['num'],
  984. 'total' => $orderInfo['total'],
  985. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  986. 'create_time' => time(),
  987. 'update_time' => time(),
  988. 'remark' => '客户买入',
  989. 'status' => 1,
  990. 'mark' => 1,
  991. ];
  992. if (!$this->capitalModel->edit($data)) {
  993. $this->error = '3014';
  994. $this->model->rollBack();
  995. return false;
  996. }
  997. // 更新收款地址数据
  998. $paymentId = isset($orderInfo['payment_id']) ? $orderInfo['payment_id'] : 0;
  999. $info = $this->paymentModel->where(['id' => $paymentId, 'mark' => 1])->select(['id', 'used_at'])->first();
  1000. if ($paymentId && $info) {
  1001. $usedAt = isset($info['used_at']) ? $info['used_at'] : '';
  1002. if ($usedAt < date('Y-m-d')) {
  1003. if (!$this->paymentModel->where(['id' => $paymentId])->update(['used_num' => 1, 'used_quota' => $orderInfo['total'], 'update_time' => time()])) {
  1004. $this->model->rollBack();
  1005. $this->error = '3018';
  1006. return false;
  1007. }
  1008. } else {
  1009. $res1 = $this->paymentModel->where(['id' => $paymentId])->increment('used_num', 1);
  1010. $res2 = $this->paymentModel->where(['id' => $paymentId])->increment('used_quota', floatval($orderInfo['total']));
  1011. if (!$res1 || !$res2) {
  1012. $this->model->rollBack();
  1013. $this->error = '3014';
  1014. return false;
  1015. }
  1016. }
  1017. }
  1018. }
  1019. $this->model->commit();
  1020. $this->error = $status == 5 ? 3032 : 3021;
  1021. return true;
  1022. }
  1023. /**
  1024. * 取消订单
  1025. * @param $userId
  1026. * @param $params
  1027. * @return false
  1028. */
  1029. public function cancel($userId, $params)
  1030. {
  1031. $orderId = isset($params['id']) ? intval($params['id']) : 0;
  1032. if ($orderId <= 0) {
  1033. $this->error = '1013';
  1034. return false;
  1035. }
  1036. $orderInfo = $this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])
  1037. ->select(['id', 'order_no', 'business_id', 'type', 'num', 'total', 'status'])
  1038. ->first();
  1039. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  1040. $businessId = isset($orderInfo['business_id']) ? $orderInfo['business_id'] : 0;
  1041. if (empty($orderInfo) || $businessId<=0) {
  1042. $this->error = '3016';
  1043. return false;
  1044. }
  1045. if ($orderInfo['status'] == 3) {
  1046. $this->error = '3027';
  1047. return false;
  1048. }
  1049. if ($orderInfo['status'] == 4) {
  1050. $this->error = '3028';
  1051. return false;
  1052. }
  1053. if ($orderInfo['status'] == 7) {
  1054. $this->error = '3030';
  1055. return false;
  1056. }
  1057. if (!in_array($orderInfo['status'], [1, 2])) {
  1058. $this->error = '3029';
  1059. return false;
  1060. }
  1061. $this->model->startTrans();
  1062. // 订单状态更新
  1063. $updateData = ['status' => 8, 'update_time' => time(), 'exception_remark' => '客户取消'];
  1064. if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update($updateData)) {
  1065. $this->model->rollBack();
  1066. $this->error = '3023';
  1067. return false;
  1068. }
  1069. // 出售订单,USDT退回
  1070. if ($tradeType == 2 && $orderInfo['num']>0) {
  1071. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1072. if (empty($info)) {
  1073. $this->model->rollBack();
  1074. $this->error = '3019';
  1075. return false;
  1076. }
  1077. // 退还币给客户
  1078. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1079. $this->model->rollBack();
  1080. $this->error = '3019';
  1081. return false;
  1082. }
  1083. // 明细处理
  1084. $data = [
  1085. 'order_no' => $orderInfo['order_no'],
  1086. 'user_id' => $userId,
  1087. 'type' => 3,
  1088. 'pay_type' => 1,
  1089. 'change_type' => 1,
  1090. 'num' => $orderInfo['num'],
  1091. 'total' => $orderInfo['total'],
  1092. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1093. 'create_time' => time(),
  1094. 'update_time' => time(),
  1095. 'remark' => '客户取消退还',
  1096. 'status' => 1,
  1097. 'mark' => 1,
  1098. ];
  1099. if (!$this->capitalModel->edit($data)) {
  1100. $this->error = '3014';
  1101. $this->model->rollBack();
  1102. return false;
  1103. }
  1104. }
  1105. // 买入取消
  1106. else if($tradeType == 1 && $orderInfo['num']>0){
  1107. $info = $this->memberModel->where(['id' => $businessId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1108. if (empty($info)) {
  1109. $this->model->rollBack();
  1110. $this->error = '3019';
  1111. return false;
  1112. }
  1113. // 退还币给客户
  1114. if (!$this->memberModel->where(['id' => $businessId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1115. $this->model->rollBack();
  1116. $this->error = '3019';
  1117. return false;
  1118. }
  1119. // 明细处理
  1120. $data = [
  1121. 'order_no' => $orderInfo['order_no'],
  1122. 'user_id' => $businessId,
  1123. 'type' => 3,
  1124. 'pay_type' => 1,
  1125. 'change_type' => 1,
  1126. 'num' => $orderInfo['num'],
  1127. 'total' => $orderInfo['total'],
  1128. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1129. 'create_time' => time(),
  1130. 'update_time' => time(),
  1131. 'remark' => '客户取消退还',
  1132. 'status' => 1,
  1133. 'mark' => 1,
  1134. ];
  1135. if (!$this->capitalModel->edit($data)) {
  1136. $this->error = '3014';
  1137. $this->model->rollBack();
  1138. return false;
  1139. }
  1140. }
  1141. $this->model->commit();
  1142. return true;
  1143. }
  1144. /**
  1145. * 取消订单
  1146. * @param $businessId 币商用户,平台为0
  1147. * @param $params
  1148. * @return false
  1149. */
  1150. public function businessCancel($businessId, $params)
  1151. {
  1152. $orderId = isset($params['id']) ? intval($params['id']) : 0;
  1153. $catchUid = isset($params['catch_uid']) ? intval($params['catch_uid']) : 0;
  1154. if ($orderId <= 0) {
  1155. $this->error = '1013';
  1156. return false;
  1157. }
  1158. $where = ['id' => $orderId, 'mark' => 1];
  1159. if ($businessId) {
  1160. $where['business_id'] = $businessId;
  1161. }
  1162. $orderInfo = $this->model->where($where)
  1163. ->select(['id', 'user_id', 'order_no', 'business_id', 'type', 'num', 'total', 'status'])
  1164. ->first();
  1165. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  1166. $userId = isset($orderInfo['user_id']) ? $orderInfo['user_id'] : 0;
  1167. $orderBusinessId = isset($orderInfo['business_id']) ? $orderInfo['business_id'] : 0;
  1168. if (empty($orderInfo) || $orderBusinessId<=0) {
  1169. $this->error = '3016';
  1170. return false;
  1171. }
  1172. if (empty($userId)) {
  1173. $this->error = '3019';
  1174. return false;
  1175. }
  1176. if ($orderInfo['status'] == 4) {
  1177. $this->error = '3028';
  1178. return false;
  1179. }
  1180. if (!in_array($orderInfo['status'], [1, 2, 3, 5, 7])) {
  1181. $this->error = '3029';
  1182. return false;
  1183. }
  1184. // 用户信息
  1185. $status = isset($params['status']) ? $params['status'] : 8;
  1186. if($businessId>0 && $status != 8){
  1187. $userInfo = MemberService::make()->getInfo($businessId);
  1188. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  1189. $username = isset($userInfo['username']) ? $userInfo['username'] : 0;
  1190. if ($status != 1) {
  1191. $this->error = '2009';
  1192. return false;
  1193. }
  1194. // 交易密码
  1195. $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
  1196. $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
  1197. if (empty($password)) {
  1198. $this->error = '2015';
  1199. return false;
  1200. }
  1201. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  1202. $this->error = '2016';
  1203. return false;
  1204. }
  1205. }
  1206. $this->model->startTrans();
  1207. // 订单状态更新,或异常处理
  1208. $updateData = ['status' => $status, 'exception_catch_user' => $catchUid, 'update_time' => time(), 'exception_remark' => '交易员取消'];
  1209. // 异常处理数据
  1210. if (isset($params['exception_type'])) {
  1211. $updateData['exception_type'] = intval($params['exception_type']);
  1212. }
  1213. if (isset($params['exception_sub_type'])) {
  1214. $updateData['exception_sub_type'] = intval($params['exception_sub_type']);
  1215. }
  1216. if (isset($params['exception_img'])) {
  1217. $updateData['exception_img'] = trim($params['exception_img']);
  1218. if (strpos($updateData['exception_img'], "temp")) {
  1219. $updateData['exception_img'] = save_image($updateData['exception_img'], 'images');
  1220. } else {
  1221. $updateData['exception_img'] = str_replace(IMG_URL, "", $updateData['exception_img']);
  1222. }
  1223. }
  1224. if (isset($params['exception_remark'])) {
  1225. $updateData['exception_remark'] = trim($params['exception_remark']);
  1226. }
  1227. if (isset($params['exception_status'])) {
  1228. $updateData['exception_status'] = intval($params['exception_status']);
  1229. }
  1230. if (isset($params['refund_status'])) {
  1231. $updateData['refund_status'] = intval($params['refund_status']);
  1232. }
  1233. if (isset($params['refund_money'])) {
  1234. $updateData['refund_money'] = intval($params['refund_money']);
  1235. }
  1236. if (!$this->model->where($where)->update($updateData)) {
  1237. $this->model->rollBack();
  1238. $this->error = '3023';
  1239. return false;
  1240. }
  1241. // 出售订单,状态已取消,USDT退回
  1242. if ($tradeType == 2 && $status == 8 && $orderInfo['num']>0) {
  1243. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1244. if (empty($info)) {
  1245. $this->model->rollBack();
  1246. $this->error = '3019';
  1247. return false;
  1248. }
  1249. // 退还币给客户
  1250. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1251. $this->model->rollBack();
  1252. $this->error = '3019';
  1253. return false;
  1254. }
  1255. // 明细处理
  1256. $data = [
  1257. 'order_no' => $orderInfo['order_no'],
  1258. 'user_id' => $userId,
  1259. 'type' => 3,
  1260. 'pay_type' => 1,
  1261. 'change_type' => 1,
  1262. 'num' => $orderInfo['num'],
  1263. 'total' => $orderInfo['total'],
  1264. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1265. 'create_time' => time(),
  1266. 'update_time' => time(),
  1267. 'remark' => '交易员取消买入退还',
  1268. 'status' => 1,
  1269. 'mark' => 1,
  1270. ];
  1271. if (!$this->capitalModel->edit($data)) {
  1272. $this->error = '3014';
  1273. $this->model->rollBack();
  1274. return false;
  1275. }
  1276. }
  1277. // 客户买入订单取消
  1278. else if($tradeType == 1 && $status == 8 && $orderInfo['num']>0){
  1279. $info = $this->memberModel->where(['id' => $orderBusinessId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1280. if (empty($info)) {
  1281. $this->model->rollBack();
  1282. $this->error = '3019';
  1283. return false;
  1284. }
  1285. // 退还币给客户
  1286. if (!$this->memberModel->where(['id' => $orderBusinessId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1287. $this->model->rollBack();
  1288. $this->error = '3019';
  1289. return false;
  1290. }
  1291. // 明细处理
  1292. $data = [
  1293. 'order_no' => $orderInfo['order_no'],
  1294. 'user_id' => $orderBusinessId,
  1295. 'type' => 3,
  1296. 'pay_type' => 1,
  1297. 'change_type' => 1,
  1298. 'num' => $orderInfo['num'],
  1299. 'total' => $orderInfo['total'],
  1300. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1301. 'create_time' => time(),
  1302. 'update_time' => time(),
  1303. 'remark' => '交易员取消卖出退还',
  1304. 'status' => 1,
  1305. 'mark' => 1,
  1306. ];
  1307. if (!$this->capitalModel->edit($data)) {
  1308. $this->error = '3014';
  1309. $this->model->rollBack();
  1310. return false;
  1311. }
  1312. }
  1313. $this->model->commit();
  1314. $this->error = $status == 5 ? 3032 : 3034;
  1315. return true;
  1316. }
  1317. /**
  1318. * 无效订单处理
  1319. * @return false
  1320. */
  1321. public function catchInvalidOrder()
  1322. {
  1323. $cacheKey = "caches:trades:cancels:";
  1324. if(RedisService::get($cacheKey.'lock')){
  1325. return false;
  1326. }
  1327. RedisService::set($cacheKey.'lock', 1, rand(3, 5));
  1328. $overtime = ConfigService::make()->getConfigByCode('trade_order_overtime');
  1329. $cancelTime = ConfigService::make()->getConfigByCode('trade_order_cancel');
  1330. $catchNum = ConfigService::make()->getConfigByCode('trade_order_catch_num');
  1331. $catchNum = $catchNum > 0 ? $catchNum : 200;
  1332. // 处理超时订单
  1333. if ($overtime > 0) {
  1334. $this->model->where(['mark' => 1])
  1335. ->where('status', '<=', 2)
  1336. ->where('create_time', '<=', time() - $overtime * 60)
  1337. ->update(['status' => 7, 'catch_at' => time()]);
  1338. }
  1339. if ($cancelTime <= 0) {
  1340. $this->error = '1023';
  1341. return false;
  1342. }
  1343. $fail = 0;
  1344. $success = 0;
  1345. $this->model->where(function ($query) use ($cancelTime) {
  1346. // 已更新为超时的订单
  1347. $query->where(['mark' => 1, 'status' => 7])
  1348. ->where('catch_at', '<=', time() - $cancelTime * 60);
  1349. })
  1350. ->orWhere(function ($query) use ($cancelTime, $overtime) {
  1351. $query->where('mark', '=', 1)
  1352. ->where('status', '<=', 2)
  1353. ->where('create_time', '<=', time() - ($cancelTime + $overtime) * 60);
  1354. })
  1355. ->select(['id', 'user_id', 'business_id','order_no', 'type', 'num','total'])
  1356. ->take($catchNum)
  1357. ->get()
  1358. ->each(function ($item, $k) use($cacheKey, &$fail, &$success){
  1359. // 客户卖出订单退还
  1360. $date = date('Y-m-d H:i:s');
  1361. $type = isset($item['type']) ? $item['type'] : 0;
  1362. if ($type == 2) {
  1363. if(!$this->orderReback($item['user_id'], $item)){
  1364. $fail++;
  1365. RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  1366. }else{
  1367. $success++;
  1368. RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  1369. }
  1370. }
  1371. else{
  1372. if(!$this->orderReback($item['business_id'], $item)){
  1373. $fail++;
  1374. RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  1375. }else{
  1376. $success++;
  1377. RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  1378. }
  1379. }
  1380. });
  1381. return ['success'=> $success,'fail'=> $fail];
  1382. }
  1383. /**
  1384. * 订单取消退还处理
  1385. * @param $userId
  1386. * @param $orderInfo
  1387. * @return bool
  1388. */
  1389. protected function orderReback($userId, $orderInfo){
  1390. try {
  1391. if($orderInfo['num']<=0){
  1392. return false;
  1393. }
  1394. $this->model->startTrans();
  1395. $updateData = ['status'=> 8, 'update_time'=> time(),'catch_at'=>time(),'exception_remark'=>'系统取消'];
  1396. if(!$this->model->where(['id'=> $orderInfo['id']])->update($updateData)){
  1397. $this->model->rollBack();
  1398. $this->error = '3043';
  1399. return false;
  1400. }
  1401. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1402. if (empty($info)) {
  1403. $this->model->rollBack();
  1404. $this->error = '3019';
  1405. return false;
  1406. }
  1407. // 退还币给客户
  1408. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1409. $this->model->rollBack();
  1410. $this->error = '3019';
  1411. return false;
  1412. }
  1413. // 明细处理
  1414. $data = [
  1415. 'order_no' => $orderInfo['order_no'],
  1416. 'user_id' => $userId,
  1417. 'type' => 3,
  1418. 'pay_type' => 1,
  1419. 'change_type' => 1,
  1420. 'num' => $orderInfo['num'],
  1421. 'total' => $orderInfo['total'],
  1422. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1423. 'create_time' => time(),
  1424. 'update_time' => time(),
  1425. 'remark' => '系统自动取消退还',
  1426. 'status' => 1,
  1427. 'mark' => 1,
  1428. ];
  1429. if (!$this->capitalModel->edit($data)) {
  1430. $this->error = '3014';
  1431. $this->model->rollBack();
  1432. return false;
  1433. }
  1434. $this->model->commit();
  1435. $this->error = '3044';
  1436. return true;
  1437. } catch (\Exception $exception){
  1438. $this->error = $exception->getMessage();
  1439. return false;
  1440. }
  1441. }
  1442. /**
  1443. * 重新派单
  1444. * @param $id 订单ID
  1445. * @param $businessId 新派单用户
  1446. * @return false
  1447. */
  1448. public function reassign($id, $businessId)
  1449. {
  1450. if (empty($id) || empty($businessId)) {
  1451. $this->error = '1013';
  1452. return false;
  1453. }
  1454. $orderInfo = $this->model->where(['id' => $id])
  1455. ->select(['id', 'user_id', 'order_no', 'business_id', 'type', 'num', 'total', 'status'])
  1456. ->first();
  1457. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  1458. if (empty($orderInfo)) {
  1459. $this->error = '3016';
  1460. return false;
  1461. }
  1462. // 状态
  1463. if (!in_array($orderInfo['status'], [1, 2, 3, 5, 7])) {
  1464. $this->error = '3039';
  1465. return false;
  1466. }
  1467. if ($businessId == $orderInfo['business_id']) {
  1468. $this->error = '3035';
  1469. return false;
  1470. }
  1471. // 派单用户信息
  1472. $businessInfo = $this->memberModel->from('member as a')
  1473. ->leftJoin('member_setting as ms', 'ms.user_id', '=', 'a.id')
  1474. ->where(['a.id' => $businessId, 'a.status' => 1, 'a.mark' => 1])
  1475. ->select(['a.id', 'a.username', 'a.usdt_num', 'ms.buy_online', 'ms.buy_online_time', 'ms.sell_online', 'ms.sell_online_time'])
  1476. ->first();
  1477. if (empty($businessInfo)) {
  1478. $this->error = '3036';
  1479. return false;
  1480. }
  1481. // 余额
  1482. if ($businessInfo['usdt_num'] < $orderInfo['num']) {
  1483. $this->error = '3036';
  1484. return false;
  1485. }
  1486. // 在线状态
  1487. if ($tradeType == 1) {
  1488. if ($businessInfo['buy_online'] != 1 || $businessInfo['buy_online_time'] <= time()) {
  1489. $this->error = '3036';
  1490. return false;
  1491. }
  1492. } else if ($tradeType == 2) {
  1493. if ($businessInfo['sell_online'] != 1 || $businessInfo['sell_online_time'] <= time()) {
  1494. $this->error = '3036';
  1495. return false;
  1496. }
  1497. }
  1498. $payment = MemberPaymentService::make()->getPayment($businessId);
  1499. if (empty($payment)) {
  1500. $this->error = '3040';
  1501. return false;
  1502. }
  1503. $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'=>'平台重新派单'];
  1504. if ($this->model->where(['id' => $id])->update($updateData)){
  1505. $this->error = '3041';
  1506. return true;
  1507. }
  1508. $this->error = '3042';
  1509. return false;
  1510. }
  1511. }