TradeOrderService.php 78 KB

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