TradeOrderService.php 69 KB

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