TradeOrderService.php 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  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. 'data_type' => 2,
  499. 'create_time' => time(),
  500. 'update_time' => time(),
  501. 'status' => 1,
  502. 'mark' => 1,
  503. ];
  504. if (!ChatMessageService::make()->pushMessage($data)) {
  505. $this->model->rollBack();
  506. $this->error = '3031';
  507. return false;
  508. }
  509. $this->model->commit();
  510. return $order;
  511. }
  512. /**
  513. * 客户卖出
  514. * @param $userId
  515. * @param $params
  516. * @return false|int|number
  517. */
  518. public function sell($userId, $params)
  519. {
  520. $num = isset($params['num']) ? floatval($params['num']) : 0;
  521. if ($userId <= 0) {
  522. $this->error = '1013';
  523. return false;
  524. }
  525. // 验证参数
  526. $config = ConfigService::make()->getConfigOptionByGroup(5);
  527. $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 0;
  528. $tradeMinNum = isset($config['trade_min']) ? $config['trade_min'] : 0;
  529. $tradeMaxNum = isset($config['trade_max']) ? $config['trade_max'] : 0;
  530. $tradePrice = isset($config['usdt_sell_price']) ? $config['usdt_sell_price'] : 0;
  531. $tradeLimitNum = isset($config['trade_no_catch']) ? $config['trade_no_catch'] : 0;
  532. // 是否开启交易
  533. if ($tradeOpen != 1) {
  534. $this->error = '1013';
  535. return false;
  536. }
  537. if ($tradePrice <= 0) {
  538. $this->error = '3002';
  539. return false;
  540. }
  541. // 验证数量或金额
  542. $total = moneyFormat($num * $tradePrice, 6);
  543. if ($num < $tradeMinNum || ($tradeMaxNum && $num > $tradeMaxNum)) {
  544. $this->error = '3003';
  545. return false;
  546. }
  547. // 用户信息
  548. $userInfo = MemberService::make()->getInfo($userId);
  549. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  550. $usdtNum = isset($userInfo['usdt_num']) ? $userInfo['usdt_num'] : 0;
  551. $username = isset($userInfo['username']) && $userInfo['username'] ? format_account($userInfo['username']) : '';
  552. if ($status != 1) {
  553. $this->error = '2009';
  554. return false;
  555. }
  556. if ($usdtNum < $num) {
  557. $this->error = '3011';
  558. return false;
  559. }
  560. // 未处理订单
  561. $noCatchOrder = $this->checkOrderNoCatch($userId, 2);
  562. if ($tradeLimitNum > 0 && $noCatchOrder >= $tradeLimitNum) {
  563. $this->error = lang(3005, ['num' => $tradeLimitNum]);
  564. return false;
  565. }
  566. // 匹配交易商家
  567. $businessInfo = \App\Services\Api\MemberService::make()->getTradeMember($num, 2, $userId);
  568. if (empty($businessInfo)) {
  569. $this->error = '3004';
  570. return false;
  571. }
  572. $idcardData = [
  573. 'idcard' => isset($userInfo['idcard']) ? $userInfo['idcard'] : '',
  574. 'idcard_check' => isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0,
  575. 'idcard_front_img' => isset($userInfo['idcard_front_img']) ? $userInfo['idcard_front_img'] : '',
  576. 'idcard_back_img' => isset($userInfo['idcard_back_img']) ? $userInfo['idcard_back_img'] : '',
  577. 'idcard_hand_img' => isset($userInfo['idcard_hand_img']) ? $userInfo['idcard_hand_img'] : '',
  578. ];
  579. // 收款方式
  580. $paymentId = isset($params['payment_id']) ? $params['payment_id'] : 0;
  581. $paymentInfo = MemberPaymentService::make()->getInfo($paymentId);
  582. if (empty($paymentInfo)) {
  583. $this->error = '3010';
  584. return false;
  585. }
  586. $paymentData = [
  587. 'payment_id' => $paymentInfo['id'],
  588. 'type' => $paymentInfo['type'],
  589. 'logo' => $paymentInfo['logo'] ? get_image_url($paymentInfo['logo']) : '',
  590. 'real_name' => $paymentInfo['real_name'],
  591. 'bank_name' => $paymentInfo['bank_name'],
  592. 'bank_card' => $paymentInfo['bank_card'],
  593. 'branch_name' => $paymentInfo['branch_name'],
  594. 'qrcode' => $paymentInfo['qrcode'] ? get_image_url($paymentInfo['qrcode']) : '',
  595. 'account' => $paymentInfo['account'],
  596. ];
  597. $orderNo = get_order_num('OT');
  598. $data = [
  599. 'user_id' => $userId,
  600. 'business_id' => isset($businessInfo['id']) ? $businessInfo['id'] : 0,
  601. 'order_no' => $orderNo,
  602. 'type' => 2,
  603. 'pay_type' => isset($params['pay_type']) ? floatval($params['pay_type']) : 1,
  604. 'price' => $tradePrice,
  605. 'num' => $num,
  606. 'payment_id' => $paymentId,
  607. 'idcard_data' => json_encode($idcardData, 256),
  608. 'payment_data' => json_encode($paymentData, 256),
  609. 'total' => $total,
  610. 'create_time' => time(),
  611. 'status' => 1,
  612. 'mark' => 1,
  613. ];
  614. $this->model->startTrans();
  615. if (!$order = $this->model->edit($data)) {
  616. $this->error = '3012';
  617. $this->model->rollBack();
  618. return false;
  619. }
  620. // 扣除币
  621. if (!$this->memberModel->where(['id' => $userId])->decrement('usdt_num', $num)) {
  622. $this->error = '3013';
  623. $this->model->rollBack();
  624. return false;
  625. }
  626. // 账户明细
  627. $data = [
  628. 'order_no' => $data['order_no'],
  629. 'user_id' => $userId,
  630. 'type' => 2,
  631. 'change_type' => 2,
  632. 'num' => $num,
  633. 'total' => $total,
  634. 'balance' => floatval($userInfo['usdt_num'] - $num),
  635. 'create_time' => time(),
  636. 'update_time' => time(),
  637. 'remark' => '客户卖出',
  638. 'status' => 1,
  639. 'mark' => 1,
  640. ];
  641. if (!$this->capitalModel->edit($data)) {
  642. $this->error = '3014';
  643. $this->model->rollBack();
  644. return false;
  645. }
  646. // 订单通知
  647. $data = [
  648. 'from_uid' => $userId,
  649. 'to_uid' => $businessInfo['id'],
  650. 'type' => 2,
  651. 'order_no' => $orderNo,
  652. 'chat_key' => getChatKey($userId, $businessInfo['id']),
  653. 'message' => "您有来自客户卖单订单:{$orderNo}的消息,请尽快回复!",
  654. 'message_type' => 1,
  655. 'data_type' => 3,
  656. 'create_time' => time(),
  657. 'update_time' => time(),
  658. 'status' => 1,
  659. 'mark' => 1,
  660. ];
  661. if (!ChatMessageService::make()->pushMessage($data)) {
  662. $this->model->rollBack();
  663. $this->error = '3023';
  664. return false;
  665. }
  666. $this->model->commit();
  667. return $order;
  668. }
  669. /**
  670. * 订单打款处理
  671. * @param $userId 用户ID
  672. * @param $params 打款参数
  673. * @return false
  674. */
  675. public function pay($userId, $params)
  676. {
  677. $orderId = isset($params['id']) ? $params['id'] : 0;
  678. if ($orderId <= 0) {
  679. $this->error = '1013';
  680. return false;
  681. }
  682. $orderInfo = $this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1, 'type' => 1])
  683. ->whereIn('status', [1, 2, 5, 7])
  684. ->select(['id', 'order_no', 'business_id', 'type', 'payment_id', 'num', 'total', 'status'])
  685. ->first();
  686. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  687. if (empty($orderInfo)) {
  688. $this->error = '3016';
  689. return false;
  690. }
  691. if ($tradeType != 1) {
  692. $this->error = '3024';
  693. return false;
  694. }
  695. // 用户信息
  696. $userInfo = MemberService::make()->getInfo($userId);
  697. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  698. if ($status != 1) {
  699. $this->error = '2009';
  700. return false;
  701. }
  702. // 交易密码
  703. $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
  704. $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
  705. if (empty($password)) {
  706. $this->error = '2015';
  707. return false;
  708. }
  709. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  710. $this->error = '2016';
  711. return false;
  712. }
  713. $data = [
  714. 'status' => 3,
  715. 'pay_type' => $params['pay_type'],
  716. 'pay_img' => $params['pay_img'],
  717. 'pay_remark' => $params['pay_remark'],
  718. 'pay_time' => time(),
  719. ];
  720. if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update($data)) {
  721. $this->error = '3018';
  722. return false;
  723. }
  724. return true;
  725. }
  726. /**
  727. * 商家订单打款处理
  728. * @param $userId 用户ID
  729. * @param $params 打款参数
  730. * @return false
  731. */
  732. public function businessPay($businessId, $params)
  733. {
  734. $orderId = isset($params['id']) ? $params['id'] : 0;
  735. if ($orderId <= 0) {
  736. $this->error = '1013';
  737. return false;
  738. }
  739. $orderInfo = $this->model->where(['business_id' => $businessId, 'id' => $orderId, 'mark' => 1, 'type' => 2])
  740. ->whereIn('status', [1, 2, 5, 7])
  741. ->select(['id', 'user_id', 'order_no', 'business_id', 'type', 'payment_id', 'num', 'total', 'status'])
  742. ->first();
  743. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  744. if (empty($orderInfo)) {
  745. $this->error = '3016';
  746. return false;
  747. }
  748. if ($tradeType != 2) {
  749. $this->error = '3025';
  750. return false;
  751. }
  752. // 用户信息
  753. $userInfo = MemberService::make()->getInfo($businessId);
  754. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  755. $username = isset($userInfo['username']) ? $userInfo['username'] : 0;
  756. if ($status != 1) {
  757. $this->error = '2009';
  758. return false;
  759. }
  760. // 交易密码
  761. $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
  762. $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
  763. if (empty($password)) {
  764. $this->error = '2015';
  765. return false;
  766. }
  767. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  768. $this->error = '2016';
  769. return false;
  770. }
  771. if (strpos($params['pay_img'], "temp")) {
  772. $params['pay_img'] = save_image($params['pay_img'], 'images');
  773. } else {
  774. $params['pay_img'] = str_replace(IMG_URL, "", $params['pay_img']);
  775. }
  776. $data = [
  777. 'status' => 3,
  778. 'pay_type' => $params['pay_type'],
  779. 'pay_img' => $params['pay_img'],
  780. 'pay_remark' => $params['pay_remark'],
  781. 'pay_time' => time(),
  782. ];
  783. if (!$this->model->where(['business_id' => $businessId, 'id' => $orderId, 'mark' => 1])->update($data)) {
  784. $this->error = '3018';
  785. return false;
  786. }
  787. $this->error = '3017';
  788. return true;
  789. }
  790. /**
  791. * 订单确认处理
  792. * @param $userId 用户ID
  793. * @param $params 打款参数
  794. * @return false
  795. */
  796. public function collection($userId, $params)
  797. {
  798. $orderId = isset($params['id']) ? $params['id'] : 0;
  799. if ($orderId <= 0) {
  800. $this->error = '1013';
  801. return false;
  802. }
  803. $orderInfo = $this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1, 'type' => 2])
  804. ->select(['id', 'user_id', 'order_no', 'business_id', 'payment_id', 'type', 'num', 'total', 'status'])
  805. ->first();
  806. $businessId = isset($orderInfo['business_id']) ? $orderInfo['business_id'] : 0;
  807. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  808. if (empty($orderInfo) || empty($businessId)) {
  809. $this->error = '3016';
  810. return false;
  811. }
  812. if ($orderInfo['status'] != 3) {
  813. $this->error = '3026';
  814. return false;
  815. }
  816. if ($tradeType != 2) {
  817. $this->error = '3024';
  818. return false;
  819. }
  820. $this->model->startTrans();
  821. // 订单状态更新
  822. if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update(['status' => 4, 'update_time' => time()])) {
  823. $this->model->rollBack();
  824. $this->error = '3023';
  825. return false;
  826. }
  827. // 交易处理
  828. if ($orderInfo['num'] > 0) {
  829. $info = $this->memberModel->where(['id' => $businessId, 'status' => 1, 'mark' => 1])->select(['id', 'username','usdt_num', 'user_type'])->first();
  830. if (empty($info)) {
  831. $this->model->rollBack();
  832. $this->error = '3019';
  833. return false;
  834. }
  835. // 商家进币
  836. if (!$this->memberModel->where(['id' => $businessId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  837. $this->model->rollBack();
  838. $this->error = '3019';
  839. return false;
  840. }
  841. // 明细处理
  842. $data = [
  843. 'order_no' => $orderInfo['order_no'],
  844. 'user_id' => $businessId,
  845. 'type' => 1,
  846. 'pay_type' => 1,
  847. 'change_type' => 1,
  848. 'num' => $orderInfo['num'],
  849. 'total' => $orderInfo['total'],
  850. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  851. 'create_time' => time(),
  852. 'remark' => '交易员买入',
  853. 'status' => 1,
  854. 'mark' => 1,
  855. ];
  856. if (!$this->capitalModel->edit($data)) {
  857. $this->error = '3014';
  858. $this->model->rollBack();
  859. return false;
  860. }
  861. }
  862. $this->model->commit();
  863. return true;
  864. }
  865. /**
  866. * 商家订单确认处理
  867. * @param $businessId 币商用户ID,平台处理时为0
  868. * @param $params 打款参数
  869. * @return false
  870. */
  871. public function businessCollection($businessId, $params)
  872. {
  873. $orderId = isset($params['id']) ? $params['id'] : 0;
  874. $catchUid = isset($params['catch_uid']) ? $params['catch_uid'] : 0;
  875. $type = isset($params['type']) ? $params['type'] : 0;
  876. if ($orderId <= 0) {
  877. $this->error = '1013';
  878. return false;
  879. }
  880. // 平台或币商处理
  881. $where = ['id' => $orderId, 'mark' => 1];
  882. if ($businessId) {
  883. $where['business_id'] = $businessId;
  884. }
  885. if($type){
  886. $where['type'] = $type;
  887. }
  888. $orderInfo = $this->model->where($where)
  889. ->select(['id', 'user_id', 'order_no', 'business_id', 'type', 'num', 'total', 'status'])
  890. ->first();
  891. $userId = isset($orderInfo['user_id']) ? $orderInfo['user_id'] : 0;
  892. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  893. if (empty($orderInfo) || empty($userId)) {
  894. $this->error = '3016';
  895. return false;
  896. }
  897. $status = isset($params['status']) ? intval($params['status']) : 4;
  898. if ($status == 4 && $orderInfo['status'] != 3 && $businessId > 0) {
  899. $this->error = '3026';
  900. return false;
  901. }
  902. if($status == 5 && $orderInfo['status'] == 8 && $businessId>0){
  903. $this->error = '3026';
  904. return false;
  905. }
  906. if($status == 5 && $businessId<=0 && $orderInfo['status'] == 4){
  907. $this->error = '3026';
  908. return false;
  909. }
  910. if ($tradeType != 1 && $status != 5) {
  911. $this->error = '3024';
  912. return false;
  913. }
  914. // 用户信息
  915. if($businessId>0){
  916. $userInfo = MemberService::make()->getInfo($businessId);
  917. $userStatus = isset($userInfo['status']) ? $userInfo['status'] : 0;
  918. $username = isset($userInfo['username']) ? $userInfo['username'] : 0;
  919. if ($userStatus != 1) {
  920. $this->error = '2009';
  921. return false;
  922. }
  923. // 交易密码
  924. $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
  925. $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
  926. if (empty($password)) {
  927. $this->error = '2015';
  928. return false;
  929. }
  930. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  931. $this->error = '2016';
  932. return false;
  933. }
  934. }
  935. $this->model->startTrans();
  936. // 订单状态更新,异常或确认
  937. $updateData = ['status' => $status, 'exception_catch_user' => $catchUid, 'update_time' => time()];
  938. // 异常处理数据
  939. if (isset($params['exception_type'])) {
  940. $updateData['exception_type'] = intval($params['exception_type']);
  941. }
  942. if (isset($params['exception_sub_type'])) {
  943. $updateData['exception_sub_type'] = intval($params['exception_sub_type']);
  944. }
  945. if (isset($params['exception_img'])) {
  946. $updateData['exception_img'] = trim($params['exception_img']);
  947. if (strpos($updateData['exception_img'], "temp")) {
  948. $updateData['exception_img'] = save_image($updateData['exception_img'], 'images');
  949. } else {
  950. $updateData['exception_img'] = str_replace(IMG_URL, "", $updateData['exception_img']);
  951. }
  952. }
  953. if (isset($params['exception_remark'])) {
  954. $updateData['exception_remark'] = trim($params['exception_remark']);
  955. }
  956. if (isset($params['exception_status'])) {
  957. $updateData['exception_status'] = intval($params['exception_status']);
  958. }
  959. if (!$this->model->where($where)->update($updateData)) {
  960. $this->model->rollBack();
  961. $this->error = '3023';
  962. return false;
  963. }
  964. // 交易处理,如果是确认状态
  965. if ($orderInfo['num'] > 0 && $status == 4) {
  966. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  967. if (empty($info)) {
  968. $this->model->rollBack();
  969. $this->error = '3019';
  970. return false;
  971. }
  972. // 客户币进账
  973. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  974. $this->model->rollBack();
  975. $this->error = '3019';
  976. return false;
  977. }
  978. // 明细处理
  979. $data = [
  980. 'order_no' => $orderInfo['order_no'],
  981. 'user_id' => $userId,
  982. 'type' => 1,
  983. 'pay_type' => 1,
  984. 'change_type' => 1,
  985. 'num' => $orderInfo['num'],
  986. 'total' => $orderInfo['total'],
  987. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  988. 'create_time' => time(),
  989. 'update_time' => time(),
  990. 'remark' => '客户买入',
  991. 'status' => 1,
  992. 'mark' => 1,
  993. ];
  994. if (!$this->capitalModel->edit($data)) {
  995. $this->error = '3014';
  996. $this->model->rollBack();
  997. return false;
  998. }
  999. // 更新收款地址数据
  1000. $paymentId = isset($orderInfo['payment_id']) ? $orderInfo['payment_id'] : 0;
  1001. $info = $this->paymentModel->where(['id' => $paymentId, 'mark' => 1])->select(['id', 'used_at'])->first();
  1002. if ($paymentId && $info) {
  1003. $usedAt = isset($info['used_at']) ? $info['used_at'] : '';
  1004. if ($usedAt < date('Y-m-d')) {
  1005. if (!$this->paymentModel->where(['id' => $paymentId])->update(['used_num' => 1, 'used_quota' => $orderInfo['total'], 'update_time' => time()])) {
  1006. $this->model->rollBack();
  1007. $this->error = '3018';
  1008. return false;
  1009. }
  1010. } else {
  1011. $res1 = $this->paymentModel->where(['id' => $paymentId])->increment('used_num', 1);
  1012. $res2 = $this->paymentModel->where(['id' => $paymentId])->increment('used_quota', floatval($orderInfo['total']));
  1013. if (!$res1 || !$res2) {
  1014. $this->model->rollBack();
  1015. $this->error = '3014';
  1016. return false;
  1017. }
  1018. }
  1019. }
  1020. }
  1021. $this->model->commit();
  1022. $this->error = $status == 5 ? 3032 : 3021;
  1023. return true;
  1024. }
  1025. /**
  1026. * 取消订单
  1027. * @param $userId
  1028. * @param $params
  1029. * @return false
  1030. */
  1031. public function cancel($userId, $params)
  1032. {
  1033. $orderId = isset($params['id']) ? intval($params['id']) : 0;
  1034. if ($orderId <= 0) {
  1035. $this->error = '1013';
  1036. return false;
  1037. }
  1038. $orderInfo = $this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])
  1039. ->select(['id', 'order_no', 'business_id', 'type', 'num', 'total', 'status'])
  1040. ->first();
  1041. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  1042. $businessId = isset($orderInfo['business_id']) ? $orderInfo['business_id'] : 0;
  1043. if (empty($orderInfo) || $businessId<=0) {
  1044. $this->error = '3016';
  1045. return false;
  1046. }
  1047. if ($orderInfo['status'] == 3) {
  1048. $this->error = '3027';
  1049. return false;
  1050. }
  1051. if ($orderInfo['status'] == 4) {
  1052. $this->error = '3028';
  1053. return false;
  1054. }
  1055. if ($orderInfo['status'] == 7) {
  1056. $this->error = '3030';
  1057. return false;
  1058. }
  1059. if (!in_array($orderInfo['status'], [1, 2])) {
  1060. $this->error = '3029';
  1061. return false;
  1062. }
  1063. $this->model->startTrans();
  1064. // 订单状态更新
  1065. $updateData = ['status' => 8, 'update_time' => time(), 'exception_remark' => '客户取消'];
  1066. if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update($updateData)) {
  1067. $this->model->rollBack();
  1068. $this->error = '3023';
  1069. return false;
  1070. }
  1071. // 出售订单,USDT退回
  1072. if ($tradeType == 2 && $orderInfo['num']>0) {
  1073. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1074. if (empty($info)) {
  1075. $this->model->rollBack();
  1076. $this->error = '3019';
  1077. return false;
  1078. }
  1079. // 退还币给客户
  1080. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1081. $this->model->rollBack();
  1082. $this->error = '3019';
  1083. return false;
  1084. }
  1085. // 明细处理
  1086. $data = [
  1087. 'order_no' => $orderInfo['order_no'],
  1088. 'user_id' => $userId,
  1089. 'type' => 3,
  1090. 'pay_type' => 1,
  1091. 'change_type' => 1,
  1092. 'num' => $orderInfo['num'],
  1093. 'total' => $orderInfo['total'],
  1094. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1095. 'create_time' => time(),
  1096. 'update_time' => time(),
  1097. 'remark' => '客户取消退还',
  1098. 'status' => 1,
  1099. 'mark' => 1,
  1100. ];
  1101. if (!$this->capitalModel->edit($data)) {
  1102. $this->error = '3014';
  1103. $this->model->rollBack();
  1104. return false;
  1105. }
  1106. }
  1107. // 买入取消
  1108. else if($tradeType == 1 && $orderInfo['num']>0){
  1109. $info = $this->memberModel->where(['id' => $businessId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1110. if (empty($info)) {
  1111. $this->model->rollBack();
  1112. $this->error = '3019';
  1113. return false;
  1114. }
  1115. // 退还币给客户
  1116. if (!$this->memberModel->where(['id' => $businessId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1117. $this->model->rollBack();
  1118. $this->error = '3019';
  1119. return false;
  1120. }
  1121. // 明细处理
  1122. $data = [
  1123. 'order_no' => $orderInfo['order_no'],
  1124. 'user_id' => $businessId,
  1125. 'type' => 3,
  1126. 'pay_type' => 1,
  1127. 'change_type' => 1,
  1128. 'num' => $orderInfo['num'],
  1129. 'total' => $orderInfo['total'],
  1130. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1131. 'create_time' => time(),
  1132. 'update_time' => time(),
  1133. 'remark' => '客户取消退还',
  1134. 'status' => 1,
  1135. 'mark' => 1,
  1136. ];
  1137. if (!$this->capitalModel->edit($data)) {
  1138. $this->error = '3014';
  1139. $this->model->rollBack();
  1140. return false;
  1141. }
  1142. }
  1143. $this->model->commit();
  1144. return true;
  1145. }
  1146. /**
  1147. * 取消订单
  1148. * @param $businessId 币商用户,平台为0
  1149. * @param $params
  1150. * @return false
  1151. */
  1152. public function businessCancel($businessId, $params)
  1153. {
  1154. $orderId = isset($params['id']) ? intval($params['id']) : 0;
  1155. $catchUid = isset($params['catch_uid']) ? intval($params['catch_uid']) : 0;
  1156. if ($orderId <= 0) {
  1157. $this->error = '1013';
  1158. return false;
  1159. }
  1160. $where = ['id' => $orderId, 'mark' => 1];
  1161. if ($businessId) {
  1162. $where['business_id'] = $businessId;
  1163. }
  1164. $orderInfo = $this->model->where($where)
  1165. ->select(['id', 'user_id', 'order_no', 'business_id', 'type', 'num', 'total', 'status'])
  1166. ->first();
  1167. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  1168. $userId = isset($orderInfo['user_id']) ? $orderInfo['user_id'] : 0;
  1169. $orderBusinessId = isset($orderInfo['business_id']) ? $orderInfo['business_id'] : 0;
  1170. if (empty($orderInfo) || $orderBusinessId<=0) {
  1171. $this->error = '3016';
  1172. return false;
  1173. }
  1174. if (empty($userId)) {
  1175. $this->error = '3019';
  1176. return false;
  1177. }
  1178. if ($orderInfo['status'] == 4) {
  1179. $this->error = '3028';
  1180. return false;
  1181. }
  1182. if (!in_array($orderInfo['status'], [1, 2, 3, 5, 7])) {
  1183. $this->error = '3029';
  1184. return false;
  1185. }
  1186. // 用户信息
  1187. $status = isset($params['status']) ? $params['status'] : 8;
  1188. if($businessId>0 && $status != 8){
  1189. $userInfo = MemberService::make()->getInfo($businessId);
  1190. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  1191. $username = isset($userInfo['username']) ? $userInfo['username'] : 0;
  1192. if ($status != 1) {
  1193. $this->error = '2009';
  1194. return false;
  1195. }
  1196. // 交易密码
  1197. $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
  1198. $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
  1199. if (empty($password)) {
  1200. $this->error = '2015';
  1201. return false;
  1202. }
  1203. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  1204. $this->error = '2016';
  1205. return false;
  1206. }
  1207. }
  1208. $this->model->startTrans();
  1209. // 订单状态更新,或异常处理
  1210. $updateData = ['status' => $status, 'exception_catch_user' => $catchUid, 'update_time' => time(), 'exception_remark' => '交易员取消'];
  1211. // 异常处理数据
  1212. if (isset($params['exception_type'])) {
  1213. $updateData['exception_type'] = intval($params['exception_type']);
  1214. }
  1215. if (isset($params['exception_sub_type'])) {
  1216. $updateData['exception_sub_type'] = intval($params['exception_sub_type']);
  1217. }
  1218. if (isset($params['exception_img'])) {
  1219. $updateData['exception_img'] = trim($params['exception_img']);
  1220. if (strpos($updateData['exception_img'], "temp")) {
  1221. $updateData['exception_img'] = save_image($updateData['exception_img'], 'images');
  1222. } else {
  1223. $updateData['exception_img'] = str_replace(IMG_URL, "", $updateData['exception_img']);
  1224. }
  1225. }
  1226. if (isset($params['exception_remark'])) {
  1227. $updateData['exception_remark'] = trim($params['exception_remark']);
  1228. }
  1229. if (isset($params['exception_status'])) {
  1230. $updateData['exception_status'] = intval($params['exception_status']);
  1231. }
  1232. if (isset($params['refund_status'])) {
  1233. $updateData['refund_status'] = intval($params['refund_status']);
  1234. }
  1235. if (isset($params['refund_money'])) {
  1236. $updateData['refund_money'] = intval($params['refund_money']);
  1237. }
  1238. if (!$this->model->where($where)->update($updateData)) {
  1239. $this->model->rollBack();
  1240. $this->error = '3023';
  1241. return false;
  1242. }
  1243. // 出售订单,状态已取消,USDT退回
  1244. if ($tradeType == 2 && $status == 8 && $orderInfo['num']>0) {
  1245. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1246. if (empty($info)) {
  1247. $this->model->rollBack();
  1248. $this->error = '3019';
  1249. return false;
  1250. }
  1251. // 退还币给客户
  1252. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1253. $this->model->rollBack();
  1254. $this->error = '3019';
  1255. return false;
  1256. }
  1257. // 明细处理
  1258. $data = [
  1259. 'order_no' => $orderInfo['order_no'],
  1260. 'user_id' => $userId,
  1261. 'type' => 3,
  1262. 'pay_type' => 1,
  1263. 'change_type' => 1,
  1264. 'num' => $orderInfo['num'],
  1265. 'total' => $orderInfo['total'],
  1266. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1267. 'create_time' => time(),
  1268. 'update_time' => time(),
  1269. 'remark' => '交易员取消买入退还',
  1270. 'status' => 1,
  1271. 'mark' => 1,
  1272. ];
  1273. if (!$this->capitalModel->edit($data)) {
  1274. $this->error = '3014';
  1275. $this->model->rollBack();
  1276. return false;
  1277. }
  1278. }
  1279. // 客户买入订单取消
  1280. else if($tradeType == 1 && $status == 8 && $orderInfo['num']>0){
  1281. $info = $this->memberModel->where(['id' => $orderBusinessId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1282. if (empty($info)) {
  1283. $this->model->rollBack();
  1284. $this->error = '3019';
  1285. return false;
  1286. }
  1287. // 退还币给客户
  1288. if (!$this->memberModel->where(['id' => $orderBusinessId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1289. $this->model->rollBack();
  1290. $this->error = '3019';
  1291. return false;
  1292. }
  1293. // 明细处理
  1294. $data = [
  1295. 'order_no' => $orderInfo['order_no'],
  1296. 'user_id' => $orderBusinessId,
  1297. 'type' => 3,
  1298. 'pay_type' => 1,
  1299. 'change_type' => 1,
  1300. 'num' => $orderInfo['num'],
  1301. 'total' => $orderInfo['total'],
  1302. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1303. 'create_time' => time(),
  1304. 'update_time' => time(),
  1305. 'remark' => '交易员取消卖出退还',
  1306. 'status' => 1,
  1307. 'mark' => 1,
  1308. ];
  1309. if (!$this->capitalModel->edit($data)) {
  1310. $this->error = '3014';
  1311. $this->model->rollBack();
  1312. return false;
  1313. }
  1314. }
  1315. $this->model->commit();
  1316. $this->error = $status == 5 ? 3032 : 3034;
  1317. return true;
  1318. }
  1319. /**
  1320. * 无效订单处理
  1321. * @return false
  1322. */
  1323. public function catchInvalidOrder()
  1324. {
  1325. $cacheKey = "caches:trades:cancels:";
  1326. if(RedisService::get($cacheKey.'lock')){
  1327. return false;
  1328. }
  1329. RedisService::set($cacheKey.'lock', 1, rand(3, 5));
  1330. $overtime = ConfigService::make()->getConfigByCode('trade_order_overtime');
  1331. $cancelTime = ConfigService::make()->getConfigByCode('trade_order_cancel');
  1332. $catchNum = ConfigService::make()->getConfigByCode('trade_order_catch_num');
  1333. $catchNum = $catchNum > 0 ? $catchNum : 200;
  1334. // 处理超时订单
  1335. if ($overtime > 0) {
  1336. $this->model->where(['mark' => 1])
  1337. ->where('status', '<=', 2)
  1338. ->where('create_time', '<=', time() - $overtime * 60)
  1339. ->update(['status' => 7, 'catch_at' => time()]);
  1340. }
  1341. if ($cancelTime <= 0) {
  1342. $this->error = '1023';
  1343. return false;
  1344. }
  1345. $fail = 0;
  1346. $success = 0;
  1347. $this->model->where(function ($query) use ($cancelTime) {
  1348. // 已更新为超时的订单
  1349. $query->where(['mark' => 1, 'status' => 7])
  1350. ->where('catch_at', '<=', time() - $cancelTime * 60);
  1351. })
  1352. ->orWhere(function ($query) use ($cancelTime, $overtime) {
  1353. $query->where('mark', '=', 1)
  1354. ->where('status', '<=', 2)
  1355. ->where('create_time', '<=', time() - ($cancelTime + $overtime) * 60);
  1356. })
  1357. ->select(['id', 'user_id', 'business_id','order_no', 'type', 'num','total'])
  1358. ->take($catchNum)
  1359. ->get()
  1360. ->each(function ($item, $k) use($cacheKey, &$fail, &$success){
  1361. // 客户卖出订单退还
  1362. $date = date('Y-m-d H:i:s');
  1363. $type = isset($item['type']) ? $item['type'] : 0;
  1364. if ($type == 2) {
  1365. if(!$this->orderReback($item['user_id'], $item)){
  1366. $fail++;
  1367. RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  1368. }else{
  1369. $success++;
  1370. RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  1371. }
  1372. }
  1373. else{
  1374. if(!$this->orderReback($item['business_id'], $item)){
  1375. $fail++;
  1376. RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  1377. }else{
  1378. $success++;
  1379. RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  1380. }
  1381. }
  1382. });
  1383. return ['success'=> $success,'fail'=> $fail];
  1384. }
  1385. /**
  1386. * 订单取消退还处理
  1387. * @param $userId
  1388. * @param $orderInfo
  1389. * @return bool
  1390. */
  1391. protected function orderReback($userId, $orderInfo){
  1392. try {
  1393. if($orderInfo['num']<=0){
  1394. return false;
  1395. }
  1396. $this->model->startTrans();
  1397. $updateData = ['status'=> 8, 'update_time'=> time(),'catch_at'=>time(),'exception_remark'=>'系统取消'];
  1398. if(!$this->model->where(['id'=> $orderInfo['id']])->update($updateData)){
  1399. $this->model->rollBack();
  1400. $this->error = '3043';
  1401. return false;
  1402. }
  1403. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1404. if (empty($info)) {
  1405. $this->model->rollBack();
  1406. $this->error = '3019';
  1407. return false;
  1408. }
  1409. // 退还币给客户
  1410. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1411. $this->model->rollBack();
  1412. $this->error = '3019';
  1413. return false;
  1414. }
  1415. // 明细处理
  1416. $data = [
  1417. 'order_no' => $orderInfo['order_no'],
  1418. 'user_id' => $userId,
  1419. 'type' => 3,
  1420. 'pay_type' => 1,
  1421. 'change_type' => 1,
  1422. 'num' => $orderInfo['num'],
  1423. 'total' => $orderInfo['total'],
  1424. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1425. 'create_time' => time(),
  1426. 'update_time' => time(),
  1427. 'remark' => '系统自动取消退还',
  1428. 'status' => 1,
  1429. 'mark' => 1,
  1430. ];
  1431. if (!$this->capitalModel->edit($data)) {
  1432. $this->error = '3014';
  1433. $this->model->rollBack();
  1434. return false;
  1435. }
  1436. $this->model->commit();
  1437. $this->error = '3044';
  1438. return true;
  1439. } catch (\Exception $exception){
  1440. $this->error = $exception->getMessage();
  1441. return false;
  1442. }
  1443. }
  1444. /**
  1445. * 重新派单
  1446. * @param $id 订单ID
  1447. * @param $businessId 新派单用户
  1448. * @return false
  1449. */
  1450. public function reassign($id, $businessId)
  1451. {
  1452. if (empty($id) || empty($businessId)) {
  1453. $this->error = '1013';
  1454. return false;
  1455. }
  1456. $orderInfo = $this->model->where(['id' => $id])
  1457. ->select(['id', 'user_id', 'order_no', 'business_id', 'type', 'num', 'total', 'status'])
  1458. ->first();
  1459. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  1460. if (empty($orderInfo)) {
  1461. $this->error = '3016';
  1462. return false;
  1463. }
  1464. // 状态
  1465. if (!in_array($orderInfo['status'], [1, 2, 3, 5, 7])) {
  1466. $this->error = '3039';
  1467. return false;
  1468. }
  1469. if ($businessId == $orderInfo['business_id']) {
  1470. $this->error = '3035';
  1471. return false;
  1472. }
  1473. // 派单用户信息
  1474. $businessInfo = $this->memberModel->from('member as a')
  1475. ->leftJoin('member_setting as ms', 'ms.user_id', '=', 'a.id')
  1476. ->where(['a.id' => $businessId, 'a.status' => 1, 'a.mark' => 1])
  1477. ->select(['a.id', 'a.username', 'a.usdt_num', 'ms.buy_online', 'ms.buy_online_time', 'ms.sell_online', 'ms.sell_online_time'])
  1478. ->first();
  1479. if (empty($businessInfo)) {
  1480. $this->error = '3036';
  1481. return false;
  1482. }
  1483. // 余额
  1484. if ($businessInfo['usdt_num'] < $orderInfo['num']) {
  1485. $this->error = '3036';
  1486. return false;
  1487. }
  1488. // 在线状态
  1489. if ($tradeType == 1) {
  1490. if ($businessInfo['buy_online'] != 1 || $businessInfo['buy_online_time'] <= time()) {
  1491. $this->error = '3036';
  1492. return false;
  1493. }
  1494. } else if ($tradeType == 2) {
  1495. if ($businessInfo['sell_online'] != 1 || $businessInfo['sell_online_time'] <= time()) {
  1496. $this->error = '3036';
  1497. return false;
  1498. }
  1499. }
  1500. $payment = MemberPaymentService::make()->getPayment($businessId);
  1501. if (empty($payment)) {
  1502. $this->error = '3040';
  1503. return false;
  1504. }
  1505. $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'=>'平台重新派单'];
  1506. if ($this->model->where(['id' => $id])->update($updateData)){
  1507. $this->error = '3041';
  1508. return true;
  1509. }
  1510. $this->error = '3042';
  1511. return false;
  1512. }
  1513. }