TradeOrderService.php 62 KB

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