AdvertOrderService.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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\AdvertModel;
  13. use App\Models\AdvertOrderModel;
  14. use App\Models\MemberModel;
  15. use App\Services\Api\MemberPaymentService;
  16. use App\Services\BaseService;
  17. use App\Services\ConfigService;
  18. use App\Services\RedisService;
  19. /**
  20. * 用户广告订单-服务类
  21. * Class AdvertOrderService
  22. * @package App\Services\Common
  23. */
  24. class AdvertOrderService extends BaseService
  25. {
  26. // 静态对象
  27. protected static $instance = null;
  28. /**
  29. * 构造函数
  30. * @since 2020/11/10
  31. * LoginService constructor.
  32. */
  33. public function __construct()
  34. {
  35. $this->model = new AdvertOrderModel();
  36. $this->advertModel = new AdvertModel();
  37. $this->memberModel = new MemberModel();
  38. }
  39. /**
  40. * 静态入口
  41. * @return static|null
  42. */
  43. public static function make()
  44. {
  45. if (!self::$instance) {
  46. self::$instance = (new static());
  47. }
  48. return self::$instance;
  49. }
  50. /**
  51. * 订单列表
  52. * @param $params
  53. * @param int $pageSize
  54. * @return array
  55. */
  56. public function getDataList($params, $pageSize = 15)
  57. {
  58. $list = $this->model->from('advert_order as a')
  59. ->leftJoin('member as b', 'b.id', '=', 'a.business_id')
  60. ->leftJoin('member as c', 'c.id', '=', 'a.user_id')
  61. ->where(function ($query) use ($params) {
  62. $query->where(['a.mark' => 1])->where('a.status', '>', 0);
  63. $orderNo = isset($params['order_no']) && $params['order_no'] ? trim($params['order_no']) : '';
  64. if ($orderNo) {
  65. $query->where('a.order_no', 'like', "%{$orderNo}%");
  66. }
  67. $type = isset($params['type']) ? intval($params['type']) : 0;
  68. if ($type > 0) {
  69. $query->where(['a.type' => $type]);
  70. }
  71. // 日期
  72. $date = isset($params['date']) ? $params['date'] : [];
  73. $start = isset($date[0])? $date[0] : '';
  74. $end = isset($date[1])? $date[1] : '';
  75. $end = $start>=$end? '' : $end;
  76. if ($start) {
  77. $query->where('a.create_time','>=', strtotime($start));
  78. }
  79. if($end){
  80. $query->where('a.create_time','<', strtotime($end));
  81. }
  82. $exceptionStatus = isset($params['exception_status']) ? intval($params['exception_status']) : 0;
  83. if ($exceptionStatus > 0) {
  84. $query->where(['a.exception_status' => $exceptionStatus]);
  85. }
  86. $status = isset($params['status']) ? intval($params['status']) : 0;
  87. if ($status > 0) {
  88. $query->where(['a.status' => $status]);
  89. }
  90. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  91. if ($userId > 0) {
  92. $query->where('a.user_id', $userId);
  93. }
  94. $businessId = isset($params['business_id']) ? $params['business_id'] : 0;
  95. if ($businessId > 0) {
  96. $query->where('a.business_id', $businessId);
  97. }
  98. })
  99. ->select(['a.*', 'b.username', 'c.username as c_username'])
  100. ->orderBy('a.create_time', 'desc')
  101. ->orderBy('a.id', 'desc')
  102. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  103. $list = $list ? $list->toArray() : [];
  104. if ($list) {
  105. $payTypes = [1 => '银行卡', 2 => '微信', 3 => '支付宝', 4 => '其他'];
  106. $overTime = ConfigService::make()->getConfigByCode('trade_order_overtime');
  107. $overTime = $overTime ? $overTime : 0;
  108. foreach ($list['data'] as &$item) {
  109. $item['idcardData'] = $item['idcard_data'] ? json_decode($item['idcard_data'], true) : [];
  110. $item['paymentData'] = $item['payment_data'] ? json_decode($item['payment_data'], true) : [];
  111. $item['create_time_text'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
  112. $item['update_time_text'] = $item['update_time'] ? datetime($item['update_time'], 'Y-m-d H:i:s') : '';
  113. $item['time_text'] = $item['create_time'] ? datetime($item['create_time'], 'H:i') : '';
  114. $item['pay_time_text'] = $item['pay_time'] ? datetime($item['pay_time'], 'Y-m-d H:i') : '';
  115. $item['username_text'] = $item['username'] ? format_account($item['username']) : '';
  116. $item['c_username_text'] = $item['c_username'] ? format_account($item['c_username']) : '';
  117. $item['exception_img'] = $item['exception_img'] ? get_image_url($item['exception_img']) : '';
  118. $item['pay_img'] = $item['pay_img'] ? get_image_url($item['pay_img']) : '';
  119. $item['paymentData']['qrcode'] = isset($item['paymentData']['qrcode']) && $item['paymentData']['qrcode'] ? get_image_url($item['paymentData']['qrcode']) : '';
  120. $overTime = max(0, intval($item['create_time']) + $overTime * 60 - time());
  121. $item['overtime_text'] = in_array($item['status'], [1, 2]) && $overTime ? date('H:i', $overTime) : '';
  122. $payType = isset($item['pay_type']) ? $item['pay_type'] : 0;
  123. $item['pay_name'] = isset($payTypes[$payType]) ? $payTypes[$payType] : '其他';
  124. $item['chat_key'] = getChatKey($item['user_id'],$item['business_id']);
  125. }
  126. }
  127. return [
  128. 'pageSize' => $pageSize,
  129. 'total' => isset($list['total']) ? $list['total'] : 0,
  130. 'list' => isset($list['data']) ? $list['data'] : []
  131. ];
  132. }
  133. /**
  134. * 购买
  135. * @param $userId
  136. * @param $params
  137. * @return false|int|number
  138. */
  139. public function buy($userId, $params)
  140. {
  141. $id = isset($params['id']) ? intval($params['id']) : 0;
  142. $num = isset($params['num']) ? intval($params['num']) : 0;
  143. if ($id <= 0 || $num<=0) {
  144. $this->error = '1013';
  145. return false;
  146. }
  147. // 验证参数
  148. $config = \App\Services\ConfigService::make()->getConfigOptionByGroup(5);
  149. $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 0;
  150. $tradePrice = isset($config['usdt_buy_price']) ? $config['usdt_buy_price'] : 0;
  151. $tradeLimitNum = isset($config['trade_no_catch']) ? $config['trade_no_catch'] : 0;
  152. // 是否开启交易
  153. if ($tradeOpen != 1) {
  154. $this->error = '1013';
  155. return false;
  156. }
  157. $info = AdvertService::make()->getInfo($id);
  158. $tradeType = isset($info['type'])? $info['type'] : 0;
  159. $priceType = isset($info['price_type'])? $info['price_type'] : 0;
  160. $price = isset($info['price'])? $info['price'] : 0;
  161. $businessId = isset($info['business_id'])? $info['business_id'] : 0;
  162. if(empty($info) || $info['status'] != 1){
  163. $this->error = '4001';
  164. return false;
  165. }
  166. if ($tradePrice <= 0 && $priceType == 2) {
  167. $this->error = '3002';
  168. return false;
  169. }
  170. // 浮动价格计算
  171. if ($priceType == 2) {
  172. $price = floatval($tradePrice + $price);
  173. }
  174. // 总价
  175. $total = floatval($price * $num);
  176. if($total<=0){
  177. $this->error = '4002';
  178. return false;
  179. }
  180. // 购买用户信息
  181. $userInfo = MemberService::make()->getInfo($userId);
  182. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  183. if (empty($userInfo) || $status != 1) {
  184. $this->error = '2009';
  185. return false;
  186. }
  187. // 未处理订单
  188. $noCatchOrder = $this->checkOrderNoCatch($userId, 1);
  189. if ($tradeLimitNum > 0 && $noCatchOrder >= $tradeLimitNum) {
  190. $this->error = lang(3005, ['num' => $tradeLimitNum]);
  191. return false;
  192. }
  193. // 交易商家
  194. $businessInfo = MemberService::make()->getInfo($businessId);
  195. if (empty($businessInfo)) {
  196. $this->error = '3004';
  197. return false;
  198. }
  199. // 购买者身份信息
  200. $idcardData = [
  201. 'idcard' => isset($userInfo['idcard']) ? $userInfo['idcard'] : '',
  202. 'idcard_check' => isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0,
  203. 'idcard_front_img' => isset($userInfo['idcard_front_img']) ? $userInfo['idcard_front_img'] : '',
  204. 'idcard_back_img' => isset($userInfo['idcard_back_img']) ? $userInfo['idcard_back_img'] : '',
  205. 'idcard_hand_img' => isset($userInfo['idcard_hand_img']) ? $userInfo['idcard_hand_img'] : '',
  206. ];
  207. // 收款方式
  208. $payment = MemberPaymentService::make()->getPayment($businessInfo['id']);
  209. if (empty($payment)) {
  210. $this->error = '3015';
  211. return false;
  212. }
  213. $this->model->startTrans();
  214. $orderNo = get_order_num('OT');
  215. $data = [
  216. 'user_id' => $userId,
  217. 'business_id' => isset($businessInfo['id']) ? $businessInfo['id'] : 0,
  218. 'order_no' => $orderNo,
  219. 'type' => 1,
  220. 'pay_type' => isset($params['pay_type']) ? floatval($params['pay_type']) : 1,
  221. 'price' => $price,
  222. 'num' => $num,
  223. 'total' => $total,
  224. 'payment_id' => isset($payment['id']) ? intval($payment['id']) : 0,
  225. 'idcard_data' => $idcardData ? json_encode($idcardData, 256) : '',
  226. 'payment_data' => $payment ? json_encode($payment, 256) : '',
  227. 'create_time' => time(),
  228. 'update_time' => time(),
  229. 'status' => 1,
  230. 'mark' => 1,
  231. ];
  232. if (!$order = $this->model->edit($data)) {
  233. $this->model->rollBack();
  234. $this->error = '3023';
  235. return false;
  236. }
  237. if(!$this->memberModel->where(['id'=> $businessInfo['id']])->decrement('usdt_num', $num)){
  238. $this->model->rollBack();
  239. $this->error = '3020';
  240. return false;
  241. }
  242. $data = [
  243. 'order_no'=> $orderNo,
  244. 'user_id'=> $businessInfo['id'],
  245. 'type'=> 2,
  246. 'pay_type'=> 1,
  247. 'trade_type'=> 2,
  248. 'change_type'=> 2,
  249. 'num'=> $num,
  250. 'total'=> $total,
  251. 'balance'=> floatval($businessInfo['usdt_num']-$num),
  252. 'create_time'=> time(),
  253. 'update_time'=> time(),
  254. 'status'=> 1,
  255. 'mark'=>1,
  256. 'remark'=> '交易员卖出',
  257. ];
  258. if(!$this->capitalModel->edit($data)){
  259. $this->model->rollBack();
  260. $this->error = '3014';
  261. return false;
  262. }
  263. // 订单通知
  264. $data = [
  265. 'from_uid' => $userId,
  266. 'to_uid' => $businessInfo['id'],
  267. 'type' => 2,
  268. 'order_no' => $orderNo,
  269. 'chat_key' => ChatMessageService::make()->getChatKey($userId, $businessInfo['id']),
  270. 'message' => "来自用户{$username}的购买订单,金额{$total},单号{$orderNo},时间" . date('Y-m-d H:i:s'),
  271. 'message_type' => 1,
  272. 'create_time' => time(),
  273. 'update_time' => time(),
  274. 'status' => 1,
  275. 'mark' => 1,
  276. ];
  277. if (!ChatMessageService::make()->pushMessage($data)) {
  278. $this->model->rollBack();
  279. $this->error = '3031';
  280. return false;
  281. }
  282. $this->model->commit();
  283. return $order;
  284. }
  285. /**
  286. * 购买
  287. * @param $userId
  288. * @param $params
  289. * @return false|int|number
  290. */
  291. public function sell($userId, $params)
  292. {
  293. $id = isset($params['id']) ? intval($params['id']) : 0;
  294. $num = isset($params['num']) ? intval($params['num']) : 0;
  295. if ($id <= 0 || $num<=0) {
  296. $this->error = '1013';
  297. return false;
  298. }
  299. // 验证参数
  300. $config = \App\Services\ConfigService::make()->getConfigOptionByGroup(5);
  301. $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 0;
  302. $tradeMinNum = isset($config['trade_min_num']) ? $config['trade_min_num'] : 0;
  303. $tradeMaxNum = isset($config['trade_max_num']) ? $config['trade_max_num'] : 0;
  304. $trademinMoney = isset($config['trade_min_money']) ? $config['trade_min_money'] : 0;
  305. $tradeMaxMoney = isset($config['trade_max_money']) ? $config['trade_max_money'] : 0;
  306. $tradePrice = isset($config['usdt_buy_price']) ? $config['usdt_sell_price'] : 0;
  307. $tradeLimitNum = isset($config['trade_no_catch']) ? $config['trade_no_catch'] : 0;
  308. // 是否开启交易
  309. if ($tradeOpen != 1) {
  310. $this->error = '1013';
  311. return false;
  312. }
  313. $info = AdvertService::make()->getInfo($id);
  314. $tradeType = isset($info['type'])? $info['type'] : 0;
  315. $priceType = isset($info['price_type'])? $info['price_type'] : 0;
  316. $price = isset($info['price'])? $info['price'] : 0;
  317. $businessId = isset($info['business_id'])? $info['business_id'] : 0;
  318. if(empty($info) || $info['status'] != 1){
  319. $this->error = '4001';
  320. return false;
  321. }
  322. if ($tradePrice <= 0 && $priceType == 2) {
  323. $this->error = '3002';
  324. return false;
  325. }
  326. // 验证数量或金额
  327. if ($priceType == 2) {
  328. $price = floatval($tradePrice + $price);
  329. }
  330. $total = floatval($price * $num);
  331. // 用户信息
  332. $userInfo = MemberService::make()->getInfo($userId);
  333. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  334. $idcardCheck = isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0;
  335. $username = isset($userInfo['username']) && $userInfo['username'] ? format_account($userInfo['username']) : '';
  336. if ($status != 1) {
  337. $this->error = '2009';
  338. return false;
  339. }
  340. // 未处理订单
  341. $noCatchOrder = $this->checkOrderNoCatch($userId, 2);
  342. if ($tradeLimitNum > 0 && $noCatchOrder >= $tradeLimitNum) {
  343. $this->error = lang(3005, ['num' => $tradeLimitNum]);
  344. return false;
  345. }
  346. // 交易商家
  347. $businessInfo = MemberService::make()->getInfo($businessId);
  348. if (empty($businessInfo)) {
  349. $this->error = '3004';
  350. return false;
  351. }
  352. // 购买者身份信息
  353. $idcardData = [
  354. 'idcard' => isset($userInfo['idcard']) ? $userInfo['idcard'] : '',
  355. 'idcard_check' => isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0,
  356. 'idcard_front_img' => isset($userInfo['idcard_front_img']) ? $userInfo['idcard_front_img'] : '',
  357. 'idcard_back_img' => isset($userInfo['idcard_back_img']) ? $userInfo['idcard_back_img'] : '',
  358. 'idcard_hand_img' => isset($userInfo['idcard_hand_img']) ? $userInfo['idcard_hand_img'] : '',
  359. ];
  360. // 收款方式
  361. $payment = MemberPaymentService::make()->getPayment($userId);
  362. if (empty($payment)) {
  363. $this->error = '3015';
  364. return false;
  365. }
  366. $this->model->startTrans();
  367. $orderNo = get_order_num('OT');
  368. $data = [
  369. 'user_id' => $userId,
  370. 'business_id' => isset($businessInfo['id']) ? $businessInfo['id'] : 0,
  371. 'order_no' => $orderNo,
  372. 'type' => 1,
  373. 'pay_type' => isset($params['pay_type']) ? floatval($params['pay_type']) : 1,
  374. 'price' => $price,
  375. 'num' => $num,
  376. 'total' => $total,
  377. 'payment_id' => isset($payment['id']) ? intval($payment['id']) : 0,
  378. 'idcard_data' => $idcardData ? json_encode($idcardData, 256) : '',
  379. 'payment_data' => $payment ? json_encode($payment, 256) : '',
  380. 'create_time' => time(),
  381. 'update_time' => time(),
  382. 'status' => 1,
  383. 'mark' => 1,
  384. ];
  385. if (!$order = $this->model->edit($data)) {
  386. $this->model->rollBack();
  387. $this->error = '3023';
  388. return false;
  389. }
  390. if(!$this->memberModel->where(['id'=> $businessInfo['id']])->decrement('usdt_num', $num)){
  391. $this->model->rollBack();
  392. $this->error = '3020';
  393. return false;
  394. }
  395. $data = [
  396. 'order_no'=> $orderNo,
  397. 'user_id'=> $businessInfo['id'],
  398. 'type'=> 2,
  399. 'pay_type'=> 1,
  400. 'trade_type'=> 2,
  401. 'change_type'=> 2,
  402. 'num'=> $num,
  403. 'total'=> $total,
  404. 'balance'=> floatval($businessInfo['usdt_num']-$num),
  405. 'create_time'=> time(),
  406. 'update_time'=> time(),
  407. 'status'=> 1,
  408. 'mark'=>1,
  409. 'remark'=> '交易员卖出',
  410. ];
  411. if(!$this->capitalModel->edit($data)){
  412. $this->model->rollBack();
  413. $this->error = '3014';
  414. return false;
  415. }
  416. // 订单通知
  417. $data = [
  418. 'from_uid' => $userId,
  419. 'to_uid' => $businessInfo['id'],
  420. 'type' => 2,
  421. 'order_no' => $orderNo,
  422. 'chat_key' => ChatMessageService::make()->getChatKey($userId, $businessInfo['id']),
  423. 'message' => "来自用户{$username}的购买订单,金额{$total},单号{$orderNo},时间" . date('Y-m-d H:i:s'),
  424. 'message_type' => 1,
  425. 'create_time' => time(),
  426. 'update_time' => time(),
  427. 'status' => 1,
  428. 'mark' => 1,
  429. ];
  430. if (!ChatMessageService::make()->pushMessage($data)) {
  431. $this->model->rollBack();
  432. $this->error = '3031';
  433. return false;
  434. }
  435. $this->model->commit();
  436. return $order;
  437. }
  438. /**
  439. * 获取未支付或处理的订单数
  440. * @param $userId
  441. * @param int $type
  442. * @return mixed
  443. */
  444. public function checkOrderNoCatch($userId, $type = 1)
  445. {
  446. return $this->model->where(['user_id' => $userId, 'type' => $type, 'mark' => 1])
  447. ->whereIn('status', [1, 2, 7])
  448. ->count('id');
  449. }
  450. /**
  451. * 自动取消广告订单处理
  452. * @return false
  453. */
  454. public function catchInvalidOrder(){
  455. $cacheKey = "caches:adverts:cancels:";
  456. if(RedisService::get($cacheKey.'lock')){
  457. return false;
  458. }
  459. RedisService::set($cacheKey.'lock', 1, rand(3, 5));
  460. $overtime = ConfigService::make()->getConfigByCode('trade_order_overtime');
  461. $cancelTime = ConfigService::make()->getConfigByCode('trade_order_cancel');
  462. $catchNum = ConfigService::make()->getConfigByCode('trade_order_catch_num');
  463. $catchNum = $catchNum > 0 ? $catchNum : 200;
  464. // 处理超时订单
  465. if ($overtime > 0) {
  466. $this->model->where(['mark' => 1])
  467. ->where('status', '<=', 2)
  468. ->where('create_time', '<=', time() - $overtime * 60)
  469. ->update(['status' => 7, 'catch_at' => time()]);
  470. }
  471. if ($cancelTime <= 0) {
  472. $this->error = '1023';
  473. return false;
  474. }
  475. $fail = 0;
  476. $success = 0;
  477. $this->model->where(function ($query) use ($cancelTime) {
  478. // 已更新为超时的订单
  479. $query->where(['mark' => 1, 'status' => 7])
  480. ->where('catch_at', '<=', time() - $cancelTime * 60);
  481. })
  482. ->orWhere(function ($query) use ($cancelTime, $overtime) {
  483. $query->where('mark', '=', 1)
  484. ->where('status', '<=', 2)
  485. ->where('create_time', '<=', time() - ($cancelTime + $overtime) * 60);
  486. })
  487. ->select(['id', 'user_id', 'business_id','advert_id','order_no', 'type', 'num','total'])
  488. ->take($catchNum)
  489. ->get()
  490. ->each(function ($item, $k) use($cacheKey, &$fail, &$success){
  491. // 客户卖出订单退还
  492. $date = date('Y-m-d H:i:s');
  493. $type = isset($item['type']) ? $item['type'] : 0;
  494. if ($type == 2) {
  495. if(!$this->orderReback($item['user_id'], $item)){
  496. $fail++;
  497. RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  498. }else{
  499. $success++;
  500. RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  501. }
  502. }
  503. else{
  504. if(!$this->orderReback($item['business_id'], $item)){
  505. $fail++;
  506. RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  507. }else{
  508. $success++;
  509. RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  510. }
  511. }
  512. });
  513. return ['success'=> $success,'fail'=> $fail];
  514. }
  515. /**
  516. * 订单取消退还处理
  517. * @param $userId
  518. * @param $orderInfo
  519. * @return bool
  520. */
  521. protected function orderReback($userId, $orderInfo){
  522. try {
  523. if($orderInfo['num']<=0){
  524. return false;
  525. }
  526. $this->model->startTrans();
  527. $updateData = ['status'=> 8, 'update_time'=> time(),'catch_at'=>time(),'exception_remark'=>'系统取消'];
  528. if(!$this->model->where(['id'=> $orderInfo['id']])->update($updateData)){
  529. $this->model->rollBack();
  530. $this->error = '3043';
  531. return false;
  532. }
  533. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  534. if (empty($info)) {
  535. $this->model->rollBack();
  536. $this->error = '3019';
  537. return false;
  538. }
  539. // 退还币给客户
  540. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  541. $this->model->rollBack();
  542. $this->error = '3019';
  543. return false;
  544. }
  545. // 明细处理
  546. $data = [
  547. 'order_no' => $orderInfo['order_no'],
  548. 'user_id' => $userId,
  549. 'type' => 3,
  550. 'pay_type' => 1,
  551. 'trade_type' => 2,
  552. 'change_type' => 1,
  553. 'num' => $orderInfo['num'],
  554. 'total' => $orderInfo['total'],
  555. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  556. 'create_time' => time(),
  557. 'update_time' => time(),
  558. 'remark' => '系统自动取消退还',
  559. 'status' => 1,
  560. 'mark' => 1,
  561. ];
  562. if (!$this->capitalModel->edit($data)) {
  563. $this->error = '3014';
  564. $this->model->rollBack();
  565. return false;
  566. }
  567. $this->model->commit();
  568. $this->error = '3044';
  569. return true;
  570. } catch (\Exception $exception){
  571. $this->error = $exception->getMessage();
  572. return false;
  573. }
  574. }
  575. }