TradeOrderService.php 62 KB

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